Skip to content

Commit 39380fe

Browse files
github-actionsgithub-actions
authored andcommitted
clang-format and clang-tidy fixes for 9cc3951
1 parent 9cc3951 commit 39380fe

10 files changed

+507
-427
lines changed

backtracking/graph_coloring.cpp

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,25 @@
11
/**
22
* @file
33
* @brief prints the assigned colors
4-
* using [Graph Coloring](https://en.wikipedia.org/wiki/Graph_coloring) algorithm
4+
* using [Graph Coloring](https://en.wikipedia.org/wiki/Graph_coloring)
5+
* algorithm
56
*
67
* @details
7-
* In graph theory, graph coloring is a special case of graph labeling;
8-
* it is an assignment of labels traditionally called "colors" to elements of a graph subject to certain constraints.
9-
* 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;
10-
* this is called a vertex coloring. Similarly, an edge coloring assigns
11-
* a color to each edge so that no two adjacent edges are of the same color,
12-
* and a face coloring of a planar graph assigns a color to each face or
8+
* In graph theory, graph coloring is a special case of graph labeling;
9+
* it is an assignment of labels traditionally called "colors" to elements of a
10+
* graph subject to certain constraints. In its simplest form, it is a way of
11+
* coloring the vertices of a graph such that no two adjacent vertices are of
12+
* the same color; this is called a vertex coloring. Similarly, an edge coloring
13+
* assigns a color to each edge so that no two adjacent edges are of the same
14+
* color, and a face coloring of a planar graph assigns a color to each face or
1315
* region so that no two faces that share a boundary have the same color.
1416
*
1517
* @author [Anup Kumar Panwar](https://github.com/AnupKumarPanwar)
1618
* @author [David Leal](https://github.com/Panquesito7)
1719
*/
18-
#include <iostream> /// for IO operations
19-
#include <array> /// for std::array
20-
#include <vector> /// for std::vector
20+
#include <array> /// for std::array
21+
#include <iostream> /// for IO operations
22+
#include <vector> /// for std::vector
2123

