-
Notifications
You must be signed in to change notification settings - Fork 51
/
MST.cpp
62 lines (62 loc) · 1.42 KB
/
MST.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
#include<iostream>
#include<algorithm>
#include<set>
using namespace std;
int minsum = 0;
int minvalue(int value[], bool mstSet[])
{
int min = INT_MAX, min_index;
for (int v = 0; v < 5; v++)
if (mstSet[v] == false && value[v] < min)
min = value[v], min_index = v;
return min_index;
}
void printMST(int conMST[], int tree[5][5])
{
cout<<"Edge \tcost\n";
for (int i = 1; i < 5; i++)
{
cout<<conMST[i]<<" - "<<i<<" \t"<<tree[i][conMST[i]]<<" \n";
minsum+=tree[i][conMST[i]];
}
cout<<"\nThe Minimum cost is:"<<minsum<<endl;
}
void primMST(int tree[5][5],int n)
{
int conMST[n];
int value[n];
bool mstSet[n];
for (int i = 0; i<n; i++)
value[i] = INT_MAX, mstSet[i] = false;
value[0] = 0;
conMST[0] = -1;
for (int count = 0; count <n-1; count++)
{
int u = minvalue(value, mstSet);
mstSet[u] = true;
for (int v = 0; v < n; v++)
if (tree[u][v] && mstSet[v] == false && tree[u][v] < value[v])
conMST[v] = u, value[v] = tree[u][v];
}
printMST(conMST, tree);
}
int main()
{
long n;
cout<<"Enter the number of nodes\n";
cin>>n;
int tree[5][5];
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{
if(j==i)
tree[i][j]=0;
else
{
cout<<"Enter the cost of going from node "<<i <<" to "<< j <<endl;
cin>>tree[i][j];
}
}
primMST(tree,n);
return 0;
}