Imagine you're a network engineer designing optimal routes through a communication network. You have an undirected weighted graph representing n network nodes (numbered from 0 to n-1) connected by m communication links.

Each link is represented as edges[i] = [ai, bi, wi], meaning there's a connection between nodes ai and bi with transmission cost wi.

Your mission: Find all the critical communication links that are part of at least one shortest path from the source node 0 to the destination node n-1.

Goal: Return a boolean array answer where answer[i] is true if edge edges[i] lies on at least one shortest path from node 0 to node n-1.

Note: The network might be disconnected, so some paths may not exist!

Input & Output

example_1.py β€” Simple Path
$ Input: n = 4, edges = [[0,1,4],[1,2,2],[2,3,3],[0,4,5],[4,3,6]]
β€Ί Output: [true, true, true, false, false]
πŸ’‘ Note: The shortest path from 0 to 3 has length 9 and follows route 0β†’1β†’2β†’3 with cost 4+2+3=9. The alternative route 0β†’4β†’3 has cost 5+6=11, so edges involving node 4 are not part of any shortest path.
example_2.py β€” Multiple Shortest Paths
$ Input: n = 4, edges = [[0,1,1],[1,3,1],[0,2,1],[2,3,1]]
β€Ί Output: [true, true, true, true]
πŸ’‘ Note: There are two shortest paths from 0 to 3, both with length 2: path 0β†’1β†’3 and path 0β†’2β†’3. Since every edge participates in at least one shortest path, all edges are marked as true.
example_3.py β€” Disconnected Graph
$ Input: n = 4, edges = [[0,1,1],[2,3,1]]
β€Ί Output: [false, false]
πŸ’‘ Note: Node 0 and node 3 are in different connected components, so no path exists between them. Therefore, no edge can be part of a shortest path from 0 to 3.

Constraints

  • 2 ≀ n ≀ 104
  • 0 ≀ edges.length ≀ min(104, n * (n - 1) / 2)
  • edges[i].length == 3
  • 0 ≀ ai, bi ≀ n - 1
  • ai β‰  bi
  • 1 ≀ wi ≀ 106
  • No multi-edges or self-loops
  • The graph may be disconnected

Visualization

Tap to expand
Network Route Optimization VisualizationSRCSource NodeDSTDestinationABCD423547Algorithm Steps:1. Forward Dijkstra: dist_from_src = [0, 4, 6, ∞, ∞]2. Backward Dijkstra: dist_to_dst = [9, 5, 3, ∞, ∞]3. Critical Path Check: SRCβ†’Aβ†’Bβ†’DST (4+2+3=9) βœ“4. Alternative Path: SRCβ†’Cβ†’Dβ†’DST (5+4+7=16) βœ—πŸ“Š Result: [true, true, true, false, false, false]
Understanding the Visualization
1
🏁 Forward Scan
Calculate shortest distances from source to all nodes using Dijkstra's algorithm
2
🎯 Backward Scan
Calculate shortest distances from destination back to all nodes
3
πŸ” Edge Analysis
For each network link, check if it's part of an optimal route using the distance formula
4
βœ… Critical Path Identification
Mark all links that contribute to at least one shortest path
Key Takeaway
🎯 Key Insight: An edge is critical if it lies on at least one shortest path. We can determine this efficiently by checking if the sum of distances (sourceβ†’edge_start + edge_weight + edge_endβ†’destination) equals the overall shortest path length.
Asked in
Google 42 Amazon 38 Meta 25 Microsoft 31
52.3K Views
High 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