-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKruskals.c
71 lines (57 loc) · 1.54 KB
/
Kruskals.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
//Lab Program
#include <stdio.h>
#define MAX 10
#define INF 999
int parent[MAX];
int cost[MAX][MAX];
int numVertices, minCost = 0;
int find(int i) {
while (parent[i])
i = parent[i];
return i;
}
int unionSets(int i, int j) {
if (i != j) {
parent[j] = i;
return 1;
}
return 0;
}
int main() {
printf("Enter the number of vertices:\n");
scanf("%d", &numVertices);
printf("Enter the graph data (cost matrix):\n");
for (int i = 1; i <= numVertices; i++) {
for (int j = 1; j <= numVertices; j++) {
scanf("%d", &cost[i][j]);
if (cost[i][j] == 0) {
cost[i][j] = INF; // Replace 0 with INF for non-connected edges
}
}
}
int numEdges = 1;
printf("The edges of the spanning tree are:\n");
while (numEdges < numVertices) {
int min = INF;
int a = -1, b = -1, u = -1, v = -1;
// Find the edge with the minimum cost
for (int i = 1; i <= numVertices; i++) {
for (int j = 1; j <= numVertices; j++) {
if (cost[i][j] < min) {
min = cost[i][j];
a = i;
b = j;
}
}
}
u = find(a);
v = find(b);
if (unionSets(u, v)) {
printf("\n%d Edge (%d,%d) = %d", numEdges++, a, b, min);
minCost += min;
}
cost[a][b] = cost[b][a] = INF; // Mark the edge as used
}
printf("\nMinimum cost = %d\n", minCost);
return 0;
}