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
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 745 additions and 241 deletions

View File

@ -82,6 +82,7 @@
## Divide And Conquer
* [Karatsuba Algorithm For Fast Multiplication](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/divide_and_conquer/karatsuba_algorithm_for_fast_multiplication.cpp)
* [Strassen Matrix Multiplication](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/divide_and_conquer/strassen_matrix_multiplication.cpp)
## Dynamic Programming
* [0 1 Knapsack](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/dynamic_programming/0_1_knapsack.cpp)
@ -110,6 +111,7 @@
* [Partition Problem](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/dynamic_programming/partition_problem.cpp)
* [Searching Of Element In Dynamic Array](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/dynamic_programming/searching_of_element_in_dynamic_array.cpp)
* [Shortest Common Supersequence](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/dynamic_programming/shortest_common_supersequence.cpp)
* [Subset Sum](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/dynamic_programming/subset_sum.cpp)
* [Tree Height](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/dynamic_programming/tree_height.cpp)
* [Word Break](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/dynamic_programming/word_break.cpp)
@ -146,6 +148,7 @@
* [Spirograph](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/graphics/spirograph.cpp)
## Greedy Algorithms
* [Boruvkas Minimum Spanning Tree](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/greedy_algorithms/boruvkas_minimum_spanning_tree.cpp)
* [Dijkstra](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/greedy_algorithms/dijkstra.cpp)
* [Huffman](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/greedy_algorithms/huffman.cpp)
* [Jumpgame](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/greedy_algorithms/jumpgame.cpp)
@ -171,6 +174,7 @@
* [Vector Ops](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/machine_learning/vector_ops.hpp)
## Math
* [Aliquot Sum](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/math/aliquot_sum.cpp)
* [Approximate Pi](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/math/approximate_pi.cpp)
* [Area](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/math/area.cpp)
* [Armstrong Number](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/math/armstrong_number.cpp)

View File

