Skip to content

Graph Algorithms

4.1 Breadth-First Search (BFS)

BFS explores the graph level by level from a source vertex.

BFS(G, s):
for each v in V:
d[v] = INF
pi[v] = NIL
d[s] = 0
Q = {s}
while Q is not empty:
u = Q.dequeue()
for each v in G.adj[u]:
if d[v] == INF:
d[v] = d[u] + 1
pi[v] = u
Q.enqueue(v)

Theorem 4.1. BFS runs in O(V+E)O(V + E) time and discovers shortest paths (in terms of number of Edges) from the source to all reachable vertices.

Proof. Time: Each vertex is enqueued at most once and dequeued at most once: O(V)O(V). Each edge is examined at most twice (once from each endpoint): O(E)O(E). Total: O(V+E)O(V + E).

Correctness: We prove by induction on the length of shortest paths. Let vv be a vertex discovered via edge (u,v)(u, v). At the time vv is discovered, d[u]d[u] equals the shortest-path distance from ss to uu (induction hypothesis). Since BFS processes vertices in non-decreasing order of distance, d[v]=d[u]+1d[v] = d[u] + 1 is the shortest distance to vv. Any path to vv must go through some vertex at distance at most d[u]d[u] (the predecessor on the path), and all such vertices have already been processed. \blacksquare

Worked Example: BFS on a Weighted Path Problem

Find the shortest path (fewest edges) from AA to all other vertices in the graph with edges: (A,B),(A,C),(B,D),(C,D),(C,E),(D,F),(E,F)(A,B), (A,C), (B,D), (C,D), (C,E), (D,F), (E,F).

Starting from AA:

  • Level 0: AA (distance 0)
  • Level 1: B,CB, C (distance 1)
  • Level 2: D,ED, E (distance 2)
  • Level 3: FF (distance 3)

Shortest path from AA to FF: ACEFA \to C \to E \to F (or ABDFA \to B \to D \to F or ACDFA \to C \to D \to F), all of length 3.

4.2 Depth-First Search (DFS)

DFS explores as far as possible along each branch before backtracking.

DFS(G):
for each v in V:
colour[v] = WHITE
pi[v] = NIL
time = 0
for each v in V:
if colour[v] == WHITE:
DFS-Visit(G, v)
DFS-Visit(G, u):
colour[u] = GREY
time += 1
d[u] = time
for each v in G.adj[u]:
if colour[v] == WHITE:
pi[v] = u
DFS-Visit(G, v)
colour[u] = BLACK
time += 1
f[u] = time

Theorem 4.2. DFS runs in O(V+E)O(V + E) time and can be used to detect cycles, find connected Components, compute topological orderings, and identify strongly connected components.

Proof (time). Each vertex is coloured exactly once (from WHITE to GREY) and finished exactly once (from GREY to BLACK): O(V)O(V). Each edge is examined at most twice (once in each direction for undirected, once for directed): O(E)O(E). Total: O(V+E)O(V + E). \blacksquare

Theorem 4.3 (Parenthesis Theorem). In any DFS, for any two vertices uu and vvExactly one of the following holds: (1) d[u]<d[v]<f[v]<f[u]d[u] \lt d[v] \lt f[v] \lt f[u] (interval nesting), (2) d[v]<d[u]<f[u]<f[v]d[v] \lt d[u] \lt f[u] \lt f[v] (interval nesting), or (3) the intervals [d[u],f[u]][d[u], f[u]] and [d[v],f[v]][d[v], f[v]] are disjoint.

Proof. The DFS call stack forms a nesting of intervals. When we start visiting vv from uuWe must finish vv before finishing uuGiving nesting. If uu and vv are in different DFS trees, their intervals are disjoint. \blacksquare

Theorem 4.4 (White-Path Theorem). vv is a descendant of uu in the DFS forest if and only if, at the time d[u]d[u] is discovered, there is a path from uu to vv consisting entirely of white vertices.

Proof. (\Rightarrow) By induction on the depth of vv in the DFS tree. If vv is a child of uuThen vv was white when discovered from uu. If vv is a deeper descendant, the path goes through intermediate white vertices.

