Number of Restricted Paths From First to Last Node - Problem
Number of Restricted Paths From First to Last Node
Imagine you're navigating through a weighted network where you can only move through paths that get you progressively closer to your destination! ๐ฏ
You're given an undirected weighted connected graph with
A restricted path from node
โข Each step takes you to a node that is strictly closer to node
โข "Closer" means having a smaller shortest distance to node
Goal: Count the total number of such restricted paths from node
Since the answer can be very large, return it modulo 109 + 7.
Imagine you're navigating through a weighted network where you can only move through paths that get you progressively closer to your destination! ๐ฏ
You're given an undirected weighted connected graph with
n nodes labeled from 1 to n, and an array edges where each edges[i] = [ui, vi, weighti] represents an edge between nodes ui and vi with weight weighti.A restricted path from node
1 to node n is a special path where:โข Each step takes you to a node that is strictly closer to node
n than your current positionโข "Closer" means having a smaller shortest distance to node
nGoal: Count the total number of such restricted paths from node
1 to node n.Since the answer can be very large, return it modulo 109 + 7.
Input & Output
example_1.py โ Small Connected Graph
$
Input:
n = 5, edges = [[1,2,3],[1,3,3],[2,3,1],[1,4,2],[5,2,2],[3,5,1],[5,4,10]]
โบ
Output:
3
๐ก Note:
There are 3 restricted paths from node 1 to node 5: [1,2,5], [1,4,2,5], and [1,3,5]. Note that path [1,2,3,5] is not valid because dist[3]=4 > dist[5]=0, violating the restriction.
example_2.py โ Linear Path
$
Input:
n = 4, edges = [[1,2,1],[2,3,1],[3,4,1]]
โบ
Output:
1
๐ก Note:
There is only one restricted path: [1,2,3,4]. Each step moves us closer to node 4: dist[1]=3, dist[2]=2, dist[3]=1, dist[4]=0.
example_3.py โ Multiple Valid Paths
$
Input:
n = 3, edges = [[1,2,1],[2,3,1],[1,3,4]]
โบ
Output:
2
๐ก Note:
Two restricted paths exist: [1,2,3] and [1,3]. The direct path [1,3] is valid because dist[1]=2 > dist[3]=0. The path through node 2 is also valid: dist[1]=2 > dist[2]=1 > dist[3]=0.
Visualization
Tap to expand
Understanding the Visualization
1
Survey the Mountain
Use Dijkstra's algorithm like a helicopter survey to measure the shortest distance from every point to base camp
2
Start Descent from Summit
Begin at node 1 (summit) and explore all possible downhill paths using memoized DFS
3
Follow Only Downhill Routes
At each location, only consider paths to neighbors that are strictly closer to base camp
4
Cache Route Counts
Remember the number of valid paths from each location to avoid recalculating the same routes
Key Takeaway
๐ฏ Key Insight: By computing shortest distances first, we transform the problem into counting paths in a DAG, making memoized DFS highly efficient with O((E + V) log V) time complexity.
Time & Space Complexity
Time Complexity
O((E + V) log V + V + E)
Dijkstra takes O((E + V) log V), then DFS with memoization visits each node and edge once O(V + E)
โก Linearithmic
Space Complexity
O(V + E)
Space for adjacency list O(E), distance array O(V), and memoization cache O(V)
โ Linear Space
Constraints
- 1 โค n โค 5 ร 104
- n - 1 โค edges.length โค min(2 ร 104, n ร (n - 1) / 2)
- edges[i].length == 3
- 1 โค ui, vi โค n
- ui โ vi
- 1 โค weighti โค 106
- The graph is connected
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code