-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kruskals.cpp
46 lines (43 loc) · 1.16 KB
/
Kruskals.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
using namespace std;
#include <iostream>
#include <vector>
#include "../DisjointSet/DisjointSet.cpp"
#include "../Graph/AdjacencyList/LGraph.cpp"
// Sorted array Implementation
// void insertElement(vector<int> &arr, int val) {
// int low = 0;
// int high = arr.size()-1;
// int pos = (high+low)/2;
// cout<<"low: "<<low<<endl;
// cout<<"high: "<<high<<endl;
// while (pos >= low && pos <= high) {
// if (val > arr[pos]) {
// low = pos+1;
// } else {
// high = pos-1;
// }
// cout<<"low: "<<low<<endl;
// cout<<"high: "<<high<<endl;
// pos = (high+low)/2;
// }
// arr.insert(arr.begin()+pos+1, val);
// }
void kruskalsAlgo(vector<pair<int,int>> edgeList, int numNodes) {
LGraph g = LGraph(numNodes);
DisjointSet ds = DisjointSet(numNodes);
int numEdge = 0;
while (edgeList.size() > 0) {
if (numEdge == numNodes-1) {
break;
}
pair<int,int> edge = edgeList[0];
edgeList.erase(edgeList.begin());
if (ds.find(edge.first) != ds.find(edge.second)) {
ds.merge(edge.first, edge.second);
g.insertEdge(edge.first, edge.second);
numEdge++;
}
}
g.printAdjacencyList();
ds.print();
}