(\Leftarrow) Suppose there is a white path from uu to vv at time d[u]d[u]. Let ww be the first vertex on this path discovered after uu. All vertices before ww on the path are still white (they can only be discovered after ww), so ww will be discovered from the path. By induction, vv is a descendant of wwHence of uu. \blacksquare

4.3 Topological Sort

A topological ordering of a DAG is a linear ordering of vertices such that for every directed edge (u,v)(u, v), uu appears before vv.

Algorithm: Run DFS on the DAG. Output vertices in reverse order of finishing times.

Theorem 4.5. The reverse post-order of a DFS on a DAG produces a valid topological ordering.

Proof. Suppose there is an edge (u,v)(u, v) but uu appears after vv in the ordering (i.e., f[u]<f[v]f[u] \lt f[v]). Since (u,v)(u, v) is an edge, when uu is being explored (coloured GREY), if vv is WHITE, then vv is discovered as a descendant of uuSo f[v]<f[u]f[v] \lt f[u]Contradiction. If vv is GREY, we have a back edge, implying a cycle, contradicting that the graph is acyclic. If vv is BLACK, then f[v]<d[u]<f[u]f[v] \lt d[u] \lt f[u]Contradicting f[u]<f[v]f[u] \lt f[v]. \blacksquare

4.4 Strongly Connected Components

Two vertices uu and vv are strongly connected if there is a path from uu to vv and from vv to uu. A strongly connected component (SCC) is a maximal set of strongly connected vertices.

Kosaraju”s Algorithm:

  1. Run DFS on GGRecording finishing times.
  2. Compute GTG^T (transpose of GG: reverse all edges).
  3. Run DFS on GTG^T in decreasing order of finishing times. Each DFS tree is an SCC.

Theorem 4.6. Kosaraju’s algorithm correctly identifies all SCCs in O(V+E)O(V + E) time.

Proof. Let CC be the SCC containing the vertex ss with the highest finishing time in the first DFS. We claim that in GTG^TNo vertex in CC can reach a vertex outside CC (otherwise ss would have a path to and from that vertex, placing it in CC). In the second DFS, starting from ss in GTG^TWe discover exactly the vertices in CC. Removing CC and repeating the argument gives the remaining SCCs. \blacksquare

Tarjan’s Algorithm finds SCCs in a single DFS pass using a stack and low-link values, also in O(V+E)O(V + E) time.

4.5 Dijkstra’s Algorithm

Problem. Find shortest paths from a single source in a weighted graph with non-negative edge Weights.

Algorithm. Maintain a priority queue of vertices keyed by their current shortest-path estimate. Repeatedly extract the vertex with minimum distance and relax its edges.

Dijkstra(G, w, s):
for each v in V:
d[v] = INF
pi[v] = NIL
d[s] = 0
S = {} // processed vertices
Q = V // priority queue keyed by d[]
while Q is not empty:
u = ExtractMin(Q)
S = S ∪ {u}
for each v in G.adj[u]:
if d[v] > d[u] + w(u, v):
d[v] = d[u] + w(u, v)
pi[v] = u
DecreaseKey(Q, v, d[v])

Theorem 4.7. Dijkstra’s algorithm correctly computes shortest paths from the source.

Proof. We prove by induction on S|S| that when a vertex uu is added to SS, d[u]=δ(s,u)d[u] = \delta(s, u) (the true shortest-path distance).

Base case: ss is the first vertex added, and d[s]=0=δ(s,s)d[s] = 0 = \delta(s, s).

Inductive step: Suppose all vertices in SS have correct distances. Let uu be the next vertex extracted from QQ. Suppose for contradiction that d[u]>δ(s,u)d[u] > \delta(s, u). Consider a shortest path PP from ss to uuAnd let (x,y)(x, y) be the first edge on PP where xSx \in S and ySy \notin S. Then δ(s,y)=δ(s,x)+w(x,y)=d[x]+w(x,y)\delta(s, y) = \delta(s, x) + w(x, y) = d[x] + w(x, y) (by induction). When xx was added to SSThe edge (x,y)(x, y) was relaxed, so d[y]d[x]+w(x,y)=δ(s,y)d[y] \leq d[x] + w(x, y) = \delta(s, y). Since edge weights are non-negative, δ(s,y)δ(s,u)\delta(s, y) \leq \delta(s, u). But d[y]δ(s,y)δ(s,u)<d[u]d[y] \leq \delta(s, y) \leq \delta(s, u) \lt d[u]And yy is in QQContradicting that uu has the minimum dd-value in QQ. \blacksquare

