-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprim.cpp
More file actions
61 lines (56 loc) · 1.47 KB
/
prim.cpp
File metadata and controls
61 lines (56 loc) · 1.47 KB
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
#include<iostream>
using namespace std;
void minimum(int **graph, int v, bool *selected){
int min = 99999 , index, from;
for(int i = 0; i < v; i++){
if(selected[i]){
for(int j = 0; j < v; j++){
if(graph[i][j] != 0 && graph[i][j] < min && !selected[j]){
min = graph[i][j];
from = i;
index = j;
}
}
}
}
selected[index] = true;
cout << (char)(from+65) << " - " << (char)(index + 65) << " : " << graph[from][index] << endl;
//return index;
}
void prim(){
int v;
cout << "Enter the number of vertices : ";
cin >> v;
bool* selected = new bool[v];
// for(int i = 0; i < v; i++){
// selected[i] = false;
// }
int **graph = new int*[v];
for(int i = 0; i < v; i++){
graph[i] = new int[v];
}
cout << "Enter the graph as adjacency matrix :" << endl;
cout << " ";
for(int i= 0 ; i<v; i++){
cout << (char)(65 + i) << " ";
}
cout << endl;
for(int i = 0; i < v; i++){
selected[i] = false;
cout << (char)(65 + i) << " ";
for(int j = 0; j < v; j++){
cin >> graph[i][j];
}
}
cout << "Edge Weight" << endl;
selected[0] = true;
for(int i = 0; i < v-1; i++){
minimum(graph,v,selected);
// for(int j = 0; j < v-1; j++){
// cout << i <<
// }
}
}
int main(){
prim();
}