From f6a887a2dd1040fa04c5b634bddfdcc336264019 Mon Sep 17 00:00:00 2001 From: sayak119 Date: Sun, 1 Oct 2017 21:29:36 +0530 Subject: [PATCH] Kruskal's algorithm added and updated readme --- README.md | 3 + greedy/kruskal's_algorithm/README.md | 9 + .../c++/kruskals_algorithm.c++ | 181 ++++++++++++++++++ math/euclids_gcd/c/a.out | Bin 0 -> 8448 bytes .../euclids_gcd/c/euclids_gcd.c | 3 +- math/{ => prime_seive/c++}/prime_seive.cpp | 0 6 files changed, 195 insertions(+), 1 deletion(-) create mode 100644 greedy/kruskal's_algorithm/README.md create mode 100644 greedy/kruskal's_algorithm/c++/kruskals_algorithm.c++ create mode 100755 math/euclids_gcd/c/a.out rename euclids-gcd.c => math/euclids_gcd/c/euclids_gcd.c (95%) rename math/{ => prime_seive/c++}/prime_seive.cpp (100%) diff --git a/README.md b/README.md index e4dcc62de8..58df712604 100644 --- a/README.md +++ b/README.md @@ -17,11 +17,14 @@ Clean examples implementations of data structures and algorithms written in diff * [russian peasant](math/russian_peasant) * [towers_of_hanoi](math/towers_of_hanoi) * [armstrong_number](math/armstrong_number) + * [euclid's gcd](math/euclids_gcd) + * [prime seive](math/prime_seive) * [Cryptography](cryptography) * [caesar cipher](cryptography/caesar_cipher) * [substitution cipher](cryptography/substitution_cipher) * [Greedy](greedy) * [dijkstra’s algorithm](greedy/dijkstra’s_algorithm) + * [kruskal's algorithm](greedy/kruskal's_algorithm) * [Graphs](graphsearch) * [breadth-first-search](graphsearch/breadth-first-search) * [depth-first-search](graphsearch/depth-first-search) diff --git a/greedy/kruskal's_algorithm/README.md b/greedy/kruskal's_algorithm/README.md new file mode 100644 index 0000000000..95aea58a25 --- /dev/null +++ b/greedy/kruskal's_algorithm/README.md @@ -0,0 +1,9 @@ +### Kruskal’s algorithm +What is Minimum Spanning Tree? +Given a connected and undirected graph, a spanning tree of that graph is a subgraph that is a tree and connects all the vertices together. A single graph can have many different spanning trees. A minimum spanning tree (MST) or minimum weight spanning tree for a weighted, connected and undirected graph is a spanning tree with weight less than or equal to the weight of every other spanning tree. The weight of a spanning tree is the sum of weights given to each edge of the spanning tree. + +Kruskal's algorithm is a minimum-spanning-tree algorithm which finds an edge of the least possible weight that connects any two trees in the forest.[1] It is a greedy algorithm in graph theory as it finds a minimum spanning tree for a connected weighted graph adding increasing cost arcs at each step.[1] This means it finds a subset of the edges that forms a tree that includes every vertex, where the total weight of all the edges in the tree is minimized. If the graph is not connected, then it finds a minimum spanning forest (a minimum spanning tree for each connected component). + + +#### A graphical example of Kruskal’s algorithm +![](https://upload.wikimedia.org/wikipedia/commons/5/5c/MST_kruskal_en.gif) diff --git a/greedy/kruskal's_algorithm/c++/kruskals_algorithm.c++ b/greedy/kruskal's_algorithm/c++/kruskals_algorithm.c++ new file mode 100644 index 0000000000..8698f613f6 --- /dev/null +++ b/greedy/kruskal's_algorithm/c++/kruskals_algorithm.c++ @@ -0,0 +1,181 @@ +#include +#include +#include + +// a structure to represent a weighted edge in graph +struct Edge +{ + int src, dest, weight; +}; + +// a structure to represent a connected, undirected and weighted graph +struct Graph +{ + // V-> Number of vertices, E-> Number of edges + int V, E; + + // graph is represented as an array of edges. Since the graph is + // undirected, the edge from src to dest is also edge from dest + // to src. Both are counted as 1 edge here. + struct Edge* edge; +}; + +// Creates a graph with V vertices and E edges +struct Graph* createGraph(int V, int E) +{ + struct Graph* graph = (struct Graph*) malloc( sizeof(struct Graph) ); + graph->V = V; + graph->E = E; + + graph->edge = (struct Edge*) malloc( graph->E * sizeof( struct Edge ) ); + + return graph; +} + +// A structure to represent a subset for union-find +struct subset +{ + int parent; + int rank; +}; + +// A utility function to find set of an element i +// (uses path compression technique) +int find(struct subset subsets[], int i) +{ + // find root and make root as parent of i (path compression) + if (subsets[i].parent != i) + subsets[i].parent = find(subsets, subsets[i].parent); + + return subsets[i].parent; +} + +// A function that does union of two sets of x and y +// (uses union by rank) +void Union(struct subset subsets[], int x, int y) +{ + int xroot = find(subsets, x); + int yroot = find(subsets, y); + + // Attach smaller rank tree under root of high rank tree + // (Union by Rank) + if (subsets[xroot].rank < subsets[yroot].rank) + subsets[xroot].parent = yroot; + else if (subsets[xroot].rank > subsets[yroot].rank) + subsets[yroot].parent = xroot; + + // If ranks are same, then make one as root and increment + // its rank by one + else + { + subsets[yroot].parent = xroot; + subsets[xroot].rank++; + } +} + +// Compare two edges according to their weights. +// Used in qsort() for sorting an array of edges +int myComp(const void* a, const void* b) +{ + struct Edge* a1 = (struct Edge*)a; + struct Edge* b1 = (struct Edge*)b; + return a1->weight > b1->weight; +} + +// The main function to construct MST using Kruskal's algorithm +void KruskalMST(struct Graph* graph) +{ + int V = graph->V; + struct Edge result[V]; // Tnis will store the resultant MST + int e = 0; // An index variable, used for result[] + int i = 0; // An index variable, used for sorted edges + + // Step 1: Sort all the edges in non-decreasing order of their weight + // If we are not allowed to change the given graph, we can create a copy of + // array of edges + qsort(graph->edge, graph->E, sizeof(graph->edge[0]), myComp); + + // Allocate memory for creating V ssubsets + struct subset *subsets = + (struct subset*) malloc( V * sizeof(struct subset) ); + + // Create V subsets with single elements + for (int v = 0; v < V; ++v) + { + subsets[v].parent = v; + subsets[v].rank = 0; + } + + // Number of edges to be taken is equal to V-1 + while (e < V - 1) + { + // Step 2: Pick the smallest edge. And increment the index + // for next iteration + struct Edge next_edge = graph->edge[i++]; + + int x = find(subsets, next_edge.src); + int y = find(subsets, next_edge.dest); + + // If including this edge does't cause cycle, include it + // in result and increment the index of result for next edge + if (x != y) + { + result[e++] = next_edge; + Union(subsets, x, y); + } + // Else discard the next_edge + } + + // print the contents of result[] to display the built MST + printf("Following are the edges in the constructed MST\n"); + for (i = 0; i < e; ++i) + printf("%d -- %d == %d\n", result[i].src, result[i].dest, + result[i].weight); + return; +} + +// Test program +int main() +{ + /* Let us create following weighted graph + 10 + 0--------1 + | \ | + 6| 5\ |15 + | \ | + 2--------3 + 4 */ + int V = 4; // Number of vertices in graph + int E = 5; // Number of edges in graph + struct Graph* graph = createGraph(V, E); + + + // add edge 0-1 + graph->edge[0].src = 0; + graph->edge[0].dest = 1; + graph->edge[0].weight = 10; + + // add edge 0-2 + graph->edge[1].src = 0; + graph->edge[1].dest = 2; + graph->edge[1].weight = 6; + + // add edge 0-3 + graph->edge[2].src = 0; + graph->edge[2].dest = 3; + graph->edge[2].weight = 5; + + // add edge 1-3 + graph->edge[3].src = 1; + graph->edge[3].dest = 3; + graph->edge[3].weight = 15; + + // add edge 2-3 + graph->edge[4].src = 2; + graph->edge[4].dest = 3; + graph->edge[4].weight = 4; + + KruskalMST(graph); + + return 0; +} diff --git a/math/euclids_gcd/c/a.out b/math/euclids_gcd/c/a.out new file mode 100755 index 0000000000000000000000000000000000000000..c6df909968616477bd725c49147618baea74057a GIT binary patch literal 8448 zcmeHMYiwLc6`u9Rk378II7x95Ah$`39H=*&G!M6xiP!cz*OBvR>J(6$+^oH8dto2e z?%mW5L_|2Pv~u0%M}<-eBqUH3kdV@XM39h3a7oAyP%9_|QKS^5!cB=}NC>WQy?p21 zbJlmS*HHc-A!V$+XU=yXb7t9-qGn|`pimTcvR z>PIenpsAJ7UJk6j4F2FE_*1~OGWuU!1Sj7c%jheNQ$G#ME;XEqr_2Fm82uv=V<4W2 zB;qGyU`fNsnvslYBqQ;Z+S}9J-eGJHY!B>IMtARiBO1%Z2IE;Xmg(K!kw~Xvy^+2I zSq>)CDYi6h)v}5dRt5fSY7vYr{V64RkXskW%&AZh+v4Ky^vA@*l`yfXcSp-T60< z>G?nDV>hP{_4Z6u?n86>MD4H9TYaK+HArF23E14N+ki<6=*M7==o6K%g6faV==pce zwFqe+u?VU5XyL{{pl-t%%m2|yiejFJ{U|9;?95nhw#>m^zdYyF^V9m}sXe;qivH%D z*#L(t+2IOuxR}4wty`d`a$EJW);}*r?FhxQLLYBk27Ia(J)Y9>xKil>@1bx`Q($){ z;S&8J^`q|%K!a)+U4Q=6Y(6(Lc4}7TD#yb!@2|q+<$s)-9S_guujGF>^&9A3n-uM< z;n`(ycQrgy2Qq$ShS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-ZJgWh>q8GCyUwugF*gI!%ugnNzNPuWFZ##2Tv8$%c8qoc6` z-5$ZLOB%}zt3WDk#sZ=C z?q)MG$lPEm7wF5yaqAq9ssM3Ak?fEPL`PHbXHzp{OHRZx*?2lt!WfWcVu=VDaBVnY zs({ti0IGq(G*~k>f+wXFl+xBK2*idsk%pphBAjh%XOwLrb+iyk#{1EH+9b~ij$UN| z!>IyD>m*XO%>4OpA=-yy$`$W8Wq&19_Tl1}be&}r7+Qj49hQBVP|c}~x~yLUZ$rgt zFZ(s2n?;eS+rAU{J?w?0{c~IqD)Gx_*}Y9U0DC$ci2X&b2tCj3(sDs7A&p?)Ys~sgtPC zn2NpZPlaA$MQPvNf9fCEx3iw?V};5%kv*N6-1aX3qcP#nNpanm^`C5fVmI`~O|G zmu6(2AoKxgh^gEDRhPZ{|Aw9a!TzT%dpQRPZE_{NjGMTy_`Sf|Ft(be#uX#vB#-bp2QMTh7iZoh#Cp8Pz7pTr~U3e69R9o1qP;>53WIhS@taV09{ O?4RKVs$B*yR{aa1SCfnY literal 0 HcmV?d00001 diff --git a/euclids-gcd.c b/math/euclids_gcd/c/euclids_gcd.c similarity index 95% rename from euclids-gcd.c rename to math/euclids_gcd/c/euclids_gcd.c index 9e6d9d8b4f..aaaca609d8 100644 --- a/euclids-gcd.c +++ b/math/euclids_gcd/c/euclids_gcd.c @@ -1,10 +1,11 @@ +#include int gcd(int a, int b) { if (a == 0) return b; return gcd(b%a, a); } - + // Driver program to test above function int main() { diff --git a/math/prime_seive.cpp b/math/prime_seive/c++/prime_seive.cpp similarity index 100% rename from math/prime_seive.cpp rename to math/prime_seive/c++/prime_seive.cpp