Loading lessons...
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 MST
1. Kruskal's Algorithm (Edge-Centric Greedy)
- Remove self-loops and keep only the minimum-weight edge among parallel edges.
- Sort all edges in ascending order of weight.
- For each edge (u, v) in sorted order: if
findSet(u) != findSet(v), add (u, v) to the MST and callunionSets(u, v); stop once |V| - 1 edges are chosen.
- Deck Trace (
S,A,B,C,D,TGraph):
- Add least-cost edges of weight
2:BD (2)andDT (2). - Add weight
3edges:AC (3)andCD (3). - Skip edges of weight
4, 5, 6because each creates a cycle (circuit)! - Add
SA (7)to connectS- 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)
- Pick an arbitrary starting vertex S and mark it in the MST.
- 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).
- 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 Path
3. Shortest Path Algorithms
- Dijkstra's Algorithm (Single-Source, Non-Negative Weights):
- Initialize
dist[source] = 0anddist[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)
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 edgesWorked 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) | Weight | Action |
|---|---|---|
| BD | 2 | accept |
| DT | 2 | accept |
| AC | 3 | accept |
| CD | 3 | accept (joins {A,C} with {B,D,T}) |
| CB | 4 | reject-C and B already connected (cycle) |
| BT | 5 | reject-cycle |
| AB | 6 | reject-cycle |
| SA | 7 | accept → 5 edges = V − 1, stop |
| SC | 8 | (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:
| Step | Tree vertices | Cheapest edge leaving the tree | Added |
|---|---|---|---|
| 1 | S | SA 7, SC 8 | SA (7) |
| 2 | S, A | AC 3, AB 6, SC 8 | AC (3) |
| 3 | S, A, C | CD 3, CB 4, AB 6 | CD (3) |
| 4 | S, A, C, D | BD 2, DT 2, CB 4 | BD (2) |
| 5 | S, A, C, D, B | DT 2, BT 5 | DT (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)
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] = uWorked 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) | Relaxations | s | 2 | 3 | 4 | 5 | 6 | 7 | t |
|---|---|---|---|---|---|---|---|---|---|
| s (0) | 2=9, 6=14, 7=15 | 0 | 9 | ∞ | ∞ | ∞ | 14 | 15 | ∞ |
| 2 (9) | 3=33 | 9 | 33 | ∞ | ∞ | 14 | 15 | ∞ | |
| 6 (14) | 3=32, 5=44 | 32 | ∞ | 44 | 14 | 15 | ∞ | ||
| 7 (15) | 5=35, t=59 | 32 | ∞ | 35 | 15 | 59 | |||
| 3 (32) | 5=34, t=51 | 32 | ∞ | 34 | 51 | ||||
| 5 (34) | 4=45, t=50 | 45 | 34 | 50 | |||||
| 4 (45) | none (3 and t are not improved) | 45 | 50 | ||||||
| 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.
📝 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 (weight17). - 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)).