forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kruskal_Algorithm.c
153 lines (132 loc) · 3.41 KB
/
Kruskal_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
// Kruskal Algorithm in C
#include <stdio.h>
#define NUM 20
// Creating the edge
typedef struct edge
{
int p; // Variable to contain all the first node of the edge
int q; // Variable to contain all the second node of the edge
int weight; // Variable to contain the weight of the edge
} edge;
// Creating the Edgelist
typedef struct edgelist
{
edge data[NUM];
int n;
} edgelist;
edgelist list;
int weights[NUM][NUM]; // Array for weights in the graph
int n;
edgelist listSpan;
void kruskal_algo();
void print();
int find(int contain[], int vertexno);
void union1(int contain[], int c1, int c2);
void sort();
// Driver Program
int main()
{
int total_cost;
printf("\nEnter number of vertices : ");
scanf("%d", &n); // Number of Vertices
printf("\nEnter the adjacency matrix : \n");
for(int i = 0; i < n; i++) // Enter the Adjacency Matrix
for(int j = 0; j < n; j++)
scanf("%d", &weights[i][j]);
kruskal_algo();
print();
return 0;
}
// Function performing Kruskal's algorithm
void kruskal_algo()
{
int contain[NUM], x, y;
list.n = 0;
for(int i = 1; i < n; i++)
for(int j = 0; j < i; j++)
{
if(weights[i][j] != 0)
{
// Containing all the first node of the edges
list.data[list.n].p = i;
// Containing all the second node of the edges
list.data[list.n].q = j;
// Containing the weight of the edges
list.data[list.n].weight = weights[i][j];
list.n++;
}
}
// Sorting the edges according to their weight.
sort();
for(int i = 0; i < n; i++)
contain[i] = i; // Intializing the array with first node
listSpan.n = 0;
for(int i = 0; i < list.n; i++)
{
x = find(contain, list.data[i].p); // x is the first node of the edge
y = find(contain, list.data[i].q); // y is the second node of the edge
if(x != y)
{
listSpan.data[listSpan.n] = list.data[i];
listSpan.n = listSpan.n+1;
union1(contain, x, y);
}
}
}
// Returns the value present at k[vertex_number]
int find(int contain[], int vertex_number)
{
return(contain[vertex_number]);
}
void union1(int contain[], int c1, int c2)
{
for(int i = 0; i < n; i++)
if(contain[i] == c2)
contain[i] = c1;
}
// Sorting all the edges in increasing order
// according to their weight.
void sort()
{
edge temp;
for(int i = 1; i < list.n; i++)
{
for(int j = 0; j < list.n-1; j++)
{
if(list.data[j].weight > list.data[j+1].weight)
{
temp = list.data[j];
list.data[j] = list.data[j+1];
list.data[j+1] = temp;
}
}
}
}
// Printing the Output
void print()
{
int cost = 0;
for(int i = 0; i < listSpan.n; i++)
{
printf("\n%d\t%d\t%d", listSpan.data[i].p, listSpan.data[i].q, listSpan.data[i].weight);
cost = cost + listSpan.data[i].weight;
}
printf("\nCost of the spanning tree = %d\n", cost);
}
/*
INPUT :
Enter number of vertices : 6
Enter the Adjacency Matrix :
0 3 0 0 6 5
3 0 1 0 0 4
0 1 0 6 0 4
0 0 6 0 8 5
6 0 0 8 0 2
5 4 4 5 2 0
OUTPUT :
2 1 1
5 4 2
1 0 3
5 1 4
Cost of the spanning tree = 15
*/