Skip to content

Commit 0895920

Browse files
committed
Day - 76 work
1 parent a405f44 commit 0895920

File tree

3 files changed

+143
-3
lines changed

3 files changed

+143
-3
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
| Current Status| Stats |
88
| :------------: | :----------: |
9-
| Total Problems | 110 |
10-
| Current Streak | 75 days |
11-
| Longest Streak | 75 ( August 17, 2015 - October 30, 2015 ) |
9+
| Total Problems | 111 |
10+
| Current Streak | 76 days |
11+
| Longest Streak | 76 ( August 17, 2015 - October 31, 2015 ) |
1212

1313
</center>
1414

@@ -165,6 +165,7 @@ Include contains single header implementation of data structures and some algori
165165
| Breadth First Traversal of a Graph | [bfsDemo.cpp](graph_problems/bfsDemo.cpp) |
166166
| calculate the shortest distance from the start position (Node S) to all of the other nodes in the graph using Dijkstra algorithm. | [dijkstra-shortest-reach.cpp](graph_problems/dijkstra-shortest-reach.cpp)|
167167
| Calculate total weight of Minimum Spanning Tree of a given graph ( sum of weights of edges which forms MST) using Prim's algorithm | [primsMST.cpp](graph_problems/primsMST.cpp)|
168+
| Print Minimum Spanning Tree( MST ) of a given graph using Kruskal's algorithm.| [kruskalMST.cpp](graph_problems/kruskalMST.cpp)|
168169

169170
###Greedy Problems
170171
| Problem | Solution |

graph_problems/kruskalMST.cpp

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
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+

graph_problems/run

51.6 KB
Binary file not shown.

0 commit comments

Comments
 (0)