Prim's Algorithm

Distribute Education like Computer Geek

Understanding Prim’s Algorithm

The algorithm was developed by Czech mathematician Vojtín Czernik in 1930 and later by computer scientists Robert C. Prim in 1957 and Edgard W. Dijkstra in 1959. Therefore, it is sometimes also called the Járnik algorithm, prim-Járnik algorithm, prime. -Dijkstra algorithm or DJP algorithm.

.

What is Prim’s Minimum Spanning Tree Algorithm?

Prim’s Algorithm is a greedy algorithm used for finding the minimum spanning tree (MST) of a connected, undirected graph.

  1. The goal of Prim’s algorithm is to connect all the vertices of the graph while minimizing the total weight of the edges in the spanning tree.
  2. The algorithm starts with an arbitrary vertex and grows the spanning tree by adding the shortest edge that connects a vertex in the tree to a vertex outside the tree at each step.
  3. The process continues until all vertices are included in the minimum spanning tree.
  4. Prim’s algorithm ensures that the resulting tree is acyclic and spans all the vertices with the least possible total edge weight.

.

Algorithm
  1. Begin with an arbitrary node.
  2. Find the shortest edge connecting any unvisited node to the current tree.
  3. Add the selected edge and the connected node to the tree.
  4. Repeat steps 2-3 until all nodes are included in the tree.

.

Working of Prim’s Algorithm

Imagine you have a network of houses, and you want to build roads between them with the least total distance.

Prim’s algorithm starts with a single node and adds the shortest edge that connects a new node to the existing structure at each step. It continues this process until all nodes are connected.

Network of homes and roads
Network of homes and roads
.
We can simplify this by arranging the alphabets in place of houses.
Graph of Prim's Algorithm
Graph of Prim’s Algorithm
.

Let’s solve for the minimum spanning tree using Prim’s algorithm with the given graph –

Vertices: A, B, C, D, E, F, G, H, I, J

Edges with weights – 16

.

Initialization

Start with an arbitrary vertex, let’s say A.

Initialize an empty set T (the minimum spanning tree) and a priority queue or min-heap to store edges.

.

Adding Edges

Add the minimum-weight edge connected to A, which is AE (weight = 1). Update T.

MST Edge 1

Now, add the next minimum-weight edge connected to A or E. So, the edges are {AB = 2, ED = 2, EG = 2, EF = 3}.

You have to select an edge of minimum weight which has one unvisited vertex, no cycles or loops in tree.

So, we select AB. Update T.

MST Edge 2

Now, add the next minimum-weight edge connected to A, B or E. So, the edges are {BD = 2, BC = 3, ED = 2, EG = 2, EF = 3}.
You have to select an edge of minimum weight which has one unvisited vertex, no cycles or loops in tree.
So, we select BD. Update T.

MST Edge 3

Now, add the next minimum-weight edge connected to A, B, D or E. So, the edges are {BC = 3, DC = 4, EG = 2, EF = 3}.

ED is not in the list because a cycle is formed.

You have to select an edge of minimum weight which has one unvisited vertex, no cycles or loops in tree.

So, we select EG. Update T.

MST Edge 4

Now, add the next minimum-weight edge connected to A, B, D, E or G. So, the edges are {BC = 3, DC = 4, EF = 3, GF = 9, GH = 2}.

You have to select an edge of minimum weight which has one unvisited vertex, no cycles or loops in tree.

So, we select GH. Update T.

MST Edge 5

Now, add the next minimum-weight edge connected to A, B, D, E, G or H. So, the edges are {BC = 3, DC = 4, EF = 3, GF = 9, HF = 2, HI = 3}.

You have to select an edge of minimum weight which has one unvisited vertex, no cycles or loops in tree.

So, we select HF. Update T.

MST Edge 6

Now, add the next minimum-weight edge connected to A, B, D, E, G, H or F. So, the edges are {BC = 3, DC = 4, HI = 3, FJ = 1}.

EF and GF are not in the list because a cycle is formed.

You have to select an edge of minimum weight which has one unvisited vertex, no cycles or loops in tree.

So, we select FJ. Update T.

MST Edge 7

Now, add the next minimum-weight edge connected to A, B, D, E, G, H, F or J. So, the edges are {BC = 3, DC = 4, CJ = 7, HI = 3, FI = 8, JI = 3}.

