0% found this document useful (0 votes)
15 views5 pages

20C23027 Prac5. Updatedpdf

The document discusses Kruskal's and Prim's algorithms for finding minimum spanning trees in graphs. It includes the pseudocode and Python implementations of both algorithms. For Kruskal's algorithm, the time complexity is O(E log E). For Prim's algorithm, the time complexity is O(E log V).

Uploaded by

purvivaghela2003
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)
15 views5 pages

20C23027 Prac5. Updatedpdf

The document discusses Kruskal's and Prim's algorithms for finding minimum spanning trees in graphs. It includes the pseudocode and Python implementations of both algorithms. For Kruskal's algorithm, the time complexity is O(E log E). For Prim's algorithm, the time complexity is O(E log V).

Uploaded by

purvivaghela2003
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/ 5

ITM SLS Baroda University

School of Computer Science Engineering & Technology


BTech Semester IV AI and CS and
BTech Semester IV CSE and IT
Batch 2020-24
Data Structure & Algorithm II Practical Lab Journal

Practical 05
Aim: Kruskal’s and Prim’s Python Implementation
Algorithm

KRUSKAL(G):
A=∅
For each vertex v ∈ G.V:
MAKE-SET(v)
For each edge (u, v) ∈ G.E ordered by increasing order by weight(u, v):
if FIND-SET(u) ≠ FIND-SET(v):
A = A ∪ {(u, v)}
UNION(u, v)
return A

Python Code
# 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:

1|Page 2 0 C 2 3 0 2 7 – P u r v i Va g h e l a
ITM SLS Baroda University
School of Computer Science Engineering & Technology
BTech Semester IV AI and CS and
BTech Semester IV CSE and IT
Batch 2020-24
Data Structure & Algorithm II Practical Lab Journal

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))

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()

2|Page 2 0 C 2 3 0 2 7 – P u r v i Va g h e l a
ITM SLS Baroda University
School of Computer Science Engineering & Technology
BTech Semester IV AI and CS and
BTech Semester IV CSE and IT
Batch 2020-24
Data Structure & Algorithm II Practical Lab Journal

Output:

The time complexity Of Kruskal's Algorithm is: O(E log E).

3|Page 2 0 C 2 3 0 2 7 – P u r v i Va g h e l a
ITM SLS Baroda University
School of Computer Science Engineering & Technology
BTech Semester IV AI and CS and
BTech Semester IV CSE and IT
Batch 2020-24
Data Structure & Algorithm II Practical Lab Journal

Prim’s Algorithm

The pseudocode for prim's algorithm shows how we create two sets of vertices U and V-U. U
contains the list of vertices that have been visited and V-U the list of vertices that haven't. One by
one, we move vertices from set V-U to set U by connecting the least weight edge.

T = ∅;
U = { 1 };
while (U ≠ V)
let (u, v) be the lowest cost edge such that u ∈ U and v ∈ V - U;
T = T ∪ {(u, v)}
U = U ∪ {v}

Python Code
# Prim's Algorithm in Python

INF = 9999999
# number of vertices in graph
V=5
# create a 2d array of size 5x5
# for adjacency matrix to represent graph
G = [[0, 9, 75, 0, 0],
[9, 0, 95, 19, 42],
[75, 95, 0, 51, 66],
[0, 19, 51, 0, 31],
[0, 42, 66, 31, 0]]
# create a array to track selected vertex
# selected will become true otherwise false
selected = [0, 0, 0, 0, 0]
# set number of edge to 0
no_edge = 0
# the number of egde in minimum spanning tree will be
# always less than(V - 1), where V is number of vertices in
# graph
# choose 0th vertex and make it true
selected[0] = True
# print for edge and weight
print("Edge : Weight\n")
while (no_edge < V - 1):
# For every vertex in the set S, find the all adjacent vertices
#, calculate the distance from the vertex selected at step 1.

4|Page 2 0 C 2 3 0 2 7 – P u r v i Va g h e l a
ITM SLS Baroda University
School of Computer Science Engineering & Technology
BTech Semester IV AI and CS and
BTech Semester IV CSE and IT
Batch 2020-24
Data Structure & Algorithm II Practical Lab Journal

# if the vertex is already in the set S, discard it otherwise


# choose another vertex nearest to selected vertex at step 1.
minimum = INF
x=0
y=0
for i in range(V):
if selected[i]:
for j in range(V):
if ((not selected[j]) and G[i][j]):
# not in selected and there is an edge
if minimum > G[i][j]:
minimum = G[i][j]
x=i
y=j
print(str(x) + "-" + str(y) + ":" + str(G[x][y]))
selected[y] = True
no_edge += 1

Output:

The time complexity of Prim's algorithm is O(E log V).

5|Page 2 0 C 2 3 0 2 7 – P u r v i Va g h e l a

You might also like