2224
/**
2325
* @namespace backtracking
@@ -26,7 +28,8 @@
2628
namespace backtracking {
2729
/**
2830
* @namespace graph_coloring
29-
* @brief Functions for the [Graph Coloring](https://en.wikipedia.org/wiki/Graph_coloring) algorith,
31+
* @brief Functions for the [Graph
32+
* Coloring](https://en.wikipedia.org/wiki/Graph_coloring) algorith,
3033
*/
3134
namespace graph_coloring {
3235
/**
@@ -35,9 +38,9 @@ namespace graph_coloring {
3538
* @param color array of colors assigned to the nodes
3639
*/
3740
template <size_t V>
38-
void printSolution(const std::array <int, V>& color) {
41+
void printSolution(const std::array<int, V>& color) {
3942
std::cout << "Following are the assigned colors\n";
40-
for (auto &col : color) {
43+
for (auto& col : color) {
4144
std::cout << col;
4245
}
4346
std::cout << "\n";
@@ -55,7 +58,8 @@ void printSolution(const std::array <int, V>& color) {
5558
* @returns `false` if the color is not safe to be assigned to the node
5659
*/
5760
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) {
61+
bool isSafe(int v, const std::array<std::array<int, V>, V>& graph,
62+
const std::array<int, V>& color, int c) {
5963
for (int i = 0; i < V; i++) {
6064
if (graph[v][i] && c == color[i]) {
6165
return false;
@@ -74,7 +78,8 @@ bool isSafe(int v, const std::array<std::array <int, V>, V>& graph, const std::a
7478
* @param v index of graph vertex to check
7579
*/
7680
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) {
81+
void graphColoring(const std::array<std::array<int, V>, V>& graph, int m,
82+
std::array<int, V> color, int v) {
7883
// base case:
7984
// If all vertices are assigned a color then return true
8085
if (v == V) {
@@ -112,15 +117,12 @@ int main() {
112117
// (0)---(1)
113118

114119
const int V = 4; // number of vertices in the graph
115-
std::array <std::array <int, V>, V> graph = {
116-
std::array <int, V>({0, 1, 1, 1}),
117-
std::array <int, V>({1, 0, 1, 0}),
118-
std::array <int, V>({1, 1, 0, 1}),
119-
std::array <int, V>({1, 0, 1, 0})
120-
};
120+
std::array<std::array<int, V>, V> graph = {
121+
std::array<int, V>({0, 1, 1, 1}), std::array<int, V>({1, 0, 1, 0}),
122+
std::array<int, V>({1, 1, 0, 1}), std::array<int, V>({1, 0, 1, 0})};
121123

122124
int m = 3; // Number of colors
123-
std::array <int, V> color{};
125+
std::array<int, V> color{};
124126

125127
backtracking::graph_coloring::graphColoring<V>(graph, m, color, 0);
126128
return 0;

backtracking/knight_tour.cpp

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/**
22
* @file
3-
* @brief [Knight's tour](https://en.wikipedia.org/wiki/Knight%27s_tour) algorithm
3+
* @brief [Knight's tour](https://en.wikipedia.org/wiki/Knight%27s_tour)
4+
* algorithm
45
*
56
* @details
67
* A knight's tour is a sequence of moves of a knight on a chessboard
@@ -12,8 +13,8 @@
1213
* @author [Nikhil Arora](https://github.com/nikhilarora068)
1314
* @author [David Leal](https://github.com/Panquesito7)
1415
*/
15-
#include <iostream> /// for IO operations
16-
#include <array> /// for std::array
16+
#include <array> /// for std::array
17+
#include <iostream> /// for IO operations
1718

1819
/**
1920
* @namespace backtracking
@@ -22,7 +23,8 @@
2223
namespace backtracking {
2324
/**
2425
* @namespace knight_tour
25-
* @brief Functions for the [Knight's tour](https://en.wikipedia.org/wiki/Knight%27s_tour) algorithm
26+
* @brief Functions for the [Knight's
27+
* tour](https://en.wikipedia.org/wiki/Knight%27s_tour) algorithm
2628
*/
2729
namespace knight_tour {
2830
/**
@@ -35,7 +37,7 @@ namespace knight_tour {
3537
* @returns `false` if ....
3638
*/
3739
template <size_t V>
38-
bool issafe(int x, int y, const std::array <std::array <int, V>, V>& sol) {
40+
bool issafe(int x, int y, const std::array<std::array<int, V>, V> &sol) {
3941
return (x < V && x >= 0 && y < V && y >= 0 && sol[x][y] == -1);
4042
}
4143

@@ -52,8 +54,8 @@ bool issafe(int x, int y, const std::array <std::array <int, V>, V>& sol) {
5254
* @returns `false` if solution does not exist
5355
*/
5456
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+
bool solve(int x, int y, int mov, std::array<std::array<int, V>, V> &sol,
58+
const std::array<int, V> &xmov, std::array<int, V> &ymov) {
5759
int k = 0, xnext = 0, ynext = 0;
5860

5961
if (mov == V * V) {
@@ -69,8 +71,7 @@ bool solve(int x, int y, int mov, std::array <std::array <int, V>, V> &sol,
6971

7072
if (solve<V>(xnext, ynext, mov + 1, sol, xmov, ymov) == true) {
7173
return true;
72-
}
73-
else {
74+
} else {
7475
sol[xnext][ynext] = -1;
7576
}
7677
}
@@ -86,25 +87,28 @@ bool solve(int x, int y, int mov, std::array <std::array <int, V>, V> &sol,
8687
*/
8788
int main() {
8889
const int n = 8;
89-
std::array <std::array <int, n>, n> sol = { 0 };
90+
std::array<std::array<int, n>, n> sol = {0};
9091

91-
int i, j;
92+
int i = 0, j = 0;
9293
for (i = 0; i < n; i++) {
93-
for (j = 0; j < n; j++) { sol[i][j] = -1; }
94+
for (j = 0; j < n; j++) {
95+
sol[i][j] = -1;
96+
}
9497
}
9598

96-
std::array <int, n> xmov = { 2, 1, -1, -2, -2, -1, 1, 2 };
97-
std::array <int, n> ymov = { 1, 2, 2, 1, -1, -2, -2, -1 };
99+
std::array<int, n> xmov = {2, 1, -1, -2, -2, -1, 1, 2};
100+
std::array<int, n> ymov = {1, 2, 2, 1, -1, -2, -2, -1};
98101

99102
sol[0][0] = 0;
100103

101104
bool flag = backtracking::knight_tour::solve<n>(0, 0, 1, sol, xmov, ymov);
102105
if (flag == false) {
103106
std::cout << "Error: Solution does not exist\n";
104-
}
105-
else {
107+
} else {
106108
for (i = 0; i < n; i++) {
107-
for (j = 0; j < n; j++) { std::cout << sol[i][j] << " "; }
109+
for (j = 0; j < n; j++) {
110+
std::cout << sol[i][j] << " ";
111+
}
108112
std::cout << "\n";
109113
}
110114
}

backtracking/minimax.cpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,22 @@
66
* @details
77
* Minimax (sometimes MinMax, MM or saddle point) is a decision rule used in
88
* artificial intelligence, decision theory, game theory, statistics,
9-
* and philosophy for minimizing the possible loss for a worst case (maximum loss) scenario.
10-
* When dealing with gains, it is referred to as "maximin"—to maximize the minimum gain.
11-
* Originally formulated for two-player zero-sum game theory, covering both the cases where players take
12-
* alternate moves and those where they make simultaneous moves, it has also been extended to more
13-
* complex games and to general decision-making in the presence of uncertainty.
14-
*
9+
* and philosophy for minimizing the possible loss for a worst case (maximum
10+
* loss) scenario. When dealing with gains, it is referred to as "maximin"—to
11+
* maximize the minimum gain. Originally formulated for two-player zero-sum game
12+
* theory, covering both the cases where players take alternate moves and those
13+
* where they make simultaneous moves, it has also been extended to more complex
14+
* games and to general decision-making in the presence of uncertainty.
15+
*
1516
* @author [Gleison Batista](https://github.com/gleisonbs)
1617
* @author [David Leal](https://github.com/Panquesito7)
1718
*/
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
19+
#include <algorithm> /// for std::max, std::min
20+
#include <array> /// for std::array
21+
#include <cmath> /// for log2
22+
#include <iostream> /// for IO operations
2223

23-
/**
24+
/**
2425
* @namespace backtracking
2526
* @brief Backtracking algorithms
2627
*/
@@ -56,7 +57,7 @@ int main() {
5657
std::array<int, 8> scores = {90, 23, 6, 33, 21, 65, 123, 34423};
5758
double height = log2(scores.size());
5859

59-
std::cout << "Optimal value: " << backtracking::minimax(0, 0, true, scores, height)
60-
<< std::endl;
60+
std::cout << "Optimal value: "
61+
<< backtracking::minimax(0, 0, true, scores, height) << std::endl;
6162
return 0;
6263
}

backtracking/n_queens.cpp

Lines changed: 50 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
* @author [David Leal](https://github.com/Panquesito7)
1616
*
1717
*/
18-
#include <iostream>
1918
#include <array>
19+
#include <iostream>
2020

2121
/**
2222
* @namespace backtracking
@@ -25,7 +25,8 @@
2525
namespace backtracking {
2626
/**
2727
* @namespace n_queens
28-
* @brief Functions for [Eight Queens](https://en.wikipedia.org/wiki/Eight_queens_puzzle) puzzle.
28+
* @brief Functions for [Eight
29+
* Queens](https://en.wikipedia.org/wiki/Eight_queens_puzzle) puzzle.
2930
*/
3031
namespace n_queens {
3132
/**
@@ -35,14 +36,14 @@ namespace n_queens {
3536
*/
3637
template <size_t n>
3738
void printSolution(const std::array<std::array<int, n>, n> &board) {
38-
std::cout << "\n";
39+
std::cout << "\n";
3940
for (int i = 0; i < n; i++) {
40-
for (int j = 0; j < n; j++) {
41-
std::cout << "" << board[i][j] << " ";
42-
}
43-
std::cout << "\n";
41+
for (int j = 0; j < n; j++) {
42+
std::cout << "" << board[i][j] << " ";
43+
}
44+
std::cout << "\n";
4445
}
45-
}
46+
}
4647

4748
/**
4849
* Check if a queen can be placed on matrix
@@ -56,28 +57,28 @@ void printSolution(const std::array<std::array<int, n>, n> &board) {
5657
template <size_t n>
5758
bool isSafe(const std::array<std::array<int, n>, n> &board, const int &row,
5859
const int &col) {
59-
int i = 0, j = 0;
60+
int i = 0, j = 0;
6061

61-
// Check this row on left side
62-
for (i = 0; i < col; i++) {
63-
if (board[row][i]) {
64-
return false;
62+
// Check this row on left side
63+
for (i = 0; i < col; i++) {
64+
if (board[row][i]) {
65+
return false;
66+
}
6567
}
66-
}
6768

68-
// Check upper diagonal on left side
69-
for (i = row, j = col; i >= 0 && j >= 0; i--, j--) {
70-
if (board[i][j]) {
71-
return false;
69+
// Check upper diagonal on left side
70+
for (i = row, j = col; i >= 0 && j >= 0; i--, j--) {
71+
if (board[i][j]) {
72+
return false;
73+
}
7274
}
73-
}
74-
// Check lower diagonal on left side
75-
for (i = row, j = col; j >= 0 && i < n; i++, j--) {
76-
if (board[i][j]) {
77-
return false;
75+
// Check lower diagonal on left side
76+
for (i = row, j = col; j >= 0 && i < n; i++, j--) {
77+
if (board[i][j]) {
78+
return false;
79+
}
7880
}
79-
}
80-
return true;
81+
return true;
8182
}
8283

8384
/**
@@ -88,43 +89,40 @@ bool isSafe(const std::array<std::array<int, n>, n> &board, const int &row,
8889
*/
8990
template <size_t n>
9091
void solveNQ(std::array<std::array<int, n>, n> board, const int &col) {
91-
if (col >= n) {
92-
printSolution<n>(board);
93-
return;
94-
}
92+
if (col >= n) {
93+
printSolution<n>(board);
94+
return;
95+
}
9596

96-
// Consider this column and try placing
97-
// this queen in all rows one by one
98-
for (int i = 0; i < n; i++) {
99-
// Check if queen can be placed
100-
// on board[i][col]
101-
if (isSafe<n>(board, i, col)) {
102-
// Place this queen in matrix
103-
board[i][col] = 1;
97+
// Consider this column and try placing
98+
// this queen in all rows one by one
99+
for (int i = 0; i < n; i++) {
100+
// Check if queen can be placed
101+
// on board[i][col]
102+
if (isSafe<n>(board, i, col)) {
103+
// Place this queen in matrix
104+
board[i][col] = 1;
104105

105-
// Recursive to place rest of the queens
106-
solveNQ<n>(board, col + 1);
106+
// Recursive to place rest of the queens
107+
solveNQ<n>(board, col + 1);
107108

108-
board[i][col] = 0; // backtrack
109+
board[i][col] = 0; // backtrack
110+
}
109111
}
110-
}
111112
}
112-
} // namespace n_queens
113-
} // namespace backtracking
113+
} // namespace n_queens
114+
} // namespace backtracking
114115

115116
/**
116117
* @brief Main function
117118
* @returns 0 on exit
118119
*/
119120
int main() {
120-
const int n = 4;
121-
std::array<std::array<int, n>, n> board = {
122-
std::array<int, n>({0, 0, 0, 0}),
123-
std::array<int, n>({0, 0, 0, 0}),
124-
std::array<int, n>({0, 0, 0, 0}),
125-
std::array<int, n>({0, 0, 0, 0})
126-
};
121+
const int n = 4;
122+
std::array<std::array<int, n>, n> board = {
123+
std::array<int, n>({0, 0, 0, 0}), std::array<int, n>({0, 0, 0, 0}),
124+
std::array<int, n>({0, 0, 0, 0}), std::array<int, n>({0, 0, 0, 0})};
127125

128-
backtracking::n_queens::solveNQ<n>(board, 0);
129-
return 0;
126+
backtracking::n_queens::solveNQ<n>(board, 0);
127+
return 0;
130128
}

0 commit comments

Comments
 (0)