[fix/docs]: Update backtracking folder (#916)

* [fix/docs]: Update backtracking/graph_coloring.cpp

* Add CMakeLists.txt in backtracking folder

* Add backtracking to CMakeLists.txt

* fix: Fix build issues

* docs: Various documentation fixes

* fix: minimax.cpp issues

* fix: sudoku_solve.cpp fixes

* formatting source-code for 8ffbbb35ce

* make he code neat and clean without global variables

* fix 2 stars in comment

* fix MSVC errors by forcing template parameter in function calls

Note: This is identical to passing it as a function parameter, and may not be helpful

* Update minimax.cpp

* docs: minimax.cpp improvements

* docs: Add Wikipedia link in minimax.cpp

* fix: minimax.cpp vector fix

* docs: fix Wikipedia link in minimax.cpp

* docs: fix return statement in minimax.cpp

* fix: sudoku_solve.cpp fixes

* fix: more sudoku_solve.cpp fixes

* fix: sudoku_solve.cpp fixes

* fix: sudoku_solve.cpp

* formatting source-code for 13b5b9b829

* docs: update graph_coloring.cpp description

* fix: use array instead of vector (minimax.cpp)

* feat: add namespace (minimax.cpp)

* docs: update namespace description (graph_coloring.cpp)

* fix: graph_coloring.cpp

* fix: sudoku_solve.cpp fixes

* fix: graph_coloring.cpp

* fix: minimax.cpp

* fix: more sudoku_solve.cpp fixes

* fix: more graph_coloring.cpp fixes

* fix: graph_coloring.cpp fixes

* fix: sudoku_solve.cpp fixes

* fix: minimax.cpp

* fix: sudoku_solve.cpp fixes

* fix: too few template arguments (std::array)

* fix: too few template arguments (std::array, minimax.cpp)

* fix: narrowing conversion from double to int (minimax.cpp)

* fix: excess elements in struct initializer (graph_coloring.cpp)

* fix: no matching function (graph_coloring.cpp)

* fix: graph_coloring.cpp issues/errors

* fix: knight_tour.cpp issues/errors

* fix: sudoku_solve.cpp issues/errors

* [fix/docs]: Various fixes in graph_coloring.cpp

* fix: More graph_coloring.cpp fixes

* docs: Add initial comment block (sudoku_solve.cpp)

* fix: Add return statement (knight_tour.cpp)

* fix: array fixes (graph_coloring.cpp)

* docs: documentation improvements (sudoku_solve.cpp)

* docs: documentation improvements (knight_tour.cpp)

* docs: documentation improvements (sudoku_solve.cpp)

* docs: documentation improvements (graph_coloring.cpp)

* docs: Documentation improvements (graph_coloring.cpp)

Thanks, @kvedala!

* docs: Documentation improvements (sudoku_solve.cpp)

* docs: Document function parameter (sudoku_solve.cpp)

* docs: Documentation improvements (knight_tour.cpp)

* docs: Add long description (graph_coloring.cpp)

* docs: Add long description (minimax.cpp)

* docs: Add long description (sudoku_solve.cpp)

* docs: Documentation improvements (knight_tour.cpp)

* docs: Documentation improvements (sudoku_solve.cpp)

* docs: Documentation improvements (minimax.cpp)

* docs: More documentation improvements (minimax.cpp)

* docs: Documentation improvements (sudoku_solve.cpp)

* fix: sudoku_solve.cpp improvements

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
Co-authored-by: Krishna Vedala <7001608+kvedala@users.noreply.github.com>
This commit is contained in:
David Leal 2020-08-07 13:35:59 -05:00 committed by GitHub
parent b36ce9a8c0
commit 25b39a34fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 390 additions and 188 deletions

View File

@ -36,6 +36,7 @@ add_subdirectory(sorting)
add_subdirectory(geometry) add_subdirectory(geometry)
add_subdirectory(graphics) add_subdirectory(graphics)
add_subdirectory(probability) add_subdirectory(probability)
add_subdirectory(backtracking)
add_subdirectory(data_structures) add_subdirectory(data_structures)
add_subdirectory(machine_learning) add_subdirectory(machine_learning)
add_subdirectory(numerical_methods) add_subdirectory(numerical_methods)

View File

@ -0,0 +1,18 @@
# If necessary, use the RELATIVE flag, otherwise each source file may be listed
# with full pathname. RELATIVE may makes it easier to extract an executable name
# automatically.
file( GLOB APP_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *.cpp )
# file( GLOB APP_SOURCES ${CMAKE_SOURCE_DIR}/*.c )
# AUX_SOURCE_DIRECTORY(${CMAKE_CURRENT_SOURCE_DIR} APP_SOURCES)
foreach( testsourcefile ${APP_SOURCES} )
# I used a simple string replace, to cut off .cpp.
string( REPLACE ".cpp" "" testname ${testsourcefile} )
add_executable( ${testname} ${testsourcefile} )
set_target_properties(${testname} PROPERTIES LINKER_LANGUAGE CXX)
if(OpenMP_CXX_FOUND)
target_link_libraries(${testname} OpenMP::OpenMP_CXX)
endif()
install(TARGETS ${testname} DESTINATION "bin/backtracking")
endforeach( testsourcefile ${APP_SOURCES} )

View File

@ -1,72 +1,117 @@
#include <stdio.h> /**
* @file
* @brief prints the assigned colors
* 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
* 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 <vector>
// Number of vertices in the graph /**
#define V 4 * @namespace
* @brief Backtracking algorithms
void printSolution(int color[]); */
namespace backtracking {
/* A utility function to check if the current color assignment /** A utility function to print solution
is safe for vertex v */ * @tparam V number of vertices in the graph
bool isSafe(int v, bool graph[V][V], int color[], int c) { * @param color array of colors assigned to the nodes
for (int i = 0; i < V; i++) */
if (graph[v][i] && c == color[i]) template <size_t V>
return false; void printSolution(const std::array <int, V>& color) {
return true; std::cout << "Following are the assigned colors\n";
} for (auto &col : color) {
std::cout << col;
/* A recursive utility function to solve m coloring problem */ }
void graphColoring(bool graph[V][V], int m, int color[], int v) { std::cout << "\n";
/* base case: If all vertices are assigned a color then
return true */
if (v == V) {
printSolution(color);
return;
} }
/* Consider this vertex v and try different colors */ /** A utility function to check if the current color assignment is safe for
for (int c = 1; c <= m; c++) { * vertex v
/* Check if assignment of color c to v is fine*/ * @tparam V number of vertices in the graph
if (isSafe(v, graph, color, c)) { * @param v index of graph vertex to check
color[v] = c; * @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;
}
/* recur to assign colors to rest of the vertices */ /** A recursive utility function to solve m coloring problem
graphColoring(graph, m, color, v + 1); * @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;
}
/* If assigning color c doesn't lead to a solution // Consider this vertex v and try different colors
then remove it */ for (int c = 1; c <= m; c++) {
color[v] = 0; // 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
/* A utility function to print solution */ /**
void printSolution(int color[]) { * Main function
printf(" Following are the assigned colors \n"); */
for (int i = 0; i < V; i++) printf(" %d ", color[i]);
printf("\n");
}
// driver program to test above function
int main() { int main() {
/* Create following graph and test whether it is 3 colorable // Create following graph and test whether it is 3 colorable
(3)---(2) // (3)---(2)
| / | // | / |
| / | // | / |
| / | // | / |
(0)---(1) // (0)---(1)
*/
bool graph[V][V] = { const int V = 4; // number of vertices in the graph
{0, 1, 1, 1}, std::array <std::array <int, V>, V> graph = {
{1, 0, 1, 0}, std::array <int, V>({0, 1, 1, 1}),
{1, 1, 0, 1}, std::array <int, V>({1, 0, 1, 0}),
{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 int m = 3; // Number of colors
std::array <int, V> color{};
int color[V]; backtracking::graphColoring<V>(graph, m, color, 0);
for (int i = 0; i < V; i++) color[i] = 0;
graphColoring(graph, m, color, 0);
return 0; return 0;
} }

View File

@ -1,60 +1,105 @@
/**
* @file
* @brief [Knight's tour](https://en.wikipedia.org/wiki/Knight%27s_tour) algorithm
*
* @details
* A knight's tour is a sequence of moves of a knight on a chessboard
* such that the knight visits every square only once. If the knight
* ends on a square that is one knight's move from the beginning
* square (so that it could tour the board again immediately, following
* the same path, the tour is closed; otherwise, it is open.
*
* @author [Nikhil Arora](https://github.com/nikhilarora068)
* @author [David Leal](https://github.com/Panquesito7)
*/
#include <iostream> #include <iostream>
#define n 8 #include <array>
/** /**
A knight's tour is a sequence of moves of a knight on a chessboard * @namespace backtracking
such that the knight visits every square only once. If the knight * @brief Backtracking algorithms
ends on a square that is one knight's move from the beginning */
square (so that it could tour the board again immediately, following namespace backtracking {
the same path), the tour is closed; otherwise, it is open. /**
**/ * A utility function to check if i,j are valid indexes for N*N chessboard
* @tparam V number of vertices in array
using std::cin; * @param x current index in rows
using std::cout; * @param y current index in columns
* @param sol matrix where numbers are saved
bool issafe(int x, int y, int sol[n][n]) { * @returns `true` if ....
return (x < n && x >= 0 && y < n && y >= 0 && sol[x][y] == -1); * @returns `false` if ....
} */
bool solve(int x, int y, int mov, int sol[n][n], int xmov[n], int ymov[n]) { template <size_t V>
int k, xnext, ynext; 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);
if (mov == n * n)
return true;
for (k = 0; k < 8; k++) {
xnext = x + xmov[k];
ynext = y + ymov[k];
if (issafe(xnext, ynext, sol)) {
sol[xnext][ynext] = mov;
if (solve(xnext, ynext, mov + 1, sol, xmov, ymov) == true)
return true;
else
sol[xnext][ynext] = -1;
}
} }
return false;
} /**
* Knight's tour algorithm
* @tparam V number of vertices in array
* @param x current index in rows
* @param y current index in columns
* @param mov movement to be done
* @param sol matrix where numbers are saved
* @param xmov next move of knight (x coordinate)
* @param ymov next move of knight (y coordinate)
* @returns `true` if solution exists
* @returns `false` if solution does not exist
*/
template <size_t V>
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) {
int k, xnext, ynext;
if (mov == V * V) {
return true;
}
for (k = 0; k < V; k++) {
xnext = x + xmov[k];
ynext = y + ymov[k];
if (backtracking::issafe<V>(xnext, ynext, sol)) {
sol[xnext][ynext] = mov;
if (backtracking::solve<V>(xnext, ynext, mov + 1, sol, xmov, ymov) == true) {
return true;
}
else {
sol[xnext][ynext] = -1;
}
}
}
return false;
}
} // namespace backtracking
/**
* Main function
*/
int main() { int main() {
// initialize(); const int n = 8;
std::array <std::array <int, n>, n> sol = { 0 };
int sol[n][n];
int i, j; int i, j;
for (i = 0; i < n; i++) for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) sol[i][j] = -1; for (j = 0; j < n; j++) { sol[i][j] = -1; }
}
std::array <int, n> xmov = { 2, 1, -1, -2, -2, -1, 1, 2 };
std::array <int, n> ymov = { 1, 2, 2, 1, -1, -2, -2, -1 };
int xmov[8] = {2, 1, -1, -2, -2, -1, 1, 2};
int ymov[8] = {1, 2, 2, 1, -1, -2, -2, -1};
sol[0][0] = 0; sol[0][0] = 0;
bool flag = solve(0, 0, 1, sol, xmov, ymov); bool flag = backtracking::solve<n>(0, 0, 1, sol, xmov, ymov);
if (flag == false) if (flag == false) {
cout << "solution doesnot exist \n"; std::cout << "Error: Solution does not exist\n";
}
else { else {
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) cout << sol[i][j] << " "; for (j = 0; j < n; j++) { std::cout << sol[i][j] << " "; }
cout << "\n"; std::cout << "\n";
} }
} }
return 0;
} }