You have to select an edge of minimum weight which has one unvisited vertex, no cycles or loops in tree.

So, we select HI. Update T.

MST Edge 8

Now, add the next minimum-weight edge connected to A, B, D, E, G, H, F, J or I. So, the edges are {BC = 3, DC = 4, CJ = 7}.

FI and JI are not in the list because a cycle is formed and there is no unvisited vertex.

You have to select an edge of minimum weight which has one unvisited vertex, no cycles or loops in tree.

So, we select BC. Update T.

MST Edge 9

The final minimum spanning tree.

The total weight of the minimum spanning tree is the sum of the weights of its edges.

Total Weight – 18

We can make multiple minimum spanning trees of prim’s with this graph and the total weight of their edges would be just 18 not more than that.

.

Source Code for Prim’s Algorithm

Dense Graphs

Definition – Dense graphs are graphs where the number of edges is close to the maximum possible edges between the vertices. In other words, there are relatively many existing connections among the vertices.

 

Prim’s Algorithm in Dense Graphs

Vertex-Focused Approach
Prim’s algorithm operates by selecting vertices and adding the minimum-weight edges that connect the growing Minimum Spanning Tree (MST) to external vertices.
In dense graphs, where there are many existing edges, the vertex-focused approach of Prim’s algorithm becomes advantageous.

.

Efficiency in Dense Connectivity

As dense graphs have a large number of existing connections, the process of selecting vertices and adding edges becomes more efficient in terms of accessing and updating adjacent vertices.
Prim’s algorithm’s efficiency is less affected by the number of vertices, making it suitable for graphs where there are many edges relative to the number of vertices.

.
Dependence on Starting Vertex
Prim’s algorithm depends on a starting vertex, and it grows the MST from this initial vertex by adding the minimum-weight edges.
In dense graphs, where the connectivity is high, starting from any vertex often leads to a relatively efficient solution.

.

Comparison with Sparse Graphs
In sparse graphs, where the number of edges is much smaller compared to the maximum possible, Prim’s algorithm might become less efficient. This is because the process of growing the MST by selecting vertices and adding edges involves more operations when the graph is not densely connected.

.

Time Complexity of Prim’s Algorithm

The time complexity of Prim’s algorithm depends on the data structure used for implementing the priority queue to efficiently select the minimum edge.

(1) Using Priority Queue with Adjacency List (Binary Heap or Fibonacci Heap)

Binary Heap – O((V + E) * log V)

Fibonacci Heap – O(E + V * log V)

Where, V is the number of vertices, E is the number of edges.

.

(2) Using Priority Queue with Adjacency Matrix (Array or Min Heap)

Using an array – O(V^2)

Using a min heap – O((V + E) * log V)

.

Space Complexity of Prim’s Algorithm

The space complexity of Prim’s algorithm is determined by the data structures used to store information during the algorithm’s execution.

If an adjacency list is used to represent the graph, the space complexity is O(V + E), where V is the number of vertices and E is the number of edges.

If an adjacency matrix is used, the space complexity is O(V^2).

.

Advantages of Prim’s Algorithm
  1. Guarantees the smallest possible total edge weight.
  2. Performs well on sparse graphs.
  3. Conceptually simple and easy to understand
  4. In the design of integrated circuits and Very Large-Scale Integration (VLSI) systems, it ensures minimizing the interconnection costs.
  5. Helps in efficiently establishing these connections in robotics and sensor networks.
  6. Can assist in planning where roads or railways need to be constructed to connect different regions.
  7. In telecommunication networks, where communication links need to be established, minimizing the total length of connections is essential for cost-effective and efficient communication.

.

Disadvantages of Prim’s Algorithm
  1. For dense graphs with many edges, the time complexity of the algorithm might be relatively high, especially if using a simple data structure like an adjacency matrix.
  2. It does not handle graphs with negative cycles. If the graph has a negative cycle, it can’t produce a minimum spanning tree.

Test Yourself

Q1- Can Prim’s algorithm handle disconnected graphs?

Prim’s algorithm assumes the input graph is connected. If the graph is disconnected, the algorithm may not produce a spanning tree that includes all vertices.

Q2- How would adding a constant value to all edge weights in the graph affect Prim’s algorithm?

