20C23027 Prac5. Updatedpdf
20C23027 Prac5. Updatedpdf
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 = []
# Search function
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
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:
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
Output:
5|Page 2 0 C 2 3 0 2 7 – P u r v i Va g h e l a