0% found this document useful (0 votes)
29 views4 pages

Homework Python Project

The document describes Kruskal's algorithm for finding the minimum spanning tree of a graph. It provides the steps of the algorithm and then shows an implementation in Python. The key steps are: (1) sorting the edges by weight from lowest to highest, (2) adding the lowest weight edge that does not create a cycle to the spanning tree, and (3) repeating step 2 until all vertices are connected.

Uploaded by

Big AL
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)
29 views4 pages

Homework Python Project

The document describes Kruskal's algorithm for finding the minimum spanning tree of a graph. It provides the steps of the algorithm and then shows an implementation in Python. The key steps are: (1) sorting the edges by weight from lowest to highest, (2) adding the lowest weight edge that does not create a cycle to the spanning tree, and (3) repeating step 2 until all vertices are connected.

Uploaded by

Big AL
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/ 4

Homework

KRUSKAL’S ALGORITHM EXAMPLE

Step 1 : Choose the edge with the least weight, if there are more than 1,
choose anyone

Step 2 : Choose the next shortest edge and add it

Step 3 : Choose the next shortest edge that doesn't


create a cycle and add it
Homework

Step 4 : Choose the next shortest


edge that doesn't create a cycle
and add it

Step 5 : Repeat until you have a


spanning tree
Homework
PYTHON IMPLEMETATION

# Kruskal's algorithm in Python


class Graph:
def __init__(self, vertices):
self.V = vertices
self.graph = []
def add_edge(self, u, v, w):
self.graph.append([u, v, w])

# Search function
def find(self, parent, i):
if parent[i] == i:
return i
return self.find(parent, parent[i])
def apply_union(self, parent, rank, x, y):
xroot = self.find(parent, x)
yroot = self.find(parent, y)
if rank[xroot] < rank[yroot]:
parent[xroot] = yroot
elif rank[xroot] > rank[yroot]:
parent[yroot] = xroot
else:
parent[yroot] = xroot
rank[xroot] += 1

# Applying Kruskal algorithm


def kruskal_algo(self):
result = []
i, e = 0, 0
self.graph = sorted(self.graph, key=lambda item: item[2])
parent = []
rank = []
for node in range(self.V):
parent.append(node)
rank.append(0)
while e < self.V - 1:
u, v, w = self.graph[i]
i = i + 1
x = self.find(parent, u)
y = self.find(parent, v)
if x != y:
e = e + 1
result.append([u, v, w])
self.apply_union(parent, rank, x, y)
for u, v, weight in result:
print("%d - %d: %d" % (u, v, weight))
Homework
g = Graph(6)
g.add_edge(0, 1, 4)
g.add_edge(0, 2, 4)
g.add_edge(1, 2, 2)
g.add_edge(1, 0, 4)
g.add_edge(2, 0, 4)
g.add_edge(2, 1, 2)
g.add_edge(2, 3, 3)
g.add_edge(2, 5, 2)
g.add_edge(2, 4, 4)
g.add_edge(3, 2, 3)
g.add_edge(3, 4, 3)
g.add_edge(4, 2, 4)
g.add_edge(4, 3, 3)
g.add_edge(5, 2, 2)
g.add_edge(5, 4, 3)
g.kruskal_algo()

You might also like