View File

@ -1,27 +1,61 @@
/**
* @file
* @brief returns which is the longest/shortest number
* using [minimax](https://en.wikipedia.org/wiki/Minimax) algorithm
*
* @details
* Minimax (sometimes MinMax, MM or saddle point) is a decision rule used in
* artificial intelligence, decision theory, game theory, statistics,
* and philosophy for minimizing the possible loss for a worst case (maximum loss) scenario.
* When dealing with gains, it is referred to as "maximin"to maximize the minimum gain.
* Originally formulated for two-player zero-sum game theory, covering both the cases where players take
* alternate moves and those where they make simultaneous moves, it has also been extended to more
* complex games and to general decision-making in the presence of uncertainty.
*
* @author [Gleison Batista](https://github.com/gleisonbs)
* @author [David Leal](https://github.com/Panquesito7)
*/
#include <algorithm>
#include <cmath> #include <cmath>
#include <iostream> #include <iostream>
#include <vector> #include <array>
using std::cout; /**
using std::endl; * @namespace backtracking
using std::max; * @brief Backtracking algorithms
using std::min; */
using std::vector; namespace backtracking {
/**
int minimax(int depth, int node_index, bool is_max, vector<int> scores, * Check which number is the maximum/minimum in the array
int height) { * @param depth current depth in game tree
if (depth == height) * @param node_index current index in array
* @param is_max if current index is the longest number
* @param scores saved numbers in array
* @param height maximum height for game tree
* @return maximum or minimum number
*/
template <size_t T>
int minimax(int depth, int node_index, bool is_max,
const std::array<int, T> &scores, double height) {
if (depth == height) {
return scores[node_index]; return scores[node_index];
}
int v1 = minimax(depth + 1, node_index * 2, !is_max, scores, height); int v1 = minimax(depth + 1, node_index * 2, !is_max, scores, height);
int v2 = minimax(depth + 1, node_index * 2 + 1, !is_max, scores, height); int v2 = minimax(depth + 1, node_index * 2 + 1, !is_max, scores, height);
return is_max ? max(v1, v2) : min(v1, v2); return is_max ? std::max(v1, v2) : std::min(v1, v2);
} }
} // namespace backtracking
/**
* Main function
*/
int main() { int main() {
vector<int> scores = {90, 23, 6, 33, 21, 65, 123, 34423}; std::array<int, 8> scores = {90, 23, 6, 33, 21, 65, 123, 34423};
int height = log2(scores.size()); double height = log2(scores.size());
cout << "Optimal value: " << minimax(0, 0, true, scores, height) << endl; std::cout << "Optimal value: " << backtracking::minimax(0, 0, true, scores, height)
<< std::endl;
return 0;
} }

