-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmst.cpp
138 lines (119 loc) · 2.7 KB
/
mst.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
// Program to find Minimum Spanning Tree of a given tree.
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
class Graph
{
int n;
int no_edg;
int edges[100][4], sets[100][10], top[100];
public:
int input();
void initialize_span_t();
void sortedges();
void kruskal_algo();
int find_node(int);
};
int Graph :: input()
{
cout << " \n\t\t\t ****** KRUSKAL'S ALGORITHM FOR MINIMUM SPANNING TREE ****** \n\t\t\t ";
cout << " \n\t\t\t Enter the no. of vertices in the graph: \n\t\t\t ";
cin >> n;
no_edg = 0;
cout << " \n\t\t\t Enter the weights of the following: \n\t\t\t ";
for( int i = 1; i <= n; i++ )
{
for( int j = i + 1; j <= n; j++ )
{
cout << " \n\t\t\t edge " << i << " , " << j << " : ";
int w;
cin >> w;
if( w != 0)
{
no_edg++;
edges[no_edg][1] = i;
edges[no_edg][2] = j;
edges[no_edg][3] = w;
}
}
}
cout << " \n\t\t\t The edges in the given graph are:: \n\t\t\t ";
for( int i = 1; i <= no_edg; i++ )
{
cout << " < " << edges[i][1] << " , " << edges[i][2] << " > " << edges[i][3] << " \n\t\t\t ";
}
}
void Graph::sortedges()
{
for( int i = 1; i <= no_edg - 1; i++ )
{
for( int j = 1; j <= no_edg-i; j++ )
{
if( edges[j][3] > edges[j + 1][3] )
{
int t = edges[j][1];
edges[j][1] = edges[j + 1][1];
edges[j + 1][1] = t;
t = edges[j][2];
edges[j][2] = edges[j + 1][2];
edges[j + 1][2] = t;
t = edges[j][3];
edges[j][3] = edges[j + 1][3];
edges[j + 1][3] = t;
}
}
}
cout << " \n\n\t\t\t After sorting the edges in the given graph are :: \n\t\t\t ";
for( int i = 1; i <= no_edg; i++ )
cout << " " << edges[i][1] << " , " << edges[i][2] << " > ::" << edges[i][3] << " \n\t\t\t ";
}
void Graph :: kruskal_algo()
{
for( int i = 1; i <= n; i++ )
{
sets[i][1] = i;
top[i] = 1;
}
cout << " \n\n\t\t\t ********* THE MINIMUM SPANNING TREE IS ************** \n\t\t\t ";
for( int i = 1; i <= no_edg; i++ )
{
int p1 = find_node( edges[i][1] );
int p2 = find_node( edges[i][2] );
if( p1 != p2 )
{
cout << " \n\t\t\t The edge included in MST is :: " << " < " << edges[i][1] << " , " << edges[i][2] << " > " << endl;
for( int j = 1; j <= top[p2]; j++ )
{
top[p1]++;
sets[p1][top[p1]] = sets[p2][j];
}
top[p2] = 0;
}
else
{
cout << " \n\t\t\t Edge " << " < " << edges[i][1] << " , " << edges[i][2] << " > " << " is not included as it forms a cycle \n\n\t\t\t ";
}
}
}
int Graph :: find_node( int n )
{
for( int i = 1; i <= no_edg; i++ )
{
for( int j = 1; j <= top[i]; j++ )
{
if( n == sets[i][j] )
return i;
}
}
return -1;
}
int main()
{
Graph obj;
obj.input();
obj.sortedges();
obj.kruskal_algo();
cout << "\n\n";
return 0;
}