-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKrushkal's.cpp
More file actions
130 lines (114 loc) · 3.18 KB
/
Copy pathKrushkal's.cpp
File metadata and controls
130 lines (114 loc) · 3.18 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
#include <iostream>
#include <vector>
using namespace std;
// Structure to store an edge
struct Edge {
int src;
int dest;
int weight;
};
// Manually sort edges using Bubble Sort
void bubbleSort(vector<Edge> &edges) {
int n = edges.size();
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (edges[j].weight > edges[j + 1].weight) {
// Swap edges[j] and edges[j+1]
Edge temp = edges[j];
edges[j] = edges[j + 1];
edges[j + 1] = temp;
}
}
}
}
//we iterate through all edges and apply the find-union algorithm
// Function to find the parent of a node
int findParent(int parent[], int node) {
while (parent[node] != node)
node = parent[node];
return node;
}
// Function to perform union of two sets
void unionSets(int parent[], int u, int v) {
int parentU = findParent(parent, u);
int parentV = findParent(parent, v);
parent[parentU] = parentV;
}
// Kruskal's algorithm using manual sorting
void kruskal(int V, vector<Edge> &edges) {
bubbleSort(edges); // Step 1: Manually sort the edges
int parent[100];
for (int i = 0; i < V; i++)
parent[i] = i;
vector<Edge> mst;
for (int i = 0; i < edges.size(); i++) {
int u = edges[i].src;
int v = edges[i].dest;
int parentU = findParent(parent, u);
int parentV = findParent(parent, v);
if (parentU != parentV) {
mst.push_back(edges[i]);
unionSets(parent, u, v);
}
}
// Output the MST
cout << "Edges in the Minimum Spanning Tree:\n";
for (int i = 0; i < mst.size(); i++) {
cout << mst[i].src << " - " << mst[i].dest << " : " << mst[i].weight << endl;
}
}
void addEdges(vector<vector<int>> &arr)
{
int connection, edges;
int weight = 0;
int Size = arr.size();
for (int i = 0; i < Size; i++)
{
cout << "Enter no of edges connected to " << i << " :";
cin >> edges;
for (size_t j = 0; j < edges; j++)
{
cout << "Enter Edge: ";
cin >> connection;
cout<<"Enter Edge weight: ";
cin>> weight;
arr[i][connection] = weight;
}
}
}
void createEdgeArray(vector<vector<int>> &arr,vector<Edge>&edges){
int size = arr.size();
int weight = 0;
for (int i = 0; i < size; i++)
{
for (size_t j = 0; j < size; j++)
{
weight = arr[i][j];
if (arr[i][j] != 0)
{
Edge edge;
edge.src = i;
edge.dest = j;
edge.weight = weight;
edges.push_back(edge);
}
}
}
}
int main() {
int V;
cout << "Enter number of Vertices: ";
cin >> V;
// Initialize 2d-matrix vector of vectors with 0
vector<vector<int>> arr(V, vector<int>(V, 0));
vector<Edge> edges;
addEdges(arr);
createEdgeArray(arr,edges);
for (int i = 0; i < V; i++)
{
Edge edge = edges[i];
cout<<"{"<< edge.src<<", "<<edge.dest<<", "<<edge.weight<<"}"<<endl;
}
kruskal(V, edges);
return 0;
}