Kruskals-Algorithm
Kruskals-Algorithm
GROUP 9 PRESENTATION
Steps for Kruskal's Algorithm
1 Sort the edges
Sort all the edges in non-decreasing order of their weights.
4 Repeat
Repeat this process until you've added enough edges to connect all the
vertices (i.e., the MST will have exactly V−1 edges, where V is the
number of vertices).
DIAGRAMATIC EXAMPLE What we will
do:
Keep including minimum edges as long they do not form
cycles
Wt = 2 + 2 + 1 + 1 + 4 + 6
= 16
Steps Applied on the Given Graph :
Pick the smallest edge that does not form a cycle and add
it to the MST.
o (f, g) = 1 → Added ✅
o (b, d) = 1 → Added ✅
o (a, b) = 2 → Added ✅
o (d, e) = 2 → Added ✅
o (j, i) = 4 → Added ✅
o (g, c) = 6 → Added ✅
• We stop here because we have included (n-1) edges,
where n is the number of nodes.
Final MST and Its Weight:
The Union-Find data structure is used to efficiently track which vertices are
class UnionFind:
connected in the MST. The find() function determines the representative of
def __init__(self, n):
a set, and the union() function merges two sets if they are not already
self.parent = list(range(n))
connected.
self.rank = [0] * n
def find(self, x):
if self.parent[x] != x:
self.parent[x] = self.find(self.parent[x])
return self.parent[x]
def union(self, x, y):
rootX = self.find(x)
rootY = self.find(y)
if rootX != rootY:
if self.rank[rootX] > self.rank[rootY]:
self.parent[rootY] = rootX
elif self.rank[rootX] < self.rank[rootY]:
self.parent[rootX] = rootY
else:
self.parent[rootY] = rootX
self.rank[rootX] += 1
Python Implementation: Kruskal's Algorithm
Kruskal's Algorithm Explanation
This code demonstrates how to use Kruskal's Algorithm to find the MST of a
graph. The output shows the edges in the MST and the total weight of the
MST.
Example Output
Edges in the MST: [(1, 2, 2), (0, 2, 3), (1, 3, 5),
(3, 4, 7)]
Total weight of MST: 17
The output shows the edges in the MST and the total weight of the
MST. The MST is a subset of the edges that connects all the
vertices in the graph without any cycles and with the minimum
possible total edge weight.
Applications of Kruskal's Algorithm
Network Design: Kruskal's Algorithm Geographic Mapping: It can be used Circuit Design: Kruskal's Algorithm
can be used to design efficient to find the shortest paths between can be applied to design efficient
networks by finding the minimum locations on a map, minimizing the circuits by finding the minimum
spanning tree that connects all the total distance traveled. spanning tree that connects all the
nodes in the network. components in the circuit.
Key Takeaways
Kruskal's Algorithm is a powerful tool for finding the minimum
spanning tree of a graph. It is efficient, easy to implement, and has
numerous applications in various fields. Understanding the
algorithm and its implementation can be valuable for solving
problems related to network design, geographic mapping, and
circuit design.
THE END