Prim's Algorithm
Prim's Algorithm
is a subset of the edges that connect all the vertices in the graph while minimizing the total edge
weight. While Prim’s algorithm is not directly a network flow algorithm, it is often used in
optimization problems where flow or connectivity in a graph needs to be minimized, such as in
network design.
In a network flow context, Prim’s algorithm can be used to find the minimum spanning tree
(MST) of a flow network, which helps minimize the overall cost or weight required to connect
all nodes.
Here’s a step-by-step numerical example of Prim’s Algorithm for finding a Minimum Spanning
Tree (MST) of a graph.
Example 1
Let us consider the following graph (undirected and weighted):
Edge Weight
A-B 1
A-C 5
B-C 4
B-D 3
C-D 2
C-E 6
D-E 7
Step-by-Step Execution
Initial Setup:
1. Start from vertex A (arbitrary choice).
2. MST starts empty.
3. Keep track of visited nodes: initially only A is visited.
4. Add the edges connected to A into a priority queue (min-heap), sorted by weight.
1
Step 1: Start with A
Edges from A:
o A-B (weight 1)
o A-C (weight 5)
Step 2: Consider B
Visited nodes: {A, B}.
Edges from B:
o B-C (weight 4)
o B-D (weight 3)
Add these edges to the priority queue. Now the queue contains:
o A-C (weight 5)
o B-C (weight 4)
o B-D (weight 3)
Step 3: Consider D
Visited nodes: {A, B, D}.
Edges from D:
o D-C (weight 2)
o D-E (weight 7)
Add these edges to the priority queue. Now the queue contains:
o A-C (weight 5)
2
o B-C (weight 4)
o D-C (weight 2)
o D-E (weight 7)
Step 4: Consider C
Visited nodes: {A, B, C, D}.
Edges from C:
o C-E (weight 6)
Add this edge to the priority queue. Now the queue contains:
o A-C (weight 5)
o B-C (weight 4)
o C-E (weight 6)
o D-E (weight 7)
Select the smallest edge: A-C (weight 5), but C is already visited, so skip it.
Next, consider C-E (weight 6).
Add E to the MST and mark it as visited.
Final MST
Edges in the MST:
A-B (weight 1)
B-D (weight 3)
D-C (weight 2)
C-E (weight 6)
Total Weight of MST: 1+3+2+6 = 12.
Example 2
3
Let's consider a weighted, undirected graph representing a network with 5 nodes (A, B, C, D, E).
The graph has the following edges with associated weights:
We will apply Prim’s Algorithm to find the Minimum Spanning Tree (MST) for this network.
Step-by-Step Process
1. Initialization:
o Start from an arbitrary node (let’s say node A).
o Initialize a priority queue to store the edge weights and the current minimum
spanning tree (MST).
o Mark all nodes as unvisited.
2. Iterate:
o Add the node with the smallest edge weight to the MST, and then explore its
neighbors. Update the priority queue accordingly.
o Repeat until all nodes are included in the MST.
4
Prim's Algorithm Execution
MST Edges:(A,C,2),(C,D,3),(A,B,4),(D,E,7)
5
Python Code for Prim’s Algorithm
Here’s the Python code to implement Prim’s algorithm for this problem:
import heapq
visited.add(start)
total_weight = 0 # Total weight of the MST
while min_heap:
weight, u, v = heapq.heappop(min_heap) # Get the edge with the smallest weight
6
print("Total weight of the MST:", total_weight)
1. Graph Representation: The graph is represented as an adjacency list where each node
has a list of tuples containing its neighbors and the associated edge weights.
2. Priority Queue (Min-Heap): We use a priority queue (min-heap) to efficiently fetch the
next minimum weight edge.
3. Visited Set: A set is used to keep track of the nodes that are already part of the MST.
4. MST Construction: The algorithm starts from the given starting node, and iteratively
selects the minimum weight edge that connects a new node to the MST. This process
continues until all nodes are included in the MST.
Conclusion
The Minimum Spanning Tree (MST) includes the edges (A,C,2), (C,D,3),(A,B,4), and
(D,E,7).
The total weight of the MST is 16, representing the minimum total cost to connect all
nodes in the network.
This implementation and example show how Prim’s Algorithm can be applied to network flow
problems, specifically focusing on finding the minimum spanning tree.