0% found this document useful (0 votes)
42 views7 pages

Lab Manual 04 - P&DC

This lab manual describes implementing Boruvka's algorithm for finding the minimum spanning tree of a graph. Boruvka's algorithm is a greedy algorithm that works by iteratively connecting components within a graph with the cheapest connecting edge. The document provides theory on minimum spanning trees and Boruvka's algorithm, outlines the algorithm's design and steps, provides a C code implementation, and gives an example run on a sample graph. The conclusion states the student learned about Boruvka's algorithm and how to implement it in C/C++.

Uploaded by

Nitasha Huma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views7 pages

Lab Manual 04 - P&DC

This lab manual describes implementing Boruvka's algorithm for finding the minimum spanning tree of a graph. Boruvka's algorithm is a greedy algorithm that works by iteratively connecting components within a graph with the cheapest connecting edge. The document provides theory on minimum spanning trees and Boruvka's algorithm, outlines the algorithm's design and steps, provides a C code implementation, and gives an example run on a sample graph. The conclusion states the student learned about Boruvka's algorithm and how to implement it in C/C++.

Uploaded by

Nitasha Huma
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Lab # 4 – Parallel & Distributed Computing (CPE-421)

Lab Manual # 4

Objective:
Implementation of Boruvka’s algorithm

Theory:
Boruvka’s algorithm is a greedy algorithm for finding a minimum spanning tree in a
graph or a minimum spanning forest in the case of a graph that is not connected. Boruvka’s
Algorithm is mainly used to find or derive a Minimum Spanning Tree of an Edge-weighted
Graph.
Let us have a quick look at the concept of a Minimum Spanning Tree. A Minimum
Spanning Tree or an MST is a subset of edges of a Weighted, Un-directed graph; such that it
connects all the vertices together. The resultant subset of the graph must have no cycles or
loops within it. Moreover, it should have minimum possible total weight of the edges
connecting the tree.
Interesting Facts about Boruvka’s algorithm:
 Time Complexity of Boruvka’s algorithm is O (E Log V) which is same as Kruskal’s
and Prim’s algorithms.
 Boruvka’s algorithm is used as a step in a faster randomized algorithm that works in
linear time O (E).
 Boruvka’s algorithm is the oldest minimum spanning tree algorithm was discovered
by Boruvka’s in 1926, long before computers even existed. The algorithm was
published as a method of constructing an efficient electricity network.
Algorithm Design:
Like Prim and Kruskal, this algorithm is also a greedy algorithm. Below is complete
algorithm:
 Input is a connected, weighted and un-directed graph.
 Initialize all vertices as individual components (or sets).
 Initialize MST as empty.
 While there is more than one component, do following for each component:
 Find the closest weight edge that connects this component to
any other component.
 Add this closest edge to MST if not already added.
 Return MST
Graph Example:
To understand Boruvka’s algorithm let us consider the following example:

Raheel Amjad 2018-CPE-07


Lab # 4 – Parallel & Distributed Computing (CPE-421)
Initially MST is empty. Every vertex is singe component as highlighted in blue color
in below diagram.

For every component, find the cheapest edge that connects it to some other
component. The cheapest edges are highlighted with green color. Now MST becomes {0-1,
2-8, 2-3, 3-4, 5-6, 6-7}..

After above step, components are {{0,1}, {2,3,4,8}, {5,6,7}}. The components are
encircled with blue color.

We again repeat the step, i.e., for every component, find the cheapest edge that
connects it to some other component. The cheapest edges are highlighted with green color.
Now MST becomes {0-1, 2-8, 2-3, 3-4, 5-6, 6-7, 1-2, 2-5}

At this stage, there is only one component {0, 1, 2, 3, 4, 5, 6, 7, 8} which has all
edges. Since there is only one component left, we stop and return MST.

Raheel Amjad 2018-CPE-07


Lab # 4 – Parallel & Distributed Computing (CPE-421)
C code:

#include <stdio.h>
#include <conio.h>
// 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];

Raheel Amjad 2018-CPE-07


Lab # 4 – Parallel & Distributed Computing (CPE-421)
// 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<E; i++)
{
// Find components (or sets) of two corners of current
edge
int set1 = find(subsets, edge[i].src);
int set2 = find(subsets, edge[i].dest);
// If two corners of current edge belong to same set,
ignore current edge
if (set1 == set2)
continue;
// Else check if current edge is closer to previous
cheapest edges of set1 and set2
else
{
if (cheapest[set1] == -1 ||
edge[cheapest[set1]].weight > edge[i].weight)
cheapest[set1] = i;

if (cheapest[set2] == -1 ||
edge[cheapest[set2]].weight > edge[i].weight)
cheapest[set2] = i;
}

Raheel Amjad 2018-CPE-07


Lab # 4 – Parallel & Distributed Computing (CPE-421)
}
// Consider the above picked cheapest edges and add them to
MST
for (int i=0; i<V; i++)
{
// Check if cheapest for current set exists
if (cheapest[i] != -1)
{
int set1 = find(subsets, edge[cheapest[i]].src);
int set2 = find(subsets, edge[cheapest[i]].dest);

if (set1 == set2)
continue;
MSTweight += edge[cheapest[i]].weight;
printf("Edge %d-%d included in MST\
n",edge[cheapest[i]].src, edge[cheapest[i]].dest);
// Do a union of set1 and set2 and decrease number of trees
Union(subsets, set1, set2);
numTrees--;
}
}
}
printf("\nWeight of MST is: %d\n", MSTweight);
return;
}
// Creates a graph with V vertices and E edges
struct Graph* createGraph(int V, int E)
{
Graph* graph = new Graph;
graph->V = 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;

Raheel Amjad 2018-CPE-07


Lab # 4 – Parallel & Distributed Computing (CPE-421)
}
// 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;

Raheel Amjad 2018-CPE-07


Lab # 4 – Parallel & Distributed Computing (CPE-421)
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;
}
Output:

Conclusion:

In this lab we learnt about the boruvka’s algorithm. We also learnt the design
algorithm with graph example of boruvka’s algorithm. We perform the boruvka’s algorithm
by using C/C++ language. Now I’m able to perform the implementation of boruvka’s
algorithm by using C/C++ language.

Raheel Amjad 2018-CPE-07

You might also like