Explain Prim's algorithm for minimum spanning tree with example.


Q.) Explain Prim's algorithm for minimum spanning tree with example.

Subject: Data Structures

Prim'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

  1. Initialization:

    • Start with a graph G that has vertices V and edges E.
    • Select an arbitrary vertex s from G 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 except s is set to infinity, and the key value of s is set to 0.
  2. Algorithm Loop:

    • While Q is not empty, do the following:
      • Extract the vertex u with the minimum key value from Q.
      • Add u to the set MST.
      • For each vertex v adjacent to u that is not yet in MST, do the following:
      • If the weight of the edge (u, v) is less than the current key value of v, update the key value of v to the weight of (u, v) and record u as the parent of v.
  3. Termination:

    • The algorithm terminates when all vertices are included in MST, at which point the MST has been found.

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:

  1. Initialization:

    • Start with vertex A.
    • MST = {A}, Q = {B, C, D, E} with key values {∞, ∞, ∞, ∞}.
  2. Algorithm Loop:

    • Extract A from Q, 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 from Q, 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 from Q, MST = {A, B, C}.
    • Update the key values of vertices adjacent to C: Q = {D: 1, E: 4} (no change since C-E is not less than the current key of E).
    • Extract D with the minimum key value from Q, MST = {A, B, C, D}.
    • Update the key values of vertices adjacent to D: Q = {E: 1}.
    • Extract E with the minimum key value from Q, MST = {A, B, C, D, E}.
  3. Termination:

    • All vertices are included in MST, so the algorithm terminates.

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.