Skip to content

Commit

Permalink
feat: Add the Subarray Sum implementation (TheAlgorithms#1527)
Browse files Browse the repository at this point in the history
* Create subarray_sum.cpp

* updating DIRECTORY.md

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

* Update backtracking/subarray_sum.cpp

Co-authored-by: David Leal <[email protected]>

* clang-format and clang-tidy fixes for f37f7b7

* Update backtracking/subarray_sum.cpp

Co-authored-by: David Leal <[email protected]>

* Update backtracking/subarray_sum.cpp

Co-authored-by: David Leal <[email protected]>

* Update subarray_sum.cpp

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

* Update backtracking/subarray_sum.cpp

* Update backtracking/subarray_sum.cpp

Co-authored-by: David Leal <[email protected]>

* Update backtracking/subarray_sum.cpp

Co-authored-by: David Leal <[email protected]>

* Update backtracking/subarray_sum.cpp

Co-authored-by: David Leal <[email protected]>

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

* Update subarray_sum.cpp

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

* Update backtracking/subarray_sum.cpp

Co-authored-by: David Leal <[email protected]>

* Update backtracking/subarray_sum.cpp

Co-authored-by: David Leal <[email protected]>

* Update backtracking/subarray_sum.cpp

Co-authored-by: David Leal <[email protected]>

* Update backtracking/subarray_sum.cpp

Co-authored-by: David Leal <[email protected]>

* fix: Apply suggestions from code review

* docs: Apply suggestions from code review

* clang-format and clang-tidy fixes for e697904

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
Co-authored-by: David Leal <[email protected]>
  • Loading branch information
3 people authored Jul 21, 2021
1 parent 0e0ba5f commit f34f93e
Show file tree
Hide file tree
Showing 5 changed files with 540 additions and 353 deletions.
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* [N Queens All Solution Optimised](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/backtracking/n_queens_all_solution_optimised.cpp)
* [Nqueen Print All Solutions](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/backtracking/nqueen_print_all_solutions.cpp)
* [Rat Maze](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/backtracking/rat_maze.cpp)
* [Subarray Sum](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/backtracking/subarray_sum.cpp)
* [Subset Sum](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/backtracking/subset_sum.cpp)
* [Sudoku Solve](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/backtracking/sudoku_solve.cpp)

Expand Down
139 changes: 70 additions & 69 deletions backtracking/graph_coloring.cpp
Original file line number Diff line number Diff line change
@@ -1,93 +1,97 @@
/**
* @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>

/**
* @namespace
* @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;
}
/** 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;
}
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;
}
/** 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;
// 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);
// 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;
}
// If assigning color c doesn't lead to a solution then remove it
color[v] = 0;
}
}
}
} // namespace backtracking

/**
Expand All @@ -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;
Expand Down
117 changes: 117 additions & 0 deletions backtracking/subarray_sum.cpp
Original file line number Diff line number Diff line change
@@ -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;
}
Loading

0 comments on commit f34f93e

Please sign in to comment.