0% found this document useful (0 votes)
15 views

MST

The document discusses spanning trees and minimum spanning trees (MST) in graph theory, explaining their definitions and importance. It details two algorithms for finding MSTs: Kruskal's algorithm and Prim's algorithm, including their steps and complexities. The document also provides examples and outlines the Union-Find data structure necessary for Kruskal's algorithm.

Uploaded by

sharvari.backup1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

MST

The document discusses spanning trees and minimum spanning trees (MST) in graph theory, explaining their definitions and importance. It details two algorithms for finding MSTs: Kruskal's algorithm and Prim's algorithm, including their steps and complexities. The document also provides examples and outlines the Union-Find data structure necessary for Kruskal's algorithm.

Uploaded by

sharvari.backup1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

10.

Trees

Dr. Swati Agarwal


Agenda

1 Spanning Tree

2 Minimum Spanning Tree

3 Kruskal’s Algorithm

4 Prim’s Algorithm

April 23, 2020 Dr. Swati Agarwal 10. Trees 2/10


Spanning Tree

Spanning tree of a graph is a subgraph that contains all the


vertices and is also a tree (connected but acyclic).
A graph can have many spanning tree.

April 23, 2020 Dr. Swati Agarwal 10. Trees 3/10


Minimum Spanning Tree

Problem: Given connected, undirected graph G = (V , E )


where each edge (u, v ) has weight w (u, v ). Find acyclic set
T ⊆ E connecting
P all vertices in V with minimal weight
w (T ) = (u,v )∈T w (u, v ).

An acyclic set connecting all vertices is called a spanning tree.


We want to find a spanning tree of minimal weight. We use
minimum spanning tree as short for minimum weight spanning
tree).
MST problem has many applications
• For example, think about connecting cities with minimal
amount of wire or roads (cities are vertices, weight of edges
are distances between city pairs).

April 23, 2020 Dr. Swati Agarwal 10. Trees 4/10


Minimum Spanning Tree

Example:
8 7
b c d
4 2 9

a 11 i 14 e
4
7
8 10
h g f
1 2

• Weight of MST is 4 + 8 + 7 + 9 + 2 + 4 + 1 + 2 = 37
• MST is not unique: e.g. (b, c) can be exchanged with (a, h)
• However, the cost remains the same.

April 23, 2020 Dr. Swati Agarwal 10. Trees 5/10


Minimum Spanning Tree
GENERIC - MST (G , w )
1. A ← φ
2. while A does not form a spanning tree
3. find an edge (u , v ) that is safe for A
4. A ← A ∪ {( u , v )}
5. return A

April 23, 2020 Dr. Swati Agarwal 10. Trees 6/10


Minimum Spanning Tree
GENERIC - MST (G , w )
1. A ← φ
2. while A does not form a spanning tree
3. find an edge (u , v ) that is safe for A
4. A ← A ∪ {( u , v )}
5. return A

We will discuss two algorithms, Kruskal’s algorithm and Prim’s algorithm.

1. In Kruskal’s algorithm, the set A is a forest. The safe edge added to


A is always a least-weight edge in the graph that connects two
distinct components.
2. In Prim’s algorithm, the set A forms a single tree. The safe edge
added to A is always a least-weight edge connecting the tree to a
vertex not in the tree.

April 23, 2020 Dr. Swati Agarwal 10. Trees 6/10


Kruskal’s Algorithm
MST - KRUSKAL (G , w )
1. A ← φ
2. for each vertex v ∈ V [G ]
3. do MAKE - SET ( v )
4. sort E into increasing order by weight w
5. for each edge (u, v ) ∈ E ,
6. if FIND - SET ( u ) 6= FIND - SET ( v )
7. then A ← A ∪ {( u , v )}
8. UNION (u , v )
9. return A

April 23, 2020 Dr. Swati Agarwal 10. Trees 7/10


Union-Find
We need (Union-Find) data structure that supports:

Make-set(v): Create set consisting of v


Union-set(u, v): Unite set containing u and set containing v
Find-set(u): Return unique representative for set containing
u

April 23, 2020 Dr. Swati Agarwal 10. Trees 8/10


Prim’s Algorithm
MST - PRIM (G , w , r )
1. T ← φ;
2. U = { 1 };
3. while ( U 6= V ){
4. let (u , v ) be the lowest cost edge
such that u ∈ U and v ∈ V - U ;
5. T = T ∪ {( u , v )}
6. U = U ∪ {v}
7. }

April 23, 2020 Dr. Swati Agarwal 10. Trees 9/10


Complexities
Algorithm Complexity
Prim’s O(E + V log V )
O(E log E )
Kruskal’s
equivalently, O(E log V )
In a complete graph, E = V 2 therefore for Prim’s algorithm,
O(E log V 2 ) = O(2 ∗ E log V ) = O(E log V )
refer to class notes for examples for both algorithms.

April 23, 2020 Dr. Swati Agarwal 10. Trees 10/10

You might also like