forked from nishitpanchal395/projecthactoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
KruskalAlgo.cpp
105 lines (98 loc) · 2.87 KB
/
KruskalAlgo.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
// Implementation of Kruskal Algorithm in CPP
#include<iostream>
#include<cstdlib>
#include<algorithm>
using namespace std;
class edge
{
public:
char source;
char destination;
int weight;
};
int findParent(int, int*);
bool comparison(edge, edge);
void kruskal(edge*, int, int);
int main()
{
int vertices, edges;
cout << "Enter the number of vertices : ";
cin >> vertices;
cout << "Enter the number of edges : ";
cin >> edges;
cout << "\n";
edge* graph = new edge[edges];
cout << "Enter the source, destination and the corresponding weight of each edge : \n";
for (int counter = 0; counter < edges; counter++)
{
cout << "Edge " << (counter + 1) << " : \n";
char s, d;
int w;
cin >> s >> d >> w;
graph[counter].source = s;
graph[counter].destination = d;
graph[counter].weight = w;
}
kruskal(graph, vertices, edges);
return 0;
}
int findParent(int vertex, int* parent)
{
if (parent[vertex] == vertex);
return vertex;
return findParent(parent[vertex], parent);
}
bool comparison(edge e1, edge e2)
{
return e1.weight < e2.weight;
}
void kruskal(edge* graph, int vertices, int edges)
{
int sum = 0;
sort(graph, graph + edges, comparison);
edge* mst = new edge[vertices - 1];
int* parent = new int[vertices];
for (int counter = 0; counter < vertices; counter++)
{
parent[counter] = counter;
}
int count = 0;
int counter = 0;
while (count != (vertices - 1))
{
edge currentedge = graph[counter];
int sourceParent = findParent(currentedge.source, parent);
int destinationParent = findParent(currentedge.destination, parent);
if (sourceParent != destinationParent)
{
mst[count] = currentedge;
count++;
parent[sourceParent] = destinationParent;
}
counter++;
}
cout << "\n";
for (int counter = 0; counter < vertices - 1; counter++)
{
cout << (counter + 1) << " edge (" << mst[counter].source << ", " << mst[counter].destination << ") = " << mst[counter].weight << endl;
sum = sum + mst[counter].weight;
}
cout << "\n";
int check[6] = { 0, 0, 0, 0, 0, 0 };
for (int counter = 0; counter < vertices; counter++)
{
if (check[(int)mst[counter].source - 65] == 0)
check[(int)mst[counter].source - 65] = 1;
if (check[(int)mst[counter].destination - 65] == 0)
check[(int)mst[counter].destination - 65] = 1;
}
int Flag = 0;
for (int counter = 0; counter < vertices; counter++)
{
Flag += check[counter];
}
if (Flag == vertices)
cout << "Cost of the Minimum Spanning Tree = " << sum << "\n";
else
cout << "The Graph is disconnected.\nMinimum spanning tree cannot be implemented for the given graph using Kruskal's algorithm.\n";
}