320 likes | 445 Views
Minimum Spanning Tree. Graph Theory Basics. - Anil Kishore. Definition. A spanning graph is a sub-graph that contains all the vertices of the graph If a spanning sub-graph is connected and has no cycles, its a tree, a spanning tree. 2. 5. 2. 5. 1. 1. 4. 4. 6. 6. 3. 3. MST.
E N D
Minimum Spanning Tree Graph Theory Basics - Anil Kishore
Definition • A spanning graph is a sub-graph that containsall the vertices of the graph • If a spanning sub-graph is connected and has no cycles, its a tree, a spanning tree 2 5 2 5 1 1 4 4 6 6 3 3
MST • A graph can have many Spanning Trees • Given a weighted ( edge weights ) graph, our goal is to find a spanning tree with minimum sum of edge weights in it, Minimum Spanning Tree (MST) 3 3 B E B E 2 2 1 4 1 4 A 7 A 7 D D F 5 F 5 10 10 C C
Simple application • Government is planning to connect cities by roads and has estimated the cost of construction of roads between some pairs of cities • Find the minimum cost to construct roads such that any city is reachable from any other city
Prim’s Algorithm Algorithm Prim(G) Initialize an empty priority queue Q Initialize an empty set S to mark already finished vertices FOR-each u in V f[u] := +infinity Insert u into Q end-for WHILE Q is not empty u := delete minimum element from Q add u to S FOR-each edge e = ( u, v ) if ( v not in S ) and ( w(e) < f[v] ) decrease f[v] to w(e) end-if end-for end-while End-Prim
Running Prim’s Algorithm 3 E B 2 1 4 A 7 D 5 F 10 C
Running Prim’s Algorithm 3 E B 2 1 4 A 0 7 D 5 F 10 C
Running Prim’s Algorithm 2 3 E B 2 1 4 A 0 7 7 D 5 F 10 C
Running Prim’s Algorithm 2 3 E B 2 1 4 A 0 7 7 D 5 F 10 C
Running Prim’s Algorithm 2 3 3 E B 2 1 4 A 0 7 7 D 5 F 10 C
Running Prim’s Algorithm 2 3 3 E B 2 1 4 A 0 7 7 D 5 F 10 C
Running Prim’s Algorithm 2 3 3 E B 2 1 4 A 0 1 7 D 4 5 F 10 C
Running Prim’s Algorithm 2 3 3 E B 2 1 4 A 0 1 7 D 4 5 F 10 C
Running Prim’s Algorithm 2 3 3 E B 2 1 4 A 0 1 7 D 4 5 F 10 10 C
Running Prim’s Algorithm 2 3 3 E B 2 1 4 A 0 1 7 D 4 5 F 10 10 C
Running Prim’s Algorithm 2 3 3 E B 2 1 4 A 0 1 7 D 4 5 F 10 10 C
Running Prim’s Algorithm 3 E B 2 1 4 A 7 D 5 F 10 C MST
Complexity of Prim’s Algorithm • Using an array • O(m) decrease key operations, each O(1) • O(n) min-key operations, each O(n) • O( m + n2 ) • Using a binary heap ( priority queue • O(m) decrease key operations, each O(log n) • O(n) min-key operations, each O(log n) • O( m logn + n logn )
Kruskal’s Algorithm Algorithm Kruskal (G) Sort the edges of G in non-decreasing order of edge weights Initialize an empty tree T FOR-each edge e in sorted order if adding e to T does not for a cycle in T Add e to T end-if end-for End-Kruskal
Running Kruskal’s Algorithm 3 E B 2 1 4 A 7 D 5 F 10 C
Running Kruskal’s Algorithm 3 E B 2 1 4 A 7 D 5 F 10 C
Running Kruskal’s Algorithm 3 E B 2 1 4 A 7 D 5 F 10 C
Running Kruskal’s Algorithm 3 E B 2 1 4 A 7 D 5 F 10 C
Running Kruskal’s Algorithm 3 E B 2 1 4 A 7 D 5 F 10 C
Running Kruskal’s Algorithm 3 E B 2 1 4 A 7 D 5 F 10 C
Running Kruskal’s Algorithm 3 E B 2 1 4 A 7 D 5 F 10 C
Running Kruskal’s Algorithm 3 E B 2 1 4 A 7 D 5 F 10 C
Running Kruskal’s Algorithm 3 E B 2 1 4 A 7 D 5 F 10 C
Running Kruskal’s Algorithm 3 E B 2 1 4 A 7 D 5 F 10 C
Running Kruskal’s Algorithm 3 E B 2 1 4 A 7 D 5 F 10 C MST
Complexity of Kruskal’s Algorithm • Using the union-find data structure • O(m logn) for sorting edges • Simple implementation of union-find : O(log n) to find representative of a set • O(m logn) • Using Path compression of union-find : almost a constant per operation • O( m )
References • Introduction to Algorithms • Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein • http://cgm.cs.mcgill.ca/~avis/courses/251/2012/slides/04mst.pdf • http://ww3.algorithmdesign.net/handouts/MST.pdf - End -