Search
 
SCRIPT & CODE EXAMPLE
 

CPP

kruskal algorithm time complexity

Time complexity:- O(ElogV)
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 :: how to code a game in c++ 
Cpp :: setFontSize QT 
Cpp :: Summation of Natural Number Sequence with c and c++. 
Cpp :: sfml thread multi argument function 
Cpp :: print float up to 3 decimal places in c++ 
Cpp :: PascalName seperate strings 
Cpp :: opengl draw cresent moon c++ 
Cpp :: c ++ Prefix Sum of Matrix (Or 2D Array) 
Cpp :: Opengl GLFW basic window 
Cpp :: sort vector in descending order c++ 
Cpp :: convert datatype of field db browser from text to timedate db browser 
Cpp :: vector with initial size 
Cpp :: library management system project in c++ using array 
Cpp :: Print value of data in c++ 
Cpp :: C++ std::ofstream class members 
Cpp :: assegnare valori in c++ 
Cpp :: KUNG FU HUSTLE 
Cpp :: atomic int c++ add 1 
Cpp :: qt_invok 
Cpp :: left margin c++ 
Cpp :: my cpp 
Cpp :: easy way to learn file handling in c++ array 
Cpp :: fasdf 
Cpp :: C++ operation 
Cpp :: multiple objects in vector C++ 
Cpp :: # in c++ 
Cpp :: determining whether a array is a subsequence of another array 
Cpp :: racing horses codechef solution c++ 
Cpp :: run c++ files on chrome book 
Cpp :: rotateArray 
ADD CONTENT
Topic
Content
Source link
Name
5+5 =