
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Prim’s Algorithm (Simple Implementation for Adjacency Matrix Representation) in C++
Prim’s Algorithm is a greedy method that is used to find minimum spanning tree for a given weighted undirected graph.
Weighted graph is a graph that has all edges with weight values.
Undirected graph is a special type of graph in which all edges are bidirectional.
Minimum spanning tree is a subset that contains all edges and vertices but no cycle and has the least possible total edge weight.
In this article, we will learn about prim’s algorithm to find minimum spanning tree. Usually, the algorithm uses two arrays but in this solution, we will use only one.
Program to show the implementation of prim’s algorithm.
Example
#include <bits/stdc++.h> using namespace std; #define V 5 bool createsMST(int u, int v, vector<bool> inMST){ if (u == v) return false; if (inMST[u] == false && inMST[v] == false) return false; else if (inMST[u] == true && inMST[v] == true) return false; return true; } void printMinSpanningTree(int cost[][V]){ vector<bool> inMST(V, false); inMST[0] = true; int edgeNo = 0, MSTcost = 0; while (edgeNo < V - 1) { int min = INT_MAX, a = -1, b = -1; for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { if (cost[i][j] < min) { if (createsMST(i, j, inMST)) { min = cost[i][j]; a = i; b = j; } } } } if (a != -1 && b != -1) { cout<<"Edge "<<edgeNo++<<" : ("<<a<<" , "<<b<<" ) : cost = "<<min<<endl; MSTcost += min; inMST[b] = inMST[a] = true; } } cout<<"Cost of Minimum spanning tree ="<<MSTcost; } int main() { int cost[][V] = { { INT_MAX, 12, INT_MAX, 25, INT_MAX }, { 12, INT_MAX, 11, 8, 12 }, { INT_MAX, 11, INT_MAX, INT_MAX, 17 }, { 25, 8, INT_MAX, INT_MAX, 15 }, { INT_MAX, 12, 17, 15, INT_MAX }, }; cout<<"The Minimum spanning tree for the given tree is :\n"; printMinSpanningTree(cost); return 0; }
Output
The Minimum spanning tree for the given tree is : Edge 0 : (0 , 1 ) : cost = 12 Edge 1 : (1 , 3 ) : cost = 8 Edge 2 : (1 , 2 ) : cost = 11 Edge 3 : (1 , 4 ) : cost = 12 Cost of Minimum spanning tree =43
- Related Articles
- Dijkstra’s Algorithm for Adjacency List Representation
- Prim's algorithm in Javascript
- Prim’s MST for Adjacency List Representation
- Program to find out an MST using Prim's algorithm in Python
- Legendre’s Conjecture: Concept, Algorithm, Implementation in C++
- Algorithm for matrix multiplication in JavaScript
- C++ Program to Implement Adjacency Matrix
- Implementation of Whale Optimization Algorithm
- C++ Program to Represent Graph Using Adjacency Matrix
- Matrix Representation of Graphs
- Page Rank Algorithm and Implementation using Python
- Matrix multiplication algorithm
- What is Implementation of Simple Stack Allocation Scheme
- Implementation of a Falling Matrix in C++
- Tarjan's Algorithm for Strongly Connected Components

Advertisements