Lab Manual 04 - P&DC
Lab Manual 04 - P&DC
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:
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.
#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;
if (cheapest[set2] == -1 ||
edge[cheapest[set2]].weight > edge[i].weight)
cheapest[set2] = i;
}
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;
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.