[feat/fix/docs]: Improvements in the...

...`backtracking` folder, and minor fixes in the `others/iterative_tree_traversals.cpp` and the `math/check_prime.cpp` files.
This commit is contained in:
David Leal
2021-08-05 13:47:05 -05:00
parent 97afa0e3e4
commit 9cc3951dba
10 changed files with 376 additions and 343 deletions

View File

@@ -15,29 +15,36 @@
* @author [Anup Kumar Panwar](https://github.com/AnupKumarPanwar) * @author [Anup Kumar Panwar](https://github.com/AnupKumarPanwar)
* @author [David Leal](https://github.com/Panquesito7) * @author [David Leal](https://github.com/Panquesito7)
*/ */
#include <iostream> #include <iostream> /// for IO operations
#include <array> #include <array> /// for std::array
#include <vector> #include <vector> /// for std::vector
/** /**
* @namespace * @namespace backtracking
* @brief Backtracking algorithms * @brief Backtracking algorithms
*/ */
namespace backtracking { namespace backtracking {
/** A utility function to print solution /**
* @namespace graph_coloring
* @brief Functions for the [Graph Coloring](https://en.wikipedia.org/wiki/Graph_coloring) algorith,
*/
namespace graph_coloring {
/**
* @brief A utility function to print the solution
* @tparam V number of vertices in the graph * @tparam V number of vertices in the graph
* @param color array of colors assigned to the nodes * @param color array of colors assigned to the nodes
*/ */
template <size_t V> template <size_t V>
void printSolution(const std::array <int, V>& color) { void printSolution(const std::array <int, V>& color) {
std::cout << "Following are the assigned colors\n"; std::cout << "Following are the assigned colors\n";
for (auto &col : color) { for (auto &col : color) {
std::cout << col; std::cout << col;
} }
std::cout << "\n"; std::cout << "\n";
} }
/** A utility function to check if the current color assignment is safe for /**
* @brief Utility function to check if the current color assignment is safe for
* vertex v * vertex v
* @tparam V number of vertices in the graph * @tparam V number of vertices in the graph
* @param v index of graph vertex to check * @param v index of graph vertex to check
@@ -47,17 +54,18 @@ namespace backtracking {
* @returns `true` if the color is safe to be assigned to the node * @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 * @returns `false` if the color is not safe to be assigned to the node
*/ */
template <size_t V> 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) { 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++) { for (int i = 0; i < V; i++) {
if (graph[v][i] && c == color[i]) { if (graph[v][i] && c == color[i]) {
return false; return false;
} }
} }
return true; return true;
} }
/** A recursive utility function to solve m coloring problem /**
* @brief Recursive utility function to solve m coloring problem
* @tparam V number of vertices in the graph * @tparam V number of vertices in the graph
* @param graph matrix of graph nonnectivity * @param graph matrix of graph nonnectivity
* @param m number of colors * @param m number of colors
@@ -65,33 +73,35 @@ namespace backtracking {
* that this parameter gets modified by the function * that this parameter gets modified by the function
* @param v index of graph vertex to check * @param v index of graph vertex to check
*/ */
template <size_t V> template <size_t V>
void graphColoring(const std::array<std::array <int, V>, V>& graph, int m, std::array <int, V> color, int v) { void graphColoring(const std::array<std::array <int, V>, V>& graph, int m, std::array <int, V> color, int v) {
// base case: // base case:
// If all vertices are assigned a color then return true // If all vertices are assigned a color then return true
if (v == V) { if (v == V) {
backtracking::printSolution<V>(color); printSolution<V>(color);
return; return;
} }
// Consider this vertex v and try different colors // Consider this vertex v and try different colors
for (int c = 1; c <= m; c++) { for (int c = 1; c <= m; c++) {
// Check if assignment of color c to v is fine // Check if assignment of color c to v is fine
if (backtracking::isSafe<V>(v, graph, color, c)) { if (isSafe<V>(v, graph, color, c)) {
color[v] = c; color[v] = c;
// recur to assign colors to rest of the vertices // recur to assign colors to rest of the vertices
backtracking::graphColoring<V>(graph, m, color, v + 1); graphColoring<V>(graph, m, color, v + 1);
// If assigning color c doesn't lead to a solution then remove it // If assigning color c doesn't lead to a solution then remove it
color[v] = 0; color[v] = 0;
} }
} }
} }
} // namespace graph_coloring
} // namespace backtracking } // namespace backtracking
/** /**
* Main function * @brief Main function
* @returns 0 on exit
*/ */
int main() { int main() {
// Create following graph and test whether it is 3 colorable // Create following graph and test whether it is 3 colorable
@@ -112,6 +122,6 @@ int main() {
int m = 3; // Number of colors int m = 3; // Number of colors
std::array <int, V> color{}; std::array <int, V> color{};
backtracking::graphColoring<V>(graph, m, color, 0); backtracking::graph_coloring::graphColoring<V>(graph, m, color, 0);
return 0; return 0;
} }

View File

@@ -12,15 +12,20 @@
* @author [Nikhil Arora](https://github.com/nikhilarora068) * @author [Nikhil Arora](https://github.com/nikhilarora068)
* @author [David Leal](https://github.com/Panquesito7) * @author [David Leal](https://github.com/Panquesito7)
*/ */
#include <iostream> #include <iostream> /// for IO operations
#include <array> #include <array> /// for std::array
/** /**
* @namespace backtracking * @namespace backtracking
* @brief Backtracking algorithms * @brief Backtracking algorithms
*/ */
namespace backtracking { namespace backtracking {
/** /**
* @namespace knight_tour
* @brief Functions for the [Knight's tour](https://en.wikipedia.org/wiki/Knight%27s_tour) algorithm
*/
namespace knight_tour {
/**
* A utility function to check if i,j are valid indexes for N*N chessboard * A utility function to check if i,j are valid indexes for N*N chessboard
* @tparam V number of vertices in array * @tparam V number of vertices in array
* @param x current index in rows * @param x current index in rows
@@ -29,12 +34,12 @@ namespace backtracking {
* @returns `true` if .... * @returns `true` if ....
* @returns `false` if .... * @returns `false` if ....
*/ */
template <size_t V> template <size_t V>
bool issafe(int x, int y, const std::array <std::array <int, V>, V>& sol) { bool issafe(int x, int y, const std::array <std::array <int, V>, V>& sol) {
return (x < V && x >= 0 && y < V && y >= 0 && sol[x][y] == -1); return (x < V && x >= 0 && y < V && y >= 0 && sol[x][y] == -1);
} }
/** /**
* Knight's tour algorithm * Knight's tour algorithm
* @tparam V number of vertices in array * @tparam V number of vertices in array
* @param x current index in rows * @param x current index in rows
@@ -46,10 +51,10 @@ namespace backtracking {
* @returns `true` if solution exists * @returns `true` if solution exists
* @returns `false` if solution does not exist * @returns `false` if solution does not exist
*/ */
template <size_t V> template <size_t V>
bool solve(int x, int y, int mov, std::array <std::array <int, V>, V> &sol, bool solve(int x, int y, int mov, std::array <std::array <int, V>, V> &sol,
const std::array <int, V> &xmov, std::array <int, V> &ymov) { const std::array <int, V> &xmov, std::array <int, V> &ymov) {
int k, xnext, ynext; int k = 0, xnext = 0, ynext = 0;
if (mov == V * V) { if (mov == V * V) {
return true; return true;
@@ -59,10 +64,10 @@ namespace backtracking {
xnext = x + xmov[k]; xnext = x + xmov[k];
ynext = y + ymov[k]; ynext = y + ymov[k];
if (backtracking::issafe<V>(xnext, ynext, sol)) { if (issafe<V>(xnext, ynext, sol)) {
sol[xnext][ynext] = mov; sol[xnext][ynext] = mov;
if (backtracking::solve<V>(xnext, ynext, mov + 1, sol, xmov, ymov) == true) { if (solve<V>(xnext, ynext, mov + 1, sol, xmov, ymov) == true) {
return true; return true;
} }
else { else {
@@ -71,11 +76,13 @@ namespace backtracking {
} }
} }
return false; return false;
} }
} // namespace knight_tour
} // namespace backtracking } // namespace backtracking
/** /**
* Main function * @brief Main function
* @returns 0 on exit
*/ */
int main() { int main() {
const int n = 8; const int n = 8;
@@ -91,7 +98,7 @@ int main() {
sol[0][0] = 0; sol[0][0] = 0;
bool flag = backtracking::solve<n>(0, 0, 1, sol, xmov, ymov); bool flag = backtracking::knight_tour::solve<n>(0, 0, 1, sol, xmov, ymov);
if (flag == false) { if (flag == false) {
std::cout << "Error: Solution does not exist\n"; std::cout << "Error: Solution does not exist\n";
} }

View File

@@ -15,10 +15,10 @@
* @author [Gleison Batista](https://github.com/gleisonbs) * @author [Gleison Batista](https://github.com/gleisonbs)
* @author [David Leal](https://github.com/Panquesito7) * @author [David Leal](https://github.com/Panquesito7)
*/ */
#include <algorithm> #include <algorithm> /// for std::max, std::min
#include <cmath> #include <cmath> /// for log2
#include <iostream> #include <iostream> /// for IO operations
#include <array> #include <array> /// for std::array
/** /**
* @namespace backtracking * @namespace backtracking
@@ -26,13 +26,13 @@
*/ */
namespace backtracking { namespace backtracking {
/** /**
* Check which number is the maximum/minimum in the array * @brief Check which is the maximum/minimum number in the array
* @param depth current depth in game tree * @param depth current depth in game tree
* @param node_index current index in array * @param node_index current index in array
* @param is_max if current index is the longest number * @param is_max if current index is the longest number
* @param scores saved numbers in array * @param scores saved numbers in array
* @param height maximum height for game tree * @param height maximum height for game tree
* @return maximum or minimum number * @returns the maximum or minimum number
*/ */
template <size_t T> template <size_t T>
int minimax(int depth, int node_index, bool is_max, int minimax(int depth, int node_index, bool is_max,
@@ -49,7 +49,8 @@ int minimax(int depth, int node_index, bool is_max,
} // namespace backtracking } // namespace backtracking
/** /**
* Main function * @brief Main function
* @returns 0 on exit
*/ */
int main() { int main() {
std::array<int, 8> scores = {90, 23, 6, 33, 21, 65, 123, 34423}; std::array<int, 8> scores = {90, 23, 6, 33, 21, 65, 123, 34423};

View File

@@ -23,18 +23,18 @@
* @brief Backtracking algorithms * @brief Backtracking algorithms
*/ */
namespace backtracking { namespace backtracking {
/** /**
* @namespace n_queens * @namespace n_queens
* @brief Functions for [Eight Queens](https://en.wikipedia.org/wiki/Eight_queens_puzzle) puzzle. * @brief Functions for [Eight Queens](https://en.wikipedia.org/wiki/Eight_queens_puzzle) puzzle.
*/ */
namespace n_queens { namespace n_queens {
/** /**
* Utility function to print matrix * Utility function to print matrix
* @tparam n number of matrix size * @tparam n number of matrix size
* @param board matrix where numbers are saved * @param board matrix where numbers are saved
*/ */
template <size_t n> template <size_t n>
void printSolution(const std::array<std::array<int, n>, n> &board) { void printSolution(const std::array<std::array<int, n>, n> &board) {
std::cout << "\n"; std::cout << "\n";
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) { for (int j = 0; j < n; j++) {
@@ -44,7 +44,7 @@ namespace backtracking {
} }
} }
/** /**
* Check if a queen can be placed on matrix * Check if a queen can be placed on matrix
* @tparam n number of matrix size * @tparam n number of matrix size
* @param board matrix where numbers are saved * @param board matrix where numbers are saved
@@ -53,8 +53,8 @@ namespace backtracking {
* @returns `true` if queen can be placed on matrix * @returns `true` if queen can be placed on matrix
* @returns `false` if queen can't be placed on matrix * @returns `false` if queen can't be placed on matrix
*/ */
template <size_t n> template <size_t n>
bool isSafe(const std::array<std::array<int, n>, n> &board, const int &row, bool isSafe(const std::array<std::array<int, n>, n> &board, const int &row,
const int &col) { const int &col) {
int i = 0, j = 0; int i = 0, j = 0;
@@ -78,16 +78,16 @@ namespace backtracking {
} }
} }
return true; return true;
} }
/** /**
* Solve n queens problem * Solve n queens problem
* @tparam n number of matrix size * @tparam n number of matrix size
* @param board matrix where numbers are saved * @param board matrix where numbers are saved
* @param col current index in columns * @param col current index in columns
*/ */
template <size_t n> template <size_t n>
void solveNQ(std::array<std::array<int, n>, n> board, const int &col) { void solveNQ(std::array<std::array<int, n>, n> board, const int &col) {
if (col >= n) { if (col >= n) {
printSolution<n>(board); printSolution<n>(board);
return; return;
@@ -108,12 +108,13 @@ namespace backtracking {
board[i][col] = 0; // backtrack board[i][col] = 0; // backtrack
} }
} }
} }
} // namespace n_queens } // namespace n_queens
} // namespace backtracking } // namespace backtracking
/** /**
* Main function * @brief Main function
* @returns 0 on exit
*/ */
int main() { int main() {
const int n = 4; const int n = 4;

View File

@@ -111,7 +111,7 @@ int main() {
std::array<std::array<int, n>, n> board{}; std::array<std::array<int, n>, n> board{};
if (n % 2 == 0) { if (n % 2 == 0) {
for (int i = 0; i <= n / 2 - 1; i++) { // 😎 for (int i = 0; i <= n / 2 - 1; i++) {
if (backtracking::n_queens_optimized::CanIMove(board, i, 0)) { if (backtracking::n_queens_optimized::CanIMove(board, i, 0)) {
board[i][0] = 1; board[i][0] = 1;
backtracking::n_queens_optimized::NQueenSol(board, 1); backtracking::n_queens_optimized::NQueenSol(board, 1);
@@ -119,7 +119,7 @@ int main() {
} }
} }
} else { } else {
for (int i = 0; i <= n / 2; i++) { // 😏 for (int i = 0; i <= n / 2; i++) {
if (backtracking::n_queens_optimized::CanIMove(board, i, 0)) { if (backtracking::n_queens_optimized::CanIMove(board, i, 0)) {
board[i][0] = 1; board[i][0] = 1;
backtracking::n_queens_optimized::NQueenSol(board, 1); backtracking::n_queens_optimized::NQueenSol(board, 1);

View File

@@ -7,8 +7,8 @@
* @author [David Leal](https://github.com/Panquesito7) * @author [David Leal](https://github.com/Panquesito7)
* *
*/ */
#include <iostream> #include <iostream> /// for IO operations
#include <array> #include <array> /// for std::array
/** /**
* @namespace backtracking * @namespace backtracking
@@ -17,12 +17,12 @@
namespace backtracking { namespace backtracking {
/** /**
* @namespace n_queens_all_solutions * @namespace n_queens_all_solutions
* @brief Functions for [Eight * @brief Functions for the [Eight
* Queens](https://en.wikipedia.org/wiki/Eight_queens_puzzle) puzzle with all solutions. * Queens](https://en.wikipedia.org/wiki/Eight_queens_puzzle) puzzle with all solutions.
*/ */
namespace n_queens_all_solutions { namespace n_queens_all_solutions {
/** /**
* Utility function to print matrix * @brief Utility function to print matrix
* @tparam n number of matrix size * @tparam n number of matrix size
* @param board matrix where numbers are saved * @param board matrix where numbers are saved
*/ */
@@ -38,7 +38,7 @@ void PrintSol(const std::array<std::array<int, n>, n>& board) {
} }
/** /**
* Check if a queen can be placed on matrix * @brief Check if a queen can be placed on the matrix
* @tparam n number of matrix size * @tparam n number of matrix size
* @param board matrix where numbers are saved * @param board matrix where numbers are saved
* @param row current index in rows * @param row current index in rows
@@ -70,7 +70,7 @@ bool CanIMove(const std::array<std::array<int, n>, n>& board, int row, int col)
} }
/** /**
* Solve n queens problem * @brief Main function to solve the N Queens problem
* @tparam n number of matrix size * @tparam n number of matrix size
* @param board matrix where numbers are saved * @param board matrix where numbers are saved
* @param col current index in columns * @param col current index in columns
@@ -93,7 +93,8 @@ void NQueenSol(std::array<std::array<int, n>, n> board, int col) {
} // namespace backtracking } // namespace backtracking
/** /**
* Main function * @brief Main function
* @returns 0 on exit
*/ */
int main() { int main() {
const int n = 4; const int n = 4;

View File

@@ -16,9 +16,9 @@
* @author [David Leal](https://github.com/Panquesito7) * @author [David Leal](https://github.com/Panquesito7)
*/ */
#include <array> #include <array> /// for std::array
#include <iostream> #include <iostream> /// for IO operations
#include <cassert> #include <cassert> /// for assert
/** /**
* @namespace backtracking * @namespace backtracking
@@ -39,7 +39,8 @@ namespace rat_maze {
* @param currposcol current position in columns * @param currposcol current position in columns
* @param maze matrix where numbers are saved * @param maze matrix where numbers are saved
* @param soln matrix to problem solution * @param soln matrix to problem solution
* @returns 0 on end * @returns `true` if there exists a solution to move one step ahead in a column or in a row
* @returns `false` for the backtracking part
*/ */
template <size_t size> template <size_t size>
bool solveMaze(int currposrow, int currposcol, bool solveMaze(int currposrow, int currposcol,
@@ -78,7 +79,7 @@ bool solveMaze(int currposrow, int currposcol,
} // namespace backtracking } // namespace backtracking
/** /**
* @brief Test implementations * @brief Self-test implementations
* @returns void * @returns void
*/ */
static void test(){ static void test(){
@@ -96,8 +97,8 @@ static void test(){
} }
} }
int currposrow = 0; // Current position in rows int currposrow = 0; // Current position in the rows
int currposcol = 0; // Current position in columns int currposcol = 0; // Current position in the columns
assert(backtracking::rat_maze::solveMaze<size>(currposrow, currposcol, maze, assert(backtracking::rat_maze::solveMaze<size>(currposrow, currposcol, maze,
soln) == 1); soln) == 1);
@@ -108,6 +109,6 @@ static void test(){
* @returns 0 on exit * @returns 0 on exit
*/ */
int main() { int main() {
test(); // run the tests test(); // run self-test implementations
return 0; return 0;
} }

View File

@@ -13,16 +13,21 @@
* @author [DarthCoder3200](https://github.com/DarthCoder3200) * @author [DarthCoder3200](https://github.com/DarthCoder3200)
* @author [David Leal](https://github.com/Panquesito7) * @author [David Leal](https://github.com/Panquesito7)
*/ */
#include <iostream> #include <iostream> /// for IO operations
#include <array> #include <array> /// for assert
/** /**
* @namespace backtracking * @namespace backtracking
* @brief Backtracking algorithms * @brief Backtracking algorithms
*/ */
namespace backtracking { namespace backtracking {
/** /**
* Checks if it's possible to place a number 'no' * @namespace sudoku_solver
* @brief Functions for the [Sudoku Solver](https://en.wikipedia.org/wiki/Sudoku) implementation
*/
namespace sudoku_solver {
/**
* @brief Check if it's possible to place a number (`no` parameter)
* @tparam V number of vertices in the array * @tparam V number of vertices in the array
* @param mat matrix where numbers are saved * @param mat matrix where numbers are saved
* @param i current index in rows * @param i current index in rows
@@ -32,16 +37,16 @@ namespace backtracking {
* @returns `true` if 'mat' is different from 'no' * @returns `true` if 'mat' is different from 'no'
* @returns `false` if 'mat' equals to 'no' * @returns `false` if 'mat' equals to 'no'
*/ */
template <size_t V> template <size_t V>
bool isPossible(const std::array <std::array <int, V>, V> &mat, int i, int j, int no, int n) { bool isPossible(const std::array <std::array <int, V>, V> &mat, int i, int j, int no, int n) {
/// 'no' shouldn't be present in either row i or column j /// `no` shouldn't be present in either row i or column j
for (int x = 0; x < n; x++) { for (int x = 0; x < n; x++) {
if (mat[x][j] == no || mat[i][x] == no) { if (mat[x][j] == no || mat[i][x] == no) {
return false; return false;
} }
} }
/// 'no' shouldn't be present in the 3*3 subgrid /// `no` shouldn't be present in the 3*3 subgrid
int sx = (i / 3) * 3; int sx = (i / 3) * 3;
int sy = (j / 3) * 3; int sy = (j / 3) * 3;
@@ -54,17 +59,17 @@ namespace backtracking {
} }
return true; return true;
} }
/** /**
* Utility function to print matrix * @brief Utility function to print the matrix
* @tparam V number of vertices in array * @tparam V number of vertices in array
* @param mat matrix where numbers are saved * @param mat matrix where numbers are saved
* @param starting_mat copy of mat, required by printMat for highlighting the differences * @param starting_mat copy of mat, required by printMat for highlighting the differences
* @param n number of times loop will run * @param n number of times loop will run
* @return void * @return void
*/ */
template <size_t V> template <size_t V>
void printMat(const std::array <std::array <int, V>, V> &mat, const std::array <std::array <int, V>, V> &starting_mat, int n) { void printMat(const std::array <std::array <int, V>, V> &mat, const std::array <std::array <int, V>, V> &starting_mat, int n) {
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) { for (int j = 0; j < n; j++) {
if (starting_mat[i][j] != mat[i][j]) { if (starting_mat[i][j] != mat[i][j]) {
@@ -81,10 +86,10 @@ namespace backtracking {
} }
std::cout << std::endl; std::cout << std::endl;
} }
} }
/** /**
* Sudoku algorithm * @brief Main function to implement the Sudoku algorithm
* @tparam V number of vertices in array * @tparam V number of vertices in array
* @param mat matrix where numbers are saved * @param mat matrix where numbers are saved
* @param starting_mat copy of mat, required by printMat for highlighting the differences * @param starting_mat copy of mat, required by printMat for highlighting the differences
@@ -93,46 +98,48 @@ namespace backtracking {
* @returns `true` if 'no' was placed * @returns `true` if 'no' was placed
* @returns `false` if 'no' was not placed * @returns `false` if 'no' was not placed
*/ */
template <size_t V> template <size_t V>
bool solveSudoku(std::array <std::array <int, V>, V> &mat, const std::array <std::array <int, V>, V> &starting_mat, int i, int j) { bool solveSudoku(std::array <std::array <int, V>, V> &mat, const std::array <std::array <int, V>, V> &starting_mat, int i, int j) {
/// Base Case /// Base Case
if (i == 9) { if (i == 9) {
/// Solved for 9 rows already /// Solved for 9 rows already
backtracking::printMat<V>(mat, starting_mat, 9); printMat<V>(mat, starting_mat, 9);
return true; return true;
} }
/// Crossed the last Cell in the row /// Crossed the last Cell in the row
if (j == 9) { if (j == 9) {
return backtracking::solveSudoku<V>(mat, starting_mat, i + 1, 0); return solveSudoku<V>(mat, starting_mat, i + 1, 0);
} }
/// Blue Cell - Skip /// Blue Cell - Skip
if (mat[i][j] != 0) { if (mat[i][j] != 0) {
return backtracking::solveSudoku<V>(mat, starting_mat, i, j + 1); return solveSudoku<V>(mat, starting_mat, i, j + 1);
} }
/// White Cell /// White Cell
/// Try to place every possible no /// Try to place every possible no
for (int no = 1; no <= 9; no++) { for (int no = 1; no <= 9; no++) {
if (backtracking::isPossible<V>(mat, i, j, no, 9)) { if (isPossible<V>(mat, i, j, no, 9)) {
/// Place the 'no' - assuming a solution will exist /// Place the 'no' - assuming a solution will exist
mat[i][j] = no; mat[i][j] = no;
bool solution_found = backtracking::solveSudoku<V>(mat, starting_mat, i, j + 1); bool solution_found = solveSudoku<V>(mat, starting_mat, i, j + 1);
if (solution_found) { if (solution_found) {
return true; return true;
} }
/// Couldn't find a solution /// Couldn't find a solution
/// loop will place the next no. /// loop will place the next `no`.
} }
} }
/// Solution couldn't be found for any of the numbers provided /// Solution couldn't be found for any of the numbers provided
mat[i][j] = 0; mat[i][j] = 0;
return false; return false;
} }
} // namespace sudoku_solver
} // namespace backtracking } // namespace backtracking
/** /**
* Main function * @brief Main function
* @returns 0 on exit
*/ */
int main() { int main() {
const int V = 9; const int V = 9;
@@ -148,10 +155,10 @@ int main() {
std::array <int, V> {0, 0, 0, 0, 8, 0, 0, 7, 9} std::array <int, V> {0, 0, 0, 0, 8, 0, 0, 7, 9}
}; };
backtracking::printMat<V>(mat, mat, 9); backtracking::sudoku_solver::printMat<V>(mat, mat, 9);
std::cout << "Solution " << std::endl; std::cout << "Solution " << std::endl;
std::array <std::array <int, V>, V> starting_mat = mat; std::array <std::array <int, V>, V> starting_mat = mat;
backtracking::solveSudoku<V>(mat, starting_mat, 0, 0); backtracking::sudoku_solver::solveSudoku<V>(mat, starting_mat, 0, 0);
return 0; return 0;
} }

View File

@@ -22,11 +22,11 @@ template <typename T>
bool is_prime(T num) { bool is_prime(T num) {
bool result = true; bool result = true;
if (num <= 1) { if (num <= 1) {
return 0; return false;
} else if (num == 2) { } else if (num == 2) {
return 1; return true;
} else if ((num & 1) == 0) { } else if ((num & 1) == 0) {
return 0; return false;
} }
if (num >= 3) { if (num >= 3) {
for (T i = 3; (i * i) < (num); i = (i + 2)) { for (T i = 3; (i * i) < (num); i = (i + 2)) {

View File

@@ -204,8 +204,9 @@ static void test2(others::iterative_tree_traversals::BinaryTree binaryTree, othe
result = binaryTree.postOrderIterative(root); result = binaryTree.postOrderIterative(root);
// Self-testing the result using `assert` // Self-testing the result using `assert`
for(int i = 0; i < result.size(); i++) for(int i = 0; i < result.size(); i++) {
assert(actual_result[i] == result[i]); assert(actual_result[i] == result[i]);
}
// Printing the result storing postorder. // Printing the result storing postorder.
std::cout<< "\nPostOrder Traversal Is : "<< std::endl; std::cout<< "\nPostOrder Traversal Is : "<< std::endl;
@@ -228,8 +229,9 @@ static void test3(others::iterative_tree_traversals::BinaryTree binaryTree, othe
result = binaryTree.inOrderIterative(root); result = binaryTree.inOrderIterative(root);
// Self-testing the result using `assert` // Self-testing the result using `assert`
for(int i = 0; i < result.size(); i++) for(int i = 0; i < result.size(); i++) {
assert(actual_result[i] == result[i]); assert(actual_result[i] == result[i]);
}
// Printing the result storing inorder. // Printing the result storing inorder.
std::cout<< "\nInOrder Traversal Is : "<< std::endl; std::cout<< "\nInOrder Traversal Is : "<< std::endl;
@@ -252,8 +254,9 @@ static void test4(others::iterative_tree_traversals::BinaryTree binaryTree, othe
result = binaryTree.preOrderIterative(root); result = binaryTree.preOrderIterative(root);
// Self-testing the result using `assert` // Self-testing the result using `assert`
for(int i = 0; i < result.size(); i++) for(int i = 0; i < result.size(); i++) {
assert(actual_result[i] == result[i]); assert(actual_result[i] == result[i]);
}
// Printing the result storing preorder. // Printing the result storing preorder.
std::cout<< "\nPreOrder Traversal Is : "<< std::endl; std::cout<< "\nPreOrder Traversal Is : "<< std::endl;
@@ -276,8 +279,9 @@ static void test5(others::iterative_tree_traversals::BinaryTree binaryTree, othe
result = binaryTree.postOrderIterative(root); result = binaryTree.postOrderIterative(root);
// Self-testing the result using `assert` // Self-testing the result using `assert`
for(int i = 0; i < result.size(); i++) for(int i = 0; i < result.size(); i++) {
assert(actual_result[i] == result[i]); assert(actual_result[i] == result[i]);
}
// Printing the result storing postorder. // Printing the result storing postorder.
std::cout<< "\nPostOrder Traversal Is : "<< std::endl; std::cout<< "\nPostOrder Traversal Is : "<< std::endl;
@@ -300,8 +304,9 @@ static void test6(others::iterative_tree_traversals::BinaryTree binaryTree, othe
result = binaryTree.inOrderIterative(root); result = binaryTree.inOrderIterative(root);
// Self-testing the result using `assert` // Self-testing the result using `assert`
for(int i = 0; i < result.size(); i++) for(int i = 0; i < result.size(); i++) {
assert(actual_result[i] == result[i]); assert(actual_result[i] == result[i]);
}
// Printing the result storing inorder. // Printing the result storing inorder.
std::cout<< "\nInOrder Traversal Is : "<< std::endl; std::cout<< "\nInOrder Traversal Is : "<< std::endl;