-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprim.c
68 lines (60 loc) · 1.42 KB
/
prim.c
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
#include <stdio.h>
#include <stdbool.h>
#define N 7
void prim(int rows,int columns,int graph[rows][columns]);
int find_min(int key[], bool selected[]);
int main(int argc, char const *argv[])
{
int graph[7][7] = {
{0,3,6,0,0,0,0},
{3,0,2,4,0,0,0},
{6,2,0,1,4,2,0},
{0,4,1,0,2,0,4},
{0,0,4,2,0,2,1},
{0,0,2,0,2,0,1},
{0,0,0,4,1,1,0}
};
prim(N,N,graph);
return 0;
}
void prim(int rows,int columns,int graph[rows][columns]){
int key[N],parent[N];
bool selected[N];
// init
for(int i = 0; i < N; i++){
key[i] = __INT32_MAX__;
parent[i] = -1;
}
int edges = 0;
int min;
key[0] = 0;
while(edges < N-1){
min = find_min(key,selected);
selected[min] = true;
for(int i = 0; i < N; i++){
if(graph[min][i] && !selected[i] && graph[min][i] < key[i]){
key[i] = graph[min][i];
parent[i] = min;
}
}
edges++;
}
int cost = 0;
printf("Cost of Edges:\n");
for(int i = 0; i < N; i++){
cost += key[i];
printf("%d,",key[i]);
}
printf("\nCost of MST: %d\n",cost);
}
int find_min(int key[],bool selected[]){
int min = __INT32_MAX__;
int pos;
for(int i = 0; i < N; i++){
if(key[i] < min && !selected[i]){
min = key[i];
pos = i;
}
}
return pos;
}