AlgoMaster Logo

Network Delay Time

Last Updated: March 28, 2026

medium
4 min read

Understanding the Problem

This is a shortest path problem in disguise. The signal spreads from node k to all other nodes through the directed, weighted edges. Each node receives the signal at the earliest possible time, which is the shortest distance from k to that node.

Once we compute the shortest path from k to every other node, the answer is the maximum of all those shortest distances. Why the maximum? Because the signal travels along all paths simultaneously, and we need every node to receive it. The last node to receive the signal determines the total delay time. If any node is unreachable, we return -1.

So the problem reduces to: compute single-source shortest paths from node k, then return the maximum distance. If any node has infinite distance, return -1.

Key Constraints:

  • 1 <= n <= 100 → The number of nodes is small. Even O(n^3) algorithms like Floyd-Warshall would work, but that's overkill since we only need single-source shortest paths.
  • 1 <= times.length <= 6000 → Up to 6000 edges. This is a sparse-to-moderate graph. Edge list traversal is cheap.
  • 0 <= w_i <= 100 → All edge weights are non-negative. This is critical because it means Dijkstra's algorithm is valid. If weights could be negative, we'd need Bellman-Ford.

Approach 1: Bellman-Ford

Intuition

The most straightforward shortest path algorithm that works on any graph (even with negative weights) is Bellman-Ford. The idea is simple: relax every edge, and repeat this V-1 times. After V-1 rounds, the shortest distances are guaranteed to be correct.

Why V-1 rounds? In the worst case, the shortest path from the source to any node passes through at most V-1 edges (visiting all other nodes along the way). Each round propagates shortest distances one hop further. So after V-1 rounds, even the longest shortest path has been fully resolved.

For this problem, we initialize a distance array where dist[k] = 0 (the source) and everything else is infinity. Then we repeatedly scan all edges and update distances when we find a shorter path.

Algorithm

  1. Create a distance array of size n + 1 (nodes are 1-indexed), initialized to infinity. Set dist[k] = 0.
  2. Repeat n - 1 times:
    • For each edge (u, v, w) in times, if dist[u] + w < dist[v], update dist[v] = dist[u] + w.
  3. After all rounds, find the maximum value in dist[1..n].
  4. If the maximum is infinity, return -1 (some node is unreachable). Otherwise, return the maximum.

Example Walkthrough

1Initialize: dist[k=2]=0, all others=INF (index 0 unused)
[-, INF, 0, INF, INF]
1/6

Code

Bellman-Ford works but scans all edges every round, even when no distances change. What if we processed nodes in order of their shortest distance, so we only explore each node's edges once?

Approach 2: Dijkstra's Algorithm (Min-Heap)

Intuition

Dijkstra's algorithm is the gold standard for single-source shortest paths when all edge weights are non-negative. The key insight is greedy: the node with the smallest tentative distance is guaranteed to have its final shortest distance. We can "lock in" that node and only explore its neighbors, rather than scanning every edge repeatedly.

Here's the mental model: imagine the signal spreading outward from node k like a wave. The wave hits the closest node first, then the next closest, and so on. A min-heap (priority queue) naturally gives us nodes in this order. When we pop a node from the heap, its distance is finalized. We then push its unvisited neighbors with updated distances.

Algorithm

  1. Build an adjacency list from times.
  2. Create a distance array initialized to infinity. Set dist[k] = 0.
  3. Push (0, k) into a min-heap (distance, node).
  4. While the heap is not empty:
    • Pop the node with the smallest distance. Call it (d, u).
    • If d > dist[u], skip (we already found a shorter path to u).
    • For each neighbor (v, w) of u, if dist[u] + w < dist[v], update dist[v] and push (dist[v], v) into the heap.
  5. After the heap is empty, find the maximum distance in dist[1..n].
  6. If the maximum is infinity, return -1. Otherwise, return it.

Example Walkthrough

1Initialize: dist[k=2]=0, heap=[(0,2)]
[-, INF, 0, INF, INF]
1/7

Code