Posts

Showing posts with the label Graph

Kruskal Algorithm.

//No one born perfect :) //Kruskal Algorithm //Finding MST (Minimum Spanning Tree) //Greedy Algo //O(ElogE or ElogV) //DS use DisJoint DS (Union Find)(very important DS) //How it is work ? (We are considering that Graph is connected ) // a). First Sort all edge in increasing order according to weight // b). Now Iterate each edge(u,v) in Edges and check whether these // vertax u and v are in same tree or not (which we are finding using union and find method) // if not include this edge otherwise continue (For more understanding see source code) //  c). You will get a MST in end. //For proof go wiki page:- // https://en.wikipedia.org/wiki/Kruskal%27s_algorithm#Proof_of_correctness // Happy Coding. ///////////////////////////////////////////////////////////////////////////////////////////// #include <iostream> #include <cstdio> #include <list> #include <cstring> using namespace std; // vector<vector <long int,long int...