@ -5,12 +5,14 @@
* (https://www.geeksforgeeks.org/travelling-salesman-problem-set-1/)
*
* @details
* Given the distance/cost(as and adjacency matrix) between each city/node to the other city/node ,
* the problem is to find the shortest possible route that visits every city exactly once
* and returns to the starting point or we can say the minimum cost of whole tour.
* Given the distance/cost(as and adjacency matrix) between each city/node to
* the other city/node , the problem is to find the shortest possible route that
* visits every city exactly once and returns to the starting point or we can
* say the minimum cost of whole tour.
*
* Explanation:
* INPUT -> You are given with a adjacency matrix A = {} which contains the distance between two cities/node.
* INPUT -> You are given with a adjacency matrix A = {} which contains the
* distance between two cities/node.
*
* OUTPUT -> Minimum cost of whole tour from starting point
*
@ -18,11 +20,11 @@
* Space complexity: O(n)
* @author [Utkarsh Yadav](https://github.com/Rytnix)
*/
#include <algorithm> /// for std::min
#include <algorithm> /// for std::min
#include <cassert> /// for assert
#include <iostream> /// for IO operations
#include <vector> /// for std::vector
#include <limits> /// for limits of integral types
#include <iostream> /// for IO operations
#include <limits> /// for limits of integral types
#include <vector> /// for std::vector
/**
* @namespace bit_manipulation
@ -42,40 +44,53 @@ namespace travelling_salesman_using_bit_manipulation {
* @param setOfCitites represents the city in bit form.\
* @param city is taken to track the current city movement.
* @param n is the no of citys .
* @param dp vector is used to keep a record of state to avoid the recomputation.
* @returns minimum cost of traversing whole nodes/cities from starting point back to starting point
* @param dp vector is used to keep a record of state to avoid the
* recomputation.
* @returns minimum cost of traversing whole nodes/cities from starting point
* back to starting point
*/
std::uint64_t travelling_salesman_using_bit_manipulation(std::vector<std::vector<uint32_t>> dist, // dist is the adjacency matrix containing the distance.
// setOfCities as a bit represent the cities/nodes. Ex: if setOfCities = 2 => 0010(in binary)
// means representing the city/node B if city/nodes are represented as D->C->B->A.
std::uint64_t setOfCities,
std::uint64_t city, // city is taken to track our current city/node movement,where we are currently.
std::uint64_t n, // n is the no of cities we have.
std::vector<std::vector<uint32_t>> &dp) //dp is taken to memorize the state to avoid recomputition
std::uint64_t travelling_salesman_using_bit_manipulation(
std::vector<std::vector<uint32_t>>
dist, // dist is the adjacency matrix containing the distance.
// setOfCities as a bit represent the cities/nodes. Ex: if
// setOfCities = 2 => 0010(in binary) means representing the
// city/node B if city/nodes are represented as D->C->B->A.
std::uint64_t setOfCities,
std::uint64_t city, // city is taken to track our current city/node
// movement,where we are currently.
std::uint64_t n, // n is the no of cities we have.
std::vector<std::vector<uint32_t>>
&dp) // dp is taken to memorize the state to avoid recomputition
{
//base case;
if (setOfCities == (1 << n) - 1) // we have covered all the cities
return dist[city][0]; //return the cost from the current city to the original city.
// base case;
if (setOfCities == (1 << n) - 1) { // we have covered all the cities
return dist[city][0]; // return the cost from the current city to the
// original city.
}
if (dp[setOfCities][city] != -1)
if (dp[setOfCities][city] != -1) {
return dp[setOfCities][city];
//otherwise try all possible options
uint64_t ans = 2147483647 ;
}
// otherwise try all possible options
uint64_t ans = 2147483647;
for (int choice = 0; choice < n; choice++) {
//check if the city is visited or not.
if ((setOfCities & (1 << choice)) == 0 ) { // this means that this perticular city is not visited.
std::uint64_t subProb = dist[city][choice] + travelling_salesman_using_bit_manipulation(dist, setOfCities | (1 << choice), choice, n, dp);
// Here we are doing a recursive call to tsp with the updated set of city/node
// and choice which tells that where we are currently.
// check if the city is visited or not.
if ((setOfCities & (1 << choice)) ==
0) { // this means that this perticular city is not visited.
std::uint64_t subProb =
dist[city][choice] +
travelling_salesman_using_bit_manipulation(
dist, setOfCities | (1 << choice), choice, n, dp);
// Here we are doing a recursive call to tsp with the updated set of
// city/node and choice which tells that where we are currently.
ans = std::min(ans, subProb);
}
}
dp[setOfCities][city] = ans;
return ans;
}
} // namespace travelling_salesman_using_bit_manipulation
} // namespace bit_manipulation
} // namespace travelling_salesman_using_bit_manipulation
} // namespace bit_manipulation
/**
* @brief Self-test implementations
@ -84,29 +99,36 @@ std::uint64_t travelling_salesman_using_bit_manipulation(std::vector<std::vector
static void test() {
// 1st test-case
std::vector<std::vector<uint32_t>> dist = {
{0, 20, 42, 35}, {20, 0, 30, 34}, {42, 30, 0, 12}, {35, 34, 12, 0}
};
{0, 20, 42, 35}, {20, 0, 30, 34}, {42, 30, 0, 12}, {35, 34, 12, 0}};
uint32_t V = dist.size();
std::vector<std::vector<uint32_t>> dp(1 << V, std::vector<uint32_t>(V, -1));
assert(bit_manipulation::travelling_salesman_using_bit_manipulation::travelling_salesman_using_bit_manipulation(dist, 1, 0, V, dp) == 97);
std::cout << "1st test-case: passed!" << "\n";
assert(bit_manipulation::travelling_salesman_using_bit_manipulation::
travelling_salesman_using_bit_manipulation(dist, 1, 0, V, dp) ==
97);
std::cout << "1st test-case: passed!"
<< "\n";
// 2nd test-case
dist = {{0, 5, 10, 15}, {5, 0, 20, 30}, {10, 20, 0, 35}, {15, 30, 35, 0}};
V = dist.size();
std::vector<std::vector<uint32_t>> dp1(1 << V, std::vector<uint32_t>(V, -1));
assert(bit_manipulation::travelling_salesman_using_bit_manipulation::travelling_salesman_using_bit_manipulation(dist, 1, 0, V, dp1) == 75);
std::cout << "2nd test-case: passed!" << "\n";
std::vector<std::vector<uint32_t>> dp1(1 << V,
std::vector<uint32_t>(V, -1));
assert(bit_manipulation::travelling_salesman_using_bit_manipulation::
travelling_salesman_using_bit_manipulation(dist, 1, 0, V, dp1) ==
75);
std::cout << "2nd test-case: passed!"
<< "\n";
// 3rd test-case
dist = {
{0, 10, 15, 20}, {10, 0, 35, 25}, {15, 35, 0, 30}, {20, 25, 30, 0}
};
dist = {{0, 10, 15, 20}, {10, 0, 35, 25}, {15, 35, 0, 30}, {20, 25, 30, 0}};
V = dist.size();
std::vector<std::vector<uint32_t>> dp2(1 << V, std::vector<uint32_t>(V, -1));
assert(bit_manipulation::travelling_salesman_using_bit_manipulation::travelling_salesman_using_bit_manipulation(dist, 1, 0, V, dp2) == 80);
std::cout << "3rd test-case: passed!" << "\n";
std::vector<std::vector<uint32_t>> dp2(1 << V,
std::vector<uint32_t>(V, -1));
assert(bit_manipulation::travelling_salesman_using_bit_manipulation::
travelling_salesman_using_bit_manipulation(dist, 1, 0, V, dp2) ==
80);
std::cout << "3rd test-case: passed!"
<< "\n";
}
/**

View File

@ -0,0 +1,471 @@
/**
* @brief [Strassen's
* algorithm](https://en.wikipedia.org/wiki/Strassen_algorithm) is one of the
* methods for multiplying two matrices. It is one of the faster algorithms for
* larger matrices than naive multiplication method.
*
* It involves dividing each matrices into 4 blocks, given they are evenly
* divisible, and are combined with new defined matrices involving 7 matrix
* multiplications instead of eight, yielding O(n^2.8073) complexity.
*
* @author [AshishYUO](https://github.com/AshishYUO)
*/
#include <cassert> /// For assert operation
#include <chrono> /// For std::chrono; time measurement
#include <iostream> /// For I/O operations
#include <tuple> /// For std::tuple
#include <vector> /// For creating dynamic arrays
/**
* @namespace divide_and_conquer
* @brief Divide and Conquer algorithms
*/
namespace divide_and_conquer {
/**
* @namespace strassens_multiplication
* @brief Namespace for performing strassen's multiplication
*/
namespace strassens_multiplication {
/// Complement of 0 is a max integer.
constexpr size_t MAX_SIZE = ~0ULL;
/**
* @brief Matrix class.
*/
template <typename T,
typename = typename std::enable_if<
std::is_integral<T>::value || std::is_floating_point<T>::value,
bool>::type>
class Matrix {
std::vector<std::vector<T>> _mat;
public:
/**
* @brief Constructor
* @tparam Integer ensuring integers are being evaluated and not other
* data types.
* @param size denoting the size of Matrix as size x size
*/
template <typename Integer,
typename = typename std::enable_if<
std::is_integral<Integer>::value, Integer>::type>
explicit Matrix(const Integer size) {
for (size_t i = 0; i < size; ++i) {
_mat.emplace_back(std::vector<T>(size, 0));
}
}
/**
* @brief Constructor
* @tparam Integer ensuring integers are being evaluated and not other
* data types.
* @param rows denoting the total rows of Matrix
* @param cols denoting the total elements in each row of Matrix
*/
template <typename Integer,
typename = typename std::enable_if<
std::is_integral<Integer>::value, Integer>::type>
Matrix(const Integer rows, const Integer cols) {
for (size_t i = 0; i < rows; ++i) {
_mat.emplace_back(std::vector<T>(cols, 0));
}
}
/**
* @brief Get the matrix shape
* @returns pair of integer denoting total rows and columns
*/
inline std::pair<size_t, size_t> size() const {
return {_mat.size(), _mat[0].size()};
}
/**
* @brief returns the address of the element at ith place
* (here ith row of the matrix)
* @tparam Integer any valid integer
* @param index index which is requested
* @returns the address of the element (here ith row or array)
*/
template <typename Integer,
typename = typename std::enable_if<
std::is_integral<Integer>::value, Integer>::type>
inline std::vector<T> &operator[](const Integer index) {
return _mat[index];
}
/**
* @brief Creates a new matrix and returns a part of it.
* @param row_start start of the row
* @param row_end end of the row
* @param col_start start of the col
* @param col_end end of the column
* @returns A slice of (row_end - row_start) x (col_end - col_start) size of
* array starting from row_start row and col_start column
*/
Matrix slice(const size_t row_start, const size_t row_end = MAX_SIZE,
const size_t col_start = MAX_SIZE,
const size_t col_end = MAX_SIZE) const {
const size_t h_size =
(row_end != MAX_SIZE ? row_end : _mat.size()) - row_start;
const size_t v_size = (col_end != MAX_SIZE ? col_end : _mat[0].size()) -
(col_start != MAX_SIZE ? col_start : 0);
Matrix result = Matrix<T>(h_size, v_size);
const size_t v_start = (col_start != MAX_SIZE ? col_start : 0);
for (size_t i = 0; i < h_size; ++i) {
for (size_t j = 0; j < v_size; ++j) {
result._mat[i][j] = _mat[i + row_start][j + v_start];
}
}
return result;
}
/**
* @brief Horizontally stack the matrix (one after the other)
* @tparam Number any type of number
* @param other the other matrix: note that this array is not modified
* @returns void, but modifies the current array
*/
template <typename Number, typename = typename std::enable_if<
std::is_integral<Number>::value ||
std::is_floating_point<Number>::value,
Number>::type>
void h_stack(const Matrix<Number> &other) {
assert(_mat.size() == other._mat.size());
for (size_t i = 0; i < other._mat.size(); ++i) {
for (size_t j = 0; j < other._mat[i].size(); ++j) {
_mat[i].push_back(other._mat[i][j]);
}
}
}
/**
* @brief Horizontally stack the matrix (current matrix above the other)
* @tparam Number any type of number (Integer or floating point)
* @param other the other matrix: note that this array is not modified
* @returns void, but modifies the current array
*/
template <typename Number, typename = typename std::enable_if<
std::is_integral<Number>::value ||
std::is_floating_point<Number>::value,
Number>::type>
void v_stack(const Matrix<Number> &other) {
assert(_mat[0].size() == other._mat[0].size());
for (size_t i = 0; i < other._mat.size(); ++i) {
_mat.emplace_back(std::vector<T>(other._mat[i].size()));
for (size_t j = 0; j < other._mat[i].size(); ++j) {
_mat.back()[j] = other._mat[i][j];
}
}
}
/**
* @brief Add two matrices and returns a new matrix
* @tparam Number any real value to add
* @param other Other matrix to add to this
* @returns new matrix
*/
template <typename Number, typename = typename std::enable_if<
std::is_integral<Number>::value ||
std::is_floating_point<Number>::value,
bool>::type>
Matrix operator+(const Matrix<Number> &other) const {
assert(this->size() == other.size());
Matrix C = Matrix<Number>(_mat.size(), _mat[0].size());
for (size_t i = 0; i < _mat.size(); ++i) {
for (size_t j = 0; j < _mat[i].size(); ++j) {
C._mat[i][j] = _mat[i][j] + other._mat[i][j];
}
}
return C;
}
/**
* @brief Add another matrices to current matrix
* @tparam Number any real value to add
* @param other Other matrix to add to this
* @returns reference of current matrix
*/
template <typename Number, typename = typename std::enable_if<
std::is_integral<Number>::value ||
std::is_floating_point<Number>::value,
bool>::type>
Matrix &operator+=(const Matrix<Number> &other) const {
assert(this->size() == other.size());
for (size_t i = 0; i < _mat.size(); ++i) {
for (size_t j = 0; j < _mat[i].size(); ++j) {
_mat[i][j] += other._mat[i][j];
}
}
return this;
}
/**
* @brief Subtract two matrices and returns a new matrix
* @tparam Number any real value to multiply
* @param other Other matrix to subtract to this
* @returns new matrix
*/
template <typename Number, typename = typename std::enable_if<
std::is_integral<Number>::value ||
std::is_floating_point<Number>::value,
bool>::type>
Matrix operator-(const Matrix<Number> &other) const {
assert(this->size() == other.size());
Matrix C = Matrix<Number>(_mat.size(), _mat[0].size());
for (size_t i = 0; i < _mat.size(); ++i) {
for (size_t j = 0; j < _mat[i].size(); ++j) {
C._mat[i][j] = _mat[i][j] - other._mat[i][j];
}
}
return C;
}
/**
* @brief Subtract another matrices to current matrix
* @tparam Number any real value to Subtract
* @param other Other matrix to Subtract to this
* @returns reference of current matrix
*/
template <typename Number, typename = typename std::enable_if<
std::is_integral<Number>::value ||
std::is_floating_point<Number>::value,
bool>::type>
Matrix &operator-=(const Matrix<Number> &other) const {
assert(this->size() == other.size());
for (size_t i = 0; i < _mat.size(); ++i) {
for (size_t j = 0; j < _mat[i].size(); ++j) {
_mat[i][j] -= other._mat[i][j];
}
}
return this;
}
/**
* @brief Multiply two matrices and returns a new matrix
* @tparam Number any real value to multiply
* @param other Other matrix to multiply to this
* @returns new matrix
*/
template <typename Number, typename = typename std::enable_if<
std::is_integral<Number>::value ||
std::is_floating_point<Number>::value,
bool>::type>
inline Matrix operator*(const Matrix<Number> &other) const {
assert(_mat[0].size() == other._mat.size());
auto size = this->size();
const size_t row = size.first, col = size.second;
// Main condition for applying strassen's method:
// 1: matrix should be a square matrix
// 2: matrix should be of even size (mat.size() % 2 == 0)
return (row == col && (row & 1) == 0)
? this->strassens_multiplication(other)
: this->naive_multiplication(other);
}
/**
* @brief Multiply matrix with a number and returns a new matrix
* @tparam Number any real value to multiply
* @param other Other real number to multiply to current matrix
* @returns new matrix
*/
template <typename Number, typename = typename std::enable_if<
std::is_integral<Number>::value ||
std::is_floating_point<Number>::value,
bool>::type>
inline Matrix operator*(const Number other) const {
Matrix C = Matrix<Number>(_mat.size(), _mat[0].size());
for (size_t i = 0; i < _mat.size(); ++i) {
for (size_t j = 0; j < _mat[i].size(); ++j) {
C._mat[i][j] = _mat[i][j] * other;
}
}
return C;
}
/**
* @brief Multiply a number to current matrix
* @tparam Number any real value to multiply
* @param other Other matrix to multiply to this
* @returns reference of current matrix
*/
template <typename Number, typename = typename std::enable_if<
std::is_integral<Number>::value ||
std::is_floating_point<Number>::value,
bool>::type>
Matrix &operator*=(const Number other) const {
for (size_t i = 0; i < _mat.size(); ++i) {
for (size_t j = 0; j < _mat[i].size(); ++j) {
_mat[i][j] *= other;
}
}
return this;
}
/**
* @brief Naive multiplication performed on this
* @tparam Number any real value to multiply
* @param other Other matrix to multiply to this
* @returns new matrix
*/
template <typename Number, typename = typename std::enable_if<
std::is_integral<Number>::value ||
std::is_floating_point<Number>::value,
bool>::type>
Matrix naive_multiplication(const Matrix<Number> &other) const {
Matrix C = Matrix<Number>(_mat.size(), other._mat[0].size());
for (size_t i = 0; i < _mat.size(); ++i) {
for (size_t k = 0; k < _mat[0].size(); ++k) {
for (size_t j = 0; j < other._mat[0].size(); ++j) {
C._mat[i][j] += _mat[i][k] * other._mat[k][j];
}
}
}
return C;
}
/**
* @brief Strassens method of multiplying two matrices
* References: https://en.wikipedia.org/wiki/Strassen_algorithm
* @tparam Number any real value to multiply
* @param other Other matrix to multiply to this
* @returns new matrix
*/
template <typename Number, typename = typename std::enable_if<
std::is_integral<Number>::value ||
std::is_floating_point<Number>::value,
bool>::type>
Matrix strassens_multiplication(const Matrix<Number> &other) const {
const size_t size = _mat.size();
// Base case: when a matrix is small enough for faster naive
// multiplication, or the matrix is of odd size, then go with the naive
// multiplication route;
// else; go with the strassen's method.
if (size <= 64ULL || (size & 1ULL)) {
return this->naive_multiplication(other);
} else {
const Matrix<Number>
A = this->slice(0ULL, size >> 1, 0ULL, size >> 1),
B = this->slice(0ULL, size >> 1, size >> 1, size),
C = this->slice(size >> 1, size, 0ULL, size >> 1),
D = this->slice(size >> 1, size, size >> 1, size),
E = other.slice(0ULL, size >> 1, 0ULL, size >> 1),
F = other.slice(0ULL, size >> 1, size >> 1, size),
G = other.slice(size >> 1, size, 0ULL, size >> 1),
H = other.slice(size >> 1, size, size >> 1, size);
Matrix P1 = A.strassens_multiplication(F - H);
Matrix P2 = (A + B).strassens_multiplication(H);
Matrix P3 = (C + D).strassens_multiplication(E);
Matrix P4 = D.strassens_multiplication(G - E);
Matrix P5 = (A + D).strassens_multiplication(E + H);
Matrix P6 = (B - D).strassens_multiplication(G + H);
Matrix P7 = (A - C).strassens_multiplication(E + F);
// Building final matrix C11 would be
// [ | ]
// [ C11 | C12 ]
// C = [ ____ | ____ ]
// [ | ]
// [ C21 | C22 ]
// [ | ]
Matrix C11 = P5 + P4 - P2 + P6;
Matrix C12 = P1 + P2;
Matrix C21 = P3 + P4;
Matrix C22 = P1 + P5 - P3 - P7;
C21.h_stack(C22);
C11.h_stack(C12);
C11.v_stack(C21);
return C11;
}
}
/**
* @brief Compares two matrices if each of them are equal or not
* @param other other matrix to compare
* @returns whether they are equal or not
*/
bool operator==(const Matrix<T> &other) const {
if (_mat.size() != other._mat.size() ||
_mat[0].size() != other._mat[0].size()) {
return false;
}
for (size_t i = 0; i < _mat.size(); ++i) {
for (size_t j = 0; j < _mat[i].size(); ++j) {
if (_mat[i][j] != other._mat[i][j]) {
return false;
}
}
}
return true;
}
friend std::ostream &operator<<(std::ostream &out, const Matrix<T> &mat) {
for (auto &row : mat._mat) {
for (auto &elem : row) {
out << elem << " ";
}
out << "\n";
}
return out << "\n";
}
};
} // namespace strassens_multiplication
} // namespace divide_and_conquer
/**
* @brief Self-test implementations
* @returns void
*/
static void test() {
const size_t s = 512;
auto matrix_demo =
divide_and_conquer::strassens_multiplication::Matrix<size_t>(s, s);
for (size_t i = 0; i < s; ++i) {
for (size_t j = 0; j < s; ++j) {
matrix_demo[i][j] = i + j;
}
}
auto matrix_demo2 =
divide_and_conquer::strassens_multiplication::Matrix<size_t>(s, s);
for (size_t i = 0; i < s; ++i) {
for (size_t j = 0; j < s; ++j) {
matrix_demo2[i][j] = 2 + i + j;
}
}
auto start = std::chrono::system_clock::now();
auto Mat3 = matrix_demo2 * matrix_demo;
auto end = std::chrono::system_clock::now();
std::chrono::duration<double> time = (end - start);
std::cout << "Strassen time: " << time.count() << "s" << std::endl;
start = std::chrono::system_clock::now();
auto conf = matrix_demo2.naive_multiplication(matrix_demo);
end = std::chrono::system_clock::now();
time = end - start;
std::cout << "Normal time: " << time.count() << "s" << std::endl;
// std::cout << Mat3 << conf << std::endl;
assert(Mat3 == conf);
}
/**
* @brief main function
* @returns 0 on exit
*/
int main() {
test(); // run self-test implementation
return 0;
}

