diff --git a/DIRECTORY.md b/DIRECTORY.md index 148776952..ada2e977b 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -20,6 +20,7 @@ * [Find Non Repeating Number](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/find_non_repeating_number.cpp) * [Hamming Distance](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/hamming_distance.cpp) * [Set Kth Bit](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/set_kth_bit.cpp) + * [Travelling Salesman Using Bit Manipulation](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/bit_manipulation/travelling_salesman_using_bit_manipulation.cpp) ## Ciphers * [A1Z26 Cipher](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/ciphers/a1z26_cipher.cpp) @@ -66,7 +67,7 @@ * [Reverse A Linked List](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/data_structures/reverse_a_linked_list.cpp) * [Skip List](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/data_structures/skip_list.cpp) * [Sparse Table](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/data_structures/sparse_table.cpp) - * [Stack](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/data_structures/stack.h) + * [Stack](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/data_structures/stack.hpp) * [Stack Using Array](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/data_structures/stack_using_array.cpp) * [Stack Using Linked List](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/data_structures/stack_using_linked_list.cpp) * [Stack Using Queue](https://github.com/TheAlgorithms/C-Plus-Plus/blob/HEAD/data_structures/stack_using_queue.cpp) diff --git a/bit_manipulation/travelling_salesman_using_bit_manipulation.cpp b/bit_manipulation/travelling_salesman_using_bit_manipulation.cpp new file mode 100644 index 000000000..fb708be2a --- /dev/null +++ b/bit_manipulation/travelling_salesman_using_bit_manipulation.cpp @@ -0,0 +1,119 @@ +/** + * @file + * @brief Implementation to + * [Travelling Salesman problem using bit-masking] + * (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. + * + * Explanation: + * 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 + * + * Worst Case Time Complexity: O(n^2 * 2^n) + * Space complexity: O(n) + * @author [Utkarsh Yadav](https://github.com/Rytnix) + */ +#include /// for std::min +#include /// for assert +#include /// for IO operations +#include /// for std::vector +#include /// for limits of integral types + +/** + * @namespace bit_manipulation + * @brief Bit manipulation algorithms + */ +namespace bit_manipulation { +/** + * @namespace travellingSalesman_bitmanipulation + * @brief Functions for the [Travelling Salesman + * Bitmask](https://www.geeksforgeeks.org/travelling-salesman-problem-set-1/) + * implementation + */ +namespace travelling_salesman_using_bit_manipulation { +/** + * @brief The function implements travellingSalesman using bitmanipulation + * @param dist is the cost to reach between two cities/nodes + * @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 + */ +std::uint64_t travelling_salesman_using_bit_manipulation(std::vector> 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> &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. + + if (dp[setOfCities][city] != -1) + return dp[setOfCities][city]; + //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. + ans = std::min(ans, subProb); + } + + } + dp[setOfCities][city] = ans; + return ans; +} +} // namespace travelling_salesman_using_bit_manipulation +} // namespace bit_manipulation + +/** + * @brief Self-test implementations + * @returns void + */ +static void test() { + // 1st test-case + std::vector> dist = { + {0, 20, 42, 35}, {20, 0, 30, 34}, {42, 30, 0, 12}, {35, 34, 12, 0} + }; + uint32_t V = dist.size(); + std::vector> dp(1 << V, std::vector(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"; + + // 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> dp1(1 << V, std::vector(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} + }; + V = dist.size(); + std::vector> dp2(1 << V, std::vector(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"; + +} + +/** + * @brief Main function + * @returns 0 on exit + */ +int main() { + test(); // run self-test implementations + return 0; +} diff --git a/data_structures/stack.h b/data_structures/stack.h deleted file mode 100644 index 483648046..000000000 --- a/data_structures/stack.h +++ /dev/null @@ -1,150 +0,0 @@ -/** - * @file stack.h - * @author danghai - * @brief This class specifies the basic operation on a stack as a linked list - **/ -#ifndef DATA_STRUCTURES_STACK_H_ -#define DATA_STRUCTURES_STACK_H_ - -#include -#include - -/** Definition of the node as a linked-list - * \tparam Type type of data nodes of the linked list should contain - */ -template -struct node { - Type data; ///< data at current node - node *next; ///< pointer to the next ::node instance -}; - -/** Definition of the stack class - * \tparam Type type of data nodes of the linked list in the stack should - * contain - */ -template -class stack { - public: - /** Show stack */ - void display() { - node *current = stackTop; - std::cout << "Top --> "; - while (current != nullptr) { - std::cout << current->data << " "; - current = current->next; - } - std::cout << std::endl; - std::cout << "Size of stack: " << size << std::endl; - } - - /** Default constructor*/ - stack() { - stackTop = nullptr; - size = 0; - } - - /** Copy constructor*/ - explicit stack(const stack &otherStack) { - node *newNode, *current, *last; - - /* If stack is no empty, make it empty */ - if (stackTop != nullptr) { - stackTop = nullptr; - } - if (otherStack.stackTop == nullptr) { - stackTop = nullptr; - } else { - current = otherStack.stackTop; - stackTop = new node; - stackTop->data = current->data; - stackTop->next = nullptr; - last = stackTop; - current = current->next; - /* Copy the remaining stack */ - while (current != nullptr) { - newNode = new node; - newNode->data = current->data; - newNode->next = nullptr; - last->next = newNode; - last = newNode; - current = current->next; - } - } - size = otherStack.size; - } - - /** Destructor */ - ~stack() {} - - /** Determine whether the stack is empty */ - bool isEmptyStack() { return (stackTop == nullptr); } - - /** Add new item to the stack */ - void push(Type item) { - node *newNode; - newNode = new node; - newNode->data = item; - newNode->next = stackTop; - stackTop = newNode; - size++; - } - - /** Return the top element of the stack */ - Type top() { - assert(stackTop != nullptr); - return stackTop->data; - } - - /** Remove the top element of the stack */ - void pop() { - node *temp; - if (!isEmptyStack()) { - temp = stackTop; - stackTop = stackTop->next; - delete temp; - size--; - } else { - std::cout << "Stack is empty !" << std::endl; - } - } - - /** Clear stack */ - void clear() { stackTop = nullptr; } - - /** Overload "=" the assignment operator */ - stack &operator=(const stack &otherStack) { - node *newNode, *current, *last; - - /* If stack is no empty, make it empty */ - if (stackTop != nullptr) { - stackTop = nullptr; - } - if (otherStack.stackTop == nullptr) { - stackTop = nullptr; - } else { - current = otherStack.stackTop; - stackTop = new node; - stackTop->data = current->data; - stackTop->next = nullptr; - last = stackTop; - current = current->next; - /* Copy the remaining stack */ - while (current != nullptr) { - newNode = new node; - newNode->data = current->data; - newNode->next = nullptr; - last->next = newNode; - last = newNode; - current = current->next; - } - } - size = otherStack.size; - return *this; - } - - private: - node *stackTop; /**< Pointer to the stack */ - int size; ///< size of stack -}; - -#endif // DATA_STRUCTURES_STACK_H_ diff --git a/data_structures/stack.hpp b/data_structures/stack.hpp new file mode 100644 index 000000000..419a07c67 --- /dev/null +++ b/data_structures/stack.hpp @@ -0,0 +1,106 @@ +/** + * @file + * @author danghai + * @author [Piotr Idzik](https://github.com/vil02) + * @brief This class specifies the basic operation on a stack as a linked list + **/ +#ifndef DATA_STRUCTURES_STACK_HPP_ +#define DATA_STRUCTURES_STACK_HPP_ + +#include /// for IO operations +#include /// for std::shared_ptr +#include /// for std::invalid_argument +#include /// for std::vector + +/** Definition of the node as a linked-list + * \tparam ValueType type of data nodes of the linked list should contain + */ +template +struct node { + ValueType data = {}; ///< data at current node + std::shared_ptr> next = + {}; ///< pointer to the next ::node instance +}; + +template +void traverse(const Node* const inNode, const Action& action) { + if (inNode) { + action(*inNode); + traverse(inNode->next.get(), action); + } +} + +/** Definition of the stack class + * \tparam value_type type of data nodes of the linked list in the stack should + * contain + */ +template +class stack { + public: + using value_type = ValueType; + + /** Show stack */ + void display() const { + std::cout << "Top --> "; + traverse(stackTop.get(), [](const node& inNode) { + std::cout << inNode.data << " "; + }); + std::cout << std::endl; + std::cout << "Size of stack: " << size << std::endl; + } + + std::vector toVector() const { + std::vector res; + res.reserve(this->size); + traverse(stackTop.get(), [&res](const node& inNode) { + res.push_back(inNode.data); + }); + return res; + } + + private: + void ensureNotEmpty() const { + if (isEmptyStack()) { + throw std::invalid_argument("Stack is empty."); + } + } + + public: + /** Determine whether the stack is empty */ + bool isEmptyStack() const { return (stackTop == nullptr); } + + /** Add new item to the stack */ + void push(const value_type& item) { + auto newNode = std::make_shared>(); + newNode->data = item; + newNode->next = stackTop; + stackTop = newNode; + size++; + } + + /** Return the top element of the stack */ + value_type top() const { + ensureNotEmpty(); + return stackTop->data; + } + + /** Remove the top element of the stack */ + void pop() { + ensureNotEmpty(); + stackTop = stackTop->next; + size--; + } + + /** Clear stack */ + void clear() { + stackTop = nullptr; + size = 0; + } + + private: + std::shared_ptr> stackTop = + {}; /**< Pointer to the stack */ + std::size_t size = 0; ///< size of stack +}; + +#endif // DATA_STRUCTURES_STACK_HPP_ diff --git a/data_structures/test_stack.cpp b/data_structures/test_stack.cpp index aa636916f..dfff0b50e 100644 --- a/data_structures/test_stack.cpp +++ b/data_structures/test_stack.cpp @@ -1,59 +1,203 @@ -#include +#include /// for assert +#include /// for std::cout +#include /// std::invalid_argument +#include /// for std::vector -#include "./stack.h" +#include "./stack.hpp" + +template +void testConstructedStackIsEmpty() { + const stack curStack; + assert(curStack.isEmptyStack()); +} + +void testPush() { + using valueType = int; + stack curStack; + curStack.push(10); + curStack.push(20); + curStack.push(30); + curStack.push(40); + const auto expectedData = std::vector({40, 30, 20, 10}); + assert(curStack.toVector() == expectedData); +} + +void testTop() { + using valueType = unsigned; + stack curStack; + curStack.push(1); + curStack.push(2); + curStack.push(3); + curStack.push(4); + assert(curStack.top() == static_cast(4)); +} + +void testPop() { + using valueType = int; + stack curStack; + curStack.push(100); + curStack.push(200); + curStack.push(300); + + assert(curStack.top() == static_cast(300)); + curStack.pop(); + assert(curStack.top() == static_cast(200)); + curStack.pop(); + assert(curStack.top() == static_cast(100)); + curStack.pop(); + assert(curStack.isEmptyStack()); +} + +void testClear() { + stack curStack; + curStack.push(1000); + curStack.push(2000); + curStack.clear(); + assert(curStack.isEmptyStack()); +} + +void testCopyOfStackHasSameData() { + stack stackA; + stackA.push(10); + stackA.push(200); + stackA.push(3000); + const auto stackB(stackA); + assert(stackA.toVector() == stackB.toVector()); +} + +void testPushingToCopyDoesNotChangeOriginal() { + using valueType = int; + stack stackA; + stackA.push(10); + stackA.push(20); + stackA.push(30); + auto stackB(stackA); + stackB.push(40); + + const auto expectedDataA = std::vector({30, 20, 10}); + const auto expectedDataB = std::vector({40, 30, 20, 10}); + + assert(stackA.toVector() == expectedDataA); + assert(stackB.toVector() == expectedDataB); +} + +void testPoppingFromCopyDoesNotChangeOriginal() { + using valueType = int; + stack stackA; + stackA.push(10); + stackA.push(20); + stackA.push(30); + auto stackB(stackA); + stackB.pop(); + + const auto expectedDataA = std::vector({30, 20, 10}); + const auto expectedDataB = std::vector({20, 10}); + + assert(stackA.toVector() == expectedDataA); + assert(stackB.toVector() == expectedDataB); +} + +void testPushingToOrginalDoesNotChangeCopy() { + using valueType = int; + stack stackA; + stackA.push(10); + stackA.push(20); + stackA.push(30); + const auto stackB(stackA); + stackA.push(40); + + const auto expectedDataA = std::vector({40, 30, 20, 10}); + const auto expectedDataB = std::vector({30, 20, 10}); + + assert(stackA.toVector() == expectedDataA); + assert(stackB.toVector() == expectedDataB); +} + +void testPoppingFromOrginalDoesNotChangeCopy() { + using valueType = int; + stack stackA; + stackA.push(10); + stackA.push(20); + stackA.push(30); + const auto stackB(stackA); + stackA.pop(); + + const auto expectedDataA = std::vector({20, 10}); + const auto expectedDataB = std::vector({30, 20, 10}); + + assert(stackA.toVector() == expectedDataA); + assert(stackB.toVector() == expectedDataB); +} + +void testAssign() { + using valueType = int; + stack stackA; + stackA.push(10); + stackA.push(20); + stackA.push(30); + stack stackB = stackA; + stackA.pop(); + stackB.push(40); + + const auto expectedDataA = std::vector({20, 10}); + const auto expectedDataB = std::vector({40, 30, 20, 10}); + + assert(stackA.toVector() == expectedDataA); + assert(stackB.toVector() == expectedDataB); + + stackB = stackA; + stackA.pop(); + stackB.push(5); + stackB.push(6); + + const auto otherExpectedDataA = std::vector({10}); + const auto otherExpectedDataB = std::vector({6, 5, 20, 10}); + + assert(stackA.toVector() == otherExpectedDataA); + assert(stackB.toVector() == otherExpectedDataB); +} + +void testTopThrowsAnvalidArgumentWhenStackEmpty() { + const stack curStack; + bool wasException = false; + try { + curStack.top(); + } catch (const std::invalid_argument&) { + wasException = true; + } + assert(wasException); +} + +void testPopThrowsAnvalidArgumentWhenStackEmpty() { + stack curStack; + bool wasException = false; + try { + curStack.pop(); + } catch (const std::invalid_argument&) { + wasException = true; + } + assert(wasException); +} int main() { - stack stk; - std::cout << "---------------------- Test construct ----------------------" - << std::endl; - stk.display(); - std::cout - << "---------------------- Test isEmptyStack ----------------------" - << std::endl; - if (stk.isEmptyStack()) - std::cout << "PASS" << std::endl; - else - std::cout << "FAIL" << std::endl; - std::cout << "---------------------- Test push ----------------------" - << std::endl; - std::cout << "After pushing 10 20 30 40 into stack: " << std::endl; - stk.push(10); - stk.push(20); - stk.push(30); - stk.push(40); - stk.display(); - std::cout << "---------------------- Test top ----------------------" - << std::endl; - int value = stk.top(); - if (value == 40) - std::cout << "PASS" << std::endl; - else - std::cout << "FAIL" << std::endl; - std::cout << "---------------------- Test pop ----------------------" - << std::endl; - stk.display(); - stk.pop(); - stk.pop(); - std::cout << "After popping 2 times: " << std::endl; - stk.display(); - std::cout << "---------------------- Test overload = operator " - "----------------------" - << std::endl; - stack stk1; - std::cout << "stk current: " << std::endl; - stk.display(); - std::cout << std::endl << "Assign stk1 = stk " << std::endl; - stk1 = stk; - stk1.display(); - std::cout << std::endl << "After pushing 8 9 10 into stk1:" << std::endl; - stk1.push(8); - stk1.push(9); - stk1.push(10); - stk1.display(); - std::cout << std::endl << "stk current: " << std::endl; - stk.display(); - std::cout << "Assign back stk = stk1:" << std::endl; - stk = stk1; - stk.display(); + testConstructedStackIsEmpty(); + testConstructedStackIsEmpty(); + + testPush(); + testPop(); + testClear(); + + testCopyOfStackHasSameData(); + testPushingToCopyDoesNotChangeOriginal(); + testPoppingFromCopyDoesNotChangeOriginal(); + testPushingToOrginalDoesNotChangeCopy(); + testPoppingFromOrginalDoesNotChangeCopy(); + + testAssign(); + + testTopThrowsAnvalidArgumentWhenStackEmpty(); + testPopThrowsAnvalidArgumentWhenStackEmpty(); + + std::cout << "All tests pass!\n"; return 0; } diff --git a/data_structures/test_stack_students.cpp b/data_structures/test_stack_students.cpp index a6048c4c9..dc3514bcb 100644 --- a/data_structures/test_stack_students.cpp +++ b/data_structures/test_stack_students.cpp @@ -9,16 +9,17 @@ ************************************************************ * */ #include +#include #include #include #include #include -#include "./stack.h" +#include "./stack.hpp" int main(int argc, char* argv[]) { - double GPA; - double highestGPA; + double GPA = NAN; + double highestGPA = NAN; std::string name; assert(argc == 2); diff --git a/dynamic_programming/subset_sum.cpp b/dynamic_programming/subset_sum.cpp new file mode 100644 index 000000000..897fc9a96 --- /dev/null +++ b/dynamic_programming/subset_sum.cpp @@ -0,0 +1,125 @@ +/** + * @file + * @brief Implements [Sub-set sum problem] + * (https://en.wikipedia.org/wiki/Subset_sum_problem) algorithm, which tells + * whether a subset with target sum exists or not. + * + * @details + * In this problem, we use dynamic programming to find if we can pull out a + * subset from an array whose sum is equal to a given target sum. The overall + * time complexity of the problem is O(n * targetSum) where n is the size of + * the array. For example, array = [1, -10, 2, 31, -6], targetSum = -14. + * Output: true => We can pick subset [-10, 2, -6] with sum as + * (-10) + 2 + (-6) = -14. + * @author [KillerAV](https://github.com/KillerAV) + */ + +#include /// for std::assert +#include /// for IO operations +#include /// for std::vector +#include /// for unordered map + +/** + * @namespace dynamic_programming + * @brief Dynamic Programming algorithms + */ +namespace dynamic_programming { + +/** + * @namespace subset_sum + * @brief Functions for [Sub-set sum problem] + * (https://en.wikipedia.org/wiki/Subset_sum_problem) algorithm + */ +namespace subset_sum { + +/** + * Recursive function using dynamic programming to find if the required sum + * subset exists or not. + * @param arr input array + * @param targetSum the target sum of the subset + * @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 &arr, + int targetSum, + std::vector> *dp, + int index = 0) { + + if(targetSum == 0) { // Found a valid subset with required sum. + return true; + } + if(index == arr.size()) { // End of array + return false; + } + + 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. + return ans; +} + +/** + * Function implementing subset sum algorithm using top-down approach + * @param arr input array + * @param targetSum the target sum of the subset + * @returns true/false based on if the target sum subset exists or not. + */ +bool subset_sum_problem(const std::vector &arr, const int targetSum) { + size_t n = arr.size(); + std::vector> dp(n); + return subset_sum_recursion(arr, targetSum, &dp); +} +} // namespace subset_sum +} // namespace dynamic_programming + +/** + * @brief Test Function + * @return void + */ +static void test() { + // custom input vector + std::vector> custom_input_arr(3); + custom_input_arr[0] = std::vector {1, -10, 2, 31, -6}; + custom_input_arr[1] = std::vector {2, 3, 4}; + custom_input_arr[2] = std::vector {0, 1, 0, 1, 0}; + + std::vector custom_input_target_sum(3); + custom_input_target_sum[0] = -14; + custom_input_target_sum[1] = 10; + custom_input_target_sum[2] = 2; + + // calculated output vector by pal_part Function + std::vector calculated_output(3); + + for (int i = 0; i < 3; i++) { + calculated_output[i] = + dynamic_programming::subset_sum::subset_sum_problem( + custom_input_arr[i], custom_input_target_sum[i]); + } + + // expected output vector + std::vector expected_output{true, false, true}; + + // Testing implementation via assert function + // It will throw error if any of the expected test fails + // Else it will give nothing + for (int i = 0; i < 3; i++) { + assert(expected_output[i] == calculated_output[i]); + } + + std::cout << "All tests passed successfully!\n"; +} + +/** + * @brief Main function + * @returns 0 on exit + */ +int main() { + test(); // execute the test + return 0; +} diff --git a/greedy_algorithms/boruvkas_minimum_spanning_tree.cpp b/greedy_algorithms/boruvkas_minimum_spanning_tree.cpp new file mode 100644 index 000000000..c5a036049 --- /dev/null +++ b/greedy_algorithms/boruvkas_minimum_spanning_tree.cpp @@ -0,0 +1,222 @@ +/** + * @author [Jason Nardoni](https://github.com/JNardoni) + * @file + * + * @brief + * [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. + * 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 + * +*/ + +#include /// for IO operations +#include /// for std::vector +#include /// for assert +#include /// for INT_MAX + +/** + * @namespace greedy_algorithms + * @brief 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 + */ +namespace boruvkas_minimum_spanning_tree { +/** + * @brief Recursively returns the vertex's parent at the root of the tree + * @param parent the array that will be checked + * @param v vertex to find parent of + * @returns the parent of the vertex + */ +int findParent(std::vector> parent, const int v) { + if (parent[v].first != v) { + parent[v].first = findParent(parent, parent[v].first); + } + + return parent[v].first; +} + +/** + * @brief the implementation of boruvka's algorithm + * @param adj a graph adjancency matrix stored as 2d vectors. + * @returns the MST as 2d vectors + */ +std::vector> boruvkas(std::vector> adj) { + + size_t size = adj.size(); + size_t total_groups = size; + + if (size <= 1) { + return adj; + } + + // Stores the current Minimum Spanning Tree. As groups are combined, they are added to the MST + std::vector> MST(size, std::vector(size, INT_MAX)); + for (int i = 0; i < size; i++) { + MST[i][i] = 0; + } + + // Step 1: Create a group for each vertex + + // Stores the parent of the vertex and its current depth, both initialized to 0 + std::vector> 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 + } + + // Repeat until all are in a single group + while (total_groups > 1) { + + std::vector> 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; + } + + // 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 (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 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; + } + + // Does the same for the second group + start = smallest_edge[parentB].first; + end = smallest_edge[parentB].second; + + if (start == -1 || adj[j][i] < adj[start][end]) { + smallest_edge[parentB].first = j; + smallest_edge[parentB].second = i; + } + } + } + } + + // 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; +} + +/** + * @brief counts the sum of edges in the given tree + * @param adj 2D vector adjacency matrix + * @returns the int size of the tree + */ +int test_findGraphSum(std::vector> adj) { + + 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; +} +} // namespace boruvkas_minimum_spanning_tree +} // namespace greedy_algorithms + +/** + * @brief Self-test implementations + * @returns void +*/ +static void tests() { + std::cout << "Starting tests...\n\n"; + std::vector> 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> 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; +} + +/** + * @brief Main function + * @returns 0 on exit + */ +int main() { + tests(); // run self-test implementations + return 0; +} diff --git a/math/aliquot_sum.cpp b/math/aliquot_sum.cpp new file mode 100644 index 000000000..79be25571 --- /dev/null +++ b/math/aliquot_sum.cpp @@ -0,0 +1,68 @@ +/** + * @file + * @brief Program to return the [Aliquot + * Sum](https://en.wikipedia.org/wiki/Aliquot_sum) of a number + * + * \details + * The Aliquot sum s(n) of a non-negative integer n is the sum of all + * proper divisors of n, that is, all the divisors of n, other than itself. + * For example, the Aliquot sum of 18 = 1 + 2 + 3 + 6 + 9 = 21 + * + * @author [SpiderMath](https://github.com/SpiderMath) + */ + +#include /// for assert +#include /// for IO operations + +/** + * @brief Mathematical algorithms + * @namespace math + */ +namespace math { +/** + * Function to return the aliquot sum of a number + * @param num The input number + */ +uint64_t aliquot_sum(const uint64_t num) { + if (num == 0 || num == 1) { + return 0; // The aliquot sum for 0 and 1 is 0 + } + + uint64_t sum = 0; + + for (uint64_t i = 1; i <= num / 2; i++) { + if (num % i == 0) { + sum += i; + } + } + + return sum; +} +} // namespace math + +/** + * @brief Self-test implementations + * @returns void + */ +static void test() { + // Aliquot sum of 10 is 1 + 2 + 5 = 8 + assert(math::aliquot_sum(10) == 8); + // Aliquot sum of 15 is 1 + 3 + 5 = 9 + assert(math::aliquot_sum(15) == 9); + // Aliquot sum of 1 is 0 + assert(math::aliquot_sum(1) == 0); + // Aliquot sum of 97 is 1 (the aliquot sum of a prime number is 1) + assert(math::aliquot_sum(97) == 1); + + std::cout << "All the tests have successfully passed!\n"; +} + +/** + * @brief Main function + * @returns 0 on exit + */ +int main() { + test(); // run the self-test implementations + + return 0; +}