Skip to content

Commit 9cc3951

Browse files
committed
[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.
1 parent 97afa0e commit 9cc3951

10 files changed

+351
-318
lines changed

backtracking/graph_coloring.cpp

Lines changed: 70 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -15,83 +15,93 @@
1515
* @author [Anup Kumar Panwar](https://github.com/AnupKumarPanwar)
1616
* @author [David Leal](https://github.com/Panquesito7)
1717
*/
18-
#include <iostream>
19-
#include <array>
20-
#include <vector>
18+
#include <iostream> /// for IO operations
19+
#include <array> /// for std::array
20+
#include <vector> /// for std::vector
2121

2222
/**
23-
* @namespace
23+
* @namespace backtracking
2424
* @brief Backtracking algorithms
2525
*/
2626
namespace backtracking {
27-
/** A utility function to print solution
28-
* @tparam V number of vertices in the graph
29-
* @param color array of colors assigned to the nodes
30-
*/
31-
template <size_t V>
32-
void printSolution(const std::array <int, V>& color) {
33-
std::cout << "Following are the assigned colors\n";
34-
for (auto &col : color) {
35-
std::cout << col;
36-
}
37-
std::cout << "\n";
27+
/**
28+
* @namespace graph_coloring
29+
* @brief Functions for the [Graph Coloring](https://en.wikipedia.org/wiki/Graph_coloring) algorith,
30+
*/
31+
namespace graph_coloring {
32+
/**
33+
* @brief A utility function to print the solution
34+
* @tparam V number of vertices in the graph
35+
* @param color array of colors assigned to the nodes
36+
*/
37+
template <size_t V>
38+
void printSolution(const std::array <int, V>& color) {
39+
std::cout << "Following are the assigned colors\n";
40+
for (auto &col : color) {
41+
std::cout << col;
3842
}
43+
std::cout << "\n";
44+
}
3945

40-
/** A utility function to check if the current color assignment is safe for
41-
* vertex v
42-
* @tparam V number of vertices in the graph
43-
* @param v index of graph vertex to check
44-
* @param graph matrix of graph nonnectivity
45-
* @param color vector of colors assigned to the graph nodes/vertices
46-
* @param c color value to check for the node `v`
47-
* @returns `true` if the color is safe to be assigned to the node
48-
* @returns `false` if the color is not safe to be assigned to the node
49-
*/
50-
template <size_t V>
51-
bool isSafe(int v, const std::array<std::array <int, V>, V>& graph, const std::array <int, V>& color, int c) {
52-
for (int i = 0; i < V; i++) {
53-
if (graph[v][i] && c == color[i]) {
54-
return false;
55-
}
46+
/**
47+
* @brief Utility function to check if the current color assignment is safe for
48+
* vertex v
49+
* @tparam V number of vertices in the graph
50+
* @param v index of graph vertex to check
51+
* @param graph matrix of graph nonnectivity
52+
* @param color vector of colors assigned to the graph nodes/vertices
53+
* @param c color value to check for the node `v`
54+
* @returns `true` if the color is safe to be assigned to the node
55+
* @returns `false` if the color is not safe to be assigned to the node
56+
*/
57+
template <size_t V>
58+
bool isSafe(int v, const std::array<std::array <int, V>, V>& graph, const std::array <int, V>& color, int c) {
59+
for (int i = 0; i < V; i++) {
60+
if (graph[v][i] && c == color[i]) {
61+
return false;
5662
}
57-
return true;
5863
}
64+
return true;
65+
}
5966

60-
/** A recursive utility function to solve m coloring problem
61-
* @tparam V number of vertices in the graph
62-
* @param graph matrix of graph nonnectivity
63-
* @param m number of colors
64-
* @param [in,out] color description // used in,out to notify in documentation
65-
* that this parameter gets modified by the function
66-
* @param v index of graph vertex to check
67-
*/
68-
template <size_t V>
69-
void graphColoring(const std::array<std::array <int, V>, V>& graph, int m, std::array <int, V> color, int v) {
70-
// base case:
71-
// If all vertices are assigned a color then return true
72-
if (v == V) {
73-
backtracking::printSolution<V>(color);
74-
return;
75-
}
67+
/**
68+
* @brief Recursive utility function to solve m coloring problem
69+
* @tparam V number of vertices in the graph
70+
* @param graph matrix of graph nonnectivity
71+
* @param m number of colors
72+
* @param [in,out] color description // used in,out to notify in documentation
73+
* that this parameter gets modified by the function
74+
* @param v index of graph vertex to check
75+
*/
76+
template <size_t V>
77+
void graphColoring(const std::array<std::array <int, V>, V>& graph, int m, std::array <int, V> color, int v) {
78+
// base case:
79+
// If all vertices are assigned a color then return true
80+
if (v == V) {
81+
printSolution<V>(color);
82+
return;
83+
}
7684

77-
// Consider this vertex v and try different colors
78-
for (int c = 1; c <= m; c++) {
79-
// Check if assignment of color c to v is fine
80-
if (backtracking::isSafe<V>(v, graph, color, c)) {
81-
color[v] = c;
85+
// Consider this vertex v and try different colors
86+
for (int c = 1; c <= m; c++) {
87+
// Check if assignment of color c to v is fine
88+
if (isSafe<V>(v, graph, color, c)) {
89+
color[v] = c;
8290

83-
// recur to assign colors to rest of the vertices
84-
backtracking::graphColoring<V>(graph, m, color, v + 1);
91+
// recur to assign colors to rest of the vertices
92+
graphColoring<V>(graph, m, color, v + 1);
8593

86-
// If assigning color c doesn't lead to a solution then remove it
87-
color[v] = 0;
88-
}
94+
// If assigning color c doesn't lead to a solution then remove it
95+
color[v] = 0;
8996
}
9097
}
98+
}
99+
} // namespace graph_coloring
91100
} // namespace backtracking
92101

93102
/**
94-
* Main function
103+
* @brief Main function
104+
* @returns 0 on exit
95105
*/
96106
int main() {
97107
// Create following graph and test whether it is 3 colorable
@@ -112,6 +122,6 @@ int main() {
112122
int m = 3; // Number of colors
113123
std::array <int, V> color{};
114124

115-
backtracking::graphColoring<V>(graph, m, color, 0);
125+
backtracking::graph_coloring::graphColoring<V>(graph, m, color, 0);
116126
return 0;
117127
}

backtracking/knight_tour.cpp

Lines changed: 56 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -12,70 +12,77 @@
1212
* @author [Nikhil Arora](https://github.com/nikhilarora068)
1313
* @author [David Leal](https://github.com/Panquesito7)
1414
*/
15-
#include <iostream>
16-
#include <array>
15+
#include <iostream> /// for IO operations
16+
#include <array> /// for std::array
1717

1818
/**
1919
* @namespace backtracking
2020
* @brief Backtracking algorithms
2121
*/
2222
namespace backtracking {
23-
/**
24-
* A utility function to check if i,j are valid indexes for N*N chessboard
25-
* @tparam V number of vertices in array
26-
* @param x current index in rows
27-
* @param y current index in columns
28-
* @param sol matrix where numbers are saved
29-
* @returns `true` if ....
30-
* @returns `false` if ....
31-
*/
32-
template <size_t V>
33-
bool issafe(int x, int y, const std::array <std::array <int, V>, V>& sol) {
34-
return (x < V && x >= 0 && y < V && y >= 0 && sol[x][y] == -1);
35-
}
23+
/**
24+
* @namespace knight_tour
25+
* @brief Functions for the [Knight's tour](https://en.wikipedia.org/wiki/Knight%27s_tour) algorithm
26+
*/
27+
namespace knight_tour {
28+
/**
29+
* A utility function to check if i,j are valid indexes for N*N chessboard
30+
* @tparam V number of vertices in array
31+
* @param x current index in rows
32+
* @param y current index in columns
33+
* @param sol matrix where numbers are saved
34+
* @returns `true` if ....
35+
* @returns `false` if ....
36+
*/
37+
template <size_t V>
38+
bool issafe(int x, int y, const std::array <std::array <int, V>, V>& sol) {
39+
return (x < V && x >= 0 && y < V && y >= 0 && sol[x][y] == -1);
40+
}
3641

37-
/**
38-
* Knight's tour algorithm
39-
* @tparam V number of vertices in array
40-
* @param x current index in rows
41-
* @param y current index in columns
42-
* @param mov movement to be done
43-
* @param sol matrix where numbers are saved
44-
* @param xmov next move of knight (x coordinate)
45-
* @param ymov next move of knight (y coordinate)
46-
* @returns `true` if solution exists
47-
* @returns `false` if solution does not exist
48-
*/
49-
template <size_t V>
50-
bool solve(int x, int y, int mov, std::array <std::array <int, V>, V> &sol,
51-
const std::array <int, V> &xmov, std::array <int, V> &ymov) {
52-
int k, xnext, ynext;
42+
/**
43+
* Knight's tour algorithm
44+
* @tparam V number of vertices in array
45+
* @param x current index in rows
46+
* @param y current index in columns
47+
* @param mov movement to be done
48+
* @param sol matrix where numbers are saved
49+
* @param xmov next move of knight (x coordinate)
50+
* @param ymov next move of knight (y coordinate)
51+
* @returns `true` if solution exists
52+
* @returns `false` if solution does not exist
53+
*/
54+
template <size_t V>
55+
bool solve(int x, int y, int mov, std::array <std::array <int, V>, V> &sol,
56+
const std::array <int, V> &xmov, std::array <int, V> &ymov) {
57+
int k = 0, xnext = 0, ynext = 0;
5358

54-
if (mov == V * V) {
55-
return true;
56-
}
59+
if (mov == V * V) {
60+
return true;
61+
}
5762

58-
for (k = 0; k < V; k++) {
59-
xnext = x + xmov[k];
60-
ynext = y + ymov[k];
63+
for (k = 0; k < V; k++) {
64+
xnext = x + xmov[k];
65+
ynext = y + ymov[k];
6166

62-
if (backtracking::issafe<V>(xnext, ynext, sol)) {
63-
sol[xnext][ynext] = mov;
67+
if (issafe<V>(xnext, ynext, sol)) {
68+
sol[xnext][ynext] = mov;
6469

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-
}
70+
if (solve<V>(xnext, ynext, mov + 1, sol, xmov, ymov) == true) {
71+
return true;
72+
}
73+
else {
74+
sol[xnext][ynext] = -1;
7175
}
7276
}
73-
return false;
7477
}
75-
} // namespace backtracking
78+
return false;
79+
}
80+
} // namespace knight_tour
81+
} // namespace backtracking
7682

7783
/**
78-
* Main function
84+
* @brief Main function
85+
* @returns 0 on exit
7986
*/
8087
int main() {
8188
const int n = 8;
@@ -91,7 +98,7 @@ int main() {
9198

9299
sol[0][0] = 0;
93100

94-
bool flag = backtracking::solve<n>(0, 0, 1, sol, xmov, ymov);
101+
bool flag = backtracking::knight_tour::solve<n>(0, 0, 1, sol, xmov, ymov);
95102
if (flag == false) {
96103
std::cout << "Error: Solution does not exist\n";
97104
}

backtracking/minimax.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,24 @@
1515
* @author [Gleison Batista](https://github.com/gleisonbs)
1616
* @author [David Leal](https://github.com/Panquesito7)
1717
*/
18-
#include <algorithm>
19-
#include <cmath>
20-
#include <iostream>
21-
#include <array>
18+
#include <algorithm> /// for std::max, std::min
19+
#include <cmath> /// for log2
20+
#include <iostream> /// for IO operations
21+
#include <array> /// for std::array
2222

2323
/**
2424
* @namespace backtracking
2525
* @brief Backtracking algorithms
2626
*/
2727
namespace backtracking {
2828
/**
29-
* Check which number is the maximum/minimum in the array
29+
* @brief Check which is the maximum/minimum number in the array
3030
* @param depth current depth in game tree
3131
* @param node_index current index in array
3232
* @param is_max if current index is the longest number
3333
* @param scores saved numbers in array
3434
* @param height maximum height for game tree
35-
* @return maximum or minimum number
35+
* @returns the maximum or minimum number
3636
*/
3737
template <size_t T>
3838
int minimax(int depth, int node_index, bool is_max,
@@ -46,10 +46,11 @@ int minimax(int depth, int node_index, bool is_max,
4646

4747
return is_max ? std::max(v1, v2) : std::min(v1, v2);
4848
}
49-
} // namespace backtracking
49+
} // namespace backtracking
5050

5151
/**
52-
* Main function
52+
* @brief Main function
53+
* @returns 0 on exit
5354
*/
5455
int main() {
5556
std::array<int, 8> scores = {90, 23, 6, 33, 21, 65, 123, 34423};

0 commit comments

Comments
 (0)