Lesson 26 +40 XP

Minimum Spanning Trees & Shortest Path Algorithms

Minimum Spanning Trees & Shortest Path Algorithms

A Spanning Tree of a connected undirected graph G = (V, E) is a subgraph that includes all V vertices with |V| - 1 edges and no cycles (it is both minimally connected and maximally acyclic; a complete graph Kn has nn-2 spanning trees by Cayley's formula). A Minimum Spanning Tree (MST) minimizes the sum of edge weights.

Kruskal vs Prim MSTKruskal vs Prim MST

1. Kruskal's Algorithm (Edge-Centric Greedy)

  1. Remove self-loops and keep only the minimum-weight edge among parallel edges.
  2. Sort all edges in ascending order of weight.
  3. For each edge (u, v) in sorted order: if findSet(u) != findSet(v), add (u, v) to the MST and call unionSets(u, v); stop once |V| - 1 edges are chosen.
  • Deck Trace (S,A,B,C,D,T Graph):
  1. Add least-cost edges of weight 2: BD (2) and DT (2).
  2. Add weight 3 edges: AC (3) and CD (3).
  3. Skip edges of weight 4, 5, 6 because each creates a cycle (circuit)!
  4. Add SA (7) to connect S - MST complete with 5 edges (total weight = 2 + 2 + 3 + 3 + 7 = 17)!
  • Time Complexity: O(E log E) (dominated by sorting; ideal for sparse graphs).

2. Prim's Algorithm (Vertex-Centric Greedy)

  1. Pick an arbitrary starting vertex S and mark it in the MST.
  2. Repeatedly select the minimum-weight edge (u, v) crossing the cut between visited vertices u and unvisited vertices v using a Min-Heap (Priority Queue).
  3. Add v to the MST and update neighbor keys until all vertices are included.
  • Time Complexity: O((V + E) log V) with a Binary Heap, or O(V2) with an Adjacency Matrix (ideal for dense graphs).

Dijkstra Shortest PathDijkstra Shortest Path

3. Shortest Path Algorithms

  • Dijkstra's Algorithm (Single-Source, Non-Negative Weights):
  • Initialize dist[source] = 0 and dist[v] = ∞ for all other vertices.
  • Repeatedly extract the unvisited vertex u with minimum dist[u] from a Min-Heap and relax every outgoing edge (u, v): if (dist[u] + w(u, v) < dist[v]) dist[v] = dist[u] + w(u, v);
  • Time: O((V + E) log V)
  • fails if negative edge weights exist!
  • Bellman-Ford Algorithm: Relaxes all E edges V - 1 times in O(V · E) - supports negative edge weights and detects negative-weight cycles on a V-th pass.
  • Floyd-Warshall Algorithm (All-Pairs Shortest Path): Dynamic Programming over intermediate vertices k = 1 … V using dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]) in O(V3).

Spanning Trees: Properties and Applications

A spanning tree of a connected graph with V vertices contains all V vertices and exactly V − 1 edges, with no cycle.

  • Removing any edge disconnects it; adding any edge creates exactly one cycle.
  • A connected graph can have many spanning trees (a complete graph Kₙ has nⁿ⁻² of them).
  • The minimum spanning tree (MST) has the least total weight; it is unique if all edge weights are distinct.

Applications: designing cable, road, water and electrical networks with the least wire; network routing protocols; cluster analysis; approximating the travelling salesman problem.

Kruskal's Algorithm (pseudocode)

TEXT
KRUSKAL(G)
    T = ∅
    for each vertex v: MAKE-SET(v)
    sort the edges by weight (ascending)
    for each edge (u, v) in that order:
        if FIND(u) ≠ FIND(v):          // different trees → no cycle
            T = T ∪ {(u, v)}
            UNION(u, v)
    return T                           // stops once T has V − 1 edges

Worked Example (graph from the notes)

Vertices S, A, B, C, D, T; edges BD 2, DT 2, AC 3, CD 3, CB 4, BT 5, AB 6, SA 7, SC 8.

Kruskal

Edge (sorted)WeightAction
BD2accept
DT2accept
AC3accept
CD3accept (joins {A,C} with {B,D,T})
CB4reject-C and B already connected (cycle)
BT5reject-cycle
AB6reject-cycle
SA7accept → 5 edges = V − 1, stop
SC8(not needed)

MST = {BD, DT, AC, CD, SA}, total weight = 2 + 2 + 3 + 3 + 7 = 17.

Prim from S - always add the cheapest edge leaving the tree:

StepTree verticesCheapest edge leaving the treeAdded
1SSA 7, SC 8SA (7)
2S, AAC 3, AB 6, SC 8AC (3)
3S, A, CCD 3, CB 4, AB 6CD (3)
4S, A, C, DBD 2, DT 2, CB 4BD (2)
5S, A, C, D, BDT 2, BT 5DT (2)

Same total, 17 - both greedy algorithms always find a minimum spanning tree (the tree itself may differ when there are equal weights).

Dijkstra's Algorithm (pseudocode)

TEXT
DIJKSTRA(G, w, s)
    for each vertex v: d[v] = ∞, prev[v] = NIL
    d[s] = 0
    Q = all vertices (min-priority queue keyed on d)
    while Q not empty:
        u = EXTRACT-MIN(Q)             // closest unfinished vertex
        for each edge (u, v):
            if d[u] + w(u, v) < d[v]:  // relaxation
                d[v] = d[u] + w(u, v)
                prev[v] = u

Worked Example (directed graph)

Edges: s→2 (9), s→6 (14), s→7 (15), 2→3 (24), 6→3 (18), 6→5 (30), 6→7 (5), 7→5 (20), 7→t (44), 3→5 (2), 3→t (19), 5→4 (11), 5→t (16), 4→3 (6), 4→t (6).

Pick (d)Relaxationss234567t
s (0)2=9, 6=14, 7=1509∞∞∞1415∞
2 (9)3=33933∞∞1415∞
6 (14)3=32, 5=4432∞441415∞
7 (15)5=35, t=5932∞351559
3 (32)5=34, t=5132∞3451
5 (34)4=45, t=50453450
4 (45)none (3 and t are not improved)4550
t (50)-50

Shortest distances: 2 = 9, 6 = 14, 7 = 15, 3 = 32, 5 = 34, 4 = 45, t = 50. Shortest path to t: s → 6 → 3 → 5 → t (14 + 18 + 2 + 16 = 50).

With a binary-heap priority queue Dijkstra runs in O((V + E) log V). It requires non-negative edge weights; with negative weights use Bellman-Ford.

🎯 END-TERM EXAM PREP

📝 IPU End-Term Exam Corner (5-10 Marks)

  • Q1. Differentiate between Prim's and Kruskal's algorithms for finding a Minimum Spanning Tree. Trace Kruskal's algorithm on a weighted graph. (10 Marks)
  • Write: Spanning tree properties (|V|-1 edges, nn-2 trees), Edge-centric (O(E log E) + DSU) vs Vertex-centric (O((V+E)log V) + Min-Heap), and the BD(2), DT(2), AC(3), CD(3), SA(7) trace (weight 17).
  • Q2. Explain Dijkstra's Single-Source Shortest Path algorithm with its relaxation step and limitations. (7.5 Marks)
  • Write: Initialization (dist[src] = 0, others ∞), edge relaxation formula, Min-Heap O((V+E)log V) complexity, and note that negative edge weights require Bellman-Ford (O(V · E)).