Minimum Spanning Trees

Weighted Graphs Minimum Spanning Trees Kruskal’s Algorithm Prim’s Algorithm

CS 5633 Analysis of Algorithms Chapter 22: Slide – 1 Weighted Graphs

⊲ weighted graphs wg problems A weighted graph is a graph in which each edge minimum spanning trees (u, v) has a weight w(u, v). Each weight is a real mst analysis kruskal number. Weights can represent distance, cost, kruskal example kruskal analysis time, capacity, etc. prim prim example 6 8 prim analysis bc d 1 5 4 7 a 2 i 9 e 43 7 h 8 g 5 f

CS 5633 Analysis of Algorithms Chapter 22: Slide – 2 Weighted Graphs Problems weighted graphs Optimization Problems for Weighted Graphs: ⊲ wg problems minimum spanning trees Find a minimum . mst analysis kruskal Find shortest paths between vertices. kruskal example kruskal analysis Maximize flow from a source to a sink. prim prim example Traveling salesman problem. prim analysis Variations on Problems: Undirected/directed graphs Negative weights

CS 5633 Analysis of Algorithms Chapter 22: Slide – 3 Minimum Spanning Trees

weighted graphs wg problems A spanning tree T of a weighted, undirected graph minimum ⊲ spanning trees G is a subset of edges that form a (free) tree. mst analysis kruskal If G has |V | vertices, then T has |V |− 1 edges. kruskal example kruskal analysis prim prim example A minimum spanning tree (MST) is a spanning prim analysis tree with a minimum sum of weights.

Consider any of vertices S and V − S. Let E′ be the edges between S and S − V .

Theorem: A MST has a minimum weight edge (called a light edge) from E′.

CS 5633 Analysis of Algorithms Chapter 22: Slide – 4 Minimum Spanning Tree Analysis

weighted graphs wg problems Proof: minimum spanning trees ⊲ mst analysis kruskal Suppose T is a spanning tree with no such edge. kruskal example kruskal analysis We can show T cannot be a MST. prim prim example ′ prim analysis Let (u, v) be a minimum weight edge from E . T has a path between u and v. Some edge (x, y) on this path is in E′. Replacing (x, y) with (u, v) decreases the sum. Implies T is not a minimum spanning tree.

Algorithm idea: Find light edges without making cycles.

CS 5633 Analysis of Algorithms Chapter 22: Slide – 5 Kruskal’s Algorithm

weighted graphs wg problems MST-Kruskal minimum spanning (G, w) trees mst analysis T ← ∅ ⊲ kruskal kruskal example for each vertex v ∈ G.V kruskal analysis Make-Set prim (v) prim example for u, v G.E prim analysis each edge ( ) ∈ in ascending order if Find-Set(u) =6 Find-Set(v) T ← T ∪{(u, v)} Union(u, v) exit loop if all vertices are unioned return T

CS 5633 Analysis of Algorithms Chapter 22: Slide – 6 Kruskal Example

weighted graphs wg problems minimum spanning trees mst analysis kruskal ⊲ kruskal example kruskal analysis prim prim example prim analysis

CS 5633 Analysis of Algorithms Chapter 22: Slide – 7 Kruskal Analysis

weighted graphs wg problems Proof of correctness: (u, v) is a light edge between minimum spanning trees Find-Set(u) and the other vertices. mst analysis kruskal kruskal example Use disjoint-set forest for union-find, almost O(E). ⊲ kruskal analysis prim O E E prim example Use ( lg ) sorting algorithm or binary heap. prim analysis MST-Kruskal is O(E lg E)= O(E lg V ).

CS 5633 Analysis of Algorithms Chapter 22: Slide – 8 Prims’s Algorithm

weighted graphs wg problems MST-Prim minimum spanning (G,w,r) trees mst analysis for each vertex v in G kruskal kruskal example v.key ← ∞ kruskal analysis r.key ← ⊲ prim 0 prim example r.π nil prim analysis ← Q ← G.V while Q is not empty u ← Extract-Min(Q) for each v ∈ G.Adj[u] if v ∈ Q and w(u, v) < v.key v.π ← u Decrease-Key(Q, v, w(u, v))

CS 5633 Analysis of Algorithms Chapter 22: Slide – 9 Prim Example

weighted graphs wg problems minimum spanning trees mst analysis kruskal kruskal example kruskal analysis prim ⊲ prim example prim analysis

CS 5633 Analysis of Algorithms Chapter 22: Slide – 10 Prim Analysis

weighted graphs wg problems Proof of correctness: minimum spanning trees Q is the vertices not yet added to the tree. mst analysis kruskal If v ∈ Q, then (v.π,v) is the smallest edge from v kruskal example kruskal analysis to V − Q, and v.key is its weight. prim prim example The minimum value in Q must be a light edge ⊲ prim analysis between Q and V − Q.

Use binary heap for , O(V ) elts. O(V ) Extract-Mins is O(V lg V ). O(E) Decrease-Keys is O(E lg V ). MST-Prim is O(E lg V )= O(E lg E). Using Fibonocci heaps makes it O(E + V lg V ).

CS 5633 Analysis of Algorithms Chapter 22: Slide – 11