-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path201651062_Prim.cpp
66 lines (49 loc) · 1.26 KB
/
201651062_Prim.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
#include<bits/stdc++.h>
using namespace std;
#define V 5;
int minKey(int key[], bool mstSet[])
{
// Initialize min value
int min = INT_MAX, min_index;
for (int v = 0; v < V; v++)
if (mstSet[v] == false && key[v] < min)
min = key[v], min_index = v;
return min_index;
}
void printMST(int parent[],int n,int Graph[V][V])
{
cout << "Edge Weight"<<endl;
for(int i=1;i<V;i++)
cout << parent[i]<< "-"<<i<<" "<<Graph[i][parent[i]]<<endl;
}
void PrimMST (int Graph[V][V])
{
int parent[V];int key[V];bool mstSet[V];
for(int i=0;i<V;i++)
{
mstSet[i] = false;key[i] = INT_MAX;
}
key[0] = 0;parent[0]= -1;
for(int counter=0;counter<V-1;counter++)
{
int u = minKey(key,mstSet);
mstSet[u] = true;
for(int v=0;v<V;v++)
{
if (graph[u][v] && mstSet[v] == false && graph[u][v] < key[v])
parent[v] = u, key[v] = graph[u][v];
}
}
printMST(parent, V, graph);
}
int main ()
{
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;
}