diff --git a/Backtracking/The knight's tour .cpp b/Backtracking/The knight's tour .cpp new file mode 100644 index 0000000..5a06454 --- /dev/null +++ b/Backtracking/The knight's tour .cpp @@ -0,0 +1,101 @@ +// C++ program for Knight Tour problem +#include +using namespace std; + +#define N 8 + +int solveKTUtil(int x, int y, int movei, int sol[N][N], + int xMove[], int yMove[]); + +/* A utility function to check if i,j are +valid indexes for N*N chessboard */ +int isSafe(int x, int y, int sol[N][N]) +{ + return (x >= 0 && x < N && y >= 0 && y < N + && sol[x][y] == -1); +} + +/* A utility function to print +solution matrix sol[N][N] */ +void printSolution(int sol[N][N]) +{ + for (int x = 0; x < N; x++) { + for (int y = 0; y < N; y++) + cout << " " << setw(2) << sol[x][y] << " "; + cout << endl; + } +} + +/* This function solves the Knight Tour problem using +Backtracking. This function mainly uses solveKTUtil() +to solve the problem. It returns false if no complete +tour is possible, otherwise return true and prints the +tour. +Please note that there may be more than one solutions, +this function prints one of the feasible solutions. */ +int solveKT() +{ + int sol[N][N]; + + /* Initialization of solution matrix */ + for (int x = 0; x < N; x++) + for (int y = 0; y < N; y++) + sol[x][y] = -1; + + /* xMove[] and yMove[] define next move of Knight. + xMove[] is for next value of x coordinate + yMove[] is for next value of y coordinate */ + int xMove[8] = { 2, 1, -1, -2, -2, -1, 1, 2 }; + int yMove[8] = { 1, 2, 2, 1, -1, -2, -2, -1 }; + + // Since the Knight is initially at the first block + sol[0][0] = 0; + + /* Start from 0,0 and explore all tours using + solveKTUtil() */ + if (solveKTUtil(0, 0, 1, sol, xMove, yMove) == 0) { + cout << "Solution does not exist"; + return 0; + } + else + printSolution(sol); + + return 1; +} + +/* A recursive utility function to solve Knight Tour +problem */ +int solveKTUtil(int x, int y, int movei, int sol[N][N], + int xMove[8], int yMove[8]) +{ + int k, next_x, next_y; + if (movei == N * N) + return 1; + + /* Try all next moves from + the current coordinate x, y */ + for (k = 0; k < 8; k++) { + next_x = x + xMove[k]; + next_y = y + yMove[k]; + if (isSafe(next_x, next_y, sol)) { + sol[next_x][next_y] = movei; + if (solveKTUtil(next_x, next_y, movei + 1, sol, + xMove, yMove) + == 1) + return 1; + else + + // backtracking + sol[next_x][next_y] = -1; + } + } + return 0; +} + +// Driver Code +int main() +{ + // Function Call + solveKT(); + return 0; +} \ No newline at end of file diff --git a/Backtracking/m coloring problem.cpp b/Backtracking/m coloring problem.cpp new file mode 100644 index 0000000..caacd0d --- /dev/null +++ b/Backtracking/m coloring problem.cpp @@ -0,0 +1,104 @@ +// C++ program for the above approach + +#include +using namespace std; + +// Number of vertices in the graph +#define V 4 + +void printSolution(int color[]); + +// check if the colored +// graph is safe or not +bool isSafe(bool graph[V][V], int color[]) +{ + // check for every edge + for (int i = 0; i < V; i++) + for (int j = i + 1; j < V; j++) + if (graph[i][j] && color[j] == color[i]) + return false; + return true; +} + +/* This function solves the m Coloring +problem using recursion. It returns +false if the m colours cannot be assigned, +otherwise, return true and prints +assignments of colours to all vertices. +Please note that there may be more than +one solutions, this function prints one +of the feasible solutions.*/ +bool graphColoring(bool graph[V][V], int m, int i, + int color[V]) +{ + // if current index reached end + if (i == V) { + + // if coloring is safe + if (isSafe(graph, color)) { + + // Print the solution + printSolution(color); + return true; + } + return false; + } + + // Assign each color from 1 to m + for (int j = 1; j <= m; j++) { + color[i] = j; + + // Recur of the rest vertices + if (graphColoring(graph, m, i + 1, color)) + return true; + + color[i] = 0; + } + + return false; +} + +/* A utility function to print solution */ +void printSolution(int color[]) +{ + cout << "Solution Exists:" + " Following are the assigned colors \n"; + for (int i = 0; i < V; i++) + cout << " " << color[i]; + cout << "\n"; +} + +// Driver code +int main() +{ + /* Create following graph and + test whether it is 3 colorable + (3)---(2) + | / | + | / | + | / | + (0)---(1) + */ + bool graph[V][V] = { + { 0, 1, 1, 1 }, + { 1, 0, 1, 0 }, + { 1, 1, 0, 1 }, + { 1, 0, 1, 0 }, + }; + int m = 3; // Number of colors + + // Initialize all color values as 0. + // This initialization is needed + // correct functioning of isSafe() + int color[V]; + for (int i = 0; i < V; i++) + color[i] = 0; + + // Function call + if (!graphColoring(graph, m, 0, color)) + cout << "Solution does not exist"; + + return 0; +} + + diff --git a/Backtracking/subset sum.cpp b/Backtracking/subset sum.cpp new file mode 100644 index 0000000..25f923a --- /dev/null +++ b/Backtracking/subset sum.cpp @@ -0,0 +1,106 @@ +#include +using namespace std; + +#define ARRAYSIZE(a) (sizeof(a))/(sizeof(a[0])) +static int total_nodes; + +// prints subset found +void printSubset(int A[], int size) +{ + for(int i = 0; i < size; i++) + { + cout<<" "<< A[i]; + } + cout<<"\n"; +} + +// qsort compare function +int comparator(const void *pLhs, const void *pRhs) +{ + int *lhs = (int *)pLhs; + int *rhs = (int *)pRhs; + return *lhs > *rhs; +} + +// inputs +// s - set vector +// t - tuplet vector +// s_size - set size +// t_size - tuplet size so far +// sum - sum so far +// ite - nodes count +// target_sum - sum to be found +void subset_sum(int s[], int t[], + int s_size, int t_size, + int sum, int ite, + int const target_sum) +{ + total_nodes++; + + if( target_sum == sum ) + { + // We found sum + printSubset(t, t_size); + + // constraint check + if( ite + 1 < s_size && sum - s[ite] + s[ite + 1] <= target_sum ) + { + + // Exclude previous added item and consider next candidate + subset_sum(s, t, s_size, t_size - 1, sum - s[ite], ite + 1, target_sum); + } + return; + } + else + { + + // constraint check + if( ite < s_size && sum + s[ite] <= target_sum ) + { + + // generate nodes along the breadth + for( int i = ite; i < s_size; i++ ) + { + t[t_size] = s[i]; + if( sum + s[i] <= target_sum ) + { + + // consider next level node (along depth) + subset_sum(s, t, s_size, t_size + 1, sum + s[i], i + 1, target_sum); + } + } + } + } +} + +// Wrapper that prints subsets that sum to target_sum +void generateSubsets(int s[], int size, int target_sum) +{ + int *tuplet_vector = (int *)malloc(size * sizeof(int)); + int total = 0; + + // sort the set + qsort(s, size, sizeof(int), &comparator); + for( int i = 0; i < size; i++ ) + { + total += s[i]; + } + if( s[0] <= target_sum && total >= target_sum ) + { + subset_sum(s, tuplet_vector, size, 0, 0, 0, target_sum); + } + free(tuplet_vector); +} + +// Driver code +int main() +{ + int weights[] = {15, 22, 14, 26, 32, 9, 16, 8}; + int target = 53; + int size = ARRAYSIZE(weights); + generateSubsets(weights, size, target); + cout << "Nodes generated " << total_nodes; + return 0; +} + +