-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprim.c
114 lines (98 loc) · 2.59 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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <stdio.h>
#include <string.h>
#define MAX 1000
#define INF 0XFFFF
int matrix[MAX][MAX];
int num;
int t_elemeents;
int visited[MAX];
int parent[MAX];
int PickMinimum()
{
int k;
int minimum = INF;
int l;
int nextnode;
for(k=0; k <num; k++) {
if (visited[k]) {
for(l=0; l <num; l++) {
if (matrix[k][l] != 0 && visited[l] != 1) {
if (matrix[k][l] <= minimum) {
minimum = matrix[k][l];
parent[l] = k;
nextnode = l;
}
}
}
}
}
if (minimum!= INF) {
for (k=0; k <num;k++) {
/* mark parents of all other non visited ndoes to INF */
if (visited[k] == 0 && k != nextnode)
parent[k] = INF;
}
/* Mark nextnode as visisted, means, it is enetered in Tree set now */
printf("\n Minimum wiegth [%d] nextnode [%d] parent of next node[%d]\n", minimum, nextnode, parent[nextnode]);
visited[nextnode] = 1;
return nextnode;
} else
return 0;
}
void printMST()
{
int k;
printf("Edge Weight num [%d]\n", num);
for (k= 0; k < num; k++) {
if (parent[k] != INF) {
printf("%d - %d %d\n", parent[k], k, matrix[parent[k]][k]);
}
}
}
void PrimMST(void)
{
int k;
int t_elements = 0;
/* Initialize*/
for(k=0; k <num; k++) {
visited[k] = 0;
parent[k] = INF;
}
/* Start with 0th node of the graph i.e 0th node will become the root node of the tree */
visited[0] = 1;
parent[0] = INF; /* Root node of the tree will not have any parent */
t_elements++;
/* loop till tree set contains num nodes in it */
/*for all the elements in tree set, pic node with smallest outgoing edge */
for (k=0; k <num; k++) {
int nextnode = PickMinimum();
if (nextnode) {
t_elements++;
if (t_elements == num)
break;
}
}
printf("Number of elements in tree set [%d]\n", t_elements);
if (t_elements)
printMST();
else
printf("MST could not be generated out of the given graph...!!!");
}
int main()
{
int r, j;
printf("Welcome to Prims MST..\n");
printf("Enter number of nodes..\n");
scanf("%d", &num);
for(r=0; r<num;r++) {
for(j=0; j<num; j++) {
scanf("%d", &matrix[r][j]);
}
}
for(r=0; r<num;r++) {
for(j=0; j<num; j++)
printf("[%d]", matrix[r][j]);
printf("\n");
}
PrimMST();
}