|
Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
|
Backtracking algorithms. More...
Functions | |
| template<size_t V> | |
| void | printSolution (const std::array< int, V > &color) |
| 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) |
| template<size_t V> | |
| void | graphColoring (const std::array< std::array< int, V >, V > &graph, int m, std::array< int, V > color, int v) |
| template<size_t V> | |
| bool | issafe (int x, int y, const std::array< std::array< int, V >, V > &sol) |
| 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) |
| template<size_t T> | |
| int | minimax (int depth, int node_index, bool is_max, const std::array< int, T > &scores, double height) |
| template<size_t V> | |
| bool | isPossible (const std::array< std::array< int, V >, V > &mat, int i, int j, int no, int n) |
| 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) |
| 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) |
Backtracking algorithms.
for std::vector
for std::count for assert for IO operations for std::list for std::accumulate
Backtracking algorithms
for assert for IO operations for unordered_map
Backtracking algorithms
for assert for IO operations
Backtracking algorithms
| void backtracking::graphColoring | ( | const std::array< std::array< int, V >, V > & | graph, |
| int | m, | ||
| std::array< int, V > | color, | ||
| int | v | ||
| ) |
A recursive utility function to solve m coloring problem
| V | number of vertices in the graph |
| graph | matrix of graph nonnectivity | |
| m | number of colors | |
| [in,out] | color | description // used in,out to notify in documentation that this parameter gets modified by the function |
| v | index of graph vertex to check |
| bool backtracking::isPossible | ( | const std::array< std::array< int, V >, V > & | mat, |
| int | i, | ||
| int | j, | ||
| int | no, | ||
| int | n | ||
| ) |
Checks if it's possible to place a number 'no'
| V | number of vertices in the array |
| mat | matrix where numbers are saved |
| i | current index in rows |
| j | current index in columns |
| no | number to be added in matrix |
| n | number of times loop will run |
true if 'mat' is different from 'no' false if 'mat' equals to 'no' 'no' shouldn't be present in either row i or column j
'no' shouldn't be present in the 3*3 subgrid
| bool backtracking::isSafe | ( | int | v, |
| const std::array< std::array< int, V >, V > & | graph, | ||
| const std::array< int, V > & | color, | ||
| int | c | ||
| ) |
A utility function to check if the current color assignment is safe for vertex v
| V | number of vertices in the graph |
| v | index of graph vertex to check |
| graph | matrix of graph nonnectivity |
| color | vector of colors assigned to the graph nodes/vertices |
| c | color value to check for the node v |
true if the color is safe to be assigned to the node false if the color is not safe to be assigned to the node | bool backtracking::issafe | ( | int | x, |
| int | y, | ||
| const std::array< std::array< int, V >, V > & | sol | ||
| ) |
A utility function to check if i,j are valid indexes for N*N chessboard
| V | number of vertices in array |
| x | current index in rows |
| y | current index in columns |
| sol | matrix where numbers are saved |
true if .... false if .... | int backtracking::minimax | ( | int | depth, |
| int | node_index, | ||
| bool | is_max, | ||
| const std::array< int, T > & | scores, | ||
| double | height | ||
| ) |
Check which number is the maximum/minimum in the array
| depth | current depth in game tree |
| node_index | current index in array |
| is_max | if current index is the longest number |
| scores | saved numbers in array |
| height | maximum height for game tree |
| void backtracking::printMat | ( | const std::array< std::array< int, V >, V > & | mat, |
| const std::array< std::array< int, V >, V > & | starting_mat, | ||
| int | n | ||
| ) |
Utility function to print matrix
| V | number of vertices in array |
| mat | matrix where numbers are saved |
| starting_mat | copy of mat, required by printMat for highlighting the differences |
| n | number of times loop will run |
| void backtracking::printSolution | ( | const std::array< int, V > & | color | ) |
A utility function to print solution
| V | number of vertices in the graph |
| color | array of colors assigned to the nodes |
| bool backtracking::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 | ||
| ) |
Knight's tour algorithm
| V | number of vertices in array |
| x | current index in rows |
| y | current index in columns |
| mov | movement to be done |
| sol | matrix where numbers are saved |
| xmov | next move of knight (x coordinate) |
| ymov | next move of knight (y coordinate) |
true if solution exists false if solution does not exist | bool backtracking::solveSudoku | ( | std::array< std::array< int, V >, V > & | mat, |
| const std::array< std::array< int, V >, V > & | starting_mat, | ||
| int | i, | ||
| int | j | ||
| ) |
Sudoku algorithm
| V | number of vertices in array |
| mat | matrix where numbers are saved |
| starting_mat | copy of mat, required by printMat for highlighting the differences |
| i | current index in rows |
| j | current index in columns |
true if 'no' was placed false if 'no' was not placed Base Case
Solved for 9 rows already
Crossed the last Cell in the row
Blue Cell - Skip
White Cell Try to place every possible no
Place the 'no' - assuming a solution will exist
Couldn't find a solution loop will place the next no.
Solution couldn't be found for any of the numbers provided