|
| 1 | +#include<iostream> |
| 2 | +#include <vector> |
| 3 | +#include <algorithm> |
| 4 | +using namespace std; |
| 5 | +// declare a class for graph |
| 6 | + |
| 7 | +struct Edge{ |
| 8 | + int src,dest,weight; |
| 9 | +}; |
| 10 | + |
| 11 | +class Graph{ |
| 12 | + int V,E; |
| 13 | + vector<Edge> edge; // an array of all the edges |
| 14 | + public: |
| 15 | + Graph(int v, int e); |
| 16 | + void addEdge(int src, int dest, int weight); |
| 17 | + void mst(); |
| 18 | +}; |
| 19 | + |
| 20 | +Graph::Graph(int v, int e){ |
| 21 | + V=v; |
| 22 | + E=e; |
| 23 | +} |
| 24 | + |
| 25 | +void Graph::addEdge(int src, int dest, int weight){ |
| 26 | + Edge *e= new Edge; |
| 27 | + e->src= src; |
| 28 | + e->dest= dest; |
| 29 | + e->weight= weight; |
| 30 | + edge.push_back(*e); |
| 31 | +} |
| 32 | +bool comp(Edge e1, Edge e2){ |
| 33 | + return e1.weight<e2.weight ; |
| 34 | +} |
| 35 | + |
| 36 | +struct subset{ |
| 37 | + int parent; |
| 38 | + int rank; |
| 39 | +}; |
| 40 | +// A utility function to find set of an element i |
| 41 | +// (uses path compression technique) |
| 42 | +int find(struct subset subsets[], int i){ |
| 43 | + // find root and make root as parent of i (path compression) |
| 44 | + if (subsets[i].parent != i) |
| 45 | + subsets[i].parent = find(subsets, subsets[i].parent); |
| 46 | + return subsets[i].parent; |
| 47 | +} |
| 48 | + |
| 49 | +// A function that does union of two sets of x and y |
| 50 | +// (uses union by rank) |
| 51 | +void Union(struct subset subsets[], int x, int y) |
| 52 | +{ |
| 53 | + int xroot = find(subsets, x); |
| 54 | + int yroot = find(subsets, y); |
| 55 | + |
| 56 | + // Attach smaller rank tree under root of high rank tree |
| 57 | + // (Union by Rank) |
| 58 | + if (subsets[xroot].rank < subsets[yroot].rank) |
| 59 | + subsets[xroot].parent = yroot; |
| 60 | + else if (subsets[xroot].rank > subsets[yroot].rank) |
| 61 | + subsets[yroot].parent = xroot; |
| 62 | + else |
| 63 | + { |
| 64 | + subsets[yroot].parent = xroot; |
| 65 | + subsets[xroot].rank++; |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +void Graph::mst(){ |
| 70 | + sort(edge.begin(), edge.end(), comp); |
| 71 | + //create disjoint set for all vertices |
| 72 | + struct subset *subsets = new subset[V]; |
| 73 | + |
| 74 | + // Create V subsets with single elements |
| 75 | + for(int i=0;i<V;i++){ |
| 76 | + subsets[i].parent = i; |
| 77 | + subsets[i].rank = 0; |
| 78 | + } |
| 79 | + vector<Edge> result; |
| 80 | + |
| 81 | + // Number of edges to be taken is equal to V-1 |
| 82 | + for(auto i=edge.begin(); i!=edge.end();i++){ |
| 83 | + int src= i->src; |
| 84 | + int dest=i->dest; |
| 85 | + //if not same root |
| 86 | + if(find(subsets, src)!= find(subsets, dest)){ |
| 87 | + result.push_back(*i); |
| 88 | + Union(subsets, src, dest); |
| 89 | + } |
| 90 | + } |
| 91 | + printf("The edges of the MST are\n"); |
| 92 | + for(auto i=result.begin(); i!=result.end(); i++) |
| 93 | + cout<<i->src<<"--"<<i->dest<<"="<<i->weight<<endl; |
| 94 | + return; |
| 95 | +} |
| 96 | + |
| 97 | +int main(){ |
| 98 | + int V; // Number of vertices in graph |
| 99 | + int E; |
| 100 | + printf("Enter no of vertices and edges\n"); |
| 101 | + scanf("%d%d",&V,&E); // Number of edges in graph |
| 102 | + Graph g(V,E); |
| 103 | + int i,u,v,x; |
| 104 | + printf("Enter\nsrc dest weight\n"); |
| 105 | + for(i=0;i<E;i++) |
| 106 | + { |
| 107 | + scanf("%d%d%d",&u,&v,&x); |
| 108 | + g.addEdge(u,v,x); |
| 109 | + } |
| 110 | + // add edge 0-1 |
| 111 | + /*g.addEdge(0,3,1); |
| 112 | + g.addEdge(4,5,2); |
| 113 | + g.addEdge(2,4,5); |
| 114 | + g.addEdge(3,4,6); |
| 115 | + g.addEdge(0,1,3); |
| 116 | + g.addEdge(1,2,1); |
| 117 | + g.addEdge(2,3,1); |
| 118 | + g.addEdge(1,3,3); |
| 119 | + g.addEdge(2,5,4);*/ |
| 120 | + g.mst(); |
| 121 | +} |
0 commit comments