From 5a1b744f3e778c8b67d2b9192f8b56308e011a7a Mon Sep 17 00:00:00 2001 From: Abdurrezzak Efe Date: Tue, 14 Nov 2017 19:58:49 +0200 Subject: [PATCH] Add files via upload --- 22.KruskalAlgorithm.cpp | 86 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 22.KruskalAlgorithm.cpp 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