Skip to content
This repository was archived by the owner on Sep 7, 2025. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions algorithms/backtracking/Knight'stour.cpp
Original file line number Diff line number Diff line change
@@ -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 <iostream>
#include <vector>
#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<vector<int>> &board) {
return (x >= 0 && x < N && y >= 0 && y < N && board[x][y] == -1);
}

// Utility function to print the board
void printSolution(const vector<vector<int>> &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<vector<int>> &board,
const vector<int> &moveX, const vector<int> &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<vector<int>> board(N, vector<int>(N, -1));

// The knight's possible moves in the x and y directions
vector<int> moveX = {2, 1, -1, -2, -2, -1, 1, 2};
vector<int> 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;
}
89 changes: 89 additions & 0 deletions algorithms/backtracking/M_ColoringProblem.cpp
Original file line number Diff line number Diff line change
@@ -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 <iostream>
#include <vector>
using namespace std;

// Function to check if the current color assignment is safe for vertex v
bool isSafe(int v, const vector<vector<int>> &graph, const vector<int> &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<vector<int>> &graph, vector<int> &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<vector<int>> &graph, int m) {
int V = graph.size();
vector<int> 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<vector<int>> 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;
}