Theorem 4.8. Dijkstra’s algorithm with a binary heap runs in O((V+E)logV)O((V + E)\log V) time. With a Fibonacci heap: O(VlogV+E)O(V \log V + E).

Proof. Each vertex is extracted from the priority queue at most once: O(VlogV)O(V \log V). Each edge is Relaxed at most once, and each relaxation may cause a DecreaseKey: O(ElogV)O(E \log V). Total: O((V+E)logV)O((V + E)\log V). With a Fibonacci heap, DecreaseKey is O(1)O(1) amortised, giving O(VlogV+E)O(V \log V + E). \blacksquare

Worked Example: Dijkstra's Algorithm

Find shortest paths from AA in the graph:

  • A4BA \xrightarrow{4} B, A2CA \xrightarrow{2} C
  • B3DB \xrightarrow{3} D, B1EB \xrightarrow{1} E
  • C1BC \xrightarrow{1} B, C5DC \xrightarrow{5} D
  • D2ED \xrightarrow{2} E

Initial: d[A]=0d[A]=0, d[B]=d[C]=d[D]=d[E]=d[B]=d[C]=d[D]=d[E]=\infty

Extract AA: relax B4B \to 4, C2C \to 2. Q=C(2),B(4),D(),E()Q = \\{C(2), B(4), D(\infty), E(\infty)\\}

Extract CC: relax Bmin(4,2+1)=3B \to \min(4, 2+1)=3, D2+5=7D \to 2+5=7. Q=B(3),D(7),E()Q = \\{B(3), D(7), E(\infty)\\}

Extract BB: relax Dmin(7,3+3)=6D \to \min(7, 3+3)=6, E3+1=4E \to 3+1=4. Q=E(4),D(6)Q = \\{E(4), D(6)\\}

Extract EE: relax Dmin(6,4+2)=6D \to \min(6, 4+2)=6. Q=D(6)Q = \\{D(6)\\}

Extract DD: no improvements.

Result: d[A]=0d[A]=0, d[B]=3d[B]=3, d[C]=2d[C]=2, d[D]=6d[D]=6, d[E]=4d[E]=4

4.6 Bellman-Ford Algorithm

Problem. Find shortest paths from a single source, allowing negative edge weights (but no negative Cycles).

BellmanFord(G, w, s):
for each v in V:
d[v] = INF
pi[v] = NIL
d[s] = 0
for i = 1 to |V| - 1:
for each edge (u, v) in E:
if d[v] > d[u] + w(u, v):
d[v] = d[u] + w(u, v)
pi[v] = u
for each edge (u, v) in E:
if d[v] > d[u] + w(u, v):
return "negative cycle detected"

Theorem 4.9. Bellman-Ford runs in O(VE)O(VE) time. It correctly detects negative-weight cycles.

Proof. After ii iterations of the relaxation loop, d[v]d[v] is at most the weight of the shortest path from ss to vv using at most ii edges. This is proved by induction on ii.

Base case (i=0i = 0): Only d[s]=0d[s] = 0 is finite, which is the weight of the empty path.

Inductive step: A shortest path from ss to vv using at most ii edges either uses at most i1i-1 edges (handled by induction) or uses exactly ii edges, ending with edge (u,v)(u, v) where the shortest path to uu uses at most i1i-1 edges. The relaxation of (u,v)(u, v) in iteration ii handles this case.

After V1V - 1 iterations, all shortest paths (which have at most V1V - 1 edges) are correct. A VV-th iteration that updates any distance indicates a path of length VV with strictly decreasing weight, which contains a cycle of negative weight. \blacksquare

Worked Example: Bellman-Ford with Negative Edge Weights

