forked from NJACKWinterOfCode/nwoc_algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prims_algorithm.cpp
73 lines (55 loc) · 1.47 KB
/
prims_algorithm.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/* Prim's Algorithm for finding Minimum Spanning Tree
Sample Input is already given in main function:
graph[V][V] = { { 0, 2, 0, 6, 0 },
{ 2, 0, 3, 8, 5 },
{ 0, 3, 0, 0, 7 },
{ 6, 8, 0, 0, 9 },
{ 0, 5, 7, 9, 0 } };
Sample Output:
Edge Value
1 - 0 2
2 - 1 3
3 - 0 6
4 - 1 5
*/
#include <bits/stdc++.h>
using namespace std;
//V is the number of vertices
#define V 5
//Function for finding MST using Prims Algorithm
void primMST(int graph[V][V])
{
//parent is used to store the index position
//key is used to store the value
int parent[V],key[V];
//All key values are initialized to maximum integer value
for(int i=0;i<V;i++)
key[i]= INT_MAX;
for(int u = 1; u<V; u++)
{
for(int row=0;row<V;row++)
{
if(graph[row][u] && graph[row][u]<key[u])
{
key[u] = graph[row][u];
parent[u] = row;
}
}
}
cout<<"Edge\tValue\n";
for(int m=1;m<V;m++)
{
cout<<m<<" - "<<parent[m]<<" \t "<<key[m]<<"\n";
}
}
int main()
{
// Variable graph contains the adjacency matrix of a graph
int graph[V][V] = { { 0, 2, 0, 6, 0 },
{ 2, 0, 3, 8, 5 },
{ 0, 3, 0, 0, 7 },
{ 6, 8, 0, 0, 9 },
{ 0, 5, 7, 9, 0 } };
primMST(graph);
return 0;
}