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

Ai 3 3166

The document describes an implementation of Prim's algorithm for finding the Minimum Spanning Tree (MST) of a graph represented as a dictionary. It includes example graphs and demonstrates how to use the algorithm, providing output for the MST edges and their total weight. The output shows the edges of the MST and their corresponding weights for two different graphs.
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)
8 views4 pages

Ai 3 3166

The document describes an implementation of Prim's algorithm for finding the Minimum Spanning Tree (MST) of a graph represented as a dictionary. It includes example graphs and demonstrates how to use the algorithm, providing output for the MST edges and their total weight. The output shows the edges of the MST and their corresponding weights for two different graphs.
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

Name :- Pragati Bhujang Jagtap

Roll no :- 3166
EXPERIMENT No :- 3

def prim_mst(graph):
"""
Prim's algorithm for Minimum Spanning Tree (MST) without heaps.
Args:
graph: A dictionary representing the graph. Keys are nodes, and values
are dictionaries of neighbor nodes with edge weights. Example:
graph = {
'A': {'B': 2, 'D': 6},
'B': {'A': 2, 'C': 3, 'D': 8, 'E': 5},
'C': {'B': 3, 'E': 7},
'D': {'A': 6, 'B': 8, 'E': 9, 'F': 11},
'E': {'B': 5, 'C': 7, 'D': 9, 'F': 15},
'F': {'D': 11, 'E': 15}
}
Returns:
A tuple containing:
- A list of tuples representing the edges of the MST.
- The total weight of the MST.
Returns None if the graph is empty.
""" if not
graph:
return None

nodes = list(graph.keys())
start_node = nodes[0] # Start from the first node
visited = {start_node} mst = [] total_weight =
0

while len(visited) < len(nodes):


min_edge = None
min_weight = float('inf')

for current_node in visited:


for neighbor, weight in graph[current_node].items():
if neighbor not in visited: if weight <
min_weight: min_weight = weight
min_edge = (current_node, neighbor, weight)

if min_edge:
u, v, weight = min_edge
mst.append((u, v, weight))
total_weight += weight
visited.add(v) else:
# Handle disconnected graph case.
break

return mst, total_weight

# Example usage: graph


={
'A': {'B': 2, 'D': 6},
'B': {'A': 2, 'C': 3, 'D': 8, 'E': 5},
'C': {'B': 3, 'E': 7},
'D': {'A': 6, 'B': 8, 'E': 9, 'F': 11},
'E': {'B': 5, 'C': 7, 'D': 9, 'F': 15},
'F': {'D': 11, 'E': 15}
}

mst_edges, mst_weight = prim_mst(graph) if


mst_edges:
print("Minimum Spanning Tree Edges:")
for u, v, weight in mst_edges:
print(f"{u} -- {v} : {weight}")
print(f"Total MST Weight: {mst_weight}")
else: print("Graph is empty.")
graph2 = {
'A': {'B': 1, 'C': 2},
'B': {'A': 1, 'C': 3, 'D': 5},
'C': {'A': 2, 'B': 3, 'D': 8},
'D': {'B': 5, 'C': 8}
}

mst_edges2, mst_weight2 = prim_mst(graph2)


if mst_edges2:
print("\nMinimum Spanning Tree Edges:")
for u, v, weight in mst_edges2:
print(f"{u} -- {v} : {weight}") print(f"Total
MST Weight: {mst_weight2}") else:
print("Graph is empty.")

OUTPUT :-

C:\Users\bhumi\PycharmProjects\pythonProject9\venv\Scripts\python.exe
C:\Users\bhumi\PycharmProjects\pythonProject9\p3.py

Minimum Spanning Tree Edges:

A -- B : 2

B -- C : 3

B -- E : 5

A -- D : 6

D -- F : 11

Total MST Weight: 27

Minimum Spanning Tree Edges:

A -- B : 1

A -- C : 2

B -- D : 5

Total MST Weight: 8


Process finished with exit code 0

You might also like