Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
Loading...
Searching...
No Matches
boruvkas_minimum_spanning_tree.cpp File Reference

[Borůvkas Algorithm](https://en.wikipedia.org/wiki/Borůvka's_algorithm) to find the Minimum Spanning Tree More...

#include <iostream>
#include <vector>
#include <cassert>
#include <climits>
Include dependency graph for boruvkas_minimum_spanning_tree.cpp:

Namespaces

namespace  greedy_algorithms
 Greedy Algorithms.
 
namespace  boruvkas_minimum_spanning_tree
 Functions for the [Borůvkas Algorithm](https://en.wikipedia.org/wiki/Borůvka's_algorithm) implementation.
 

Functions

int greedy_algorithms::boruvkas_minimum_spanning_tree::findParent (std::vector< std::pair< int, int > > parent, const int v)
 Recursively returns the vertex's parent at the root of the tree.
 
std::vector< std::vector< int > > greedy_algorithms::boruvkas_minimum_spanning_tree::boruvkas (std::vector< std::vector< int > > adj)
 the implementation of boruvka's algorithm
 
int greedy_algorithms::boruvkas_minimum_spanning_tree::test_findGraphSum (std::vector< std::vector< int > > adj)
 counts the sum of edges in the given tree
 
static void tests ()
 Self-test implementations.
 
int main ()
 Main function.
 

Detailed Description

[Borůvkas Algorithm](https://en.wikipedia.org/wiki/Borůvka's_algorithm) to find the Minimum Spanning Tree

Author
Jason Nardoni

Boruvka's algorithm is a greepy algorithm to find the MST by starting with small trees, and combining them to build bigger ones.

  1. Creates a group for every vertex.
  2. looks through each edge of every vertex for the smallest weight. Keeps track of the smallest edge for each of the current groups.
  3. Combine each group with the group it shares its smallest edge, adding the smallest edge to the MST.
  4. Repeat step 2-3 until all vertices are combined into a single group.

It assumes that the graph is connected. Non-connected edges can be represented using 0 or INT_MAX

Function Documentation

◆ boruvkas()

std::vector< std::vector< int > > greedy_algorithms::boruvkas_minimum_spanning_tree::boruvkas ( std::vector< std::vector< int > >  adj)

the implementation of boruvka's algorithm

Parameters
adja graph adjancency matrix stored as 2d vectors.
Returns
the MST as 2d vectors
57 {
58
59 size_t size = adj.size();
60 size_t total_groups = size;
61
62 if (size <= 1) {
63 return adj;
64 }
65
66 // Stores the current Minimum Spanning Tree. As groups are combined, they are added to the MST
67 std::vector<std::vector<int>> MST(size, std::vector<int>(size, INT_MAX));
68 for (int i = 0; i < size; i++) {
69 MST[i][i] = 0;
70 }
71
72 // Step 1: Create a group for each vertex
73
74 // Stores the parent of the vertex and its current depth, both initialized to 0
76
77 for (int i = 0; i < size; i++) {
78 parent[i].first = i; // Sets parent of each vertex to itself, depth remains 0
79 }
80
81 // Repeat until all are in a single group
82 while (total_groups > 1) {
83
84 std::vector<std::pair<int,int>> smallest_edge(size, std::make_pair(-1, -1)); //Pairing: start node, end node
85
86 // Step 2: Look throught each vertex for its smallest edge, only using the right half of the adj matrix
87 for (int i = 0; i < size; i++) {
88 for (int j = i+1; j < size; j++) {
89
90 if (adj[i][j] == INT_MAX || adj[i][j] == 0) { // No connection
91 continue;
92 }
93
94 // Finds the parents of the start and end points to make sure they arent in the same group
95 int parentA = findParent(parent, i);
96 int parentB = findParent(parent, j);
97
98 if (parentA != parentB) {
99
100 // Grabs the start and end points for the first groups current smallest edge
101 int start = smallest_edge[parentA].first;
102 int end = smallest_edge[parentA].second;
103
104 // If there is no current smallest edge, or the new edge is smaller, records the new smallest
105 if (start == -1 || adj [i][j] < adj[start][end]) {
106 smallest_edge[parentA].first = i;
107 smallest_edge[parentA].second = j;
108 }
109
110 // Does the same for the second group
111 start = smallest_edge[parentB].first;
112 end = smallest_edge[parentB].second;
113
114 if (start == -1 || adj[j][i] < adj[start][end]) {
115 smallest_edge[parentB].first = j;
116 smallest_edge[parentB].second = i;
117 }
118 }
119 }
120 }
121
122 // Step 3: Combine the groups based off their smallest edge
123
124 for (int i = 0; i < size; i++) {
125
126 // Makes sure the smallest edge exists
127 if (smallest_edge[i].first != -1) {
128
129 // Start and end points for the groups smallest edge
130 int start = smallest_edge[i].first;
131 int end = smallest_edge[i].second;
132
133 // Parents of the two groups - A is always itself
134 int parentA = i;
135 int parentB = findParent(parent, end);
136
137 // Makes sure the two nodes dont share the same parent. Would happen if the two groups have been
138 //merged previously through a common shortest edge
139 if (parentA == parentB) {
140 continue;
141 }
142
143 // Tries to balance the trees as much as possible as they are merged. The parent of the shallower
144 //tree will be pointed to the parent of the deeper tree.
145 if (parent[parentA].second < parent[parentB].second) {
146 parent[parentB].first = parentA; //New parent
147 parent[parentB].second++; //Increase depth
148 }
149 else {
150 parent[parentA].first = parentB;
151 parent[parentA].second++;
152 }
153 // Add the connection to the MST, using both halves of the adj matrix
154 MST[start][end] = adj[start][end];
155 MST[end][start] = adj[end][start];
156 total_groups--; // one fewer group
157 }
158 }
159 }
160 return MST;
161}
int findParent(std::vector< std::pair< int, int > > parent, const int v)
Recursively returns the vertex's parent at the root of the tree.
Definition: boruvkas_minimum_spanning_tree.cpp:44
T end(T... args)
T make_pair(T... args)
T size(T... args)
Here is the call graph for this function:

◆ findParent()

int greedy_algorithms::boruvkas_minimum_spanning_tree::findParent ( std::vector< std::pair< int, int > >  parent,
const int  v 
)

Recursively returns the vertex's parent at the root of the tree.

Parameters
parentthe array that will be checked
vvertex to find parent of
Returns
the parent of the vertex
44 {
45 if (parent[v].first != v) {
46 parent[v].first = findParent(parent, parent[v].first);
47 }
48
49 return parent[v].first;
50}
Here is the call graph for this function:

◆ main()

int main ( void  )

Main function.

Returns
0 on exit
219 {
220 tests(); // run self-test implementations
221 return 0;
222}
static void tests()
Self-test implementations.
Definition: boruvkas_minimum_spanning_tree.cpp:190
Here is the call graph for this function:

◆ test_findGraphSum()

int greedy_algorithms::boruvkas_minimum_spanning_tree::test_findGraphSum ( std::vector< std::vector< int > >  adj)

counts the sum of edges in the given tree

Parameters
adj2D vector adjacency matrix
Returns
the int size of the tree
168 {
169
170 size_t size = adj.size();
171 int sum = 0;
172
173 //Moves through one side of the adj matrix, counting the sums of each edge
174 for (int i = 0; i < size; i++) {
175 for (int j = i + 1; j < size; j++) {
176 if (adj[i][j] < INT_MAX) {
177 sum += adj[i][j];
178 }
179 }
180 }
181 return sum;
182}
T sum(const std::vector< std::valarray< T > > &A)
Definition: vector_ops.hpp:232
Here is the call graph for this function:

◆ tests()

static void tests ( )
static

Self-test implementations.

Returns
void
190 {
191 std::cout << "Starting tests...\n\n";
193 {0, 5, INT_MAX, 3, INT_MAX} ,
194 {5, 0, 2, INT_MAX, 5} ,
195 {INT_MAX, 2, 0, INT_MAX, 3} ,
196 {3, INT_MAX, INT_MAX, 0, INT_MAX} ,
197 {INT_MAX, 5, 3, INT_MAX, 0} ,
198 };
200 assert(greedy_algorithms::boruvkas_minimum_spanning_tree::test_findGraphSum(MST) == 13);
201 std::cout << "1st test passed!" << std::endl;
202
203 graph = {
204 { 0, 2, 0, 6, 0 },
205 { 2, 0, 3, 8, 5 },
206 { 0, 3, 0, 0, 7 },
207 { 6, 8, 0, 0, 9 },
208 { 0, 5, 7, 9, 0 }
209 };
211 assert(greedy_algorithms::boruvkas_minimum_spanning_tree::test_findGraphSum(MST) == 16);
212 std::cout << "2nd test passed!" << std::endl;
213}
std::vector< std::vector< int > > boruvkas(std::vector< std::vector< int > > adj)
the implementation of boruvka's algorithm
Definition: boruvkas_minimum_spanning_tree.cpp:57
T endl(T... args)
Graph Algorithms.
Here is the call graph for this function: