-
Notifications
You must be signed in to change notification settings - Fork 340
/
prims.cpp
62 lines (47 loc) · 1.21 KB
/
prims.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<bits/stdc++.h>
using namespace std;
void primsAlgorithm(int **G, int v){
int selected[v];
memset(selected, false, sizeof(selected));
selected[0] = true;
int x,y, no_edge = 0;
cout << "Edge : Weight"<<endl;
while (no_edge < v - 1){
int min = INT_MAX;
x = 0;
y = 0;
for(int i=0; i<v; i++){
if(selected[i]){
for(int j=0; j<v; j++){
if(!selected[j] && G[i][j]){
if(min > G[i][j]){
min = G[i][j];
x=i;
y=j;
}
}
}
}
}
cout << x << " - " << y << " : " << G[x][y];
cout << endl;
selected[y] = true;
no_edge++;
}
}
int main() {
int v;
cout<<"Enter the number of verices: ";
cin>>v;
int **G = new int *[v];
for(int i=0; i<v; i++)
G[i] = new int[v];
cout<<"Enter the adjency Matrix:\n";
for(int i=0; i<v; i++){
for(int j=0; j<v; j++){
cin>>G[i][j];
}
}
primsAlgorithm(G, v);
return 0;
}