Find shortest paths from AA in the graph:

  • A6BA \xrightarrow{6} B, A7CA \xrightarrow{7} C
  • B5CB \xrightarrow{5} C, B4DB \xrightarrow{-4} D, B8EB \xrightarrow{8} E
  • C3DC \xrightarrow{-3} D, C9EC \xrightarrow{9} E
  • D2ED \xrightarrow{2} E, D7FD \xrightarrow{7} F
  • E5FE \xrightarrow{-5} F

Initial: d=[,,,,,]d = [\infty, \infty, \infty, \infty, \infty, \infty] for [A,B,C,D,E,F][A, B, C, D, E, F]. d[A]=0d[A] = 0.

Iteration 1: Relax all edges.

  • (A,B)(A,B): d[B]=6d[B] = 6. (A,C)(A,C): d[C]=7d[C] = 7.
  • (B,C)(B,C): min(7,6+5)=7\min(7, 6+5)=7. (B,D)(B,D): d[D]=6+(4)=2d[D] = 6+(-4) = 2. (B,E)(B,E): d[E]=6+8=14d[E] = 6+8 = 14.
  • (C,D)(C,D): min(2,7+(3))=2\min(2, 7+(-3)) = 2. (C,E)(C,E): min(14,7+9)=14\min(14, 7+9) = 14.
  • (D,E)(D,E): min(14,2+2)=4\min(14, 2+2) = 4. (D,F)(D,F): d[F]=2+7=9d[F] = 2+7 = 9.
  • (E,F)(E,F): min(9,4+(5))=1\min(9, 4+(-5)) = -1. After iter 1: d=[0,6,7,2,4,1]d = [0, 6, 7, 2, 4, -1].

Iteration 2: Relax all edges.

  • (B,D)(B,D): min(2,6+(4))=2\min(2, 6+(-4)) = 2. (B,E)(B,E): min(4,6+8)=4\min(4, 6+8) = 4.
  • (D,E)(D,E): min(4,2+2)=4\min(4, 2+2) = 4. (D,F)(D,F): min(1,2+7)=1\min(-1, 2+7) = -1.
  • (E,F)(E,F): min(1,4+(5))=1\min(-1, 4+(-5)) = -1. No changes: d=[0,6,7,2,4,1]d = [0, 6, 7, 2, 4, -1].

Iteration 3—5: No changes. Algorithm terminates.

Negative cycle check: No edge can be relaxed. No negative cycle.

Result: d=[0,6,7,2,4,1]d = [0, 6, 7, 2, 4, -1]. Shortest path to FF: ABDEFA \to B \to D \to E \to FCost 1-1.

Worked Example: Bellman-Ford Negative Cycle Detection

Graph: A1BA \xrightarrow{1} B, B3CB \xrightarrow{-3} C, C2AC \xrightarrow{2} A. This has a cycle ABCAA \to B \to C \to A of weight 1+(3)+2=01 + (-3) + 2 = 0. Not negative.

Now add C1AC \xrightarrow{-1} A. Cycle weight: 1+(3)+(1)=31 + (-3) + (-1) = -3. Negative cycle.

Initial: d[A]=0d[A] = 0Rest \infty.

Iteration 1: d[B]=1d[B] = 1, d[C]=2d[C] = -2, d[A]=min(0,2+(1))=3d[A] = \min(0, -2 + (-1)) = -3.

Iteration 2: d[B]=2d[B] = -2, d[C]=5d[C] = -5, d[A]=6d[A] = -6.

Iteration 3: d[B]=5d[B] = -5, d[C]=8d[C] = -8, d[A]=9d[A] = -9.

Iteration 4: d[B]=8d[B] = -8, d[C]=11d[C] = -11, d[A]=12d[A] = -12.

Iteration 5: d[B]=11d[B] = -11, d[C]=14d[C] = -14, d[A]=15d[A] = -15.

Check (iteration 6): (A,B)(A,B): 15+1=14<11-15 + 1 = -14 \lt -11Can still relax. Negative cycle detected!

4.7 Floyd-Warshall Algorithm

Problem. Find all-pairs shortest paths.

Algorithm. For k=1,,Vk = 1, \ldots, V: for each pair (i,j)(i, j)Check if going through vertex kk Improves the path.

dij(k)=min(dij(k1),dik(k1)+dkj(k1))d_{ij}^{(k)} = \min(d_{ij}^{(k-1)}, d_{ik}^{(k-1)} + d_{kj}^{(k-1)})

