0% found this document useful (0 votes)
10 views2 pages

Greedy

The document presents an implementation of Prim's Minimal Spanning Tree algorithm using a greedy search approach in Python. It defines a Graph class that allows the addition of edges and computes the minimum spanning tree based on user input for vertices and edges. The output displays the edges included in the minimum spanning tree along with their weights.

Uploaded by

motenancy7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views2 pages

Greedy

The document presents an implementation of Prim's Minimal Spanning Tree algorithm using a greedy search approach in Python. It defines a Graph class that allows the addition of edges and computes the minimum spanning tree based on user input for vertices and edges. The output displays the edges included in the minimum spanning tree along with their weights.

Uploaded by

motenancy7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 2

ASSIGNMENT _03

Implement Greedy Search Algorithm for any of the following application: Prim's Minimal

Code:
import heapq

class Graph:
def __init__(self, vertices):
self.V = vertices
self.graph = []
self.nodes = [chr(i + 65) for i in range(vertices)] # Converts 0 to 'A', 1 to 'B', etc.

def add_edge(self, u, v, w):


u_index = self.nodes.index(u)
v_index = self.nodes.index(v)
heapq.heappush(self.graph, (w, u_index, v_index))

def prim_mst(self):
# Initializing variables
mst_set = set()
mst_edges = []
min_heap = []

# Starting with vertex 'A' (index 0)


mst_set.add(0)
for edge in self.graph:
if edge[1] == 0 or edge[2] == 0:
heapq.heappush(min_heap, edge)

while len(mst_set) < self.V and min_heap:


weight, u, v = heapq.heappop(min_heap)
if v not in mst_set:
mst_set.add(v)
mst_edges.append((self.nodes[u], self.nodes[v], weight))
for edge in self.graph:
if (edge[1] == v and edge[2] not in mst_set) or (edge[2] == v and edge[1] not in
mst_set):
heapq.heappush(min_heap, edge)

return mst_edges

# Taking input from the user


num_vertices = int(input("Enter the number of vertices: "))
g = Graph(num_vertices)

num_edges = int(input("Enter the number of edges: "))

for i in range(num_edges):
while True:
u = input(f"Enter the starting vertex of edge {i+1} (A, B, C, etc.): ").upper()
v = input(f"Enter the ending vertex of edge {i+1} (A, B, C, etc.): ").upper()
if u in g.nodes and v in g.nodes:
break
else:
print(f"Invalid input. Please enter vertices between A and {chr(num_vertices + 64)}.")
w = int(input(f"Enter the weight of edge {i+1}: "))
g.add_edge(u, v, w)

mst = g.prim_mst()
print("Edges in Minimum Spanning Tree:")
for u, v, weight in mst:
print(f"{u} - {v}: {weight}")

OUTPUT:

Enter the number of vertices: 4


Enter the number of edges: 4
Enter the starting vertex of edge 1 (A, B, C, etc.): A
Enter the ending vertex of edge 1 (A, B, C, etc.): B
Enter the weight of edge 1: 6
Enter the starting vertex of edge 2 (A, B, C, etc.): A
Enter the ending vertex of edge 2 (A, B, C, etc.): C
Enter the weight of edge 2: 5
Enter the starting vertex of edge 3 (A, B, C, etc.): A
Enter the ending vertex of edge 3 (A, B, C, etc.): D
Enter the weight of edge 3: 6
Enter the starting vertex of edge 4 (A, B, C, etc.): C
Enter the ending vertex of edge 4 (A, B, C, etc.): D
Enter the weight of edge 4: 7
Edges in Minimum Spanning Tree:
A - C: 5
A - B: 6
A - D: 6

You might also like