Skip to content

Kruskal algorithm #237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 101 additions & 0 deletions Graphs/cpp/kruskal_mst.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
#include <iostream>
#include <vector>
#include <algorithm>

/*
Kruskal's algorithm is a minimum-spanning-tree algorithm which finds
an edge of the least possible weight that connects any two trees
in the forest.[1] It is a greedy algorithm in graph theory as it finds a
minimum spanning tree for a connected weighted graph adding increasing cost
arcs at each step.[1] This means it finds a subset of the edges that forms
a tree that includes every vertex, where the total weight of all the edges
in the tree is minimized. If the graph is not connected, then it finds a minimum
spanning forest (a minimum spanning tree for each connected component).
(c) Wikipedia.org

This implementation is using DSU. O = M*log(N);
M - edges, N - nodes.
*/
//FUNCTIONS
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for this comment
We know that you are listing functions.
So you can remove //Functions.

int dsu_get(int v);
void dsu_unite(int a, int b);
std::vector<std::pair<int, std::pair<int, int> > > convertMatrixToEdges(int n, std::vector<std::vector<int> > m);
void find_mst(int weight, std::vector<std::pair<int, std::pair<int, int> > > graph);
void print_result(int weight, std::vector<std::pair<int, int> > *mst);
bool compare(std::pair<int, std::pair<int, int> > a,
std::pair<int, std::pair<int, int> > b);
//Global array for DSU
std::vector<int> dsu;

int main()
{
int n = 4;
//We assign dsu array.
dsu.assign(n, 0);
for (int i = 0; i < n; ++i)
dsu[i] = i;

std::vector<std::vector<int> > graph;
/* Define graph like below
A B C D
A 0 1 3 0
B 0 0 2 0
C 0 0 0 4
D 0 0 0 0
*/
graph.push_back({ 0,1,3,0 });
graph.push_back({ 0,0,2,0 });
graph.push_back({ 0,0,0,4 });
graph.push_back({ 0,0,0,0 });
std::vector<std::pair<int, std::pair<int, int> > > new_graph = convertMatrixToEdges(n, graph);
find_mst(n, new_graph);
return 0;
}
int dsu_get(int v) {
return (v == dsu[v]) ? v : (dsu[v] = dsu_get(dsu[v]));
}
void dsu_unite(int a, int b) {
a = dsu_get(a);
b = dsu_get(b);
if (a != b)
dsu[a] = b;
}
std::vector<std::pair<int, std::pair<int, int> > > convertMatrixToEdges(int n, std::vector<std::vector<int> > m) {
std::vector<std::pair<int, std::pair<int, int> > > graph;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (m[i][j] != 0) {
graph.push_back({ m[i][j],{ i,j } });
}
}
}
std::sort(graph.begin(), graph.end(), compare);
return graph;
}

void find_mst(int n, std::vector<std::pair<int, std::pair<int, int> > > graph) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return the value that you want to be printed.
Don't make this function find as well as print the values.

int weight = 0, m = graph.size();
std::vector<std::pair<int, int> > mst;

for (int i = 0; i < m; ++i) {
int a = graph[i].second.first, b = graph[i].second.second, w = graph[i].first;
if (dsu_get(a) != dsu_get(b)) {
weight += w;
mst.push_back(graph[i].second);
dsu_unite(a, b);
}
}
print_result(weight, &mst);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Take this function call and place it in main()

}

void print_result(int weight, std::vector<std::pair<int, int> >* mst) {
std::cout << "An MST has weight " << weight << "\n";
std::cout << "It consists of " << mst->size() << " edges: \n";
for (int i = 0; i < mst->size(); ++i) {
std::cout << mst->at(i).first << " " << mst->at(i).second << "\n";
}
}
bool compare(std::pair<int, std::pair<int, int> > a,
std::pair<int, std::pair<int, int> > b) {
return (a.first < b.first);
}