Derivation. Define dij(k)d_{ij}^{(k)} as the shortest-path distance from ii to jj using only intermediate vertices from 1,2,,k\\{1, 2, \ldots, k\\}. Then:

  • dij(0)=w(i,j)d_{ij}^{(0)} = w(i,j) (the weight of edge (i,j)(i,j)Or \infty if no edge).
  • For k1k \geq 1: The shortest path from ii to jj through vertices 1,,k\\{1, \ldots, k\\} either does not use vertex kk (giving dij(k1)d_{ij}^{(k-1)}) or uses vertex kk (giving dik(k1)+dkj(k1)d_{ik}^{(k-1)} + d_{kj}^{(k-1)}).

Theorem 4.10. Floyd-Warshall runs in O(V3)O(V^3) time and O(V2)O(V^2) space.

Proof. The triple nested loop (kk, ii, jj) executes V3V^3 iterations, each doing O(1)O(1) work. The distance matrix requires V2V^2 space. Note that dij(k)d_{ij}^{(k)} can overwrite dij(k1)d_{ij}^{(k-1)} in place because dik(k)=dik(k1)d_{ik}^{(k)} = d_{ik}^{(k-1)} and dkj(k)=dkj(k1)d_{kj}^{(k)} = d_{kj}^{(k-1)} (paths from ii to kk and kk to jj using vertices up to kk cannot be improved by going through kk again without a negative cycle). \blacksquare

Worked Example: Floyd-Warshall on 4 Vertices

Find all-pairs shortest paths for the graph with vertices 1,2,3,4\\{1, 2, 3, 4\\} and edges: w(1,2)=3w(1,2) = 3, w(1,3)=8w(1,3) = 8, w(1,4)=4w(1,4) = -4 w(2,1)=5w(2,1) = 5, w(2,3)=7w(2,3) = 7, w(2,4)=2w(2,4) = 2 w(3,1)=2w(3,1) = 2, w(3,4)=1w(3,4) = -1 w(4,1)=6w(4,1) = 6, w(4,3)=9w(4,3) = 9.

Initial distance matrix D(0)D^{(0)}: D(0)=(03845072201690)D^{(0)} = \begin{pmatrix} 0 & 3 & 8 & -4 \\ 5 & 0 & 7 & 2 \\ 2 & \infty & 0 & -1 \\ 6 & \infty & 9 & 0 \end{pmatrix}

k=1k = 1 (through vertex 1): D(1)[2][3]=min(7,5+8)=7D^{(1)}[2][3] = \min(7, 5 + 8) = 7. D(1)[2][4]=min(2,5+(4))=1D^{(1)}[2][4] = \min(2, 5 + (-4)) = 1. D(1)[3][2]=min(,2+3)=5D^{(1)}[3][2] = \min(\infty, 2 + 3) = 5. D(1)[3][4]=min(1,2+(4))=2D^{(1)}[3][4] = \min(-1, 2 + (-4)) = -2. D(1)[4][2]=min(,6+3)=9D^{(1)}[4][2] = \min(\infty, 6 + 3) = 9. D(1)[4][3]=min(9,6+8)=9D^{(1)}[4][3] = \min(9, 6 + 8) = 9.

D(1)=(0384507125026990)D^{(1)} = \begin{pmatrix} 0 & 3 & 8 & -4 \\ 5 & 0 & 7 & 1 \\ 2 & 5 & 0 & -2 \\ 6 & 9 & 9 & 0 \end{pmatrix}

k=2k = 2 (through vertex 2): D(2)[1][3]=min(8,3+7)=8D^{(2)}[1][3] = \min(8, 3 + 7) = 8. D(2)[1][4]=min(4,3+1)=4D^{(2)}[1][4] = \min(-4, 3 + 1) = -4. D(2)[3][1]=min(2,5+5)=2D^{(2)}[3][1] = \min(2, 5 + 5) = 2. D(2)[3][4]=min(2,5+1)=2D^{(2)}[3][4] = \min(-2, 5 + 1) = -2. D(2)[4][1]=min(6,9+5)=6D^{(2)}[4][1] = \min(6, 9 + 5) = 6. D(2)[4][3]=min(9,9+7)=9D^{(2)}[4][3] = \min(9, 9 + 7) = 9.

