Prim Algorithm for Finding the Minimum Spanning Tree
Posted onViews: Word count in article: 923Reading time ≈3 mins.
Minimum Spanning Tree
For an undirected graph, the Minimum Spanning Tree is a connected subgraph of the original graph, which contains all the vertices of the original graph, and the sum of its edge weights is the smallest. We can use Prim algorithm to get the minimum spanning tree.
Prim Algorithm steps
The algorithm usually includes the following steps:
Step 1: Select any vertex from the graph as the root node of the tree.
Step 2: Find the edge with the smallest weight that is not yet connected to the tree and add it to the tree.
Step 3: Repeat step 2 until all vertices of the graph are in the tree.
Example
Take this undirected weighted graph above as an example to show the process of the Prim algorithm.
First, let's start with node A. As shown below:
Then, the set of visited nodes is: [A]. The closest node to the set of visited nodes is B (with a weight of 5), which is added to the set of visited nodes. As shown below:
Then, the set of visited nodes is: [A,B]. The closest node to the set of visited nodes is D (with a weight of 5), which is added to the set of visited nodes. As shown below:
Then, the set of visited nodes is: [A,B,D]. The closest node to the set of visited nodes is C (with a weight of 20), which is added to the set of visited nodes. As shown below:
Then, the set of visited nodes is: [A,B,D,C]. The closest node to the set of visited nodes is F (with a weight of 10), which is added to the set of visited nodes. As shown below:
Then, the set of visited nodes is: [A,B,D,C,F]. The closest node to the set of visited nodes is E (with a weight of 20), which is added to the set of visited nodes. As shown below:
Then, the set of visited nodes is: [A,B,D,C,F,E]. The closest node to the set of visited nodes is G (with a weight of 20), which is added to the set of visited nodes. As shown below:
Finally, all vertices are added to the tree, and we can get the following minimum spanning tree:
# Prim algorithm defprim(adjacency_table_map): # Set of visited vertices. Use the first node as the starting node and add it to the visited_node_set. visited_node_set = {next(iter(adjacency_table_map.keys()))}
# Priority queue to store candidate edges (visited_node, next_node, weight), sorted by weight candidate_edges_pq = []
# Record the neighbor node information of the first node for neighbor_node, weight in adjacency_table_map[next(iter(adjacency_table_map.keys()))].items(): heapq.heappush(candidate_edges_pq, (weight, next(iter(adjacency_table_map.keys())), neighbor_node))
# Initialize the Minimum Spanning Tree with an empty edge list and total weight of zero mst_edges_list = [] total_weight = 0
# Process vertices until all vertices are visited whilelen(visited_node_set) < len(adjacency_table_map.keys()): # Pop the edge with the minimum weight from the priority queue weight, visited_node, next_node = heapq.heappop(candidate_edges_pq) print("Among the nodes aren't visited, the node closest to the visited nodes is : " + str(next_node) + ". The corresponding weight is:" + str(weight))
# If the next node is not visited yet, add the edge to the MST and mark it as visited if next_node notin visited_node_set: mst_edges_list.append((visited_node, next_node)) total_weight += weight visited_node_set.add(next_node)
# Record the neighbor node information of the next node for neighbor_node, edge_weight in adjacency_table_map[next_node].items(): if neighbor_node notin visited_node_set: heapq.heappush(candidate_edges_pq, (edge_weight, next_node, neighbor_node))
# Call the Prim's algorithm function mst_edges, total_weight = prim(adjacency_table_map)
print("Minimum Spanning Tree Edges:", mst_edges) print("Total Weight of the Minimum Spanning Tree:", total_weight)
The output is as follows:
1 2 3 4 5 6 7 8
Among the nodes aren't visited, the node closest to the visited nodes is : B. The corresponding weight is:5 Among the nodes aren't visited, the node closest to the visited nodes is : D. The corresponding weight is:5 Among the nodes aren't visited, the node closest to the visited nodes is : C. The corresponding weight is:20 Among the nodes aren't visited, the node closest to the visited nodes is : F. The corresponding weight is:10 Among the nodes aren't visited, the node closest to the visited nodes is : E. The corresponding weight is:20 Among the nodes aren't visited, the node closest to the visited nodes is : G. The corresponding weight is:20 Minimum Spanning Tree Edges: [('A', 'B'), ('A', 'D'), ('A', 'C'), ('C', 'F'), ('A', 'E'), ('F', 'G')] Total Weight of the Minimum Spanning Tree: 80