Network Signal Propagation Problem

Imagine you're managing a network communication system where signals need to reach all nodes. You have a directed network of n nodes labeled from 1 to n. The network connections are defined by times, where each entry times[i] = [u, v, w] represents a directed edge from node u to node v with a signal travel time of w.

Your mission: Send a signal from a source node k and determine the minimum time required for all nodes in the network to receive the signal. If any node cannot be reached, return -1.

Key Insights:
• This is a classic shortest path problem
• We need to find the longest of all shortest paths (bottleneck)
• The answer is the time when the last node receives the signal

Example: If we have nodes [1,2,3] with edges [[2,1,1],[2,3,1],[3,4,1]] and source k=2, the signal reaches node 1 in 1 unit, node 3 in 1 unit, but node 4 takes 2 units total (2→3→4).

Input & Output

example_1.py — Basic Network
$ Input: times = [[2,1,1],[2,3,1],[3,4,1]], n = 4, k = 2
Output: 2
💡 Note: Signal starts at node 2. It reaches node 1 in 1 unit and node 3 in 1 unit. From node 3, it takes another 1 unit to reach node 4. So the total time is max(1, 1, 2) = 2 units.
example_2.py — Single Node
$ Input: times = [], n = 1, k = 1
Output: 0
💡 Note: Since there's only one node and we start from it, the signal reaches all nodes (just itself) in 0 time.
example_3.py — Unreachable Node
$ Input: times = [[1,2,1]], n = 2, k = 2
Output: -1
💡 Note: Starting from node 2, there's no path to reach node 1, so it's impossible for all nodes to receive the signal.

Constraints

  • 1 ≤ k ≤ n ≤ 100
  • 1 ≤ times.length ≤ 6000
  • times[i].length == 3
  • 1 ≤ ui, vi ≤ n
  • ui != vi
  • 0 ≤ wi ≤ 100
  • All the pairs (ui, vi) are unique (no duplicate edges)

Visualization

Tap to expand
Network Signal PropagationKt=0: StartAt=2Bt=1Ct=4213Maximum Time = 4 unitsNode C is the bottleneck (last to receive signal)
Understanding the Visualization
1
Start Broadcasting
Begin signal transmission from source node K
2
Spread to Neighbors
Signal propagates to directly connected nodes
3
Continue Propagation
Newly reached nodes relay signal to their neighbors
4
Find Bottleneck
The last node to receive signal determines total time
Key Takeaway
🎯 Key Insight: The network delay time is determined by the node that takes the longest to reach from the source - this is why we need the maximum of all shortest path distances.
Asked in
Google 45 Amazon 38 Facebook 25 Microsoft 22 Apple 15
78.0K Views
High Frequency
~25 min Avg. Time
1.9K 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