Prim's algorithm using priority_queue in STL
Last Updated :
26 Feb, 2024
Given an undirected, connected and weighted graph, find Minimum Spanning Tree (MST) of the graph using Prim's algorithm.
Input : Adjacency List representation
of above graph
Output : Edges in MST
0 - 1
1 - 2
2 - 3
3 - 4
2 - 5
5 - 6
6 - 7
2 - 8
Note: There are two possible MSTs, the other
MST includes edge 0-7 in place of 1-2.
We have discussed below Prim's MST implementations.
The second implementation is time complexity wise better, but is really complex as we have implemented our own priority queue. STL provides priority_queue, but the provided priority queue doesn’t support decrease key operation. And in Prim’s algorithm, we need a priority queue and below operations on priority queue :
- ExtractMin : from all those vertices which have not yet been included in MST, we need to get vertex with minimum key value.
- DecreaseKey : After extracting vertex we need to update keys of its adjacent vertices, and if new key is smaller, then update that in data structure.
The algorithm discussed here can be modified so that decrease key is never required. The idea is, not to insert all vertices in priority queue, but only those which are not MST and have been visited through a vertex that has included in MST. We keep track of vertices included in MST in a separate boolean array inMST[].
1) Initialize keys of all vertices as infinite and
parent of every vertex as -1.
2) Create an empty priority_queue pq. Every item
of pq is a pair (weight, vertex). Weight (or
key) is used as first item of pair
as first item is by default used to compare
two pairs.
3) Initialize all vertices as not part of MST yet.
We use boolean array inMST[] for this purpose.
This array is required to make sure that an already
considered vertex is not included in pq again. This
is where Prim's implementation differs from Dijkstra.
In Dijkstra's algorithm, we didn't need this array as
distances always increase. We require this array here
because key value of a processed vertex may decrease
if not checked.
4) Insert source vertex into pq and make its key as 0.
5) While either pq doesn't become empty
a) Extract minimum key vertex from pq.
Let the extracted vertex be u.
b) Include u in MST using inMST[u] = true.
c) Loop through all adjacent of u and do
following for every vertex v.
// If weight of edge (u,v) is smaller than
// key of v and v is not already in MST
If inMST[v] = false && key[v] > weight(u, v)
(i) Update key of v, i.e., do
key[v] = weight(u, v)
(ii) Insert v into the pq
(iv) parent[v] = u
6) Print MST edges using parent array.
Below is C++ implementation of above idea.
C++
// STL implementation of Prim's algorithm for MST
#include<bits/stdc++.h>
using namespace std;
# define INF 0x3f3f3f3f
// iPair ==> Integer Pair
typedef pair<int, int> iPair;
// This class represents a directed graph using
// adjacency list representation
class Graph
{
int V; // No. of vertices
// In a weighted graph, we need to store vertex
// and weight pair for every edge
list< pair<int, int> > *adj;
public:
Graph(int V); // Constructor
// function to add an edge to graph
void addEdge(int u, int v, int w);
// Print MST using Prim's algorithm
void primMST();
};
// Allocates memory for adjacency list
Graph::Graph(int V)
{
this->V = V;
adj = new list<iPair> [V];
}
void Graph::addEdge(int u, int v, int w)
{
adj[u].push_back(make_pair(v, w));
adj[v].push_back(make_pair(u, w));
}
// Prints shortest paths from src to all other vertices
void Graph::primMST()
{
// Create a priority queue to store vertices that
// are being primMST. This is weird syntax in C++.
// Refer below link for details of this syntax
// https://www.geeksforgeeks.org/implement-min-heap-using-stl/
priority_queue< iPair, vector <iPair> , greater<iPair> > pq;
int src = 0; // Taking vertex 0 as source
// Create a vector for keys and initialize all
// keys as infinite (INF)
vector<int> key(V, INF);
// To store parent array which in turn store MST
vector<int> parent(V, -1);
// To keep track of vertices included in MST
vector<bool> inMST(V, false);
// Insert source itself in priority queue and initialize
// its key as 0.
pq.push(make_pair(0, src));
key[src] = 0;
/* Looping till priority queue becomes empty */
while (!pq.empty())
{
// The first vertex in pair is the minimum key
// vertex, extract it from priority queue.
// vertex label is stored in second of pair (it
// has to be done this way to keep the vertices
// sorted key (key must be first item
// in pair)
int u = pq.top().second;
pq.pop();
//Different key values for same vertex may exist in the priority queue.
//The one with the least key value is always processed first.
//Therefore, ignore the rest.
if(inMST[u] == true){
continue;
}
inMST[u] = true; // Include vertex in MST
// 'i' is used to get all adjacent vertices of a vertex
list< pair<int, int> >::iterator i;
for (i = adj[u].begin(); i != adj[u].end(); ++i)
{
// Get vertex label and weight of current adjacent
// of u.
int v = (*i).first;
int weight = (*i).second;
// If v is not in MST and weight of (u,v) is smaller
// than current key of v
if (inMST[v] == false && key[v] > weight)
{
// Updating key of v
key[v] = weight;
pq.push(make_pair(key[v], v));
parent[v] = u;
}
}
}
// Print edges of MST using parent array
for (int i = 1; i < V; ++i)
printf("%d - %d\n", parent[i], i);
}
// Driver program to test methods of graph class
int main()
{
// create the graph given in above figure
int V = 9;
Graph g(V);
// making above shown graph
g.addEdge(0, 1, 4);
g.addEdge(0, 7, 8);
g.addEdge(1, 2, 8);
g.addEdge(1, 7, 11);
g.addEdge(2, 3, 7);
g.addEdge(2, 8, 2);
g.addEdge(2, 5, 4);
g.addEdge(3, 4, 9);
g.addEdge(3, 5, 14);
g.addEdge(4, 5, 10);
g.addEdge(5, 6, 2);
g.addEdge(6, 7, 1);
g.addEdge(6, 8, 6);
g.addEdge(7, 8, 7);
g.primMST();
return 0;
}
Java
import java.util.*;
// iPair ==> Integer Pair
class iPair {
int first, second;
public iPair(int first, int second)
{
this.first = first;
this.second = second;
}
}
// This class represents a directed graph using
// adjacency list representation
class Graph {
int V; // No. of vertices
// In a weighted graph, we need to store vertex
// and weight pair for every edge
List<List<iPair> > adj;
public Graph(int V)
{
this.V = V;
adj = new ArrayList<>();
for (int i = 0; i < V; i++) {
adj.add(new ArrayList<>());
}
}
// function to add an edge to graph
void addEdge(int u, int v, int w)
{
adj.get(u).add(new iPair(v, w));
adj.get(v).add(new iPair(u, w));
}
// Print MST using Prim's algorithm
void primMST()
{
// Create a priority queue to store vertices that
// are being primMST. This is weird syntax in Java.
// Refer below link for details of this syntax
// https://www.geeksforgeeks.org/implement-min-heap-using-stl/
PriorityQueue<iPair> pq = new PriorityQueue<>(
V, new Comparator<iPair>() {
public int compare(iPair a, iPair b)
{
return a.second - b.second;
}
});
int src = 0; // Taking vertex 0 as source
// Create a vector for keys and initialize all
// keys as infinite (INF)
int INF = Integer.MAX_VALUE;
int[] key = new int[V];
Arrays.fill(key, INF);
// To store parent array which in turn store MST
int[] parent = new int[V];
Arrays.fill(parent, -1);
// To keep track of vertices included in MST
boolean[] inMST = new boolean[V];
// Insert source itself in priority queue and
// initialize its key as 0.
pq.offer(new iPair(0, src));
key[src] = 0;
/* Looping till priority queue becomes empty */
while (!pq.isEmpty()) {
// The first vertex in pair is the minimum key
// vertex, extract it from priority queue.
// vertex label is stored in second of pair (it
// has to be done this way to keep the vertices
// sorted key (key must be first item
// in pair)
int u = pq.peek().second;
pq.poll();
// Different key values for same vertex may
// exist in the priority queue. The one with the
// least key value is always processed first.
// Therefore, ignore the rest.
if (inMST[u]) {
continue;
}
inMST[u] = true; // Include vertex in MST
// 'i' is used to get all adjacent vertices of a
// vertex
for (iPair i : adj.get(u)) {
// Get vertex label and weight of current
// adjacent of u.
int v = i.first;
int weight = i.second;
// If v is not in MST and weight of (u,v)
// is smaller
// than current key of v
if (!inMST[v] && key[v] > weight) {
// Updating key of v
key[v] = weight;
pq.offer(new iPair(key[v], v));
parent[v] = u;
}
}
}
// Print edges of MST using parent array
for (int i = 1; i < V; i++) {
System.out.println(parent[i] + " - " + i);
}
}
}
// Driver class
public class Main {
public static void main(String[] args)
{
// create the graph given in above figure
int V = 9;
Graph graph = new Graph(V);
// making above shown graph
graph.addEdge(0, 1, 4);
graph.addEdge(0, 7, 8);
graph.addEdge(1, 2, 8);
graph.addEdge(1, 7, 11);
graph.addEdge(2, 3, 7);
graph.addEdge(2, 8, 2);
graph.addEdge(2, 5, 4);
graph.addEdge(3, 4, 9);
graph.addEdge(3, 5, 14);
graph.addEdge(4, 5, 10);
graph.addEdge(5, 6, 2);
graph.addEdge(6, 7, 1);
graph.addEdge(6, 8, 6);
graph.addEdge(7, 8, 7);
graph.primMST();
}
}
Python3
import heapq
# This class represents a directed graph using adjacency list representation
class Graph:
def __init__(self, V):
self.V = V
self.adj = [[] for _ in range(V)]
# Function to add an edge to the graph
def add_edge(self, u, v, w):
self.adj[u].append((v, w))
self.adj[v].append((u, w))
# Function to print MST using Prim's algorithm
def prim_mst(self):
pq = [] # Priority queue to store vertices that are being processed
src = 0 # Taking vertex 0 as the source
# Create a list for keys and initialize all keys as infinite (INF)
key = [float('inf')] * self.V
# To store the parent array which, in turn, stores MST
parent = [-1] * self.V
# To keep track of vertices included in MST
in_mst = [False] * self.V
# Insert source itself into the priority queue and initialize its key as 0
heapq.heappush(pq, (0, src))
key[src] = 0
# Loop until the priority queue becomes empty
while pq:
# The first vertex in the pair is the minimum key vertex
# Extract it from the priority queue
# The vertex label is stored in the second of the pair
u = heapq.heappop(pq)[1]
# Different key values for the same vertex may exist in the priority queue.
# The one with the least key value is always processed first.
# Therefore, ignore the rest.
if in_mst[u]:
continue
in_mst[u] = True # Include the vertex in MST
# Iterate through all adjacent vertices of a vertex
for v, weight in self.adj[u]:
# If v is not in MST and the weight of (u, v) is smaller than the current key of v
if not in_mst[v] and key[v] > weight:
# Update the key of v
key[v] = weight
heapq.heappush(pq, (key[v], v))
parent[v] = u
# Print edges of MST using the parent array
for i in range(1, self.V):
print(f"{parent[i]} - {i}")
# Driver program to test methods of the graph class
if __name__ == "__main__":
# Create the graph given in the above figure
V = 9
g = Graph(V)
# Making the above shown graph
g.add_edge(0, 1, 4)
g.add_edge(0, 7, 8)
g.add_edge(1, 2, 8)
g.add_edge(1, 7, 11)
g.add_edge(2, 3, 7)
g.add_edge(2, 8, 2)
g.add_edge(2, 5, 4)
g.add_edge(3, 4, 9)
g.add_edge(3, 5, 14)
g.add_edge(4, 5, 10)
g.add_edge(5, 6, 2)
g.add_edge(6, 7, 1)
g.add_edge(6, 8, 6)
g.add_edge(7, 8, 7)
g.prim_mst()
C#
using System;
using System.Collections.Generic;
class Graph
{
private int V;
private List<Tuple<int, int>>[] adj;
public Graph(int V)
{
this.V = V;
adj = new List<Tuple<int, int>>[V];
for (int i = 0; i < V; ++i)
adj[i] = new List<Tuple<int, int>>();
}
public void AddEdge(int u, int v, int w)
{
adj[u].Add(new Tuple<int, int>(v, w));
adj[v].Add(new Tuple<int, int>(u, w));
}
public void PrimMST()
{
PriorityQueue<(int key, int vertex)> pq = new PriorityQueue<(int key, int vertex)>(Comparer<(int key, int vertex)>.Create((x, y) => x.key.CompareTo(y.key)));
int src = 0;
int[] key = new int[V];
int[] parent = new int[V];
bool[] inMST = new bool[V];
for (int i = 0; i < V; ++i)
{
key[i] = int.MaxValue;
parent[i] = -1;
inMST[i] = false;
}
pq.Enqueue((0, src));
key[src] = 0;
while (pq.Count > 0)
{
int u = pq.Dequeue().vertex;
if (inMST[u])
continue;
inMST[u] = true;
foreach (var edge in adj[u])
{
int v = edge.Item1;
int weight = edge.Item2;
if (!inMST[v] && key[v] > weight)
{
key[v] = weight;
pq.Enqueue((key[v], v));
parent[v] = u;
}
}
}
for (int i = 1; i < V; ++i)
{
Console.WriteLine($"{parent[i]} - {i}");
}
}
}
class PriorityQueue<T>
{
private List<T> heap = new List<T>();
private readonly Comparer<T> comparer;
public int Count => heap.Count;
public PriorityQueue(Comparer<T> comparer)
{
this.comparer = comparer;
}
// Added parameterless constructor
public PriorityQueue() : this(Comparer<T>.Default) { }
public void Enqueue(T item)
{
heap.Add(item);
int i = heap.Count - 1;
while (i > 0)
{
int parent = (i - 1) / 2;
if (comparer.Compare(heap[parent], heap[i]) <= 0)
break;
Swap(parent, i);
i = parent;
}
}
public T Dequeue()
{
int count = heap.Count - 1;
T front = heap[0];
heap[0] = heap[count];
heap.RemoveAt(count);
count--;
int i = 0;
while (true)
{
int left = i * 2 + 1;
int right = i * 2 + 2;
if (left > count)
break;
int smallest = left;
if (right <= count && comparer.Compare(heap[right], heap[left]) < 0)
smallest = right;
if (comparer.Compare(heap[i], heap[smallest]) <= 0)
break;
Swap(i, smallest);
i = smallest;
}
return front;
}
private void Swap(int i, int j)
{
T temp = heap[i];
heap[i] = heap[j];
heap[j] = temp;
}
}
class Program
{
static void Main()
{
int V = 9;
Graph g = new Graph(V);
g.AddEdge(0, 1, 4);
g.AddEdge(0, 7, 8);
g.AddEdge(1, 2, 8);
g.AddEdge(1, 7, 11);
g.AddEdge(2, 3, 7);
g.AddEdge(2, 8, 2);
g.AddEdge(2, 5, 4);
g.AddEdge(3, 4, 9);
g.AddEdge(3, 5, 14);
g.AddEdge(4, 5, 10);
g.AddEdge(5, 6, 2);
g.AddEdge(6, 7, 1);
g.AddEdge(6, 8, 6);
g.AddEdge(7, 8, 7);
g.PrimMST();
}
}
JavaScript
class Graph {
constructor(V) {
this.V = V;
this.adj = Array.from({ length: V }, () => []);
}
addEdge(u, v, w) {
this.adj[u].push([v, w]);
this.adj[v].push([u, w]);
}
primMST() {
const pq = [];
const src = 0;
const key = new Array(this.V).fill(Infinity);
const parent = new Array(this.V).fill(-1);
const inMST = new Array(this.V).fill(false);
pq.push([0, src]);
key[src] = 0;
while (pq.length > 0) {
const [uDist, u] = pq.shift();
if (inMST[u]) {
continue;
}
inMST[u] = true;
for (const [v, weight] of this.adj[u]) {
if (!inMST[v] && key[v] > weight) {
key[v] = weight;
pq.push([key[v], v]);
parent[v] = u;
}
}
// Sort the priority queue based on the second element (vertex)
pq.sort((a, b) => a[1] - b[1]);
}
// Print edges of MST using the parent array in the desired order
console.log("Edge Weight");
for (let i = 1; i < this.V; ++i) {
console.log(`${parent[i]} - ${i} ${key[i]}`);
}
}
}
// Driver program to test methods of the graph class
function main() {
const V = 9;
const g = new Graph(V);
g.addEdge(0, 1, 4);
g.addEdge(0, 7, 8);
g.addEdge(1, 2, 8);
g.addEdge(1, 7, 11);
g.addEdge(2, 3, 7);
g.addEdge(2, 8, 2);
g.addEdge(2, 5, 4);
g.addEdge(3, 4, 9);
g.addEdge(3, 5, 14);
g.addEdge(4, 5, 10);
g.addEdge(5, 6, 2);
g.addEdge(6, 7, 1);
g.addEdge(6, 8, 6);
g.addEdge(7, 8, 7);
g.primMST();
}
// Call the main function
main();
Output0 - 1
1 - 2
2 - 3
3 - 4
2 - 5
5 - 6
6 - 7
2 - 8
Time complexity : O(E Log V))
Auxiliary Space :O(V)
A Quicker Implementation using array of vectors representation of a weighted graph :
C++
// STL implementation of Prim's algorithm for MST
#include<bits/stdc++.h>
using namespace std;
# define INF 0x3f3f3f3f
// iPair ==> Integer Pair
typedef pair<int, int> iPair;
// To add an edge
void addEdge(vector <pair<int, int> > adj[], int u,
int v, int wt)
{
adj[u].push_back(make_pair(v, wt));
adj[v].push_back(make_pair(u, wt));
}
// Prints shortest paths from src to all other vertices
void primMST(vector<pair<int,int> > adj[], int V)
{
// Create a priority queue to store vertices that
// are being primMST. This is weird syntax in C++.
// Refer below link for details of this syntax
// https://www.geeksforgeeks.org/implement-min-heap-using-stl/
priority_queue< iPair, vector <iPair> , greater<iPair> > pq;
int src = 0; // Taking vertex 0 as source
// Create a vector for keys and initialize all
// keys as infinite (INF)
vector<int> key(V, INF);
// To store parent array which in turn store MST
vector<int> parent(V, -1);
// To keep track of vertices included in MST
vector<bool> inMST(V, false);
// Insert source itself in priority queue and initialize
// its key as 0.
pq.push(make_pair(0, src));
key[src] = 0;
/* Looping till priority queue becomes empty */
while (!pq.empty())
{
// The first vertex in pair is the minimum key
// vertex, extract it from priority queue.
// vertex label is stored in second of pair (it
// has to be done this way to keep the vertices
// sorted key (key must be first item
// in pair)
int u = pq.top().second;
pq.pop();
//Different key values for same vertex may exist in the priority queue.
//The one with the least key value is always processed first.
//Therefore, ignore the rest.
if(inMST[u] == true){
continue;
}
inMST[u] = true; // Include vertex in MST
// Traverse all adjacent of u
for (auto x : adj[u])
{
// Get vertex label and weight of current adjacent
// of u.
int v = x.first;
int weight = x.second;
// If v is not in MST and weight of (u,v) is smaller
// than current key of v
if (inMST[v] == false && key[v] > weight)
{
// Updating key of v
key[v] = weight;
pq.push(make_pair(key[v], v));
parent[v] = u;
}
}
}
// Print edges of MST using parent array
for (int i = 1; i < V; ++i)
printf("%d - %d\n", parent[i], i);
}
// Driver program to test methods of graph class
int main()
{
int V = 9;
vector<iPair > adj[V];
// making above shown graph
addEdge(adj, 0, 1, 4);
addEdge(adj, 0, 7, 8);
addEdge(adj, 1, 2, 8);
addEdge(adj, 1, 7, 11);
addEdge(adj, 2, 3, 7);
addEdge(adj, 2, 8, 2);
addEdge(adj, 2, 5, 4);
addEdge(adj, 3, 4, 9);
addEdge(adj, 3, 5, 14);
addEdge(adj, 4, 5, 10);
addEdge(adj, 5, 6, 2);
addEdge(adj, 6, 7, 1);
addEdge(adj, 6, 8, 6);
addEdge(adj, 7, 8, 7);
primMST(adj, V);
return 0;
}
Java
import java.util.*;
// Pair class to represent pairs of integers
class Pair {
int first, second;
Pair(int first, int second) {
this.first = first;
this.second = second;
}
}
public class PrimMST {
static final int INF = 0x3f3f3f3f; // Represents infinity
// Method to add an edge to the adjacency list
static void addEdge(ArrayList<Pair>[] adj, int u, int v, int wt) {
adj[u].add(new Pair(v, wt));
adj[v].add(new Pair(u, wt));
}
// Method to find the MST using Prim's algorithm
static void primMST(ArrayList<Pair>[] adj, int V) {
int src = 0; // Start from vertex 0
// Priority queue to store vertices being processed
PriorityQueue<Pair> pq = new PriorityQueue<>(Comparator.comparingInt(p -> p.second));
// Create arrays for keys, parent vertices, and visited vertices
int[] key = new int[V];
int[] parent = new int[V];
boolean[] inMST = new boolean[V];
// Initialize arrays
Arrays.fill(key, INF);
Arrays.fill(parent, -1);
// Insert source vertex into priority queue and set its key to 0
pq.add(new Pair(src, 0));
key[src] = 0;
// Loop until priority queue becomes empty
while (!pq.isEmpty()) {
int u = pq.poll().first; // Extract vertex with minimum key
if (inMST[u])
continue;
inMST[u] = true; // Include vertex in MST
// Traverse all adjacent vertices of u
for (Pair x : adj[u]) {
int v = x.first;
int weight = x.second;
// If v is not in MST and weight of (u,v) is smaller than current key of v
if (!inMST[v] && key[v] > weight) {
// Update key of v
key[v] = weight;
pq.add(new Pair(v, key[v]));
parent[v] = u;
}
}
}
// Print edges of MST using parent array
for (int i = 1; i < V; ++i)
System.out.println(parent[i] + " - " + i);
}
// Driver program to test Prim's algorithm
public static void main(String[] args) {
int V = 9;
ArrayList<Pair>[] adj = new ArrayList[V];
for (int i = 0; i < V; i++)
adj[i] = new ArrayList<>();
// Adding edges to the graph
addEdge(adj, 0, 1, 4);
addEdge(adj, 0, 7, 8);
addEdge(adj, 1, 2, 8);
addEdge(adj, 1, 7, 11);
addEdge(adj, 2, 3, 7);
addEdge(adj, 2, 8, 2);
addEdge(adj, 2, 5, 4);
addEdge(adj, 3, 4, 9);
addEdge(adj, 3, 5, 14);
addEdge(adj, 4, 5, 10);
addEdge(adj, 5, 6, 2);
addEdge(adj, 6, 7, 1);
addEdge(adj, 6, 8, 6);
addEdge(adj, 7, 8, 7);
primMST(adj, V);
}
}
//This code is contributed by Utkarsh
Python3
import heapq
# To add an edge
def addEdge(adj, u, v, wt):
adj[u].append((v, wt))
adj[v].append((u, wt))
# Prints shortest paths from src to all other vertices
def primMST(adj, V):
# Create a priority queue to store vertices that
# are being primMST. This is implemented using heapq in Python.
# It is a min heap, and we use tuples for pairs (key, vertex).
pq = [(0, 0)] # Start from vertex 0 with key 0
# Create a list for keys and initialize all
# keys as infinite (float('inf') is used for infinity)
key = [float('inf')] * V
# To store parent array which in turn store MST
parent = [-1] * V
# To keep track of vertices included in MST
inMST = [False] * V
# Insert source itself in priority queue and initialize
# its key as 0.
key[0] = 0
# Looping till priority queue becomes empty
while pq:
# The first vertex in tuple is the minimum key
# vertex, extract it from priority queue.
# vertex label is stored in second of tuple (it
# has to be done this way to keep the vertices
# sorted by key, key must be first item
# in tuple)
u = heapq.heappop(pq)[1]
# Different key values for the same vertex may exist in the priority queue.
# The one with the least key value is always processed first.
# Therefore, ignore the rest.
if inMST[u]:
continue
inMST[u] = True # Include vertex in MST
# Traverse all adjacent vertices of u
for x in adj[u]:
# Get vertex label and weight of the current adjacent
# of u.
v, weight = x
# If v is not in MST and weight of (u, v) is smaller
# than the current key of v
if not inMST[v] and key[v] > weight:
# Updating key of v
key[v] = weight
heapq.heappush(pq, (key[v], v))
parent[v] = u
# Print edges of MST using parent array
for i in range(1, V):
print(f"{parent[i]} - {i}")
# Driver program to test methods of graph class
def main():
V = 9
adj = [[] for _ in range(V)]
# Making the graph as shown in the C++ code
addEdge(adj, 0, 1, 4)
addEdge(adj, 0, 7, 8)
addEdge(adj, 1, 2, 8)
addEdge(adj, 1, 7, 11)
addEdge(adj, 2, 3, 7)
addEdge(adj, 2, 8, 2)
addEdge(adj, 2, 5, 4)
addEdge(adj, 3, 4, 9)
addEdge(adj, 3, 5, 14)
addEdge(adj, 4, 5, 10)
addEdge(adj, 5, 6, 2)
addEdge(adj, 6, 7, 1)
addEdge(adj, 6, 8, 6)
addEdge(adj, 7, 8, 7)
primMST(adj, V)
if __name__ == "__main__":
main()
C#
using System;
using System.Collections.Generic;
public class PrimMST
{
// iPair ==> Integer Pair
private class iPair : IComparable<iPair>
{
public int First { get; set; }
public int Second { get; set; }
public iPair(int first, int second)
{
First = first;
Second = second;
}
public int CompareTo(iPair other)
{
return First.CompareTo(other.First);
}
}
// To add an edge
private static void AddEdge(List<List<iPair>> adj, int u, int v, int wt)
{
adj[u].Add(new iPair(v, wt));
adj[v].Add(new iPair(u, wt));
}
// Prints shortest paths from src to all other vertices
private static void PrimMSTAlgorithm(List<List<iPair>> adj, int V)
{
// Create a priority queue to store vertices that are being primMST.
// It is a min heap, and we use tuples for pairs (key, vertex).
PriorityQueue<iPair> pq = new PriorityQueue<iPair>();
int src = 0; // Taking vertex 0 as the source
// Create a list for keys and initialize all keys as infinite
List<int> key = new List<int>(new int[V]);
for (int i = 0; i < V; i++)
{
key[i] = int.MaxValue;
}
// To store the parent array which, in turn, stores MST
List<int> parent = new List<int>(new int[V]);
for (int i = 0; i < V; i++)
{
parent[i] = -1;
}
// To keep track of vertices included in MST
List<bool> inMST = new List<bool>(new bool[V]);
// Insert the source itself into the priority queue and initialize its key as 0.
pq.Enqueue(new iPair(0, src));
key[src] = 0;
// Looping until the priority queue becomes empty
while (pq.Count > 0)
{
// The first vertex in the pair is the minimum key vertex, extract it from the priority queue.
// The vertex label is stored in the second of the pair (it has to be done this way to keep the vertices
// sorted by key; key must be the first item in the pair)
int u = pq.Dequeue().Second;
// Different key values for the same vertex may exist in the priority queue.
// The one with the least key value is always processed first.
// Therefore, ignore the rest.
if (inMST[u])
{
continue;
}
inMST[u] = true; // Include the vertex in MST
// Traverse all adjacent vertices of u
foreach (var x in adj[u])
{
// Get the vertex label and weight of the current adjacent of u.
int v = x.First;
int weight = x.Second;
// If v is not in MST and the weight of (u, v) is smaller than the current key of v
if (!inMST[v] && key[v] > weight)
{
// Updating the key of v
key[v] = weight;
pq.Enqueue(new iPair(key[v], v));
parent[v] = u;
}
}
}
// Print edges of MST using the parent array
for (int i = 1; i < V; ++i)
{
Console.WriteLine($"{parent[i]} - {i}");
}
}
// Driver program to test methods of the PrimMST class
public static void Main()
{
int V = 9;
List<List<iPair>> adj = new List<List<iPair>>(new List<iPair>[V]);
// Making the graph as shown in the C++ code
for (int i = 0; i < V; ++i)
{
adj[i] = new List<iPair>();
}
AddEdge(adj, 0, 1, 4);
AddEdge(adj, 0, 7, 8);
AddEdge(adj, 1, 2, 8);
AddEdge(adj, 1, 7, 11);
AddEdge(adj, 2, 3, 7);
AddEdge(adj, 2, 8, 2);
AddEdge(adj, 2, 5, 4);
AddEdge(adj, 3, 4, 9);
AddEdge(adj, 3, 5, 14);
AddEdge(adj, 4, 5, 10);
AddEdge(adj, 5, 6, 2);
AddEdge(adj, 6, 7, 1);
AddEdge(adj, 6, 8, 6);
AddEdge(adj, 7, 8, 7);
PrimMSTAlgorithm(adj, V);
}
}
// A simple priority queue implementation
public class PriorityQueue<T> where T : IComparable<T>
{
private List<T> heap;
public int Count => heap.Count;
public PriorityQueue()
{
heap = new List<T>();
}
public void Enqueue(T item)
{
heap.Add(item);
int i = heap.Count - 1;
while (i > 0)
{
int parent = (i - 1) / 2;
if (heap[parent].CompareTo(heap[i]) <= 0)
break;
Swap(parent, i);
i = parent;
}
}
public T Dequeue()
{
if (heap.Count == 0)
throw new InvalidOperationException("Priority queue is empty.");
T item = heap[0];
int lastIndex = heap.Count - 1;
heap[0] = heap[lastIndex];
heap.RemoveAt(lastIndex);
int i = 0;
while (true)
{
int leftChild = 2 * i + 1;
if (leftChild >= heap.Count)
break;
int rightChild = leftChild + 1;
int minChild = (rightChild < heap.Count && heap[rightChild].CompareTo(heap[leftChild]) < 0) ? rightChild : leftChild;
if (heap[i].CompareTo(heap[minChild]) <= 0)
break;
Swap(i, minChild);
i = minChild;
}
return item;
}
private void Swap(int i, int j)
{
T temp = heap[i];
heap[i] = heap[j];
heap[j] = temp;
}
}
JavaScript
class Graph {
constructor(V) {
this.V = V;
this.adj = Array.from({ length: V }, () => []); // Adjacency list to store the graph
}
addEdge(u, v, weight) {
this.adj[u].push({ v, weight }); // Add edge to the graph
this.adj[v].push({ v: u, weight }); // Undirected graph, so add edge in both directions
}
primMST() {
const pq = new PriorityQueue((a, b) => a.weight - b.weight);
// Priority queue to store vertices
const src = 0;
const key = new Array(this.V).fill(Number.MAX_SAFE_INTEGER);
// Key values to track the minimum edge weight to each vertex
const parent = new Array(this.V).fill(-1);
// Parent array to store the resulting MST
const inMST = new Array(this.V).fill(false);
// To keep track of vertices included in MST
pq.enqueue({ v: src, weight: 0 }); // Start with the source vertex
key[src] = 0;
while (!pq.isEmpty()) {
const { v: u, weight } = pq.dequeue(); // Extract the vertex with the minimum key
if (inMST[u]) {
continue; // Skip if the vertex is already in the MST
}
inMST[u] = true;
for (const edge of this.adj[u]) {
const { v, weight } = edge;
if (!inMST[v] && key[v] > weight) {
// If vertex v is not in MST and the weight of edge (u, v) is smaller than the current key of v
key[v] = weight;
pq.enqueue({ v, weight: key[v] });
parent[v] = u;
}
}
}
// Print edges of MST using parent array
for (let i = 1; i < this.V; ++i) {
console.log(parent[i] + " - " + i);
}
}
}
class PriorityQueue {
constructor(compare) {
this.heap = [];
this.compare = compare;
}
enqueue(element) {
this.heap.push(element);
this.bubbleUp();
}
dequeue() {
const root = this.heap[0];
const last = this.heap.pop();
if (this.heap.length > 0) {
this.heap[0] = last;
this.bubbleDown();
}
return root;
}
isEmpty() {
return this.heap.length === 0;
}
bubbleUp() {
let index = this.heap.length - 1;
while (index > 0) {
const element = this.heap[index];
const parentIndex = Math.floor((index - 1) / 2);
const parent = this.heap[parentIndex];
if (this.compare(parent, element) < 0) {
break;
}
this.heap[index] = parent;
this.heap[parentIndex] = element;
index = parentIndex;
}
}
bubbleDown() {
let index = 0;
const length = this.heap.length;
const element = this.heap[0];
while (true) {
let leftChildIndex = 2 * index + 1;
let rightChildIndex = 2 * index + 2;
let leftChild, rightChild;
let swap = null;
if (leftChildIndex < length) {
leftChild = this.heap[leftChildIndex];
if (this.compare(leftChild, element) < 0) {
swap = leftChildIndex;
}
}
if (rightChildIndex < length) {
rightChild = this.heap[rightChildIndex];
if (
(swap === null && this.compare(rightChild, element) < 0) ||
(swap !== null && this.compare(rightChild, leftChild) < 0)
) {
swap = rightChildIndex;
}
}
if (swap === null) {
break;
}
this.heap[index] = this.heap[swap];
this.heap[swap] = element;
index = swap;
}
}
}
// Driver program to test methods of graph class
const V = 9;
const graph = new Graph(V);
graph.addEdge(0, 1, 4);
graph.addEdge(0, 7, 8);
graph.addEdge(1, 2, 8);
graph.addEdge(1, 7, 11);
graph.addEdge(2, 3, 7);
graph.addEdge(2, 8, 2);
graph.addEdge(2, 5, 4);
graph.addEdge(3, 4, 9);
graph.addEdge(3, 5, 14);
graph.addEdge(4, 5, 10);
graph.addEdge(5, 6, 2);
graph.addEdge(6, 7, 1);
graph.addEdge(6, 8, 6);
graph.addEdge(7, 8, 7);
graph.primMST();
Output0 - 1
1 - 2
2 - 3
3 - 4
2 - 5
5 - 6
6 - 7
2 - 8
Note: Like Dijkstra's priority_queue implementation, we may have multiple entries for same vertex as we do not (and we can not) make isMST[v] = true in if condition.
C++
// If v is not in MST and weight of (u,v) is smaller
// than current key of v
if (inMST[v] == false && key[v] > weight)
{
// Updating key of v
key[v] = weight;
pq.push(make_pair(key[v], v));
parent[v] = u;
}
Java
// If v is not in MST and weight of (u,v) is smaller
// than current key of v
if (inMST[v] == false && key[v] > weight)
{
// Updating key of v
key[v] = weight;
pq.add(new Pair<Integer, Integer>(key[v], v));
parent[v] = u;
}
// This code is contributed by avanitrachhadiya2155
Python3
# If v is not in MST and weight of (u,v) is smaller
# than current key of v
if (inMST[v] == False and key[v] > weight) :
# Updating key of v
key[v] = weight
pq.append([key[v], v])
parent[v] = u
# This code is contributed by divyeshrabadiya07.
C#
// If v is not in MST and weight of (u,v) is smaller
// than current key of v
if (inMST[v] == false && key[v] > weight)
{
// Updating key of v
key[v] = weight;
pq.Add(new Tuple<int,int>(key[v], v));
parent[v] = u;
}
// This code is contributed by divyesh072019.
JavaScript
<script>
// If v is not in MST and weight of (u,v)
// is smaller than current key of v
if (inMST[v] == false && key[v] > weight)
{
// Updating key of v
key[v] = weight;
value = [key[v], v];
pq.push(value);
parent[v] = u;
}
// This code is contributed by suresh07
</script>
But as explained in Dijkstra's algorithm, time complexity remains O(E Log V) as there will be at most O(E) vertices in priority queue and O(Log E) is same as O(Log V).
Unlike Dijkstra's implementation, a boolean array inMST[] is mandatory here because the key values of newly inserted items can be less than the key values of extracted items. So we must not consider extracted items.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Similar Reads
Dijkstra's Shortest Path Algorithm using priority_queue of STL
Given a graph and a source vertex in graph, find shortest paths from source to all vertices in the given graph.Input : Source = 0Output : Vertex Distance from Source 0 0 1 4 2 12 3 19 4 21 5 11 6 9 7 8 8 14We have discussed Dijkstraâs shortest Path implementations.Dijkstraâs Algorithm for Adjacency
15+ min read
Priority Queue Using Array
A priority queue is a data structure that stores elements with associated priorities. In a priority queue, elements are dequeued in order of their priority, with the highest priority elements being removed first. It is commonly used in algorithms like Dijkstra's for shortest path and in real-time sc
8 min read
Priority Queue using Linked List
Implement Priority Queue using Linked Lists. The Linked List should be so created so that the highest priority ( priority is assigned from 0 to n-1 where n is the number of elements, where 0 means the highest priority and n-1 being the least ) element is always at the head of the list. The list is a
11 min read
Preemptive Priority CPU Scheduling Algorithm
Preemptive Priority CPU Scheduling Algorithm is a pre-emptive method of CPU scheduling algorithm that works based on the priority of a process. In this algorithm, the scheduler schedules the tasks to work as per the priority, which means that a higher priority process should be executed first. In ca
15+ min read
priority_queue::swap() in C++ STL
Priority queues are a type of container adaptors, specifically designed such that the first element of the queue is either the greatest or the smallest of all elements in the queue. However, in C++ STL (by default) the largest element is at the top. We can also create a priority queue having the sma
3 min read
How to implement stack using priority queue or heap?
How to Implement stack using a priority queue(using min heap)? Asked In: Microsoft, Adobe. Solution: In the priority queue, we assign priority to the elements that are being pushed. A stack requires elements to be processed in the Last in First Out manner. The idea is to associate a count that dete
6 min read
Minimum Spanning Tree using Priority Queue and Array List
Given a bi-directed weighted (positive) graph without self-loops, the task is to generate the minimum spanning tree of the graph.Examples: Input: N = 9, E = 14, edges = {{0, 1, 4}, {0, 7, 8}, {1, 2, 8}, {1, 7, 11}, {2, 3, 7}, {2, 8, 2}, {2, 5, 4}, {3, 4, 9}, {3, 5, 14}, {4, 5, 10}, {5, 6, 2}, {6, 7,
5 min read
Priority Queue in C++ STL
In C++, priority queue is a type of queue in which there is some priority assigned to the elements. According to this priority, elements are removed from the queue. By default, the value of the element being inserted is considered as priority. Higher its value, higher its priority. But this can be c
6 min read
Custom Comparator in Priority_queue in C++ STL
Prerequisites:Priority Queue in C++Heap in C++Min Heap and Max Heap Priority_queue<template datatype> is a very famous STL Container generally used when we need to use Heap Data structure. The Characteristics of the heap data structure are:O(1) or constant time retrieval of min/max in the list
3 min read
Merge two sorted arrays using Priority queue
Given two sorted arrays A[] and B[] of sizes N and M respectively, the task is to merge them in a sorted manner. Examples: Input: A[] = { 5, 6, 8 }, B[] = { 4, 7, 8 }Output: 4 5 6 7 8 8 Input: A[] = {1, 3, 4, 5}, B] = {2, 4, 6, 8} Output: 1 2 3 4 4 5 6 8 Input: A[] = {5, 8, 9}, B[] = {4, 7, 8} Outpu
6 min read