Adding a constant value to all edge weights doesn’t affect the relative order of the weights. Therefore, the resulting minimum spanning tree would be the same, and only the total weight of the tree would change.

Q3- Can Prim’s algorithm be adapted to handle directed graphs?

Prim’s algorithm is inherently designed for undirected graphs. Adapting it for directed graphs would require modifications, and the resulting algorithm might not guarantee a minimum spanning tree.

Q4- How does introducing negative edge weights in the graph impact Prim’s algorithm?

Prim’s algorithm is not designed to handle graphs with negative edge weights. If negative weights are present, the algorithm may produce incorrect results, and the minimum spanning tree may not be valid.

Q5- Is it possible for Prim’s algorithm to produce different spanning trees for the same graph?

Yes, Prim’s algorithm can produce different minimum spanning trees for the same graph, especially when there are multiple edges with the same minimum weight, and the algorithm has flexibility in choosing between them.

Q6- Which data structure is NOT suitable for implementing the priority queue in Prim’s algorithm?
  1. Binary Heap
  2. Fibonacci Heap
  3. Stack
  4. Min Heap

Ans – (3)

Explanation

A stack is not suitable for implementing the priority queue in Prim’s algorithm. The priority queue requires efficient extraction of the minimum key, and a stack does not provide this operation in constant time.

Q7- How does the time complexity of Prim’s algorithm change if the graph is represented using an adjacency matrix instead of an adjacency list?
  1. It improves.
  2. It worsens.
  3. It remains the same.
  4. It depends on the number of vertices.

Ans – (2)

Explanation – Using an adjacency matrix result in a time complexity of O(V^2), where V is the number of vertices. This is less efficient than using an adjacency list, which has a time complexity of O((V + E) * log V) with appropriate data structures.

Q8- How to Find Minimum Spanning Tree Using Prim’s Algorithm? 

Graph Used by prim's algorithm

Let’s say vertex 1 is our starting point. You can choose any starting point.

Move 1

Visited Vertex(1) = Edges(5, 2)

We must select minimum weight, So, we choose 2.

The minimum spanning tree is

Graph MST Edge 1

Total weight is 2.

Move 2

Visited Vertex(1, 3) = Edges(5, 4, 6)

We must select minimum weight, So, we choose 4.

The minimum spanning tree is

Graph MST Edge 2

Total weight is 6.

Move 3

Visited Vertex(1, 3, 2) = Edges(1, 6)

We must select minimum weight, So, we choose 1.

The minimum spanning tree is

Graph MST Edge 3

Total weight is 7.

Move 4

Visited Vertex(1, 3, 2, 5) = Edges(6, 3)

We must select minimum weight, So, we choose 3.

The minimum spanning tree is

Graph MST Edge 4

Total weight is 10.

Q9- Using Prim’s algorithm to construct a minimum spanning tree starting with node a, which one of the following sequences of edges represents a possible order in which the edge would be added to construct the minimum spanning tree?

Graph used by Prim's Algorithm

1. (a,b), (b,h), (g,h), (f, g), (c, f), (c, i), (c, d), (d, e)
2. (a,b), (b,c), (c,i), (c, f), (f, g), (g, h), (c, d), (d, e)
3. (a,b), (b,h), (g,h), (g, i), (c, f), (c, i), (c, d), (d, e)
4. (a,b), (g,h), (g,f), (c, f), (c, i), (f, e), (b, c), (d, e)

Ans – (2)

Explanation – 

Prim's Algorithm MST

Q10- Prim’s algorithm is also known as __________
  1. Dijkstra–Scholten algorithm
  2. Borůvka’s algorithm
  3. Floyd–Warshall algorithm
  4. DJP Algorithm

Ans – (4)

Explanation – The algorithm was developed by Czech mathematician Vojtín Czernik in 1930 and later by computer scientists Robert C. Prim in 1957 and Edgard W. Dijkstra. Therefore, it is sometimes also called the Járnik algorithm, prim-Járnik algorithm, prime. -Dijkstra algorithm or DJP algorithm.

BOOKS

Algorithm T H CoremanAlgorithm by Udit AgarwalAlgorithm Ellis HorowitzData Structure & Algorithm Made Easy Narasimha

Â