-
Notifications
You must be signed in to change notification settings - Fork 2
/
10.easy_krushkal.c
94 lines (76 loc) · 1.97 KB
/
10.easy_krushkal.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
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTICES 20
int parent[MAX_VERTICES];
// Find operation with path compression
int find(int vertex)
{
if (parent[vertex] == -1)
return vertex;
return find(parent[vertex]);
}
// Union operation
void unionVertices(int root1, int root2)
{
parent[root1] = root2;
}
void kruskal(int vertices, int cost[][MAX_VERTICES])
{
int totalEdges = 0, minCost = 0;
// Initialize parent array
for (int i = 0; i < vertices; i++)
{
parent[i] = -1;
}
printf("Minimum Spanning Tree edges:\n");
while (totalEdges < vertices - 1)
{
int min = 9999, a = -1, b = -1;
// Find the minimum cost edge
for (int i = 0; i < vertices; i++)
{
for (int j = 0; j < vertices; j++)
{
if (find(i) != find(j) && cost[i][j] < min)
{
min = cost[i][j];
a = i;
b = j;
}
}
}
if (a != -1 && b != -1)
{
int rootA = find(a);
int rootB = find(b);
// Add edge to MST
if (rootA != rootB)
{
printf("(%d, %d) - %d\n", a, b, min);
unionVertices(rootA, rootB);
minCost += min;
totalEdges++;
}
cost[a][b] = cost[b][a] = 9999; // Mark the edge as visited
}
}
printf("Minimum cost of spanning tree: %d\n", minCost);
}
int main()
{
int vertices, cost[MAX_VERTICES][MAX_VERTICES];
printf("Kruskal's algorithm in C\n");
printf("========================\n");
printf("Enter the number of vertices: ");
scanf("%d", &vertices);
printf("Enter the cost adjacency matrix:\n");
for (int i = 0; i < vertices; i++)
{
for (int j = 0; j < vertices; j++)
{
scanf("%d", &cost[i][j]);
}
}
kruskal(vertices, cost);
return 0;
}