Explain Prim's algorithm for minimum spanning tree with example.
Q.) Explain Prim's algorithm for minimum spanning tree with example.
Subject: Data StructuresPrim's algorithm is a greedy algorithm that finds a minimum spanning tree for a weighted undirected graph. This means it finds a subset of the edges that forms a tree that includes every vertex, where the total weight of all the edges in the tree is minimized. Here's a step-by-step approach to explain Prim's algorithm:
Step-by-Step Approach
Initialization:
- Start with a graph
G
that has verticesV
and edgesE
. - Select an arbitrary vertex
s
fromG
to start the minimum spanning tree (MST). - Create a set
MST
that keeps track of vertices included in the minimum spanning tree. - Create a priority queue
Q
that will contain all the vertices with their key values. The key value for a vertex is the minimum weight edge connecting it to the tree. Initially, the key value of all vertices excepts
is set to infinity, and the key value ofs
is set to 0.
- Start with a graph
Algorithm Loop:
- While
Q
is not empty, do the following:- Extract the vertex
u
with the minimum key value fromQ
. - Add
u
to the setMST
. - For each vertex
v
adjacent tou
that is not yet inMST
, do the following: - If the weight of the edge
(u, v)
is less than the current key value ofv
, update the key value ofv
to the weight of(u, v)
and recordu
as the parent ofv
.
- Extract the vertex
- While
Termination:
- The algorithm terminates when all vertices are included in
MST
, at which point the MST has been found.
- The algorithm terminates when all vertices are included in
Example
Let's consider a simple graph with 5 vertices and the following edges with weights:
A-B: 2
A-C: 3
B-C: 1
B-D: 1
B-E: 4
C-E: 5
D-E: 1
Here's how Prim's algorithm would work on this graph:
Initialization:
- Start with vertex
A
. MST = {A}
,Q = {B, C, D, E}
with key values{∞, ∞, ∞, ∞}
.
- Start with vertex
Algorithm Loop:
- Extract
A
fromQ
,MST = {A}
. - Update the key values of vertices adjacent to
A
:Q = {B: 2, C: 3, D: ∞, E: ∞}
. - Extract
B
with the minimum key value fromQ
,MST = {A, B}
. - Update the key values of vertices adjacent to
B
:Q = {C: 1, D: 1, E: 4}
. - Extract
C
with the minimum key value fromQ
,MST = {A, B, C}
. - Update the key values of vertices adjacent to
C
:Q = {D: 1, E: 4}
(no change sinceC-E
is not less than the current key ofE
). - Extract
D
with the minimum key value fromQ
,MST = {A, B, C, D}
. - Update the key values of vertices adjacent to
D
:Q = {E: 1}
. - Extract
E
with the minimum key value fromQ
,MST = {A, B, C, D, E}
.
- Extract
Termination:
- All vertices are included in
MST
, so the algorithm terminates.
- All vertices are included in
The resulting minimum spanning tree is:
A-B: 2
B-C: 1
B-D: 1
D-E: 1
Differences and Important Points
Aspect | Prim's Algorithm |
---|---|
Initialization | Starts with a single vertex and grows the MST one vertex at a time. |
Edge Selection | Adds the least weight edge that connects a vertex in the MST with a vertex outside. |
Data Structures | Uses a priority queue to select the next vertex with the minimum key value. |
Time Complexity | O(E log V) with a binary heap or O(V^2) with an adjacency matrix without a heap. |
Suitable for | Dense graphs where E is close to V^2. |
Graph Type | Works on both connected, undirected graphs with or without cycles. |
Cycle Handling | Naturally avoids cycles as it grows the MST incrementally. |
In summary, Prim's algorithm is efficient for finding the minimum spanning tree in a connected graph with weighted edges. It incrementally builds the MST by selecting the smallest weight edge that connects the growing tree to the rest of the graph.