mirror of
https://github.com/TheAlgorithms/C-Plus-Plus.git
synced 2026-04-09 13:39:17 +08:00
[feat/fix/docs]: Improvements in the backtracking folder (#1553)
* [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.
* clang-format and clang-tidy fixes for 9cc3951d
Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
Co-authored-by: Abhinn Mishra <49574460+mishraabhinn@users.noreply.github.com>
This commit is contained in:
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user