|
| 1 | +/** |
| 2 | + * @author [Jason Nardoni](https://github.com/JNardoni) |
| 3 | + * @file |
| 4 | + * |
| 5 | + * @brief |
| 6 | + * [Borůvkas Algorithm](https://en.wikipedia.org/wiki/Borůvka's_algorithm) to find the Minimum Spanning Tree |
| 7 | + * |
| 8 | + * |
| 9 | + * @details |
| 10 | + * Boruvka's algorithm is a greepy algorithm to find the MST by starting with small trees, and combining |
| 11 | + * them to build bigger ones. |
| 12 | + * 1. Creates a group for every vertex. |
| 13 | + * 2. looks through each edge of every vertex for the smallest weight. Keeps track |
| 14 | + * of the smallest edge for each of the current groups. |
| 15 | + * 3. Combine each group with the group it shares its smallest edge, adding the smallest |
| 16 | + * edge to the MST. |
| 17 | + * 4. Repeat step 2-3 until all vertices are combined into a single group. |
| 18 | + * |
| 19 | + * It assumes that the graph is connected. Non-connected edges can be represented using 0 or INT_MAX |
| 20 | + * |
| 21 | +*/ |
| 22 | + |
| 23 | +#include <iostream> /// for IO operations |
| 24 | +#include <vector> /// for std::vector |
| 25 | +#include <cassert> /// for assert |
| 26 | +#include <climits> /// for INT_MAX |
| 27 | + |
| 28 | +/** |
| 29 | + * @namespace greedy_algorithms |
| 30 | + * @brief Greedy Algorithms |
| 31 | + */ |
| 32 | +namespace greedy_algorithms { |
| 33 | +/** |
| 34 | + * @namespace boruvkas_minimum_spanning_tree |
| 35 | + * @brief Functions for the [Borůvkas Algorithm](https://en.wikipedia.org/wiki/Borůvka's_algorithm) implementation |
| 36 | + */ |
| 37 | +namespace boruvkas_minimum_spanning_tree { |
| 38 | +/** |
| 39 | + * @brief Recursively returns the vertex's parent at the root of the tree |
| 40 | + * @param parent the array that will be checked |
| 41 | + * @param v vertex to find parent of |
| 42 | + * @returns the parent of the vertex |
| 43 | + */ |
| 44 | +int findParent(std::vector<std::pair<int,int>> parent, const int v) { |
| 45 | + if (parent[v].first != v) { |
| 46 | + parent[v].first = findParent(parent, parent[v].first); |
| 47 | + } |
| 48 | + |
| 49 | + return parent[v].first; |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | + * @brief the implementation of boruvka's algorithm |
| 54 | + * @param adj a graph adjancency matrix stored as 2d vectors. |
| 55 | + * @returns the MST as 2d vectors |
| 56 | + */ |
| 57 | +std::vector<std::vector<int>> boruvkas(std::vector<std::vector<int>> adj) { |
| 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 |
| 75 | + std::vector<std::pair<int, int>> parent(size, std::make_pair(0, 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 | +} |
| 162 | + |
| 163 | +/** |
| 164 | + * @brief counts the sum of edges in the given tree |
| 165 | + * @param adj 2D vector adjacency matrix |
| 166 | + * @returns the int size of the tree |
| 167 | + */ |
| 168 | +int test_findGraphSum(std::vector<std::vector<int>> adj) { |
| 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 | +} |
| 183 | +} // namespace boruvkas_minimum_spanning_tree |
| 184 | +} // namespace greedy_algorithms |
| 185 | + |
| 186 | +/** |
| 187 | + * @brief Self-test implementations |
| 188 | + * @returns void |
| 189 | +*/ |
| 190 | +static void tests() { |
| 191 | + std::cout << "Starting tests...\n\n"; |
| 192 | + std::vector<std::vector<int>> graph = { |
| 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 | + }; |
| 199 | + std::vector<std::vector<int>> MST = greedy_algorithms::boruvkas_minimum_spanning_tree::boruvkas(graph); |
| 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 | + }; |
| 210 | + MST = greedy_algorithms::boruvkas_minimum_spanning_tree::boruvkas(graph); |
| 211 | + assert(greedy_algorithms::boruvkas_minimum_spanning_tree::test_findGraphSum(MST) == 16); |
| 212 | + std::cout << "2nd test passed!" << std::endl; |
| 213 | +} |
| 214 | + |
| 215 | +/** |
| 216 | + * @brief Main function |
| 217 | + * @returns 0 on exit |
| 218 | + */ |
| 219 | +int main() { |
| 220 | + tests(); // run self-test implementations |
| 221 | + return 0; |
| 222 | +} |
0 commit comments