 # Minimum Spanning Tree Graph Theory Basics - Anil Kishore.

## Presentation on theme: "Minimum Spanning Tree Graph Theory Basics - Anil Kishore."— Presentation transcript:

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 1 6 5 4 3 2 1 6 5 4 3 2

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) A F E D C B 2 7 1 10 5 4 3 A F E D C B 2 7 1 5 4 3

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 A F E D C B 2 7 1 10 5 4 3

Running Prim’s Algorithm A F E D C B 2 7 1 10 5 4 3 0

Running Prim’s Algorithm A F E D C B 2 7 1 10 5 4 3 0 7 2

Running Prim’s Algorithm A F E D C B 2 7 1 10 5 4 3 0 7 2

Running Prim’s Algorithm A F E D C B 2 7 1 10 5 4 3 0 7 2 3

Running Prim’s Algorithm A F E D C B 2 7 1 10 5 4 3 0 7 2 3

Running Prim’s Algorithm A F E D C B 2 7 1 10 5 4 3 0 1 2 3 4

Running Prim’s Algorithm A F E D C B 2 7 1 10 5 4 3 0 1 2 3 4

Running Prim’s Algorithm A F E D C B 2 7 1 10 5 4 3 0 1 2 3 4

Running Prim’s Algorithm A F E D C B 2 7 1 10 5 4 3 0 1 2 3 4

Running Prim’s Algorithm A F E D C B 2 7 1 10 5 4 3 0 1 2 3 4

Running Prim’s Algorithm A F E D C B 2 7 1 10 5 4 3 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 + n 2 ) 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 A F E D C B 2 7 1 10 5 4 3

Running Kruskal’s Algorithm A F E D C B 2 7 1 10 5 4 3

Running Kruskal’s Algorithm A F E D C B 2 7 1 10 5 4 3

Running Kruskal’s Algorithm A F E D C B 2 7 1 10 5 4 3

Running Kruskal’s Algorithm A F E D C B 2 7 1 10 5 4 3

Running Kruskal’s Algorithm A F E D C B 2 7 1 10 5 4 3

Running Kruskal’s Algorithm A F E D C B 2 7 1 10 5 4 3

Running Kruskal’s Algorithm A F E D C B 2 7 1 10 5 4 3

Running Kruskal’s Algorithm A F E D C B 2 7 1 10 5 4 3

Running Kruskal’s Algorithm A F E D C B 2 7 1 10 5 4 3

Running Kruskal’s Algorithm A F E D C B 2 7 1 10 5 4 3 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 -