diff --git a/algorithms/backtracking/Knight'stour.cpp b/algorithms/backtracking/Knight'stour.cpp new file mode 100644 index 00000000..a0d8efb8 --- /dev/null +++ b/algorithms/backtracking/Knight'stour.cpp @@ -0,0 +1,82 @@ +// +// C/C++ program of Knight's Tour using Backtracking +// +// The All â–²lgorithms Project + +// https://github.com/allalgorithms/cpp +// +// Contributed by: Prayag Thakur +// Github: @PrAyAg9 +// + +#include +#include +#define N 8 // Chessboard is N x N (8 x 8 in this case) +using namespace std; + +// Utility function to check if the move is valid +bool isSafe(int x, int y, vector> &board) { + return (x >= 0 && x < N && y >= 0 && y < N && board[x][y] == -1); +} + +// Utility function to print the board +void printSolution(const vector> &board) { + for (int x = 0; x < N; ++x) { + for (int y = 0; y < N; ++y) { + cout << board[x][y] << "\t"; + } + cout << endl; + } +} + +// A recursive utility function to solve Knight Tour problem +bool solveKnightTour(int x, int y, int movei, vector> &board, + const vector &moveX, const vector &moveY) { + int nextX, nextY; + // Base case: If all squares are visited, return true + if (movei == N * N) + return true; + + // Try all next moves from the current coordinate x, y + for (int k = 0; k < 8; ++k) { + nextX = x + moveX[k]; + nextY = y + moveY[k]; + if (isSafe(nextX, nextY, board)) { + board[nextX][nextY] = movei; + if (solveKnightTour(nextX, nextY, movei + 1, board, moveX, moveY)) + return true; + else + // Backtrack: Undo the current move + board[nextX][nextY] = -1; + } + } + return false; +} + +// This function solves the Knight Tour problem using Backtracking. +// This function mainly uses solveKnightTour() to solve the problem. +bool knightTour() { + // Initialize the chessboard with -1 (unvisited squares) + vector> board(N, vector(N, -1)); + + // The knight's possible moves in the x and y directions + vector moveX = {2, 1, -1, -2, -2, -1, 1, 2}; + vector moveY = {1, 2, 2, 1, -1, -2, -2, -1}; + + // Knight starts at the first block (0, 0) + board[0][0] = 0; + + // Start the Knight's Tour from the first position + if (!solveKnightTour(0, 0, 1, board, moveX, moveY)) { + cout << "Solution does not exist." << endl; + return false; + } else + printSolution(board); + + return true; +} + +int main() { + knightTour(); + return 0; +} diff --git a/algorithms/backtracking/M_ColoringProblem.cpp b/algorithms/backtracking/M_ColoringProblem.cpp new file mode 100644 index 00000000..39b09767 --- /dev/null +++ b/algorithms/backtracking/M_ColoringProblem.cpp @@ -0,0 +1,89 @@ +// C/C++ M-colouring Problem using Backtracking +// +// The All â–²lgorithms Project + +// https://github.com/allalgorithms/cpp +// +// Contributed by: Prayag Thakur +// Github: @PrAyAg9 + +// Problem Statement: +// Given an undirected graph and a number M, determine if the graph can be colored using at most M different colors such that no two adjacent vertices have the same color. + +#include +#include +using namespace std; + +// Function to check if the current color assignment is safe for vertex v +bool isSafe(int v, const vector> &graph, const vector &color, int c) { + // Check for each adjacent vertex + for (int i = 0; i < graph.size(); i++) { + // If the vertex is adjacent and has the same color, return false + if (graph[v][i] == 1 && color[i] == c) { + return false; + } + } + return true; +} + +// A recursive utility function to solve the M-coloring problem +bool solveMColoring(int v, const vector> &graph, vector &color, int m) { + // Base case: If all vertices are assigned a color, return true + if (v == graph.size()) { + return true; + } + + // Try assigning each color from 1 to m + for (int c = 1; c <= m; c++) { + // Check if assigning color c to vertex v is safe + if (isSafe(v, graph, color, c)) { + color[v] = c; // Assign the color + + // Recursively assign colors to the rest of the vertices + if (solveMColoring(v + 1, graph, color, m)) { + return true; + } + + // If assigning color c doesn't lead to a solution, backtrack + color[v] = 0; + } + } + return false; // If no color can be assigned, return false +} + +// Function to solve the M-coloring problem +bool graphColoring(const vector> &graph, int m) { + int V = graph.size(); + vector color(V, 0); // Initialize all vertices with no color (0) + + // Call the solveMColoring function starting from the first vertex + if (!solveMColoring(0, graph, color, m)) { + cout << "Solution does not exist." << endl; + return false; + } + + // Print the solution + cout << "Solution exists: Following are the assigned colors" << endl; + for (int i = 0; i < V; i++) { + cout << "Vertex " << i << " ---> Color " << color[i] << endl; + } + return true; +} + +int main() { + // Example: A graph represented as an adjacency matrix + // (1 means an edge between two vertices, 0 means no edge) + vector> graph = { + {0, 1, 1, 1}, + {1, 0, 1, 0}, + {1, 1, 0, 1}, + {1, 0, 1, 0} + }; + + int m = 3; // Number of colors + + // Solve the M-Coloring problem + graphColoring(graph, m); + + return 0; +}