Greedy
Greedy
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 prim_mst(self):
# Initializing variables
mst_set = set()
mst_edges = []
min_heap = []
return mst_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: