C++ Program to Find Minimum Spanning Tree using Prim?s Algorithm
Posted by Superadmin on August 09 2022 06:54:50

C++ Program to Find Minimum Spanning Tree using Prim’s Algorithm

 

 

This C++ program depicts the Prim’s Algorithm which finds the minimal spanning tree(tree consisting of the minimum weights of edges connecting any two vertices) in a graph.

 

Here is the source code of the C++ program to display the destination node, start node and the weight of node connecting the two using Prim’s Algorithm such that the resultant values of weights of edges are the smallest possible. This C++ program is successfully compiled and run on DevCpp, a C++ compiler. The program output is given below.

  1. /*
  2.  * C++ Program to find MST(Minimum Spanning Tree) using 
  3.  * Prim's Algorithm
  4.  */
  5. #include <iostream>
  6. #include <conio.h>
  7. using namespace std;
  8. struct node
  9. {
  10.     int fr, to, cost;
  11. }p[6];
  12. int c = 0, temp1 = 0, temp = 0;
  13. void prims(int *a, int b[][7], int i, int j)
  14. {
  15.     a[i] = 1;
  16.     while (c < 6)
  17.     {
  18.         int min = 999;
  19.         for (int i = 0; i < 7; i++)
  20.         {
  21.             if (a[i] == 1)
  22.             {
  23.                 for (int j = 0; j < 7; )
  24.                 {
  25.                     if (b[i][j] >= min || b[i][j] == 0)
  26.                     {
  27.                         j++;
  28.                     }
  29.                     else if (b[i][j] < min)
  30.                     {
  31.                         min = b[i][j];
  32.                         temp = i;
  33.                         temp1 = j;
  34.                     }
  35.                 }
  36.             }
  37.         }
  38.         a[temp1] = 1;
  39.         p[c].fr = temp;
  40.         p[c].to = temp1;
  41.         p[c].cost = min;
  42.         c++;       
  43.         b[temp][temp1] = b[temp1][temp]=1000;
  44.     }
  45.     for (int k = 0; k < 6; k++)
  46.     {
  47.         cout<<"source node:"<<p[k].fr<<endl;
  48.         cout<<"destination node:"<<p[k].to<<endl;
  49.         cout<<"weight of node"<<p[k].cost<<endl;
  50.     }
  51. }
  52. int main()
  53. {
  54.     int a[7];
  55.     for (int i = 0; i < 7; i++)
  56.     {
  57.         a[i] = 0;
  58.     }
  59.     int b[7][7];
  60.     for (int i = 0; i < 7; i++)
  61.     {
  62.         cout<<"enter values for "<<(i+1)<<" row"<<endl;
  63.         for (int j = 0; j < 7; j++)
  64.         {
  65.             cin>>b[i][j];
  66.         }
  67.     }
  68.     prims(a, b, 0, 0);
  69.     getch();
  70. }

 

Output
enter values of adjacency matrix for a 7 node graph:
 
enter values for 1 row
0
3
6
0
0
0
0
enter values for 2 row
3
0
2
4
0
0
0
enter values for 3 row
6
2
0
1
4
2
0
enter values for 4 row
0
4
1
0
2
0
4
enter values for 5 row
0
0
4
2
0
2
1
enter values for 6 row
00
0
2
0
2
0
1
enter values for 7 row
0
0
0
4
1
1
0
MINIMUM SPANNING TREE AND ORDER OF TRAVERSAL:
 
source node:0
destination node:1
weight of node3
source node:1
destination node:2
weight of node2
source node:2
destination node:3
weight of node1
source node:2
destination node:5
weight of node2
source node:5
destination node:6
weight of node1
source node:6
destination node:4
weight of node1