View File

@ -1,91 +1,150 @@
/**
* @file
* @brief [Sudoku Solver](https://en.wikipedia.org/wiki/Sudoku) algorithm.
*
* @details
* Sudoku (, sūdoku, digit-single) (/suːˈdoʊkuː/, /-ˈdɒk-/, /-/, originally called
* Number Place) is a logic-based, combinatorial number-placement puzzle.
* In classic sudoku, the objective is to fill a 9×9 grid with digits so that each column,
* each row, and each of the nine 3×3 subgrids that compose the grid (also called "boxes", "blocks", or "regions")
* contain all of the digits from 1 to 9. The puzzle setter provides a
* partially completed grid, which for a well-posed puzzle has a single solution.
*
* @author [DarthCoder3200](https://github.com/DarthCoder3200)
* @author [David Leal](https://github.com/Panquesito7)
*/
#include <iostream> #include <iostream>
using namespace std; #include <array>
/// N=9;
int n = 9;
bool isPossible(int mat[][9], int i, int j, int no) { /**
/// Row or col nahin hona chahiye * @namespace backtracking
for (int x = 0; x < n; x++) { * @brief Backtracking algorithms
if (mat[x][j] == no || mat[i][x] == no) { */
return false; namespace backtracking {
} /**
} * Checks if it's possible to place a 'no'
* @tparam V number of vertices in the array
/// Subgrid mein nahi hona chahiye * @param mat matrix where numbers are saved
int sx = (i / 3) * 3; * @param i current index in rows
int sy = (j / 3) * 3; * @param j current index in columns
* @param no number to be added in matrix
for (int x = sx; x < sx + 3; x++) { * @param n number of times loop will run
for (int y = sy; y < sy + 3; y++) { * @returns `true` if 'mat' is different from 'no'
if (mat[x][y] == no) { * @returns `false` if 'mat' equals to 'no'
*/
template <size_t V>
bool isPossible(const std::array <std::array <int, V>, V> &mat, int i, int j, int no, int n) {
/// Row or col nahin hona chahiye
for (int x = 0; x < n; x++) {
if (mat[x][j] == no || mat[i][x] == no) {
return false; return false;
} }
} }
}
return true; /// Subgrid mein nahi hona chahiye
} int sx = (i / 3) * 3;
void printMat(int mat[][9]) { int sy = (j / 3) * 3;
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) { for (int x = sx; x < sx + 3; x++) {
cout << mat[i][j] << " "; for (int y = sy; y < sy + 3; y++) {
if ((j + 1) % 3 == 0) { if (mat[x][y] == no) {
cout << '\t'; return false;
}
} }
} }
if ((i + 1) % 3 == 0) {
cout << endl;
}
cout << endl;
}
}
bool solveSudoku(int mat[][9], int i, int j) {
/// Base Case
if (i == 9) {
/// Solve kr chuke hain for 9 rows already
printMat(mat);
return true; return true;
} }
/**
/// Crossed the last Cell in the row * Utility function to print matrix
if (j == 9) { * @tparam V number of vertices in array
return solveSudoku(mat, i + 1, 0); * @param mat matrix where numbers are saved
} * @param n number of times loop will run
* @return void
/// Blue Cell - Skip */
if (mat[i][j] != 0) { template <size_t V>
return solveSudoku(mat, i, j + 1); void printMat(const std::array <std::array <int, V>, V> &mat, int n) {
} for (int i = 0; i < n; i++) {
/// White Cell for (int j = 0; j < n; j++) {
/// Try to place every possible no std::cout << mat[i][j] << " ";
for (int no = 1; no <= 9; no++) { if ((j + 1) % 3 == 0) {
if (isPossible(mat, i, j, no)) { std::cout << '\t';
/// Place the no - assuming solution aa jayega }
mat[i][j] = no;
bool aageKiSolveHui = solveSudoku(mat, i, j + 1);
if (aageKiSolveHui) {
return true;
} }
/// Nahin solve hui if ((i + 1) % 3 == 0) {
/// loop will place the next no. std::cout << std::endl;
}
std::cout << std::endl;
} }
} }
/// Sare no try kr liey, kisi se bhi solve nahi hui
mat[i][j] = 0;
return false;
}
/**
* Sudoku algorithm
* @tparam V number of vertices in array
* @param mat matrix where numbers are saved
* @param i current index in rows
* @param j current index in columns
* @returns `true` if 'no' was placed
* @returns `false` if 'no' was not placed
*/
template <size_t V>
bool solveSudoku(std::array <std::array <int, V>, V> &mat, int i, int j) {
/// Base Case
if (i == 9) {
/// Solve kr chuke hain for 9 rows already
backtracking::printMat<V>(mat, 9);
return true;
}
/// Crossed the last Cell in the row
if (j == 9) {
return backtracking::solveSudoku<V>(mat, i + 1, 0);
}
/// Blue Cell - Skip
if (mat[i][j] != 0) {
return backtracking::solveSudoku<V>(mat, i, j + 1);
}
/// White Cell
/// Try to place every possible no
for (int no = 1; no <= 9; no++) {
if (backtracking::isPossible<V>(mat, i, j, no, 9)) {
/// Place the no - assuming solution aa jayega
mat[i][j] = no;
bool aageKiSolveHui = backtracking::solveSudoku<V>(mat, i, j + 1);
if (aageKiSolveHui) {
return true;
}
/// Nahin solve hui
/// loop will place the next no.
}
}
/// Sare no try kr liey, kisi se bhi solve nahi hui
mat[i][j] = 0;
return false;
}
} // namespace backtracking
/**
* Main function
*/
int main() { int main() {
int mat[9][9] = {{5, 3, 0, 0, 7, 0, 0, 0, 0}, {6, 0, 0, 1, 9, 5, 0, 0, 0}, const int V = 9;
{0, 9, 8, 0, 0, 0, 0, 6, 0}, {8, 0, 0, 0, 6, 0, 0, 0, 3}, std::array <std::array <int, V>, V> mat = {
{4, 0, 0, 8, 0, 3, 0, 0, 1}, {7, 0, 0, 0, 2, 0, 0, 0, 6}, std::array <int, V> {5, 3, 0, 0, 7, 0, 0, 0, 0},
{0, 6, 0, 0, 0, 0, 2, 8, 0}, {0, 0, 0, 4, 1, 9, 0, 0, 5}, std::array <int, V> {6, 0, 0, 1, 9, 5, 0, 0, 0},
{0, 0, 0, 0, 8, 0, 0, 7, 9}}; std::array <int, V> {0, 9, 8, 0, 0, 0, 0, 6, 0},
std::array <int, V> {8, 0, 0, 0, 6, 0, 0, 0, 3},
std::array <int, V> {4, 0, 0, 8, 0, 3, 0, 0, 1},
std::array <int, V> {7, 0, 0, 0, 2, 0, 0, 0, 6},
std::array <int, V> {0, 6, 0, 0, 0, 0, 2, 8, 0},
std::array <int, V> {0, 0, 0, 4, 1, 9, 0, 0, 5},
std::array <int, V> {0, 0, 0, 0, 8, 0, 0, 7, 9}
};
printMat(mat); backtracking::printMat<V>(mat, 9);
cout << "Solution " << endl; std::cout << "Solution " << std::endl;
solveSudoku(mat, 0, 0); backtracking::solveSudoku<V>(mat, 0, 0);
return 0; return 0;
} }