-
Notifications
You must be signed in to change notification settings - Fork 1
/
prim_geek.c
92 lines (70 loc) · 1.6 KB
/
prim_geek.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <stdio.h>
#define MAX 1000
#define INF 99999
int arr[MAX][MAX];
int FindMinKey(int num, int MSTSet[], int key[])
{
int k;
int min = INF;
int minnode;
for(k=0; k < num; k++) {
if (!MSTSet[k] && key[k] < min) {
min = key[k];
minnode = k;
}
}
return minnode;
}
int main()
{
int key[MAX];
int MSTSet[MAX];
int parent[MAX];
int k;
int num;
int r;
int c;
int l;
printf("Welcome to PRIM's shortest path algo..");
printf("Enter number of elements..\n");
scanf("%d", &num);
for(r=0; r< num; r++) {
for(c=0; c< num; c++) {
scanf("%d", &arr[r][c]);
}
}
for(r=0; r< num; r++) {
for(c=0; c< num; c++) {
printf("[%d]", arr[r][c]);
}
printf("\n");
}
for(k=0; k < num; k++) {
parent[k] = INF;
key[k] = INF;
MSTSet[k] = 0;
}
/* Initialize key 0*/
key[0] = 0;
parent[0] = -1;
for(k=0; k < num; k++) {
int minkey = FindMinKey(num, MSTSet, key);
/* Include Minkey in MST set*/
MSTSet[minkey] = 1;
for(l=0; l < num; l++) {
if (arr[minkey][l] && !MSTSet[l]) {
if (arr[minkey][l] < key[l]) {
key[l] = arr[minkey][l];
parent[l] = minkey;
}
}
}
}
printf("Parent child\n");
printf("===============\n");
/* Print MST */
for(k=1; k< num; k++) {
printf("[%d] [%d] [%d]\n", parent[k], k, arr[parent[k]][k]);
}
return 0;
}