LVR Shankar MST
LVR Shankar MST
Dr. S.Shankar
NIT Warangal
1
Problem: Laying Telephone Wire
Central office
2
Wiring: Naive Approach
Central office
Expensive!
3
Wiring: Better Approach
Central office
4
Spanning Tree and Minimum Spanning Tree
5
General Properties of MST
1) Remove any edge from the spanning tree, then it becomes disconnected.
Therefore, we cannot remove any edge from the spanning tree.
2) Add an edge to the spanning tree then it creates a loop. Therefore, we
cannot add any edge to the spanning tree.
3) In a graph, each edge has a distinct weight, then there exists only a single
and unique minimum spanning tree. If the edge weight is not distinct, then
there can be more than one minimum spanning tree.
4) A complete undirected graph can have an nn-2 number of spanning trees.
5) Every connected and undirected graph contains atleast one spanning tree.
6) The disconnected graph does not have any spanning tree.
7) In a complete graph, we can remove maximum (e-n+1) edges to construct a
spanning tree. The number of spanning trees that can be made from the above
complete graph equals to nn-2 = 44-2 = 16.
Therefore, 16 spanning trees can be created from the above
graph. The maximum number of edges that can be removed to
construct a spanning tree equals to e-n+1 = 6 - 4 + 1 = 3.
6
Contd….
What is a minimum spanning tree?
The minimum spanning tree is a spanning tree whose sum of the
edges is minimum. Consider the below graph that contains the
edge weight:
•The first spanning tree is a tree in which we have removed the
edge between the vertices 1 and 5.
The sum of the edges of the above tree is (1 + 4 + 5 + 2): 12
•The second spanning tree is a tree in which we have removed the
edge between the vertices 1 and 2
The sum of the edges of the above tree is (3 + 2 + 5 + 4) : 14
•The third spanning tree is a tree in which we have removed the
edge between the vertices 2 and 3 .
The sum of the edges of the above tree is (1 + 3 + 2 + 5) : 11
•The fourth spanning tree is a tree in which we have removed the
edge between the vertices 3 and 4 .
The sum of the edges of the above tree is (1 + 3 + 2 + 4) : 10. The
edge cost 10 is minimum so it is a minimum spanning tree.
7
Minimum-cost spanning trees
▪ Suppose you have a connected undirected graph with a weight (or cost)
associated with each edge
▪ The cost of a spanning tree would be the sum of the costs of its edges
▪ A minimum-cost spanning tree is a spanning tree that has the lowest cost.
16 16
A B A B
21 11 6 11 6
19 5 5
F C F C
33 14
10
E 18 D E 18 D
A connected, undirected graph A minimum-cost spanning tree
8
How Can We Generate a MST?
9 b 9 b
a 2 6 a 2 6
d d
4 5 4 5
5 4 5 4
5 e 5 e
c c
9
PRIM’S and KRUSKAL
• Prim's algorithm and Kruskal's algorithm are greedy algorithms used to find the
minimum spanning tree in a weighted graph.
10
Prims algorithm
11
PRIMS Vs Krushkal
1.In prim's algorithm, we choose the nodes greedily i.e. the node is chosen at a point
such that the edge weight for this node is minimum while in the case
of Krushkal's algorithm we choose the minimum weight edges greedily.
2.In Kruskal's algorithm, sort the edges by weight in increasing order and then select
the N−1 edges without cycle having minimum weight; but in the case
of Prim's algorithm, we choose the vertices considering the edge weights, and even if
the number of edges goes high it still stays efficient.
3.Time complexity for the Prim's algorithm is O(N2) while for Krushkal it is O(ElogN).
4.Prim's algorithm only works on a connected graph because to select the adjacent
node for a selected node with minimum weight, while Krushkal can work for the
disconnected component as well.
12
Kruskal algorithim
13