-
Notifications
You must be signed in to change notification settings - Fork 1
/
P17_Prim_MST.cpp
94 lines (86 loc) · 2.09 KB
/
P17_Prim_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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <vector>
using namespace std;
int getInd(vector<pair<int,int>> v)
{
int min = v[0].second;
int minIndex;
for(int i = 0; i < v.size(); i++)
{
if(v[i].second < min && v[i].second != -1)
{
min = v[i].second;
minIndex = i;
}
}
return v[minIndex].first;
}
int findFun(vector<pair<int,int>> k, int element)
{
for(int i = 1; i < k.size(); i++)
{
if(k[i].first == element && k[i].second != -1)
return 1;
}
return -1;
}
void MST(vector<vector<pair<int,int>>> g)
{
vector<pair<int,int>> key(g.size());
for(int i = 2; i < g.size();i++)
{
key[i].second = 99999;
key[i].first = i;
}
key[1].second = 0;
key[1].first = 1;
key[0].second = 99999;
key[0].first = 99999;
int output[g.size()];
int counter = 1;
while(counter < g.size())
{
int vertix = getInd(key);
for(int i = 0; i < g[vertix].size();i++)
{
pair<int,int> v = g[vertix][i];
if(findFun(key, v.first) != -1 && v.second < key[v.first].second)
{
key[v.first].second = v.second;
output[v.first] = vertix;
}
}
key[vertix].second = -1;
counter++;
}
cout<<"v1 start"<<endl;
for(int i = 2; i < g.size(); i++)
{
cout<< 'v'<< i << " "<<'v'<<output[i]<<endl;
}
}
int main() {
int vertices, edges;
cout<<"Enter Number of Vertex in the graph: "<<endl;
cin>>vertices;
cout<<"Enter Number of Edges in the graph: "<<endl;
cin>>edges;
vector<vector<pair<int,int>>> g(vertices+1);
for(int i = 0; i < edges; i++)
{
string v1,v2;
int w;
cout<<"Enter First vertex: "<<endl;
cin>>v1;
cout<<"Enter Second vertex: "<<endl;
cin>>v2;
cout<<"Enter Edge weight: "<<endl;
cin>>w;
v1 = v1[1];
v2 = v2[1];
g[stoi(v1)].push_back(make_pair(stoi(v2),w));
g[stoi(v2)].push_back(make_pair(stoi(v1),w));
}
MST(g);
return 0;
}