Search
 
SCRIPT & CODE EXAMPLE
 

CPP

c++ kruskal algorithm

#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

#define ll long long
#define llu unsigned llu
#define F first
#define S second

typedef pair<int,int> ii;
typedef pair<int,ii> iii;
typedef vector<int> vi;
vector <iii> g;
vi par;

int fnd(int x){
    if (x == par[x])
        return x;
    par[x] = fnd(par[x]);
    return par[x];
}

void onion(int a, int b){
    par[fnd(a)] = par[fnd(b)];
}

int main() {
    int n, m=0;
    cin>>n;
    
    int i, ans = 0;
    for (i = 0; i<n; i++)
        par.push_back(i);
    
    int a,b,w;
    while( cin>>a>>b>>w ){
        m++;
        g.push_back(iii(w, ii(a,b)));
    }
    sort(g.begin(), g.end());
            
    for(i = 0; i<m; i++){
        if (fnd(g[i].S.F) != fnd(g[i].S.S)){
            ans += g[i].F;
            onion(g[i].S.F, g[i].S.S);
        }
    }
                    
    cout<<ans<<endl;
                    
    return 0;
}

/*
Sample Input:
6
0 1 1
0 3 5
1 3 7
1 2 6
2 5 8
2 4 3
3 4 6
4 5 9

Sample Output:
23
*/
Comment

kruskal algorithm

Take all weighted edges and sort them from smallest to highest
Pick the smallest edge and check if it forms a cycle with the existing tree. If so discard. If not add to tree.
Keep picking until you have v-1 edges. 
All answers should have v-1 edges
Comment

Kruskal algorithm in c++

Kruskal MST 
Comment

kruskal algorithm

// C++ program for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// DSU data structure
// path compression + rank by union
 
class DSU {
    int* parent;
    int* rank;
 
public:
    DSU(int n)
    {
        parent = new int[n];
        rank = new int[n];
 
        for (int i = 0; i < n; i++) {
            parent[i] = -1;
            rank[i] = 1;
        }
    }
 
    // Find function
    int find(int i)
    {
        if (parent[i] == -1)
            return i;
 
        return parent[i] = find(parent[i]);
    }
 
    // Union function
    void unite(int x, int y)
    {
        int s1 = find(x);
        int s2 = find(y);
 
        if (s1 != s2) {
            if (rank[s1] < rank[s2]) {
                parent[s1] = s2;
                rank[s2] += rank[s1];
            }
            else {
                parent[s2] = s1;
                rank[s1] += rank[s2];
            }
        }
    }
};
 
class Graph {
    vector<vector<int> > edgelist;
    int V;
 
public:
    Graph(int V) { this->V = V; }
 
    void addEdge(int x, int y, int w)
    {
        edgelist.push_back({ w, x, y });
    }
 
    void kruskals_mst()
    {
        // 1. Sort all edges
        sort(edgelist.begin(), edgelist.end());
 
        // Initialize the DSU
        DSU s(V);
        int ans = 0;
        cout << "Following are the edges in the "
                "constructed MST"
             << endl;
        for (auto edge : edgelist) {
            int w = edge[0];
            int x = edge[1];
            int y = edge[2];
 
            // Take this edge in MST if it does
            // not forms a cycle
            if (s.find(x) != s.find(y)) {
                s.unite(x, y);
                ans += w;
                cout << x << " -- " << y << " == " << w
                     << endl;
            }
        }
 
        cout << "Minimum Cost Spanning Tree: " << ans;
    }
};
 
// Driver's code
int main()
{
    /* Let us create following weighted graph
                   10
              0--------1
              |       |
             6|   5   |15
              |       |
              2--------3
                  4       */
    Graph g(4);
    g.addEdge(0, 1, 10);
    g.addEdge(1, 3, 15);
    g.addEdge(2, 3, 4);
    g.addEdge(2, 0, 6);
    g.addEdge(0, 3, 5);
 
    // Function call
    g.kruskals_mst();
    return 0;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: C++ bool 
Cpp :: std::string(size_t , char ) constructor: 
Cpp :: return value optimization example 
Cpp :: c++ to c converter 
Cpp :: three-way comparison c++ 
Cpp :: delete node in a linked list leetcode 
Cpp :: c++ sleep function 
Cpp :: round function in c++ 
Cpp :: intage1 was not declared in this scope C++ 
Cpp :: Max / Min Stack in c++ using stl in O(1) time and space 
Cpp :: c++ string to vector int 
Cpp :: combination sum iv leetcode 
Cpp :: how to append two vectors in c++ 
Cpp :: iteration in c++ 
Cpp :: are arrays faster than vectors c++ 
Cpp :: aliasing c++ 
Cpp :: c++ environment setup 
Cpp :: is the c++ 20 char te same as the old one 
C :: powershell search files for string 
C :: conio.h linux 
C :: arma 3 get group size 
C :: string to int c 
C :: how to open jupyter notebook in local disk D 
C :: successeur ("123") 
C :: c Program for Sum of the digits of a given number 
C :: nested switch case in c 
C :: c random array 
C :: mongodb update 
C :: c int 
C :: double array in c 
ADD CONTENT
Topic
Content
Source link
Name
8+3 =