Download presentation
Presentation is loading. Please wait.
1
Lecture 22 Minimum Spanning Tree
2
Connect the cities using minimum length cable!
Bikaner 325 Jaipur 1843 142 50 Kota 849 ChoMu 1205 337 Reengus 1743 802 1387 1099 Ajmer 1233 Jodhpur 1120 Sikar
3
Minimum Spanning Tree (MST) is a Spanning tree of a weighted graph with minimum total edge weight!
4
D 10 1 E A 6 7 9 3 F C 4 8 5 2 B G
5
Is MST unique?
6
MST Cycle Property 8 4 2 3 6 7 9 C e f 8 4 2 3 6 7 9 e C f
7
Cut Vertices 2 parts Edges across the parts U V 7 c 4 a 9 5 2 f e 8 8
3 d b 7
8
Cut Property U V U V 10 10 f f 4 4 9 9 5 5 2 2 8 8 8 3 8 3 e e 7 7
9
Prim-Jarnik’s MST Algorithm
Start from s and grow MST as a cloud The sets are cloud and not-cloud At each step, pick smallest edge from cut, and move the vertex to the cloud Update the edges in cut
10
Example
11
B D C A F E 7 4 2 8 5 3 9 7 D 2 B 4 8 9 5 2 F C 8 3 8 E A 7 7 B D C A F E 7 4 2 8 5 3 9 B D C A F E 7 4 2 8 5 3 9
12
B D C A F E 7 4 2 8 5 3 9 B D C A F E 7 4 2 8 5 3 9
13
Analysis m log n for update n log n for insert/remove
So O((m+n) log n)
14
Kruskal’s MST Algorithm
greedily add the edge with minimum weight to the MST ensure that the added edge does not form a cycle
15
Example
16
B G C A F D 4 1 3 5 10 2 8 7 6 E H 11 9 B G C A F D 4 1 3 5 10 2 8 7 6 E H 11 9 B G C A F D 4 1 3 5 10 2 8 7 6 E H 11 9 B G C A F D 4 1 3 5 10 2 8 7 6 E H 11 9
17
four steps two steps B G C A F D 4 1 3 5 10 2 8 7 6 E H 11 9 B G C A F
18
Kruksa’s Algorithm MST-Kruksal (G) A = ϕ for each vertex v in G.V
MAKE-SET (v) Sort edges in non decreasing order of weights for each edge (u,v) taken in non-decreasing order if FIND-SET(u)≠ FIND-SET(v) A = A U {(u,v)} UNION (Su, Sv) return A;
19
Analysis of Kruskal’s Algorithm
m log n
20
Minimum Spanning Tree is only defined for undirected graphs!
21
How do we check if a cycle is formed when an edge is included?
22
Cycle is formed if and only if u and v are already connected!
23
Maintain a data structure of sets!
24
Union-Find Partition Structures
25
Partitions with Union-Find Operations
union(A,B ): Return the set A U B, destroying the old A and B find(p): Return the set containing the element at position p
26
List-based Implementation
27
Time Complexity When doing a union, always move elements from the smaller set to the larger set Total time needed to do n unions and finds is O(n log n).
28
The basic difference is in which edge you choose to add next to the spanning tree in each step.
In Prim's, you always keep a connected component, starting with a single vertex. You look at all edges from the current component to other vertices and find the smallest among them. You then add the neighbouring vertex to the component, increasing its size by 1. In N-1 steps, every vertex would be merged to the current one if we have a connected graph. In Kruskal's, you do not keep one connected component but a forest. At each stage, you look at the globally smallest edge that does not create a cycle in the current forest. Such an edge has to necessarily merge two trees in the current forest into one. Since you start with N single-vertex trees, in N-1 steps, they would all have merged into one if the graph was connected.
29
What is the difference between Kruskal’s and Prim’s Algorithm?
• Prim’s algorithm initializes with a node, whereas Kruskal’s algorithm initiates with an edge. • Prim’s algorithms span from one node to another while Kruskal’s algorithm select the edges in a way that the position of the edge is not based on the last step. • In prim’s algorithm, graph must be a connected graph while the Kruskal’s can function on disconnected graphs too. • Prim’s algorithm has a time complexity of O(V2), and Kruskal’s time complexity is O(logV).
30
Correctness of Kruskal’s Algorithm
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.