Beruflich Dokumente
Kultur Dokumente
#include <vector>
#include <unordered_map>
#include <algorithm>
struct Edge {
class DisjointSet
public:
void makeSet(int N)
{
int Find(int k)
{
if (parent[k] == k)
return k;
return Find(parent[k]);
int x = Find(a);
int y = Find(b);
parent[x] = y;
}
};
vector<Edge> KruskalAlgo(vector<Edge> edges, int N)
{
vector<Edge> MST;
DisjointSet ds;
ds.makeSet(N);
while (MST.size() != N - 1)
{
edges.pop_back();
int x = ds.Find(next_edge.src);
int y = ds.Find(next_edge.dest);
if (x != y)
{
MST.push_back(next_edge);
ds.Union(x, y);
}
}
return MST;
}
bool compare
(Edge const &a, Edge const &b)
{
int main()
{
vector<Edge> edges =
{
{ 0, 1, 7 }, { 1, 2, 8 }, { 0, 3, 5 }, { 1, 3, 9 }, { 1, 4, 7 }, { 2, 4, 5 },
{ 3, 4, 15 }, { 3, 5, 6 }, { 4, 5, 8 }, { 4, 6, 9 }, { 5, 6, 11 }
};
int N = 7;
vector<Edge>::iterator i=e.begin();
while(i!=e.end()){
cout<<i->src<<"----"<<i->dest<<"-----"<<i->weight<<"\n";
i++;
}
return 0;
}