View File

@ -16,8 +16,8 @@
#include <cassert> /// for std::assert
#include <iostream> /// for IO operations
#include <vector> /// for std::vector
#include <unordered_map> /// for unordered map
#include <unordered_map> /// for unordered map
#include <vector> /// for std::vector
/**
* @namespace dynamic_programming
@ -40,26 +40,24 @@ namespace subset_sum {
* @param dp the map storing the results
* @returns true/false based on if the target sum subset exists or not.
*/
bool subset_sum_recursion(
const std::vector<int> &arr,
int targetSum,
std::vector<std::unordered_map<int, bool>> *dp,
int index = 0) {
if(targetSum == 0) { // Found a valid subset with required sum.
bool subset_sum_recursion(const std::vector<int> &arr, int targetSum,
std::vector<std::unordered_map<int, bool>> *dp,
int index = 0) {
if (targetSum == 0) { // Found a valid subset with required sum.
return true;
}
if(index == arr.size()) { // End of array
if (index == arr.size()) { // End of array
return false;
}
if ((*dp)[index].count(targetSum)) { // Answer already present in map
if ((*dp)[index].count(targetSum)) { // Answer already present in map
return (*dp)[index][targetSum];
}
bool ans = subset_sum_recursion(arr, targetSum - arr[index], dp, index + 1)
|| subset_sum_recursion(arr, targetSum, dp, index + 1);
(*dp)[index][targetSum] = ans; // Save ans in dp map.
bool ans =
subset_sum_recursion(arr, targetSum - arr[index], dp, index + 1) ||
subset_sum_recursion(arr, targetSum, dp, index + 1);
(*dp)[index][targetSum] = ans; // Save ans in dp map.
return ans;
}
@ -84,9 +82,9 @@ bool subset_sum_problem(const std::vector<int> &arr, const int targetSum) {
static void test() {
// custom input vector
std::vector<std::vector<int>> custom_input_arr(3);
custom_input_arr[0] = std::vector<int> {1, -10, 2, 31, -6};
custom_input_arr[1] = std::vector<int> {2, 3, 4};
custom_input_arr[2] = std::vector<int> {0, 1, 0, 1, 0};
custom_input_arr[0] = std::vector<int>{1, -10, 2, 31, -6};
custom_input_arr[1] = std::vector<int>{2, 3, 4};
custom_input_arr[2] = std::vector<int>{0, 1, 0, 1, 0};
std::vector<int> custom_input_target_sum(3);
custom_input_target_sum[0] = -14;

View File

@ -3,27 +3,29 @@
* @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
@ -32,7 +34,8 @@
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,12 +44,12 @@ 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;
}
/**
@ -55,109 +58,113 @@ int findParent(std::vector<std::pair<int,int>> parent, const int v) {
* @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) {
std::vector<std::pair<int, int>> smallest_edge(
size, std::make_pair(-1, -1)); // Pairing: start node, end node
// Repeat until all are in a single group
while (total_groups > 1) {
// 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;
}
std::vector<std::pair<int,int>> smallest_edge(size, std::make_pair(-1, -1)); //Pairing: start node, end node
// 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);
// 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 (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;
if (adj[i][j] == INT_MAX || adj[i][j] == 0) { // No connection
continue;
}
// 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;
}
// 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);
// Does the same for the second group
start = smallest_edge[parentB].first;
end = smallest_edge[parentB].second;
if (parentA != parentB) {
if (start == -1 || adj[j][i] < adj[start][end]) {
smallest_edge[parentB].first = j;
smallest_edge[parentB].second = i;
}
}
}
}
// 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;
// Step 3: Combine the groups based off their smallest edge
// 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;
}
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;
// Does the same for the second group
start = smallest_edge[parentB].first;
end = smallest_edge[parentB].second;
// Parents of the two groups - A is always itself
int parentA = i;
int parentB = findParent(parent, end);
if (start == -1 || adj[j][i] < adj[start][end]) {
smallest_edge[parentB].first = j;
smallest_edge[parentB].second = i;
}
}
}
}
// 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;
}
// Step 3: Combine the groups based off their smallest edge
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;
}

52
range_queries/sparse_table.cpp Executable file → Normal file
View File

@ -1,21 +1,23 @@
/**
* @file sparse_table.cpp
* @brief Implementation of [Sparse Table](https://en.wikipedia.org/wiki/Range_minimum_query) data structure
* @brief Implementation of [Sparse
* Table](https://en.wikipedia.org/wiki/Range_minimum_query) data structure
*
* @details
* Sparse Table is a data structure, that allows answering range queries.
* It can answer most range queries in O(logn), but its true power is answering range minimum queries
* or equivalent range maximum queries). For those queries it can compute the answer in O(1) time.
* It can answer most range queries in O(logn), but its true power is answering
* range minimum queries or equivalent range maximum queries). For those queries
* it can compute the answer in O(1) time.
*
* * Running Time Complexity \n
* * Build : O(NlogN) \n
* * Range Query : O(1) \n
*/
*/
#include <vector>
#include <algorithm>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <vector>
/**
* @namespace range_queries
@ -26,19 +28,19 @@ namespace range_queries {
* @namespace sparse_table
* @brief Range queries using sparse-tables
*/
namespace sparse_table {
namespace sparse_table {
/**
* This function precomputes intial log table for further use.
* @param n value of the size of the input array
* @return corresponding vector of the log table
*/
template<typename T>
template <typename T>
std::vector<T> computeLogs(const std::vector<T>& A) {
int n = A.size();
std::vector<T> logs(n);
logs[1] = 0;
for (int i = 2 ; i < n ; i++) {
logs[i] = logs[i/2] + 1;
for (int i = 2; i < n; i++) {
logs[i] = logs[i / 2] + 1;
}
return logs;
}
@ -50,19 +52,20 @@ std::vector<T> computeLogs(const std::vector<T>& A) {
* @param logs array of the log table
* @return created sparse table data structure
*/
template<typename T>
std::vector<std::vector<T> > buildTable(const std::vector<T>& A, const std::vector<T>& logs) {
template <typename T>
std::vector<std::vector<T> > buildTable(const std::vector<T>& A,
const std::vector<T>& logs) {
int n = A.size();
std::vector<std::vector<T> > table(20, std::vector<T>(n+5, 0));
std::vector<std::vector<T> > table(20, std::vector<T>(n + 5, 0));
int curLen = 0;
for (int i = 0 ; i <= logs[n] ; i++) {
for (int i = 0; i <= logs[n]; i++) {
curLen = 1 << i;
for (int j = 0 ; j + curLen < n ; j++) {
for (int j = 0; j + curLen < n; j++) {
if (curLen == 1) {
table[i][j] = A[j];
}
else {
table[i][j] = std::min(table[i-1][j], table[i-1][j + curLen/2]);
} else {
table[i][j] =
std::min(table[i - 1][j], table[i - 1][j + curLen / 2]);
}
}
}
@ -77,14 +80,15 @@ std::vector<std::vector<T> > buildTable(const std::vector<T>& A, const std::vect
* @param table sparse table data structure for the input array
* @return minimum value for the [beg, end] range for the input array
*/
template<typename T>
int getMinimum(int beg, int end, const std::vector<T>& logs, const std::vector<std::vector<T> >& table) {
template <typename T>
int getMinimum(int beg, int end, const std::vector<T>& logs,
const std::vector<std::vector<T> >& table) {
int p = logs[end - beg + 1];
int pLen = 1 << p;
return std::min(table[p][beg], table[p][end - pLen + 1]);
}
}
} // namespace range_queries
} // namespace sparse_table
} // namespace range_queries
/**
* Main function
@ -92,10 +96,10 @@ int getMinimum(int beg, int end, const std::vector<T>& logs, const std::vector<s
int main() {
std::vector<int> A{1, 2, 0, 3, 9};
std::vector<int> logs = range_queries::sparse_table::computeLogs(A);
std::vector<std::vector<int> > table = range_queries::sparse_table::buildTable(A, logs);
std::vector<std::vector<int> > table =
range_queries::sparse_table::buildTable(A, logs);
assert(range_queries::sparse_table::getMinimum(0, 0, logs, table) == 1);
assert(range_queries::sparse_table::getMinimum(0, 4, logs, table) == 0);
assert(range_queries::sparse_table::getMinimum(2, 4, logs, table) == 0);
return 0;
}