-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkruskal.cpp
178 lines (115 loc) · 3.75 KB
/
kruskal.cpp
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
/***********************************************
***********************************************
**********AUTHOR:CS14BTECH11030****************
*************COMPILATION:G++*******************
********OPERATING SYSTEM:UBUNTU(LINUX)*********
************DATE CREATED:29/AUG/2015************
***********************************************
***********************************************/
#include<iostream> //including required libraries
using namespace std;
int id[100],nnodes,nedges,size1[100],array[100],set1[100][3];
void quicksort(int set1[][3],int start,int end);
int find(int a);
void union_of_subsets(int x,int y); //required functions
int form_minimumtree_with_kruskal(int set1[][3]);
int main(int argc, char const *argv[])
{
/* code */
int i,x,y, weight,size=1;
cout<<"enter the number of nodes and edges in the graph\n"; //scannning number of node and edges in graph
cin>>nnodes>>nedges;
for(i=0;i<nnodes;i++){
id[i] = i;
size1[i] =0; //initiating id and size of all nodes to be i and 0
}
cout<<"enter the endpoints of each edge and the corresponding weight\n";
for(i=0;i<nedges;i++){
cin>>x>>y>>weight;
set1[i][0] = weight; //scanning the edgelist
set1[i][1] = x;
set1[i][2] = y;
size++;
}
cout<<"the minimum spanning tree with kruskal algorithm is\n";
int min_cost = form_minimumtree_with_kruskal(set1);
cout<<"minimum cost is :"<<min_cost<<endl;
return 0;
}
int find(int a){
if(id[a]!=a){
id[a] = id[id[a]];
a = id[a]; //going recursively to top most parent and finding they are in same set or not
}
return a;
}
void union_of_subsets(int x,int y){
int p = find(x);
int q= find(y);
if(size1[p]<size1[q]){
id[p] = q;
size1[q] = size1[q] + size1[p]; //finding sizes of both sets and forming parent accordingly
}else if(size1[p]>size1[q]){
id[q] = p;
size1[p] = size1[p] + size1[q];
}else if(size1[p]==size1[q]){
id[q] = p;
size1[p] = size1[p] + size1[q];
}
}
/*
int form_minimumtree_with_kruskal(pair <int,pair<int,int> >set[]){
*/
int form_minimumtree_with_kruskal(int set1[][3]){
int a,b,j,cost=0,min_cost=0;
quicksort(set1,0,nedges-1);
// code
for (j = 0; j < nedges; j++)
{
a = set1[j][1];
b = set1[j][2];
cost = set1[j][0];
if(find(a)!=find(b)){ //seeing the sorted edge list belong to same set or not and doing union accordingly
min_cost = min_cost + cost;
union_of_subsets(a,b);
cout<<a<<"--"<<b<<'\t'<<cost<<endl;
}
}
return min_cost;
}
void quicksort(int set1[][3],int start,int end){
int holder,j,t1,t2,t3,i;
if(start<end){
holder=start;
i=start;
j=end; //selecting holder as start and soring by quicksort
while(i<j){
while(set1[i][0]<=set1[holder][0]&&i<end)
i++;
while(set1[j][0]>set1[holder][0])
j--;
if(i<j){
t1=set1[i][0];
t2 = set1[i][1];
t3 = set1[i][2];
set1[i][0]=set1[j][0];
set1[i][1]=set1[j][1];
set1[i][2]=set1[j][2];
set1[j][0]=t1;
set1[j][1]=t2;
set1[j][2]=t3;
}
}
t1=set1[holder][0];
t2=set1[holder][1];
t3=set1[holder][2];
set1[holder][0]=set1[j][0];
set1[holder][1]=set1[j][1]; //swapping elements
set1[holder][2]=set1[j][2];
set1[j][0]=t1;
set1[j][1]=t2;
set1[j][2]=t3;
quicksort(set1,start,j-1);
quicksort(set1,j+1,end); //dividing array and calling quicksort recursively
}
}