-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKruskal.cpp
More file actions
167 lines (147 loc) · 3.76 KB
/
Kruskal.cpp
File metadata and controls
167 lines (147 loc) · 3.76 KB
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
#include <iostream.h>
#include <conio.h>
#include <timer.h>
#include <stdlib.h>
#define MAX 20
/*---------------------------------------------------------------------
for general purpose comment the randomize() statement in main() i.e., G.randomize()
and for getting the bulk data...comment all except G.randomize
telling this just for clarity
---------------------------------------------------------------------*/
class Edge
{
public :
int start,end,weight;
Edge()
{
start = end = weight = 0;
}
void print()
{
cout << "From " << start << " To " << end << " (wt : " << weight << ")\n";
}
};
class Graph
{
int **edges,nodes,sides,x,*parent,Wsum;
Edge *e;
public :
Graph()
{
nodes = sides = Wsum = 0;
x = -1;
}
void input();
void start_Kruskal();
void printTree();
void randomize();
};
void Graph :: input()
{
while(1)
{
cout << "\nGive the number of nodes : ";cin >> nodes;
if(nodes >= MAX) cout << "Give the number lesser than 22\n";
else break;
}
//General initialisation
parent = new int [nodes];
*edges = new int [nodes];
for(int j = 0;j < nodes;j++) edges[j] = new int [nodes];
for(int row = 0;row < nodes;row++)
for(int col = 0;col < nodes;col++)
{
parent[row] = -1;
edges[row][col] = 999;
}
//--------------------------
cout << "\nGive the number of edges : ";cin >> sides;
e = new Edge [sides];
cout << "\nGive the edges one-by-one\n";
while(sides--)
{
int start,end,weight;
cout << "From : ";cin >> start;
cout << "To : ";cin >> end;
cout << "Weight : ";cin >> weight;cout << endl;
edges[start - 1][end - 1] = weight;edges[end - 1][start - 1] = weight;
}
}
void Graph :: start_Kruskal()
{
while(x < nodes - 2)
{
int min = 999,start,end,u,v;
for(int i = 0;i < nodes;i++) //finding the minimum
for(int j = 0;j < nodes;j++)
if(edges[i][j] < min)
{
min = edges[i][j];
start = u = i;
end = v = j;
}
//checking for cycles
while(parent[u] != -1) u = parent[u];
while(parent[v] != -1) v = parent[v];
if(u != v)
{
e[++x].start = start + 1;
e[x].end = end + 1;
e[x].weight = min;
Wsum += min;
parent[v] = u;
}
edges[start][end] = 999;edges[end][start] = 999;
}
}
void Graph :: printTree()
{
cout << "\nFirst of all .. draw all the nodes without any edges\n";
for(int i = 0;i <= x;i++)
{
cout << "then draw ";
e[i].print();
cout << "\n";
}
cout << "\nThe weight of the minimum spanning tree thus formed : " << Wsum;
Wsum = 0;
}
void Graph :: randomize()
{
Timer t;e = new Edge [MAX];
for(int i = 1;i < MAX;i++)
{
nodes = i;
parent = new int [nodes];
*edges = new int [nodes];
for(int j = 0;j < nodes;j++) edges[j] = new int [nodes];
x = -1;
for(int row = 0;row < nodes;row++)
for(int col = 0;col < nodes;col++)
parent [row] = edges[row][col] = -1;
for(int k = 0;k < nodes;k++)
for(int l = 0;l < nodes;l++)
{
int tmp = (rand() % 999) + 1;
if(edges[k][l] != -1) continue;
else edges[k][l] = edges[l][k] = tmp;
}
t.start();start_Kruskal();t.stop();
cout << "\nFor operation " << nodes;
cout << " time taken is : " << t.time() << " seconds";
t.reset();
}
}
int main()
{
Graph G;
Timer t;
clrscr();
G.input();
t.start();G.start_Kruskal();t.stop();
G.printTree();
cout << "\nTime taken for this is : " << t.time() << " seconds\n";
//G.randomize(); //uncomment this only for bulk operation..,otherwise its gonna mess up the screen
getch();
return 0;
}