|
| 1 | +/** |
| 2 | + * Given an undirected weighted connected graph, it is required to find the Really Special SubTree in it. |
| 3 | + * The Really Special SubTree is defined as a subgraph consisting of all the nodes in the graph and |
| 4 | + * There is only one exclusive path from a node to every other node. |
| 5 | + * The subgraph is of minimum overall weight (sum of all edges) among all such subgraphs. |
| 6 | + * While creating the Really Special SubTree, start of by picking the edge with smallest weight. |
| 7 | + * If there are edges of equal weight available at an instant, then the edge to be chosen first among them is the |
| 8 | + * one with minimum value of sum of the following expression : |
| 9 | + * u + wt + v , where u and v are the node numbers of the corresponding edge and wt is the weight. |
| 10 | + * Even then if there is a collision, choose any one of them. |
| 11 | + * While doing the above, ensure that no cycle is formed while picking up edges. |
| 12 | + */ |
| 13 | + |
| 14 | +#include <cmath> |
| 15 | +#include <cstdio> |
| 16 | +#include <vector> |
| 17 | +#include <iostream> |
| 18 | +#include <algorithm> |
| 19 | +using namespace std; |
| 20 | + |
| 21 | +struct Edge { |
| 22 | + int x; |
| 23 | + int y; |
| 24 | + int r; |
| 25 | + Edge( int a, int b, int c) : x{ a-1 }, y{ b-1 }, r{ c } { } |
| 26 | +}; |
| 27 | + |
| 28 | +bool sortComparer( Edge a, Edge b ) { |
| 29 | + if ( a.r < b.r ) { |
| 30 | + return true; |
| 31 | + } else if ( a.r > b.r ) { |
| 32 | + return false; |
| 33 | + } |
| 34 | + return ( a.x + a.y + a.r <= b.x + b.y + b.r); |
| 35 | +} |
| 36 | + |
| 37 | +class Graph { |
| 38 | + public: |
| 39 | + Graph(int N, int M) : V{ N }, E{ M }, edges(E, Edge(0,0,0)) { } |
| 40 | + void getEdges(); |
| 41 | + void sortEdges(); |
| 42 | + int numberOfVertices() { return V; } |
| 43 | + int numberOfEdges() { return E; } |
| 44 | + std::vector<Edge> kruskalMST(); |
| 45 | + private: |
| 46 | + int V; //number of vertices |
| 47 | + int E; //number of Edges |
| 48 | + std::vector<Edge> edges; |
| 49 | +}; |
| 50 | + |
| 51 | +void Graph::getEdges( ) { |
| 52 | + int x, y, r; |
| 53 | + for ( int i = 0; i < E; ++i ) { |
| 54 | + std::cin >> x >> y >> r; |
| 55 | + edges.push_back(Edge(x, y, r)); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +void Graph::sortEdges() { |
| 60 | + std::sort(edges.begin(), edges.end(), sortComparer); |
| 61 | +} |
| 62 | + |
| 63 | +struct subset { |
| 64 | + int rank; |
| 65 | + int parent; |
| 66 | +}; |
| 67 | + |
| 68 | +int find( std::vector<subset> & subsets, int i ) { |
| 69 | + if ( subsets[i].parent != i ) { |
| 70 | + subsets[i].parent = find(subsets, subsets[i].parent); |
| 71 | + } |
| 72 | + return subsets[i].parent; |
| 73 | +} |
| 74 | + |
| 75 | +void union_edges( std::vector<subset> & subsets, int a, int b) { |
| 76 | + int aroot = find(subsets, a); |
| 77 | + int broot = find(subsets, b); |
| 78 | + if ( subsets[aroot].rank < subsets[broot].rank ) { |
| 79 | + subsets[aroot].parent = broot; |
| 80 | + } else if ( subsets[aroot].rank > subsets[broot].rank ) { |
| 81 | + subsets[broot].parent = aroot; |
| 82 | + } else { |
| 83 | + subsets[broot].parent = aroot; |
| 84 | + subsets[broot].rank++; |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +std::vector<Edge> Graph::kruskalMST() { |
| 89 | + sortEdges(); |
| 90 | + std::vector<subset> subsets(numberOfEdges()); |
| 91 | + for ( int i = 0; i < numberOfEdges(); ++i ) { |
| 92 | + subsets[i].rank = 0; |
| 93 | + subsets[i].parent = i; |
| 94 | + } |
| 95 | + int e = 0; |
| 96 | + int i = 0; |
| 97 | + std::vector<Edge> resultMST; |
| 98 | + while ( e < numberOfVertices() - 1) { |
| 99 | + Edge next_edge = edges[i++]; |
| 100 | + int a = find( subsets, next_edge.x ); |
| 101 | + int b = find( subsets, next_edge.y ); |
| 102 | + if ( a != b ) { |
| 103 | + resultMST.push_back(next_edge); |
| 104 | + union_edges(subsets, a, b); |
| 105 | + ++e; |
| 106 | + } |
| 107 | + } |
| 108 | + return resultMST; |
| 109 | +} |
| 110 | + |
| 111 | +void printMST(std::vector<Edge> & MST) { |
| 112 | + std::cout << std::endl << "Given Graph's MST:\n"; |
| 113 | + std::cout << "V1\tV2\tWeight\n"; |
| 114 | + for ( auto e : MST ) { |
| 115 | + std::cout << e.x + 1 << "\t" << e.y + 1 << "\t" << e.r << std::endl; |
| 116 | + } |
| 117 | + std::cout << std::endl; |
| 118 | +} |
| 119 | + |
| 120 | +void inputFormat() { |
| 121 | + std::cout << "Input Format:\nFirst line has two integers N, denoting the number of nodes in the graph and M," |
| 122 | + "denoting the number of edges in the graph\n" |
| 123 | + "The next M lines each consist of three space separated integers x y r,\n" |
| 124 | + "where x and y denote the two nodes between which the undirected edge exists," |
| 125 | + "r denotes the weight of edge between the corresponding nodes.\n"; |
| 126 | +} |
| 127 | + |
| 128 | +int main() { |
| 129 | + int N, M; |
| 130 | + inputFormat(); |
| 131 | + std::cin >> N >> M; |
| 132 | + Graph G(N, M); |
| 133 | + G.getEdges(); |
| 134 | + std::vector<Edge> MST = G.kruskalMST(); |
| 135 | + printMST(MST); |
| 136 | + return 0; |
| 137 | +} |
| 138 | + |
| 139 | + |
0 commit comments