-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathprims-algorithm.c
267 lines (223 loc) · 5.15 KB
/
prims-algorithm.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*
Copyright (C) Deepali Srivastava - All Rights Reserved
This code is part of DSA course available on CourseGalaxy.com
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MAX 30
#define TEMPORARY 1
#define PERMANENT 2
#define NIL -1
#define INFINITY 99999
struct Vertex
{
char name[50];
int status;
int predecessor;
int length;
};
struct Vertex vertexList[MAX];
int adj[MAX][MAX]; /* Adjacency Matrix */
int n=0; /* Number of vertices in the graph */
int e=0; /* Number of edges in the graph */
void display();
int getIndex(char s[]);
void insertVertex(char s[]);
void deleteVertex(char s[]);
void insertEdge(char s1[], char s2[], int wt);
void deleteEdge(char s1[], char s2[]);
void prims();
int tempVertexMinL();
main()
{
insertVertex("Zero");
insertVertex("One");
insertVertex("Two");
insertVertex("Three");
insertVertex("Four");
insertVertex("Five");
insertVertex("Six");
insertVertex("Seven");
insertVertex("Eight");
insertVertex("Nine");
insertEdge("Zero", "One", 19);
insertEdge("Zero", "Three", 14);
insertEdge("Zero", "Four", 12);
insertEdge("One", "Two", 20);
insertEdge("One", "Four", 18);
insertEdge("Two", "Four", 17);
insertEdge("Two", "Five", 15);
insertEdge("Two", "Nine", 29);
insertEdge("Three", "Four", 13);
insertEdge("Three", "Six", 28);
insertEdge("Four", "Five", 16);
insertEdge("Four", "Six", 21);
insertEdge("Four", "Seven", 22);
insertEdge("Four", "Eight", 24);
insertEdge("Five", "Eight", 26);
insertEdge("Five", "Nine", 27);
insertEdge("Six", "Seven", 23);
insertEdge("Seven", "Eight", 30);
insertEdge("Eight", "Nine", 35);
prims();
}
void prims()
{
int c,v;
int edgesInTree = 0;
int wtTree = 0;
int root;
for(v=0; v<n; v++)
{
vertexList[v].status = TEMPORARY;
vertexList[v].length = INFINITY;
vertexList[v].predecessor = NIL;
}
root = 0;
vertexList[root].length = 0;
while(1)
{
c = tempVertexMinL();
if(c == NIL)
{
if(edgesInTree == n-1)
printf("Weight of minimum spanning tree is %d\n", wtTree);
else
printf("Graph is not connected, Spanning tree not possible");
return;
}
vertexList[c].status = PERMANENT;
/* Include edge ( vertexList[c].predecessor,c ) in the tree*/
if(c != root)
{
edgesInTree++;
printf("(%s, %s)\n", vertexList[vertexList[c].predecessor].name,vertexList[c].name);
wtTree = wtTree + adj[vertexList[c].predecessor][c];
}
for(v = 0; v < n; v++)
if(adj[c][v]!=0 && vertexList[v].status == TEMPORARY)
if (adj[c][v] < vertexList[v].length)
{
vertexList[v].length = adj[c][v];
vertexList[v].predecessor = c;
}
}
}
int tempVertexMinL()
{
int min = INFINITY;
int v, x = NIL;
for (v = 0; v < n; v++)
{
if(vertexList[v].status == TEMPORARY && vertexList[v].length < min)
{
min = vertexList[v].length;
x = v;
}
}
return x;
}
void display()
{
int i,j;
printf("Adjacency Matrix - \n");
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
printf("%4d",adj[i][j]);
printf("\n");
}
printf("\nVertices - ");
for(i=0; i<n; i++)
printf("%s ", vertexList[i].name);
printf("\nEdges - ");
for(i=0; i<n; i++)
for(j=0; j<i; j++)
if( adj[i][j] != 0 )
printf("(%s, %s, %d ) ", vertexList[i].name,vertexList[j].name, adj[i][j]);
printf("\n\n");
}
int getIndex(char s[])
{
int i;
for (i = 0; i<n; i++)
if ( strcmp(s,vertexList[i].name) == 0 )
return i;
printf("Invalid Vertex - %s \n\n", s);
return -1;
}
void insertVertex(char s[])
{
int i;
for(i = 0; i<n; i++ )
if( strcmp(vertexList[i].name, s) == 0)
{
printf("Vertex already present\n\n");
return;
}
strcpy(vertexList[n].name, s);
n++;
}
void deleteVertex(char s[])
{
int i, x = getIndex(s), u;
if(x == -1)
return;
u = x;
while (u < n-1)
{
for (i = 0; i < n; i++) /* shift rows to the left */
adj[i][u] = adj[i][u+1];
for (i = 0; i < n; i++) /* shift columns up */
adj[u][i] = adj[u+1][i];
u++;
}
for(i = x; i < n-1; i++)
vertexList[i] = vertexList[i+1];
n--;
}
int isAdjacent(char s1[], char s2[])
{
int u,v;
u = getIndex(s1);
v = getIndex(s2);
if (u == -1 || v == -1)
return -1;
return adj[u][v];
}
/* Insert an edge (s1,s2) */
void insertEdge(char s1[], char s2[], int wt)
{
int u = getIndex(s1);
int v = getIndex(s2);
if (u == v)
{
printf("Not a valid edge\n\n");
return;
}
if (adj[u][v] != 0 )
printf("Edge already present\n\n");
else
{
adj[u][v] = wt;
adj[v][u] = wt;
e++;
}
}
/* Delete the edge (s1,s2) */
void deleteEdge(char s1[], char s2[])
{
int u = getIndex(s1);
int v = getIndex(s2);
if(u==-1 || v ==-1)
return;
if( adj[u][v] == 0 )
printf("Edge not present\n\n");
else
{
adj[u][v] = 0;
adj[v][u] = 0;
e--;
}
}