mediumGraphHeapGraphs

Network Delay Time (Dijkstra)

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

Signals to notice

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

Brute force first

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.

The key insight

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.

What must stay true

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.

Easy way to go wrong

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