mediumGraphHeapGraphs

Network Delay Time (Dijkstra)

mediumTime: O((V+E) log V)Space: O(V+E)

Recognize the pattern

shortest time for signal to reach all nodesweighted directed graphsingle-source shortest paths

Brute force idea

The naive version of Network Delay Time (Dijkstra) sounds like this: BFS/DFS trying all paths — doesn't account for edge weights properly. That direct path helps you understand the question, but it tends to treat every possibility as brand new instead of learning from earlier steps.

Better approach

A calmer way to see Network Delay Time (Dijkstra) is this: Dijkstra's algorithm: min-heap of (distance, node). Process the nearest unvisited node, relax its neighbors. The answer is the maximum distance to any node — that's when the last node receives the signal. The goal is not to be clever for its own sake, but to remember the one relationship that keeps the solution grounded as you move forward.

Key invariant

The truth you want to protect throughout Network Delay Time (Dijkstra) is this: Dijkstra's guarantees that when a node is popped from the heap, its distance is final. The signal reaches all nodes when the farthest one is reached — max(distances). If that remains true after every update, the rest of the reasoning has a stable place to stand.

Watch out for

A common way to get lost in Network Delay Time (Dijkstra) is this: Using BFS for weighted graphs — BFS only finds shortest paths in unweighted graphs. Dijkstra's handles variable edge weights correctly. Most mistakes here are not about syntax; they come from losing track of what your state, pointer, or structure is supposed to mean.

Graphs Pattern