Skip to content

Commit

Permalink
Merge branch 'master' into fix-prime-numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
nella17 authored Oct 29, 2021
2 parents 7a30521 + 4e3abd4 commit 0afd463
Show file tree
Hide file tree
Showing 8 changed files with 346 additions and 301 deletions.
40 changes: 26 additions & 14 deletions backtracking/graph_coloring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,38 @@
* @author [Anup Kumar Panwar](https://github.com/AnupKumarPanwar)
* @author [David Leal](https://github.com/Panquesito7)
*/
#include <array>
#include <iostream>
#include <vector>

#include <array> /// for std::array
#include <iostream> /// for IO operations
#include <vector> /// for std::vector

/**
* @namespace
* @namespace backtracking
* @brief Backtracking algorithms
*/
namespace backtracking {
/** A utility function to print solution
/**
* @namespace graph_coloring
* @brief Functions for the [Graph
* Coloring](https://en.wikipedia.org/wiki/Graph_coloring) algorithm,
*/
namespace graph_coloring {
/**
* @brief A utility function to print the solution
* @tparam V number of vertices in the graph
* @param color array of colors assigned to the nodes
*/
template <size_t V>
void printSolution(const std::array<int, V>& color) {
std::cout << "Following are the assigned colors" << std::endl;
std::cout << "Following are the assigned colors\n";
for (auto& col : color) {
std::cout << col;
}
std::cout << std::endl;
std::cout << "\n";
}

/** A utility function to check if the current color assignment is safe for
/**
* @brief Utility function to check if the current color assignment is safe for
* vertex v
* @tparam V number of vertices in the graph
* @param v index of graph vertex to check
Expand All @@ -60,7 +69,8 @@ bool isSafe(int v, const std::array<std::array<int, V>, V>& graph,
return true;
}

/** A recursive utility function to solve m coloring problem
/**
* @brief Recursive utility function to solve m coloring problem
* @tparam V number of vertices in the graph
* @param graph matrix of graph nonnectivity
* @param m number of colors
Expand All @@ -74,28 +84,30 @@ void graphColoring(const std::array<std::array<int, V>, V>& graph, int m,
// base case:
// If all vertices are assigned a color then return true
if (v == V) {
backtracking::printSolution<V>(color);
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)) {
if (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);
graphColoring<V>(graph, m, color, v + 1);

// If assigning color c doesn't lead to a solution then remove it
color[v] = 0;
}
}
}
} // namespace graph_coloring
} // namespace backtracking

/**
* Main function
* @brief Main function
* @returns 0 on exit
*/
int main() {
// Create following graph and test whether it is 3 colorable
Expand All @@ -113,6 +125,6 @@ int main() {
int m = 3; // Number of colors
std::array<int, V> color{};

backtracking::graphColoring<V>(graph, m, color, 0);
backtracking::graph_coloring::graphColoring<V>(graph, m, color, 0);
return 0;
}
127 changes: 69 additions & 58 deletions backtracking/knight_tour.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* @file
* @brief [Knight's tour](https://en.wikipedia.org/wiki/Knight%27s_tour) algorithm
* @brief [Knight's tour](https://en.wikipedia.org/wiki/Knight%27s_tour)
* algorithm
*
* @details
* A knight's tour is a sequence of moves of a knight on a chessboard
Expand All @@ -12,92 +13,102 @@
* @author [Nikhil Arora](https://github.com/nikhilarora068)
* @author [David Leal](https://github.com/Panquesito7)
*/
#include <iostream>
#include <array>
#include <array> /// for std::array
#include <iostream> /// for IO operations

/**
* @namespace backtracking
* @brief Backtracking algorithms
*/
namespace backtracking {
/**
* A utility function to check if i,j are valid indexes for N*N chessboard
* @tparam V number of vertices in array
* @param x current index in rows
* @param y current index in columns
* @param sol matrix where numbers are saved
* @returns `true` if ....
* @returns `false` if ....
*/
template <size_t V>
bool issafe(int x, int y, const std::array <std::array <int, V>, V>& sol) {
return (x < V && x >= 0 && y < V && y >= 0 && sol[x][y] == -1);
}
/**
* @namespace knight_tour
* @brief Functions for the [Knight's
* tour](https://en.wikipedia.org/wiki/Knight%27s_tour) algorithm
*/
namespace knight_tour {
/**
* A utility function to check if i,j are valid indexes for N*N chessboard
* @tparam V number of vertices in array
* @param x current index in rows
* @param y current index in columns
* @param sol matrix where numbers are saved
* @returns `true` if ....
* @returns `false` if ....
*/
template <size_t V>
bool issafe(int x, int y, const std::array<std::array<int, V>, V> &sol) {
return (x < V && x >= 0 && y < V && y >= 0 && sol[x][y] == -1);
}

/**
* Knight's tour algorithm
* @tparam V number of vertices in array
* @param x current index in rows
* @param y current index in columns
* @param mov movement to be done
* @param sol matrix where numbers are saved
* @param xmov next move of knight (x coordinate)
* @param ymov next move of knight (y coordinate)
* @returns `true` if solution exists
* @returns `false` if solution does not exist
*/
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) {
int k, xnext, ynext;
/**
* Knight's tour algorithm
* @tparam V number of vertices in array
* @param x current index in rows
* @param y current index in columns
* @param mov movement to be done
* @param sol matrix where numbers are saved
* @param xmov next move of knight (x coordinate)
* @param ymov next move of knight (y coordinate)
* @returns `true` if solution exists
* @returns `false` if solution does not exist
*/
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) {
int k = 0, xnext = 0, ynext = 0;

if (mov == V * V) {
return true;
}
if (mov == V * V) {
return true;
}

for (k = 0; k < V; k++) {
xnext = x + xmov[k];
ynext = y + ymov[k];
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 (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;
}
if (solve<V>(xnext, ynext, mov + 1, sol, xmov, ymov) == true) {
return true;
} else {
sol[xnext][ynext] = -1;
}
}
return false;
}
} // namespace backtracking
return false;
}
} // namespace knight_tour
} // namespace backtracking

/**
* Main function
* @brief Main function
* @returns 0 on exit
*/
int main() {
const int n = 8;
std::array <std::array <int, n>, n> sol = { 0 };
std::array<std::array<int, n>, n> sol = {0};

int i, j;
int i = 0, j = 0;
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) { sol[i][j] = -1; }
for (j = 0; j < n; j++) {
sol[i][j] = -1;
}
}

std::array <int, n> xmov = { 2, 1, -1, -2, -2, -1, 1, 2 };
std::array <int, n> ymov = { 1, 2, 2, 1, -1, -2, -2, -1 };
std::array<int, n> xmov = {2, 1, -1, -2, -2, -1, 1, 2};
std::array<int, n> ymov = {1, 2, 2, 1, -1, -2, -2, -1};

sol[0][0] = 0;

bool flag = backtracking::solve<n>(0, 0, 1, sol, xmov, ymov);
bool flag = backtracking::knight_tour::solve<n>(0, 0, 1, sol, xmov, ymov);
if (flag == false) {
std::cout << "Error: Solution does not exist\n";
}
else {
} else {
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) { std::cout << sol[i][j] << " "; }
for (j = 0; j < n; j++) {
std::cout << sol[i][j] << " ";
}
std::cout << "\n";
}
}
Expand Down
36 changes: 19 additions & 17 deletions backtracking/minimax.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,34 @@
* @details
* Minimax (sometimes MinMax, MM or saddle point) is a decision rule used in
* artificial intelligence, decision theory, game theory, statistics,
* and philosophy for minimizing the possible loss for a worst case (maximum loss) scenario.
* When dealing with gains, it is referred to as "maximin"—to maximize the minimum gain.
* Originally formulated for two-player zero-sum game theory, covering both the cases where players take
* alternate moves and those where they make simultaneous moves, it has also been extended to more
* complex games and to general decision-making in the presence of uncertainty.
*
* and philosophy for minimizing the possible loss for a worst case (maximum
* loss) scenario. When dealing with gains, it is referred to as "maximin"—to
* maximize the minimum gain. Originally formulated for two-player zero-sum game
* theory, covering both the cases where players take alternate moves and those
* where they make simultaneous moves, it has also been extended to more complex
* games and to general decision-making in the presence of uncertainty.
*
* @author [Gleison Batista](https://github.com/gleisonbs)
* @author [David Leal](https://github.com/Panquesito7)
*/
#include <algorithm>
#include <cmath>
#include <iostream>
#include <array>
#include <algorithm> /// for std::max, std::min
#include <array> /// for std::array
#include <cmath> /// for log2
#include <iostream> /// for IO operations

/**
/**
* @namespace backtracking
* @brief Backtracking algorithms
*/
namespace backtracking {
/**
* Check which number is the maximum/minimum in the array
* @brief Check which is the maximum/minimum number in the array
* @param depth current depth in game tree
* @param node_index current index in array
* @param is_max if current index is the longest number
* @param scores saved numbers in array
* @param height maximum height for game tree
* @return maximum or minimum number
* @returns the maximum or minimum number
*/
template <size_t T>
int minimax(int depth, int node_index, bool is_max,
Expand All @@ -46,16 +47,17 @@ int minimax(int depth, int node_index, bool is_max,

return is_max ? std::max(v1, v2) : std::min(v1, v2);
}
} // namespace backtracking
} // namespace backtracking

/**
* Main function
* @brief Main function
* @returns 0 on exit
*/
int main() {
std::array<int, 8> scores = {90, 23, 6, 33, 21, 65, 123, 34423};
double height = log2(scores.size());

std::cout << "Optimal value: " << backtracking::minimax(0, 0, true, scores, height)
<< std::endl;
std::cout << "Optimal value: "
<< backtracking::minimax(0, 0, true, scores, height) << std::endl;
return 0;
}
Loading

0 comments on commit 0afd463

Please sign in to comment.