-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kruskal's Algo.cpp
136 lines (118 loc) · 2.9 KB
/
Kruskal's Algo.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
#include <iostream>
#include <algorithm>
#include <vector>
#include <unordered_map>
using namespace std;
struct Edge {
char vertex1;
char vertex2;
int weight;
Edge(char v1, char v2, int w):vertex1(v1), vertex2(v2), weight(w) {}
};
struct Graph {
vector<char> vertice;
vector<Edge> edges;
};
unordered_map<char, char> PARENT;
unordered_map<char, int> RANK;
char find(char vertex) {
if (PARENT[vertex] == vertex)
return PARENT[vertex];
else
return find(PARENT[vertex]);
}
void Union( char root1, char root2 ) {
if(RANK[root1]>RANK[root2]){
PARENT[root2]=root1;
}
else if (RANK[root2]> RANK[root1]) {
PARENT[root1]=root2;
}
else{
PARENT[root1]=root2;
RANK[root2]++;
}
}
void MST(Graph& g) {
vector<Edge> res;
for (auto c : g.vertice) {
PARENT[c] = c;
RANK[c] = 0;
}
sort(g.edges.begin(), g.edges.end(), [](Edge x, Edge y) {return x.weight < y.weight;}); // O(E*log(E))
//for ( auto itr = g.edges.begin(); itr!=g.edges.end(); itr++)
//{
//cout<<(*itr).vertex1<<"\t";
//cout<<(*itr).vertex2<<"\t";
//cout<<(*itr).weight<<"\t";
//cout<<"\n";
//}
//for (auto i : PARENT)
//cout << i.first << " " << i.second
//<< endl;
for (Edge e : g.edges) { // O(E)
//cout<<"\nvertex1:"<<e.vertex1<<"\tvertex2:"<<e.vertex2;
if(e.weight!=0){
char root1 = find(e.vertex1); // O(E) worst case
char root2 = find(e.vertex2);
cout<<"\nroot1:"<<root1<<"\troot2:"<<root2;
if (root1 != root2) {
res.push_back(e);
Union(root1,root2);
}
}
}
for (auto i : PARENT)
cout << i.first << " " << i.second
<< endl;
for (auto i : RANK)
cout << i.first << " " << i.second
<< endl;
for (Edge e : res) {
cout << e.vertex1 << " -- " << e.vertex2 << " " << e.weight << endl;
}
}
int main() {
int nodes;
cout<<"Enter no. of nodes: ";
cin>>nodes;
int matrix[nodes][nodes];
int ascii=97;
char t[nodes];
int k=0;
for ( char i =ascii; i <ascii+nodes; i++) {
t[k++]=i;
}
Graph g;
g.vertice = vector<char>(t, t + sizeof(t)/sizeof(t[0]));
for(int i=0;i<nodes;i++)
{
for(int j=0;j<nodes;j++)
matrix[i][j]=-1;
}
cout<<"Enter the connections into the Adjacency Matrix: ";
for(int i=0; i<nodes; i++)
{
for(int j=i; j<nodes; j++)
{
cout<<endl<<t[i]<<","<<t[j]<<": ";
if(i==j)
matrix[i][j]=0;
else if(matrix[i][j]==-1)
{
cin>>matrix[i][j];
g.edges.push_back(Edge(t[i],t[j], matrix[i][j]));
matrix[j][i]=matrix[i][j]; //Setting the equal value to the transpose of the value inputted
}
}
}
for ( auto itr = g.edges.begin(); itr!=g.edges.end(); itr++)
{
cout<<(*itr).vertex1<<"\t";
cout<<(*itr).vertex2<<"\t";
cout<<(*itr).weight<<"\t";
cout<<"\n";
}
MST(g);
return 0;
}