feat: add Strassen's Matrix Multiplication (#2413)

* Feat: Add Strassen's matrix multiplication

* updating DIRECTORY.md

* Fix cpp lint error

* updating DIRECTORY.md

* clang-format and clang-tidy fixes for 02439b57

* Fix windows error

* Add namespaces

* updating DIRECTORY.md

* Proper documentation

* Reduce the matrix size.

* updating DIRECTORY.md

* clang-format and clang-tidy fixes for 0545555a

Co-authored-by: toastedbreadandomelette <toastedbreadandomelette@gmail.com>
Co-authored-by: github-actions[bot] <github-actions@users.noreply.github.com>
Co-authored-by: David Leal <halfpacho@gmail.com>
This commit is contained in:
Ashish Bhanu Daulatabad
2023-01-25 01:33:06 +05:30
committed by GitHub
parent a6a9d8e75a
commit 5b238724b8
6 changed files with 745 additions and 241 deletions

View File

@@ -3,36 +3,39 @@
* @file
*
* @brief
* [Borůvkas Algorithm](https://en.wikipedia.org/wiki/Borůvka's_algorithm) to find the Minimum Spanning Tree
*
*
* [Borůvkas Algorithm](https://en.wikipedia.org/wiki/Borůvka's_algorithm) to
*find the Minimum Spanning Tree
*
*
* @details
* Boruvka's algorithm is a greepy algorithm to find the MST by starting with small trees, and combining
* them to build bigger ones.
* 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.
* 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
*
*/
*
* It assumes that the graph is connected. Non-connected edges can be
*represented using 0 or INT_MAX
*
*/
#include <iostream> /// for IO operations
#include <vector> /// for std::vector
#include <cassert> /// for assert
#include <climits> /// for INT_MAX
#include <cassert> /// for assert
#include <climits> /// for INT_MAX
#include <iostream> /// for IO operations
#include <vector> /// for std::vector
/**
* @namespace greedy_algorithms
* @brief Greedy Algorithms
*/
namespace greedy_algorithms {
namespace greedy_algorithms {
/**
* @namespace boruvkas_minimum_spanning_tree
* @brief Functions for the [Borůvkas Algorithm](https://en.wikipedia.org/wiki/Borůvka's_algorithm) implementation
* @brief Functions for the [Borůvkas
* Algorithm](https://en.wikipedia.org/wiki/Borůvka's_algorithm) implementation
*/
namespace boruvkas_minimum_spanning_tree {
/**
@@ -41,123 +44,127 @@ namespace boruvkas_minimum_spanning_tree {
* @param v vertex to find parent of
* @returns the parent of the vertex
*/
int findParent(std::vector<std::pair<int,int>> parent, const int v) {
if (parent[v].first != v) {
parent[v].first = findParent(parent, parent[v].first);
}
int findParent(std::vector<std::pair<int, int>> parent, const int v) {
if (parent[v].first != v) {
parent[v].first = findParent(parent, parent[v].first);
}
return parent[v].first;
return parent[v].first;
}
/**
* @brief the implementation of boruvka's algorithm
* @param adj a graph adjancency matrix stored as 2d vectors.
* @param adj a graph adjancency matrix stored as 2d vectors.
* @returns the MST as 2d vectors
*/
std::vector<std::vector<int>> boruvkas(std::vector<std::vector<int>> adj) {
size_t size = adj.size();
size_t total_groups = size;
size_t size = adj.size();
size_t total_groups = size;
if (size <= 1) {
return adj;
}
if (size <= 1) {
return adj;
}
// Stores the current Minimum Spanning Tree. As groups are combined, they
// are added to the MST
std::vector<std::vector<int>> MST(size, std::vector<int>(size, INT_MAX));
for (int i = 0; i < size; i++) {
MST[i][i] = 0;
}
// Stores the current Minimum Spanning Tree. As groups are combined, they are added to the MST
std::vector<std::vector<int>> MST(size, std::vector<int>(size, INT_MAX));
for (int i = 0; i < size; i++) {
MST[i][i] = 0;
}
// Step 1: Create a group for each vertex
// Step 1: Create a group for each vertex
// Stores the parent of the vertex and its current depth, both initialized to 0
std::vector<std::pair<int, int>> parent(size, std::make_pair(0, 0));
// Stores the parent of the vertex and its current depth, both initialized
// to 0
std::vector<std::pair<int, int>> parent(size, std::make_pair(0, 0));
for (int i = 0; i < size; i++) {
parent[i].first = i; // Sets parent of each vertex to itself, depth remains 0
}
for (int i = 0; i < size; i++) {
parent[i].first =
i; // Sets parent of each vertex to itself, depth remains 0
}
// Repeat until all are in a single group
while (total_groups > 1) {
// Repeat until all are in a single group
while (total_groups > 1) {
std::vector<std::pair<int, int>> smallest_edge(
size, std::make_pair(-1, -1)); // Pairing: start node, end node
std::vector<std::pair<int,int>> smallest_edge(size, std::make_pair(-1, -1)); //Pairing: start node, end node
// Step 2: Look throught each vertex for its smallest edge, only using
// the right half of the adj matrix
for (int i = 0; i < size; i++) {
for (int j = i + 1; j < size; j++) {
if (adj[i][j] == INT_MAX || adj[i][j] == 0) { // No connection
continue;
}
// Step 2: Look throught each vertex for its smallest edge, only using the right half of the adj matrix
for (int i = 0; i < size; i++) {
for (int j = i+1; j < size; j++) {
// Finds the parents of the start and end points to make sure
// they arent in the same group
int parentA = findParent(parent, i);
int parentB = findParent(parent, j);
if (adj[i][j] == INT_MAX || adj[i][j] == 0) { // No connection
continue;
}
if (parentA != parentB) {
// Grabs the start and end points for the first groups
// current smallest edge
int start = smallest_edge[parentA].first;
int end = smallest_edge[parentA].second;
// Finds the parents of the start and end points to make sure they arent in the same group
int parentA = findParent(parent, i);
int parentB = findParent(parent, j);
// If there is no current smallest edge, or the new edge is
// smaller, records the new smallest
if (start == -1 || adj[i][j] < adj[start][end]) {
smallest_edge[parentA].first = i;
smallest_edge[parentA].second = j;
}
if (parentA != parentB) {
// Does the same for the second group
start = smallest_edge[parentB].first;
end = smallest_edge[parentB].second;
// Grabs the start and end points for the first groups current smallest edge
int start = smallest_edge[parentA].first;
int end = smallest_edge[parentA].second;
if (start == -1 || adj[j][i] < adj[start][end]) {
smallest_edge[parentB].first = j;
smallest_edge[parentB].second = i;
}
}
}
}
// If there is no current smallest edge, or the new edge is smaller, records the new smallest
if (start == -1 || adj [i][j] < adj[start][end]) {
smallest_edge[parentA].first = i;
smallest_edge[parentA].second = j;
}
// Step 3: Combine the groups based off their smallest edge
// Does the same for the second group
start = smallest_edge[parentB].first;
end = smallest_edge[parentB].second;
for (int i = 0; i < size; i++) {
// Makes sure the smallest edge exists
if (smallest_edge[i].first != -1) {
// Start and end points for the groups smallest edge
int start = smallest_edge[i].first;
int end = smallest_edge[i].second;
if (start == -1 || adj[j][i] < adj[start][end]) {
smallest_edge[parentB].first = j;
smallest_edge[parentB].second = i;
}
}
}
}
// Parents of the two groups - A is always itself
int parentA = i;
int parentB = findParent(parent, end);
// Step 3: Combine the groups based off their smallest edge
// Makes sure the two nodes dont share the same parent. Would
// happen if the two groups have been
// merged previously through a common shortest edge
if (parentA == parentB) {
continue;
}
for (int i = 0; i < size; i++) {
// Makes sure the smallest edge exists
if (smallest_edge[i].first != -1) {
// Start and end points for the groups smallest edge
int start = smallest_edge[i].first;
int end = smallest_edge[i].second;
// Parents of the two groups - A is always itself
int parentA = i;
int parentB = findParent(parent, end);
// Makes sure the two nodes dont share the same parent. Would happen if the two groups have been
//merged previously through a common shortest edge
if (parentA == parentB) {
continue;
}
// Tries to balance the trees as much as possible as they are merged. The parent of the shallower
//tree will be pointed to the parent of the deeper tree.
if (parent[parentA].second < parent[parentB].second) {
parent[parentB].first = parentA; //New parent
parent[parentB].second++; //Increase depth
}
else {
parent[parentA].first = parentB;
parent[parentA].second++;
}
// Add the connection to the MST, using both halves of the adj matrix
MST[start][end] = adj[start][end];
MST[end][start] = adj[end][start];
total_groups--; // one fewer group
}
}
}
return MST;
// Tries to balance the trees as much as possible as they are
// merged. The parent of the shallower
// tree will be pointed to the parent of the deeper tree.
if (parent[parentA].second < parent[parentB].second) {
parent[parentB].first = parentA; // New parent
parent[parentB].second++; // Increase depth
} else {
parent[parentA].first = parentB;
parent[parentA].second++;
}
// Add the connection to the MST, using both halves of the adj
// matrix
MST[start][end] = adj[start][end];
MST[end][start] = adj[end][start];
total_groups--; // one fewer group
}
}
}
return MST;
}
/**
@@ -166,19 +173,18 @@ std::vector<std::vector<int>> boruvkas(std::vector<std::vector<int>> adj) {
* @returns the int size of the tree
*/
int test_findGraphSum(std::vector<std::vector<int>> adj) {
size_t size = adj.size();
int sum = 0;
size_t size = adj.size();
int sum = 0;
//Moves through one side of the adj matrix, counting the sums of each edge
for (int i = 0; i < size; i++) {
for (int j = i + 1; j < size; j++) {
if (adj[i][j] < INT_MAX) {
sum += adj[i][j];
}
}
}
return sum;
// Moves through one side of the adj matrix, counting the sums of each edge
for (int i = 0; i < size; i++) {
for (int j = i + 1; j < size; j++) {
if (adj[i][j] < INT_MAX) {
sum += adj[i][j];
}
}
}
return sum;
}
} // namespace boruvkas_minimum_spanning_tree
} // namespace greedy_algorithms
@@ -186,30 +192,29 @@ int test_findGraphSum(std::vector<std::vector<int>> adj) {
/**
* @brief Self-test implementations
* @returns void
*/
*/
static void tests() {
std::cout << "Starting tests...\n\n";
std::vector<std::vector<int>> graph = {
{0, 5, INT_MAX, 3, INT_MAX} ,
{5, 0, 2, INT_MAX, 5} ,
{INT_MAX, 2, 0, INT_MAX, 3} ,
{3, INT_MAX, INT_MAX, 0, INT_MAX} ,
{INT_MAX, 5, 3, INT_MAX, 0} ,
};
std::vector<std::vector<int>> MST = greedy_algorithms::boruvkas_minimum_spanning_tree::boruvkas(graph);
assert(greedy_algorithms::boruvkas_minimum_spanning_tree::test_findGraphSum(MST) == 13);
std::cout << "1st test passed!" << std::endl;
std::cout << "Starting tests...\n\n";
std::vector<std::vector<int>> graph = {
{0, 5, INT_MAX, 3, INT_MAX}, {5, 0, 2, INT_MAX, 5},
{INT_MAX, 2, 0, INT_MAX, 3}, {3, INT_MAX, INT_MAX, 0, INT_MAX},
{INT_MAX, 5, 3, INT_MAX, 0},
};
std::vector<std::vector<int>> MST =
greedy_algorithms::boruvkas_minimum_spanning_tree::boruvkas(graph);
assert(greedy_algorithms::boruvkas_minimum_spanning_tree::test_findGraphSum(
MST) == 13);
std::cout << "1st test passed!" << std::endl;
graph = {
{ 0, 2, 0, 6, 0 },
{ 2, 0, 3, 8, 5 },
{ 0, 3, 0, 0, 7 },
{ 6, 8, 0, 0, 9 },
{ 0, 5, 7, 9, 0 }
};
MST = greedy_algorithms::boruvkas_minimum_spanning_tree::boruvkas(graph);
assert(greedy_algorithms::boruvkas_minimum_spanning_tree::test_findGraphSum(MST) == 16);
std::cout << "2nd test passed!" << std::endl;
graph = {{0, 2, 0, 6, 0},
{2, 0, 3, 8, 5},
{0, 3, 0, 0, 7},
{6, 8, 0, 0, 9},
{0, 5, 7, 9, 0}};
MST = greedy_algorithms::boruvkas_minimum_spanning_tree::boruvkas(graph);
assert(greedy_algorithms::boruvkas_minimum_spanning_tree::test_findGraphSum(
MST) == 16);
std::cout << "2nd test passed!" << std::endl;
}
/**
@@ -217,6 +222,6 @@ static void tests() {
* @returns 0 on exit
*/
int main() {
tests(); // run self-test implementations
return 0;
tests(); // run self-test implementations
return 0;
}