D(2)=(0384507125026990)D^{(2)} = \begin{pmatrix} 0 & 3 & 8 & -4 \\ 5 & 0 & 7 & 1 \\ 2 & 5 & 0 & -2 \\ 6 & 9 & 9 & 0 \end{pmatrix}

k=3k = 3 (through vertex 3): D(3)[1][2]=min(3,8+5)=3D^{(3)}[1][2] = \min(3, 8 + 5) = 3. D(3)[1][4]=min(4,8+(2))=4D^{(3)}[1][4] = \min(-4, 8 + (-2)) = -4. D(3)[2][1]=min(5,7+2)=5D^{(3)}[2][1] = \min(5, 7 + 2) = 5. D(3)[2][4]=min(1,7+(2))=1D^{(3)}[2][4] = \min(1, 7 + (-2)) = 1. D(3)[4][1]=min(6,9+2)=6D^{(3)}[4][1] = \min(6, 9 + 2) = 6. D(3)[4][2]=min(9,9+5)=9D^{(3)}[4][2] = \min(9, 9 + 5) = 9.

D(3)=(0384507125026990)D^{(3)} = \begin{pmatrix} 0 & 3 & 8 & -4 \\ 5 & 0 & 7 & 1 \\ 2 & 5 & 0 & -2 \\ 6 & 9 & 9 & 0 \end{pmatrix}

k=4k = 4 (through vertex 4): D(4)[1][2]=min(3,4+9)=3D^{(4)}[1][2] = \min(3, -4 + 9) = 3. D(4)[1][3]=min(8,4+9)=5D^{(4)}[1][3] = \min(8, -4 + 9) = 5. D(4)[2][1]=min(5,1+6)=5D^{(4)}[2][1] = \min(5, 1 + 6) = 5. D(4)[2][3]=min(7,1+9)=7D^{(4)}[2][3] = \min(7, 1 + 9) = 7. D(4)[3][1]=min(2,2+6)=2D^{(4)}[3][1] = \min(2, -2 + 6) = 2. D(4)[3][2]=min(5,2+9)=5D^{(4)}[3][2] = \min(5, -2 + 9) = 5.

D(4)=(0354507125026990)D^{(4)} = \begin{pmatrix} 0 & 3 & 5 & -4 \\ 5 & 0 & 7 & 1 \\ 2 & 5 & 0 & -2 \\ 6 & 9 & 9 & 0 \end{pmatrix}

4.8 Minimum Spanning Tree

Kruskal’s Algorithm. Sort edges by weight, add edges to the MST as long as they don’t create a Cycle (using Union-Find). O(ElogE)O(E \log E).

Prim’s Algorithm. Start from any vertex, repeatedly add the minimum-weight edge connecting the Tree to a non-tree vertex (using a priority queue). O((V+E)logV)O((V + E)\log V).

Theorem 4.11 (Cut Property). For any cut of the graph, the minimum-weight edge crossing the cut Belongs to some MST.

Proof. Let (S,VS)(S, V \setminus S) be a cut and e=(u,v)e = (u, v) be the minimum-weight crossing edge with uSu \in S, vSv \notin S. Let TT be an MST. If eTe \in TWe are done. Otherwise, adding ee to TT creates a cycle. This cycle must cross the cut at least once more (it goes from uu to vv via some other path). Let ee' be another crossing edge on this cycle. Since ee is the minimum-weight crossing edge, w(e)w(e)w(e) \leq w(e'). Replacing ee' with ee in TT gives a spanning tree of weight no greater than TTHence an MST containing ee. \blacksquare

Theorem 4.12 (Cycle Property). For any cycle, the maximum-weight edge on the cycle does not belong To any MST.

