From 089069837cb19a9653099eeb485a66b0d2f1fafa Mon Sep 17 00:00:00 2001 From: apoorvsingh500 <52244299+apoorvsingh500@users.noreply.github.com> Date: Sat, 12 Oct 2019 11:32:04 +0530 Subject: [PATCH 1/2] Update Contributors.md --- Contributors.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Contributors.md b/Contributors.md index 9c826ec..0b1ccfc 100644 --- a/Contributors.md +++ b/Contributors.md @@ -14,4 +14,4 @@ 12. [Hacker-set](https://github.com/Hacker-set)] 13. [Prateek Sengar]https://github.com/prtksengar3) 14. [ANant Rungta](https://github.com/Anant016) - +15. [Apoorv Singh](https://github.com/apoorvsingh500) From ec1d24b50a104f4495a139d2440f3a2b47b31108 Mon Sep 17 00:00:00 2001 From: apoorvsingh500 <52244299+apoorvsingh500@users.noreply.github.com> Date: Sat, 12 Oct 2019 11:34:52 +0530 Subject: [PATCH 2/2] Added Boruvka's Algo --- boruvka's aglo.cpp | 222 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 222 insertions(+) create mode 100644 boruvka's aglo.cpp diff --git a/boruvka's aglo.cpp b/boruvka's aglo.cpp new file mode 100644 index 0000000..bcedf4c --- /dev/null +++ b/boruvka's aglo.cpp @@ -0,0 +1,222 @@ +// Boruvka's algorithm to find Minimum Spanning +// Tree of a given connected, undirected and +// weighted graph +#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 as a collection of edges. +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. + Edge* edge; +}; + +// A structure to represent a subset for union-find +struct subset +{ + int parent; + int rank; +}; + +// Function prototypes for union-find (These functions are defined +// after boruvkaMST() ) +int find(struct subset subsets[], int i); +void Union(struct subset subsets[], int x, int y); + +// The main function for MST using Boruvka's algorithm +void boruvkaMST(struct Graph* graph) +{ + // Get data of given graph + int V = graph->V, E = graph->E; + Edge *edge = graph->edge; + + // Allocate memory for creating V subsets. + struct subset *subsets = new subset[V]; + + // An array to store index of the cheapest edge of + // subset. The stored index for indexing array 'edge[]' + int *cheapest = new int[V]; + + // Create V subsets with single elements + for (int v = 0; v < V; ++v) + { + subsets[v].parent = v; + subsets[v].rank = 0; + cheapest[v] = -1; + } + + // Initially there are V different trees. + // Finally there will be one tree that will be MST + int numTrees = V; + int MSTweight = 0; + + // Keep combining components (or sets) until all + // compnentes are not combined into single MST. + while (numTrees > 1) + { + // Everytime initialize cheapest array + for (int v = 0; v < V; ++v) + { + cheapest[v] = -1; + } + + // Traverse through all edges and update + // cheapest of every component + for (int i=0; i edge[i].weight) + cheapest[set1] = i; + + if (cheapest[set2] == -1 || + edge[cheapest[set2]].weight > edge[i].weight) + cheapest[set2] = i; + } + } + + // Consider the above picked cheapest edges and add them + // to MST + for (int i=0; iV = V; + graph->E = E; + graph->edge = new Edge[E]; + return graph; +} + +// 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++; + } +} + +// Driver program to test above functions +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; + + boruvkaMST(graph); + + return 0; +} +