diff --git a/22.KruskalAlgorithm.cpp b/22.KruskalAlgorithm.cpp new file mode 100644 index 0000000..1709847 --- /dev/null +++ b/22.KruskalAlgorithm.cpp @@ -0,0 +1,86 @@ +/* + * This function takes a graph from the user and finds a minimum + * spanning tree using + * Kruskal's Algorithm + * + * Coded by: Abdurrezak Efe + * + * */ + +#include +#include +#include +using namespace std; +typedef pair< int , int> edge; + +class Graph{ + +public: + + vector > edges; + int v; + + Graph(int v, int e) + { + //edges.resize(e); + this->v = v; + } + + void add_edge(int x, int y, int w) + { + edge ee = {x,y}; + pair newedge= {w, ee}; + edges.push_back(newedge); + } + +}; + +//function to what set a vertex belongs to +int find_parent(int x, int par[]) +{ + if(par[x] == x) + return x; + return par[x] = find_parent(par[x], par); +} + +void Kruskal(Graph g) +{ + int par[g.v+1]; + for(int i=0;i> mst; + + for(int i=0;i> v >> e; + + //constructing the graph + Graph g(v,e); + int x,y,w; + for(int i=0;i> x >> y >> w, g.add_edge(x,y,w); + + Kruskal(g); +} \ No newline at end of file