Algorithms_in_C++ 1.0.0
Set of algorithms implemented in C++.
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.

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

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
Vnumber of vertices in the graph
Parameters
graphmatrix of graph nonnectivity
mnumber of colors
[in,out]colordescription // used in,out to notify in documentation that this parameter gets modified by the function
vindex of graph vertex to check
73 {
74 // base case:
75 // If all vertices are assigned a color then return true
76 if (v == V) {
77 backtracking::printSolution<V>(color);
78 return;
79 }
80
81 // Consider this vertex v and try different colors
82 for (int c = 1; c <= m; c++) {
83 // Check if assignment of color c to v is fine
84 if (backtracking::isSafe<V>(v, graph, color, c)) {
85 color[v] = c;
86
87 // recur to assign colors to rest of the vertices
88 backtracking::graphColoring<V>(graph, m, color, v + 1);
89
90 // If assigning color c doesn't lead to a solution then remove it
91 color[v] = 0;
92 }
93 }
94}
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
Vnumber of vertices in the array
Parameters
matmatrix where numbers are saved
icurrent index in rows
jcurrent index in columns
nonumber to be added in matrix
nnumber 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

36 {
37 /// 'no' shouldn't be present in either row i or column j
38 for (int x = 0; x < n; x++) {
39 if (mat[x][j] == no || mat[i][x] == no) {
40 return false;
41 }
42 }
43
44 /// 'no' shouldn't be present in the 3*3 subgrid
45 int sx = (i / 3) * 3;
46 int sy = (j / 3) * 3;
47
48 for (int x = sx; x < sx + 3; x++) {
49 for (int y = sy; y < sy + 3; y++) {
50 if (mat[x][y] == no) {
51 return false;
52 }
53 }
54 }
55
56 return true;
57 }

◆ 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
Vnumber of vertices in the graph
Parameters
vindex of graph vertex to check
graphmatrix of graph nonnectivity
colorvector of colors assigned to the graph nodes/vertices
ccolor 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
54 {
55 for (int i = 0; i < V; i++) {
56 if (graph[v][i] && c == color[i]) {
57 return false;
58 }
59 }
60 return true;
61}

◆ 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
Vnumber of vertices in array
Parameters
xcurrent index in rows
ycurrent index in columns
solmatrix where numbers are saved
Returns
true if ....
false if ....
33 {
34 return (x < V && x >= 0 && y < V && y >= 0 && sol[x][y] == -1);
35 }

◆ 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
depthcurrent depth in game tree
node_indexcurrent index in array
is_maxif current index is the longest number
scoressaved numbers in array
heightmaximum height for game tree
Returns
maximum or minimum number
39 {
40 if (depth == height) {
41 return scores[node_index];
42 }
43
44 int v1 = minimax(depth + 1, node_index * 2, !is_max, scores, height);
45 int v2 = minimax(depth + 1, node_index * 2 + 1, !is_max, scores, height);
46
47 return is_max ? std::max(v1, v2) : std::min(v1, v2);
48}
int height(node *root)
Definition: avltree.cpp:31
T max(T... args)
int minimax(int depth, int node_index, bool is_max, const std::array< int, T > &scores, double height)
Definition: minimax.cpp:38
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
Vnumber of vertices in array
Parameters
matmatrix where numbers are saved
starting_matcopy of mat, required by printMat for highlighting the differences
nnumber of times loop will run
Returns
void
67 {
68 for (int i = 0; i < n; i++) {
69 for (int j = 0; j < n; j++) {
70 if (starting_mat[i][j] != mat[i][j]) {
71 std::cout << "\033[93m" << mat[i][j] << "\033[0m" << " ";
72 } else {
73 std::cout << mat[i][j] << " ";
74 }
75 if ((j + 1) % 3 == 0) {
76 std::cout << '\t';
77 }
78 }
79 if ((i + 1) % 3 == 0) {
81 }
83 }
84 }
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
Vnumber of vertices in the graph
Parameters
colorarray of colors assigned to the nodes
34 {
35 std::cout << "Following are the assigned colors" << std::endl;
36 for (auto& col : color) {
37 std::cout << col;
38 }
40}
Here is the call graph for this function:

◆ 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
Vnumber of vertices in array
Parameters
xcurrent index in rows
ycurrent index in columns
movmovement to be done
solmatrix where numbers are saved
xmovnext move of knight (x coordinate)
ymovnext move of knight (y coordinate)
Returns
true if solution exists
false if solution does not exist
51 {
52 int k, xnext, ynext;
53
54 if (mov == V * V) {
55 return true;
56 }
57
58 for (k = 0; k < V; k++) {
59 xnext = x + xmov[k];
60 ynext = y + ymov[k];
61
62 if (backtracking::issafe<V>(xnext, ynext, sol)) {
63 sol[xnext][ynext] = mov;
64
65 if (backtracking::solve<V>(xnext, ynext, mov + 1, sol, xmov, ymov) == true) {
66 return true;
67 }
68 else {
69 sol[xnext][ynext] = -1;
70 }
71 }
72 }
73 return false;
74 }
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
Vnumber of vertices in array
Parameters
matmatrix where numbers are saved
starting_matcopy of mat, required by printMat for highlighting the differences
icurrent index in rows
jcurrent 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

97 {
98 /// Base Case
99 if (i == 9) {
100 /// Solved for 9 rows already
101 backtracking::printMat<V>(mat, starting_mat, 9);
102 return true;
103 }
104
105 /// Crossed the last Cell in the row
106 if (j == 9) {
107 return backtracking::solveSudoku<V>(mat, starting_mat, i + 1, 0);
108 }
109
110 /// Blue Cell - Skip
111 if (mat[i][j] != 0) {
112 return backtracking::solveSudoku<V>(mat, starting_mat, i, j + 1);
113 }
114 /// White Cell
115 /// Try to place every possible no
116 for (int no = 1; no <= 9; no++) {
117 if (backtracking::isPossible<V>(mat, i, j, no, 9)) {
118 /// Place the 'no' - assuming a solution will exist
119 mat[i][j] = no;
120 bool solution_found = backtracking::solveSudoku<V>(mat, starting_mat, i, j + 1);
121 if (solution_found) {
122 return true;
123 }
124 /// Couldn't find a solution
125 /// loop will place the next no.
126 }
127 }
128 /// Solution couldn't be found for any of the numbers provided
129 mat[i][j] = 0;
130 return false;
131 }