Lecture 2 (Extra) - Minimum Spanning Tree
Lecture 2 (Extra) - Minimum Spanning Tree
CSE 2202: Algorithm Analysis and Design Laboratory September 23, 2024
Characteristics 3
• Same vertices: The spanning tree has the same number of vertices (V) as the original
graph.
• Edges: The spanning tree has 𝐸 = 𝑉−1 edges, where V is the number of vertices.
• Connected: The spanning tree must be connected, with only one component.
• Acyclic: The spanning tree cannot have any cycles.
• Total cost: The cost (or weight) of the spanning tree is the sum of its edge weights.
• Multiple trees: A graph can have several possible spanning trees.
CSE 2202: Algorithm Analysis and Design Laboratory September 23, 2024
Kruskal’s Algorithm 4
Kruskal’s algorithm to find the minimum cost spanning tree uses the greedy
approach.
Algorithm steps:
1. Sort all the edges in non-decreasing order of their weight.
2. Pick the smallest edge. Check if it forms a cycle with the spanning tree formed
so far. If the cycle is not formed, include this edge. Else, discard it.
3. Repeat step#2 until there are (V-1) edges in the spanning tree.
CSE 2202: Algorithm Analysis and Design Laboratory September 23, 2024
Kruskal’s Algorithm 5
Kruskal’s algorithm to find the minimum cost spanning tree uses the greedy
approach.
CSE 2202: Algorithm Analysis and Design Laboratory September 23, 2024
Kruskal’s Algorithm 6
CSE 2202: Algorithm Analysis and Design Laboratory September 23, 2024
Kruskal’s Algorithm 7
CSE 2202: Algorithm Analysis and Design Laboratory September 23, 2024
Kruskal’s Algorithm 8
CSE 2202: Algorithm Analysis and Design Laboratory September 23, 2024
Kruskal’s Algorithm 9
CSE 2202: Algorithm Analysis and Design Laboratory September 23, 2024
Kruskal’s Algorithm 10
CSE 2202: Algorithm Analysis and Design Laboratory September 23, 2024
Kruskal’s Algorithm 11
CSE 2202: Algorithm Analysis and Design Laboratory September 23, 2024
Kruskal’s Algorithm 12
CSE 2202: Algorithm Analysis and Design Laboratory September 23, 2024
Kruskal’s Algorithm 13
CSE 2202: Algorithm Analysis and Design Laboratory September 23, 2024
Kruskal’s Algorithm 14
CSE 2202: Algorithm Analysis and Design Laboratory September 23, 2024
Kruskal’s Algorithm 15
CSE 2202: Algorithm Analysis and Design Laboratory September 23, 2024
Kruskal’s Algorithm 16
CSE 2202: Algorithm Analysis and Design Laboratory September 23, 2024
Kruskal’s Algorithm 17
CSE 2202: Algorithm Analysis and Design Laboratory September 23, 2024
Kruskal’s Algorithm 18
CSE 2202: Algorithm Analysis and Design Laboratory September 23, 2024
Kruskal’s Algorithm 19
CSE 2202: Algorithm Analysis and Design Laboratory September 23, 2024
Kruskal’s Algorithm 20
For cycle detection, this algorithm uses Union- Find algorithms.
• For each edge, the algorithm checks if the vertices of the edge belong to different
sets using the Find operation.
• If they are in different sets, it adds the edge to the MST.
• It uses Union to merge the sets of the two vertices so that they are considered part
of the same connected component.
CSE 2202: Algorithm Analysis and Design Laboratory September 23, 2024
Kruskal’s Algorithm 21
For example, If we have vertices A, B, and C, and an edge between A and B, the Union
operation will merge the sets containing A and B. The Find operation will ensure no
cycles are formed by checking if A and B are already in the same set.
CSE 2202: Algorithm Analysis and Design Laboratory September 23, 2024
What happens in a directed graph? 22
1 2
A B D
4
1
C
CSE 2202: Algorithm Analysis and Design Laboratory September 23, 2024
Time Complexity 23
• Sorting of edges takes O(E * logE) time.
• The find and union operations can take at most O(logV) time. So, it becomes O(E logV)
• Overall complexity is O(E * logE + E * logV) time.
• The value of E can be at most O(V2), so O(logV) and O(logE) are the same. Therefore, the
overall time complexity is O(E * logE) or O(E*logV)
CSE 2202: Algorithm Analysis and Design Laboratory September 23, 2024
References 24
• Introduction to Algorithm, 4th ed, Leiserson, Charles Eric, Ronald L. Rivest, Thomas H.
Cormen, and Clifford Stein.
• https://www.geeksforgeeks.org/kruskals-minimum-spanning-tree-algorithm-greedy-algo-2/
CSE 2202: Algorithm Analysis and Design Laboratory September 23, 2024