feat: Add the Subarray Sum implementation (#1527)

* Create subarray_sum.cpp

* updating DIRECTORY.md

* clang-format and clang-tidy fixes for 0a293ece

* Update backtracking/subarray_sum.cpp

Co-authored-by: David Leal <halfpacho@gmail.com>

* clang-format and clang-tidy fixes for f37f7b7c

* Update backtracking/subarray_sum.cpp

Co-authored-by: David Leal <halfpacho@gmail.com>

* Update backtracking/subarray_sum.cpp

Co-authored-by: David Leal <halfpacho@gmail.com>

* Update subarray_sum.cpp

* clang-format and clang-tidy fixes for 9b0b5f87

* Update backtracking/subarray_sum.cpp

* Update backtracking/subarray_sum.cpp

Co-authored-by: David Leal <halfpacho@gmail.com>

* Update backtracking/subarray_sum.cpp

Co-authored-by: David Leal <halfpacho@gmail.com>

* Update backtracking/subarray_sum.cpp

Co-authored-by: David Leal <halfpacho@gmail.com>

* clang-format and clang-tidy fixes for 047366a8

* Update subarray_sum.cpp

* clang-format and clang-tidy fixes for 512b1887

* Update backtracking/subarray_sum.cpp

Co-authored-by: David Leal <halfpacho@gmail.com>

* Update backtracking/subarray_sum.cpp

Co-authored-by: David Leal <halfpacho@gmail.com>

* Update backtracking/subarray_sum.cpp

Co-authored-by: David Leal <halfpacho@gmail.com>

* Update backtracking/subarray_sum.cpp

Co-authored-by: David Leal <halfpacho@gmail.com>

* fix: Apply suggestions from code review

* docs: Apply suggestions from code review

* clang-format and clang-tidy fixes for e6979047

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
Co-authored-by: David Leal <halfpacho@gmail.com>
This commit is contained in:
Swastika Gupta
2021-07-22 00:52:16 +05:30
committed by GitHub
parent 0e0ba5fc89
commit f34f93e77a
5 changed files with 549 additions and 362 deletions

View File

@@ -1,22 +1,24 @@
/**
* @file
* @brief prints the assigned colors
* using [Graph Coloring](https://en.wikipedia.org/wiki/Graph_coloring) algorithm
* using [Graph Coloring](https://en.wikipedia.org/wiki/Graph_coloring)
* algorithm
*
* @details
* In graph theory, graph coloring is a special case of graph labeling;
* it is an assignment of labels traditionally called "colors" to elements of a graph subject to certain constraints.
* In its simplest form, it is a way of coloring the vertices of a graph such that no two adjacent vertices are of the same color;
* this is called a vertex coloring. Similarly, an edge coloring assigns
* a color to each edge so that no two adjacent edges are of the same color,
* and a face coloring of a planar graph assigns a color to each face or
* In graph theory, graph coloring is a special case of graph labeling;
* it is an assignment of labels traditionally called "colors" to elements of a
* graph subject to certain constraints. In its simplest form, it is a way of
* coloring the vertices of a graph such that no two adjacent vertices are of
* the same color; this is called a vertex coloring. Similarly, an edge coloring
* assigns a color to each edge so that no two adjacent edges are of the same
* color, and a face coloring of a planar graph assigns a color to each face or
* region so that no two faces that share a boundary have the same color.
*
* @author [Anup Kumar Panwar](https://github.com/AnupKumarPanwar)
* @author [David Leal](https://github.com/Panquesito7)
*/
#include <iostream>
#include <array>
#include <iostream>
#include <vector>
/**
@@ -24,70 +26,72 @@
* @brief Backtracking algorithms
*/
namespace backtracking {
/** A utility function to print solution
* @tparam V number of vertices in the graph
* @param color array of colors assigned to the nodes
*/
template <size_t V>
void printSolution(const std::array <int, V>& color) {
std::cout << "Following are the assigned colors" << std::endl;
for (auto &col : color) {
std::cout << col;
}
std::cout << std::endl;
/** A utility function to print solution
* @tparam V number of vertices in the graph
* @param color array of colors assigned to the nodes
*/
template <size_t V>
void printSolution(const std::array<int, V>& color) {
std::cout << "Following are the assigned colors" << std::endl;
for (auto& col : color) {
std::cout << col;
}
std::cout << std::endl;
}
/** A utility function to check if the current color assignment is safe for
* vertex v
* @tparam V number of vertices in the graph
* @param v index of graph vertex to check
* @param graph matrix of graph nonnectivity
* @param color vector of colors assigned to the graph nodes/vertices
* @param c color value to check for the node `v`
* @returns `true` if the color is safe to be assigned to the node
* @returns `false` if the color is not safe to be assigned to the node
*/
template <size_t V>
bool isSafe(int v, const std::array<std::array <int, V>, V>& graph, const std::array <int, V>& color, int c) {
for (int i = 0; i < V; i++) {
if (graph[v][i] && c == color[i]) {
return false;
}
}
return true;
}
/** A recursive utility function to solve m coloring problem
* @tparam V number of vertices in the graph
* @param graph matrix of graph nonnectivity
* @param m number of colors
* @param [in,out] color description // used in,out to notify in documentation
* that this parameter gets modified by the function
* @param v index of graph vertex to check
*/
template <size_t V>
void graphColoring(const std::array<std::array <int, V>, V>& graph, int m, std::array <int, V> color, int v) {
// base case:
// If all vertices are assigned a color then return true
if (v == V) {
backtracking::printSolution<V>(color);
return;
}
// Consider this vertex v and try different colors
for (int c = 1; c <= m; c++) {
// Check if assignment of color c to v is fine
if (backtracking::isSafe<V>(v, graph, color, c)) {
color[v] = c;
// recur to assign colors to rest of the vertices
backtracking::graphColoring<V>(graph, m, color, v + 1);
// If assigning color c doesn't lead to a solution then remove it
color[v] = 0;
}
/** A utility function to check if the current color assignment is safe for
* vertex v
* @tparam V number of vertices in the graph
* @param v index of graph vertex to check
* @param graph matrix of graph nonnectivity
* @param color vector of colors assigned to the graph nodes/vertices
* @param c color value to check for the node `v`
* @returns `true` if the color is safe to be assigned to the node
* @returns `false` if the color is not safe to be assigned to the node
*/
template <size_t V>
bool isSafe(int v, const std::array<std::array<int, V>, V>& graph,
const std::array<int, V>& color, int c) {
for (int i = 0; i < V; i++) {
if (graph[v][i] && c == color[i]) {
return false;
}
}
return true;
}
/** A recursive utility function to solve m coloring problem
* @tparam V number of vertices in the graph
* @param graph matrix of graph nonnectivity
* @param m number of colors
* @param [in,out] color description // used in,out to notify in documentation
* that this parameter gets modified by the function
* @param v index of graph vertex to check
*/
template <size_t V>
void graphColoring(const std::array<std::array<int, V>, V>& graph, int m,
std::array<int, V> color, int v) {
// base case:
// If all vertices are assigned a color then return true
if (v == V) {
backtracking::printSolution<V>(color);
return;
}
// Consider this vertex v and try different colors
for (int c = 1; c <= m; c++) {
// Check if assignment of color c to v is fine
if (backtracking::isSafe<V>(v, graph, color, c)) {
color[v] = c;
// recur to assign colors to rest of the vertices
backtracking::graphColoring<V>(graph, m, color, v + 1);
// If assigning color c doesn't lead to a solution then remove it
color[v] = 0;
}
}
}
} // namespace backtracking
/**
@@ -102,15 +106,12 @@ int main() {
// (0)---(1)
const int V = 4; // number of vertices in the graph
std::array <std::array <int, V>, V> graph = {
std::array <int, V>({0, 1, 1, 1}),
std::array <int, V>({1, 0, 1, 0}),
std::array <int, V>({1, 1, 0, 1}),
std::array <int, V>({1, 0, 1, 0})
};
std::array<std::array<int, V>, V> graph = {
std::array<int, V>({0, 1, 1, 1}), std::array<int, V>({1, 0, 1, 0}),
std::array<int, V>({1, 1, 0, 1}), std::array<int, V>({1, 0, 1, 0})};
int m = 3; // Number of colors
std::array <int, V> color{};
std::array<int, V> color{};
backtracking::graphColoring<V>(graph, m, color, 0);
return 0;

View File

@@ -0,0 +1,117 @@
/**
* @file
* @brief [Subset-sum](https://en.wikipedia.org/wiki/Subset_sum_problem) (only
* continuous subsets) problem
* @details We are given an array and a sum value. The algorithms find all
* the subarrays of that array with sum equal to the given sum and return such
* subarrays count. This approach will have \f$O(n)\f$ time complexity and
* \f$O(n)\f$ space complexity. NOTE: In this problem, we are only referring to
* the continuous subsets as subarrays everywhere. Subarrays can be created
* using deletion operation at the end of the front of an array only. The parent
* array is also counted in subarrays having 0 number of deletion operations.
*
* @author [Swastika Gupta](https://github.com/Swastyy)
*/
#include <cassert> /// for assert
#include <iostream> /// for IO operations
#include <unordered_map> /// for unordered_map
#include <vector> /// for std::vector
/**
* @namespace backtracking
* @brief Backtracking algorithms
*/
namespace backtracking {
/**
* @namespace subarray_sum
* @brief Functions for the [Subset
* sum](https://en.wikipedia.org/wiki/Subset_sum_problem) implementation
*/
namespace subarray_sum {
/**
* @brief The main function that implements the count of the subarrays
* @param sum is the required sum of any subarrays
* @param in_arr is the input array
* @returns count of the number of subsets with required sum
*/
uint64_t subarray_sum(int64_t sum, const std::vector<int64_t> &in_arr) {
int64_t nelement = in_arr.size();
int64_t count_of_subset = 0;
int64_t current_sum = 0;
std::unordered_map<int64_t, int64_t>
sumarray; // to store the subarrays count
// frequency having some sum value
for (int64_t i = 0; i < nelement; i++) {
current_sum += in_arr[i];
if (current_sum == sum) {
count_of_subset++;
}
// If in case current_sum is greater than the required sum
if (sumarray.find(current_sum - sum) != sumarray.end()) {
count_of_subset += (sumarray[current_sum - sum]);
}
sumarray[current_sum]++;
}
return count_of_subset;
}
} // namespace subarray_sum
} // namespace backtracking
/**
* @brief Self-test implementations
* @returns void
*/
static void test() {
// 1st test
std::cout << "1st test ";
std::vector<int64_t> array1 = {-7, -3, -2, 5, 8}; // input array
assert(
backtracking::subarray_sum::subarray_sum(0, array1) ==
1); // first argument in subarray_sum function is the required sum and
// second is the input array, answer is the subarray {(-3,-2,5)}
std::cout << "passed" << std::endl;
// 2nd test
std::cout << "2nd test ";
std::vector<int64_t> array2 = {1, 2, 3, 3};
assert(backtracking::subarray_sum::subarray_sum(6, array2) ==
2); // here we are expecting 2 subsets which sum up to 6 i.e.
// {(1,2,3),(3,3)}
std::cout << "passed" << std::endl;
// 3rd test
std::cout << "3rd test ";
std::vector<int64_t> array3 = {1, 1, 1, 1};
assert(backtracking::subarray_sum::subarray_sum(1, array3) ==
4); // here we are expecting 4 subsets which sum up to 1 i.e.
// {(1),(1),(1),(1)}
std::cout << "passed" << std::endl;
// 4rd test
std::cout << "4th test ";
std::vector<int64_t> array4 = {3, 3, 3, 3};
assert(backtracking::subarray_sum::subarray_sum(6, array4) ==
3); // here we are expecting 3 subsets which sum up to 6 i.e.
// {(3,3),(3,3),(3,3)}
std::cout << "passed" << std::endl;
// 5th test
std::cout << "5th test ";
std::vector<int64_t> array5 = {};
assert(backtracking::subarray_sum::subarray_sum(6, array5) ==
0); // here we are expecting 0 subsets which sum up to 6 i.e. we
// cannot select anything from an empty array
std::cout << "passed" << std::endl;
}
/**
* @brief Main function
* @returns 0 on exit
*/
int main() {
test(); // run self-test implementations
return 0;
}