backtracking Namespace Reference

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)

## Detailed Description

Backtracking algorithms.

## Function Documentation

## graphColoring()

template<size_t V>

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

Template Parameters

  V | number of vertices in the graph

Parameters

  | 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

{
// 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;
}
}
}
graph
Graph Algorithms.

## isPossible()

template<size_t V>

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'

Template Parameters

  V | number of vertices in the array

Parameters

  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

Returns
  `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

{
/// 'no' shouldn't be present in either row i or column j
for (int x = 0; x < n; x++) {
if (mat[x][j] == no || mat[i][x] == no) {
return false;
}
}
/// 'no' shouldn't be present in the 3*3 subgrid
int sx = (i / 3) * 3;
int sy = (j / 3) * 3;
for (int x = sx; x < sx + 3; x++) {
for (int y = sy; y < sy + 3; y++) {
if (mat[x][y] == no) {
return false;
}
}
}
return true;
}

## isSafe()

template<size_t V>

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

Template Parameters

  V | number of vertices in the graph

Parameters

  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`

Returns
  `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

{
for (int i = 0; i < V; i++) {
if (graph[v][i] && c == color[i]) {
return false;
}
}
return true;
}

## issafe()

template<size_t V>

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

Template Parameters

  V | number of vertices in array

Parameters

  x | current index in rows
  y | current index in columns
  sol | matrix where numbers are saved

Returns
  `true` if ....
  `false` if ....

{
return (x < V && x >= 0 && y < V && y >= 0 && sol[x][y] == -1);
}

## minimax()

template<size_t T>

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

Parameters

  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

Returns
  maximum or minimum number

{
if (depth == height) {
return scores[node_index];
}
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);
return is_max ? std::max(v1, v2) : std::min(v1, v2);
}
height
int height(node *root)
Definition: avltree.cpp:31
std::max
T max(T... args)
std::min
T min(T... args)
backtracking::minimax
int minimax(int depth, int node_index, bool is_max, const std::array< int, T > &scores, double height)
Definition: minimax.cpp:38
std
STL namespace.
Here is the call graph for this function:

## printMat()

template<size_t V>

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

Template Parameters

  V | number of vertices in array

Parameters

  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

Returns
  void

{
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (starting_mat[i][j] != mat[i][j]) {
std::cout << "\033[93m" << mat[i][j] << "\033[0m" << " ";
} else {
std::cout << mat[i][j] << " ";
}
if ((j + 1) % 3 == 0) {
std::cout << '\t';
}
}
if ((i + 1) % 3 == 0) {
std::cout << std::endl;
}
std::cout << std::endl;
}
}
std::cout
std::endl
T endl(T... args)
Here is the call graph for this function:

## printSolution()

template<size_t V>

void backtracking::printSolution | ( | const std::array< int, V > & | color | ) |

A utility function to print solution

Template Parameters

  V | number of vertices in the graph

Parameters

  color | array of colors assigned to the nodes

{
std::cout << "Following are the assigned colors\n";
for (auto &col : color) {
std::cout << col;
}
std::cout << "\n";
}

## solve()

template<size_t V>

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

Template Parameters

  V | number of vertices in array

Parameters

  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)

Returns
  `true` if solution exists
  `false` if solution does not exist

{
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;
}
mov
void mov(tower *From, tower *To)
Definition: tower_of_hanoi.cpp:39
Here is the call graph for this function:

## solveSudoku()

template<size_t V>

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

Template Parameters

  V | number of vertices in array

Parameters

  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

Returns
  `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

{
/// Base Case
if (i == 9) {
/// Solved for 9 rows already
backtracking::printMat<V>(mat, starting_mat, 9);
return true;
}
/// Crossed the last Cell in the row
if (j == 9) {
return backtracking::solveSudoku<V>(mat, starting_mat, i + 1, 0);
}
/// Blue Cell - Skip
if (mat[i][j] != 0) {
return backtracking::solveSudoku<V>(mat, starting_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 a solution will exist
mat[i][j] = no;
bool solution_found = backtracking::solveSudoku<V>(mat, starting_mat, i, j + 1);
if (solution_found) {
return true;
}
/// Couldn't find a solution
/// loop will place the next no.
}
}
/// Solution couldn't be found for any of the numbers provided
mat[i][j] = 0;
return false;
}