Proof. Let CC be a cycle and ee be the maximum-weight edge on CC. Let TT be an MST. If eTe \notin TWe are done. Otherwise, removing ee from TT disconnects it into two components. The rest of cycle CC must contain an edge eee' \neq e crossing this cut. Since w(e)<w(e)w(e') \lt w(e) (if w(e)=w(e)w(e') = w(e)We can replace either), replacing ee with ee' gives a spanning tree of strictly smaller weight, contradicting the optimality of TT. \blacksquare

Theorem 4.13. Kruskal’s algorithm produces a minimum spanning tree.

Proof. We prove by induction on the number of edges added. At each step, the algorithm adds the minimum-weight edge that does not create a cycle. Consider the cut formed by the two connected components that the new edge connects. By the cut property, this minimum-weight crossing edge belongs to some MST. Since all previously added edges are in every MST (by induction), the edge set maintained by the algorithm is always a subset of some MST. \blacksquare

Theorem 4.14. Prim’s algorithm produces a minimum spanning tree.

Proof. By induction on the size of the tree. At each step, Prim’s adds the minimum-weight edge connecting the current tree TT to a vertex outside TT. Consider the cut (T,VT)(T, V \setminus T). By the cut property, the minimum-weight crossing edge belongs to some MST. \blacksquare

Worked Example: Kruskal's Algorithm

Find the MST of the graph with edges (sorted by weight): (D,E,2)(D, E, 2), (C,E,3)(C, E, 3), (A,B,4)(A, B, 4), (B,C,5)(B, C, 5), (B,E,6)(B, E, 6), (A,E,7)(A, E, 7), (A,D,8)(A, D, 8), (C,D,9)(C, D, 9).

Vertices: A,B,C,D,E\\{A, B, C, D, E\\}.

Sorted edges: (D,E,2)(D,E,2), (C,E,3)(C,E,3), (A,B,4)(A,B,4), (B,C,5)(B,C,5), (B,E,6)(B,E,6), (A,E,7)(A,E,7), (A,D,8)(A,D,8), (C,D,9)(C,D,9).

Process each edge:

  1. (D,E,2)(D, E, 2): Add. Forest: DE\\{D-E\\}. Cost: 2.
  2. (C,E,3)(C, E, 3): Add. Forest: CDE\\{C-D-E\\}. Cost: 5.
  3. (A,B,4)(A, B, 4): Add. Forest: AB\\{A-B\\}, CDE\\{C-D-E\\}. Cost: 9.
  4. (B,C,5)(B, C, 5): Add (connects two components). Forest: ABCDE\\{A-B-C-D-E\\}. Cost: 14.
  5. (B,E,6)(B, E, 6): Skip (creates cycle BCDEBB-C-D-E-B).
  6. (A,E,7)(A, E, 7): Skip (creates cycle).
  7. (A,D,8)(A, D, 8): Skip (creates cycle).
  8. (C,D,9)(C, D, 9): Skip (creates cycle).

MST: (D,E,2)(D,E,2), (C,E,3)(C,E,3), (A,B,4)(A,B,4), (B,C,5)(B,C,5). Total weight: 14.

Worked Example: Prim's Algorithm

Find the MST of the same graph starting from vertex AA.

Edges from AA: (A,B,4)(A,B,4), (A,E,7)(A,E,7), (A,D,8)(A,D,8). Minimum: (A,B,4)(A,B,4). Tree: A,B\\{A, B\\}. Cost: 4.

Edges crossing cut: (B,C,5)(B,C,5), (B,E,6)(B,E,6), (A,E,7)(A,E,7), (A,D,8)(A,D,8). Minimum: (B,C,5)(B,C,5). Tree: A,B,C\\{A, B, C\\}. Cost: 9.

Edges crossing cut: (C,E,3)(C,E,3), (C,D,9)(C,D,9), (B,E,6)(B,E,6), (A,E,7)(A,E,7), (A,D,8)(A,D,8). Minimum: (C,E,3)(C,E,3). Tree: A,B,C,E\\{A, B, C, E\\}. Cost: 12.

Edges crossing cut: (D,E,2)(D,E,2), (C,D,9)(C,D,9), (A,D,8)(A,D,8). Minimum: (D,E,2)(D,E,2). Tree: A,B,C,D,E\\{A, B, C, D, E\\}. Cost: 14.

MST: (A,B,4)(A,B,4), (B,C,5)(B,C,5), (C,E,3)(C,E,3), (D,E,2)(D,E,2). Total weight: 14 (same as Kruskal’s).