-
Notifications
You must be signed in to change notification settings - Fork 181
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
nonme
wants to merge
1
commit into
Asiatik:master
Choose a base branch
from
nonme:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Kruskal algorithm #237
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. return the value that you want to be printed. |
||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.