Skip to content

Start typing to search patterns, problems and pages.

Advanced Graphs

Ripples on a pond reach the closest shore first. Dijkstra works the same way: always finish the nearest place you know about, because nothing later can get there sooner. Union-Find is the other tool here: give every group one leader, and two people are in the same group when they name the same leader.

⏱ about 20 minutesNeeds: graphs, heapsTime O(E log V)Space O(V + E)

2 · Watch

Advanced Graphs in 60 seconds

A signal leaves node 0 and the roads have different costs. Walking every route repeats the same roads again and again.

3 · Now you drive

Keys:←→ stepSpace playR resetHomeEnd jump

Network Delay Time

or try
2–6 nodes, then | and roads like 0>1:4 (0 to 1 takes 4). Up to 10. Starts at node 0.
The signal starts at node 0 at time 0. Every other node waits at ∞.Step 1 of 15. 5 nodes and 6 roads.
Step 1 of 15
solution.ts
function networkDelay(n: number, links: number[][], start: number): number {
const roads: number[][][] = Array.from({ length: n }, () => []);
for (const [from, to, cost] of links) roads[from].push([to, cost]);
const best = new Array(n).fill(Infinity);
best[start] = 0;
const heap = new MinHeap([[0, start]]);
while (heap.size() > 0) {
const [time, node] = heap.pop();
if (time > best[node]) continue;
for (const [to, cost] of roads[node]) {
const arrive = time + cost;
if (arrive < best[to]) {
best[to] = arrive;
heap.push([arrive, to]);
}
}
}
const slowest = Math.max(...best);
return slowest === Infinity ? -1 : slowest;
}
▶ marks the line running now

4 · Your turn

0 of 3 problems solved

Start from this

// Dijkstra: the heap hands back the closest node first.
const best = new Array(n).fill(Infinity);
best[start] = 0;
const heap = new MinHeap([[0, start]]);
while (heap.size() > 0) {
  const [time, node] = heap.pop();
  // Old entry: a faster way here was already found.
  if (time > best[node]) continue;
  for (const [next, cost] of roads[node]) {
    // A cheaper way to next? Save it and queue it.
    if (time + cost < best[next]) {
      best[next] = time + cost;
      heap.push([best[next], next]);
    }
  }
}

// Union-Find: each node points toward its group's leader.
const parent = Array.from({ length: n }, (_, i) => i);
const find = (x: number): number => {
  // Point straight at the leader, so the next find is quick.
  if (parent[x] !== x) parent[x] = find(parent[x]);
  return parent[x];
};
// Join two groups. False means they were already one group,
// so this link closes a loop.
const union = (a: number, b: number): boolean => {
  const leaderA = find(a), leaderB = find(b);
  if (leaderA === leaderB) return false;
  parent[leaderB] = leaderA;
  return true;
};

Easy ways to go wrong

  • Using Dijkstra with negative costs. It assumes a finished node can't get cheaper.
  • Skipping the old-entry check, which redoes work on finished nodes.
  • Reading parent[x] directly. That may not be the leader, so call find(x).
  • Dropping the shortcut in find, which lets long chains slow every lookup.
  1. Medium01

    Number of Connected Components in an Undirected Graph

    LeetCode 323 · Blind 75 · NeetCode 150

    LeetCode Premium

    Start with n groups. Each useful link removes one.

  2. Medium02

    Graph Valid Tree

    LeetCode 261 · Blind 75 · NeetCode 150

    LeetCode Premium

    A link inside one group means a loop.

  3. Hard03

    Alien Dictionary

    LeetCode 269 · Blind 75 · NeetCode 150

    LeetCode Premium

    Turn word pairs into arrows, then sort them.

UP NEXT · LESSON 15 OF 201-D DPSolve small, remember, build up.