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 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 n

Goal: 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
๐Ÿ”๏ธ Mountain Descent: Counting Restricted Paths1Summit (dist=7)2Mid-slope (dist=2)3Mid-slope (dist=3)4Mid-slope (dist=5)5๐Ÿ•๏ธ Base Camp (dist=0)๐ŸŽฏ Goal: Count all valid downhill paths1. Survey mountain (Dijkstra)2. Only follow downhill routes3. Cache path counts (Memoization)
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)

n
2n
โšก Linearithmic
Space Complexity
O(V + E)

Space for adjacency list O(E), distance array O(V), and memoization cache O(V)

n
2n
โœ“ 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
Asked in
Google 23 Amazon 18 Meta 15 Microsoft 12
67.4K Views
Medium Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen