Network Delay Time
Network Delay Time is LeetCode 743 (Medium). A network has n nodes labelled 1 to n, and a list times of directed edges [u, v, w]: a signal sent from u reaches v after w time units.
A signal is sent from node k. Return the time it takes for all n nodes to receive it, or -1 if some node can never receive it.
- Every time
wis between 0 and 100, so none is negative. nis at most 100 and there are at most 6,000 edges.
- 1 <= k <= n <= 100
- 1 <= times.length <= 6000
- times[i] = [u, v, w], 1 <= u, v <= n, u != v
- 0 <= w <= 100 — no negative times
- No two edges share the same (u, v) pair
Intuition
The signal spreads along every edge at once, so each node first hears it at its shortest travel time from k. The whole network has heard it when the last node does.
- Each node: the earliest arrival is the shortest-path distance from
k. - The answer: the largest of those distances, because that node is the last to hear.
- Unreachable: if some node has no path from
k, it never hears, and the answer is-1.
So this is one run of Dijkstra's algorithm from k, followed by a maximum. Dijkstra fits because no time is negative: the closest unfinished node can never be reached sooner by a longer detour.
"How long until everyone is reached" on a weighted graph means: shortest distance from the source to every node, then the maximum, because the signal travels every route at once and the network is done when the last node hears it. This problem is the standard example. With non-negative weights that is Dijkstra; with all weights equal, plain BFS is enough.
Approach
Before reading on: on the second example, when does node 2 first receive the signal? Why can't BFS, which counts hops, give the right time when edges have different weights?
Two ways to solve it
The heap always settles the node with the earliest arrival next, and stale entries are skipped.
- Work: each edge is relaxed once.
- Needs: no negative times, which this problem guarantees.
- Extra state: a heap and an adjacency list.
The standard answer here.
Go over every edge again and again, lowering dist[v] whenever dist[u] + w is smaller, until a full pass changes nothing.
- Work: up to n - 1 passes over all edges.
- Needs: nothing extra; it also handles negative times.
- Extra state: only the
distarray.
Shorter to write, slower on big graphs.
Both give the same arrival times; Dijkstra does far less work when there are many edges. The steps, code and live demo follow Dijkstra, and the Bellman-Ford code comes after the demo.
Build the graph one way only
For each [u, v, w], add (v, w) to graph[u]. The edges are directed, so do not add the reverse edge: that would invent routes the network does not have.
Start Dijkstra at k
Set dist[k] = 0 and every other dist to infinity. Push (0, k) onto a min-heap. The heap always gives back the node with the smallest arrival time found so far.
Pop the earliest arrival
Pop (d, u):
- if
d > dist[u], a faster route touwas found after this entry was pushed, so skip it; - otherwise
dis the final arrival time ofu, because any other route would leave through a node that is reached no earlier, and times never go down.
Relax the edges out of u
For each (v, w) in graph[u], if d + w < dist[v], the signal reaches v sooner through u. Set dist[v] = d + w and push (dist[v], v).
Take the latest arrival
When the heap is empty, dist holds every arrival time. If any dist is still infinity, that node never hears the signal, so return -1. Otherwise return the largest dist.
Code in Python, C++ and Java
Bellman-Ford version
Start with dist[k] = 0. Each pass checks every edge u → v and lowers dist[v] to dist[u] + w if that is smaller. A shortest path uses at most n - 1 edges, so n - 1 passes are enough; stop early when a pass changes nothing.
Common pitfalls
Adding each edge in both directions
graph[u].append((v, w)) graph[v].append((u, w))
graph[u].append((v, w))
A signal can only travel from u to v. The reverse edge creates shortcuts that do not exist, so the answer can come out too small, or a node that should be unreachable looks reachable.
Counting hops instead of time
# BFS: every edge counts as 1 heappush(heap, (d + 1, v))
heappush(heap, (d + w, v))
The route with the fewest edges is not always the fastest. In the second example, the direct edge 1 → 2 takes 4, but 1 → 3 → 2 takes only 3.
Including node 0 in the maximum
return max(dist)
return max(dist[1:])
Nodes are labelled from 1, so dist[0] is an unused slot that stays at infinity. Including it makes every answer -1.
Edge cases
Its dist stays at infinity, so the answer is -1 even if every other node is reached, as in the third example.
Complexity
Network Delay Time FAQ
How does the Dijkstra solution work?
- Graph: directed adjacency list,
graph[u] = [(v, w), ...]. - Start:
dist[k] = 0, heap =[(0, k)]. - Loop: pop
(d, u); skip ifd > dist[u]; for each edge, ifd + w < dist[v], updatedist[v]and push it. - Answer:
max(dist[1..n]), or-1if any is infinity. - Complexity: O(E log E) time, O(n + E) space.