0% found this document useful (0 votes)
30 views6 pages

Backtracking (Nqueen, Sum of Subsets, Graph Coloring), C2 - 26

Uploaded by

manuja joshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views6 pages

Backtracking (Nqueen, Sum of Subsets, Graph Coloring), C2 - 26

Uploaded by

manuja joshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

DAA Lab Manual

Lab-10
Name- Manuja Joshi
Gr. No: 11910355
Division: ET-C
Roll No. : 26

N Queen Problem using Backtracking -


 This problem is to find an arrangement of N queens on a chess board, such
that no queen can attack any other queens on the board.
 The chess queens can attack in any direction as horizontal, vertical,
horizontal and diagonal way.
 A binary matrix is used to display the positions of N Queens, where no
queens can attack other queens.

Algorithm-
bool nqueen(int **arr,int x,int n){
if(x>=n){
return true;
}
for(int i=0; i<n;i++){
if(issafe(arr,x,i,n)){ // to check queen is not getting attacked
arr[x][i]=1;
if(nqueen(arr,x+1,n)){
return true;
}
arr[x][i]=0; //backtrack step
}
}
return false;
}
OUTPUT-
For n=6 , 1 shows the placement of n queens in n rows-

Sum of Subsets Problem using Backtracking -

 In this problem, there is a given set with some integer elements. And another
some value is also provided, we have to find a subset of the given set whose
sum is the same as the given sum value.
 Here backtracking approach is used for trying to select a valid subset when an
item is not valid, we will backtrack to get the previous subset and add another
element to get the solution.
 Input:

The Set: {1,2,3,4,5}


The sum Value: 7
 Output:
{1,2,4}
{2,5}
{3,4}
Algorithm-
void generate(vector<int> &sub,int i,int arr[],int n,int sum,int total){
if(total>=sum || i==n){
if(total==sum){
subsets.push_back(sub);
return;
}
return;
}
// if element is considered
sub.push_back(arr[i]);
generate(sub,i+1,arr,n,sum,total+arr[i]);
sub.pop_back(); //backtracking step

//if element is not considered


generate(sub,i+1,arr,n,sum,total);

OUTPUT-
Graph Coloring problem using Backtracking -
 In this problem, an undirected graph is given. There is also provided m colors.
The problem is to find if it is possible to assign nodes with m different colors,
such that no two adjacent vertices of the graph are of the same colors. If the
solution exists, then display which color is assigned on which vertex.
 Starting from vertex 0, we will try to assign colors one by one to different
nodes. But before assigning, we have to check whether the color is safe or not.
A color is not safe whether adjacent vertices are containing the same color.
 Input :
m=3 colours , 4 vertices
graph[V][V] = {
{0, 1, 1, 1},
{1, 0, 1, 0},
{1, 1, 0, 1},
{1, 0, 1, 0},}
 Output : Assigned Colors are:
1232

Algorithm-
bool graphColoring(int colors, int color[], int vertex) {
if (vertex == V)
return true;

for (int col = 1; col <= colors; col++) {


if (isValid(vertex,color, col)) { //check whether color col is valid or not
color[vertex] = col;
if (graphColoring (colors, color, vertex+1) == true)
return true;
color[vertex] = 0;
}
}
return false; //when no colors can be assigned
}

bool checkSolution(int m) {
int *color = new int[V]; //make color matrix for each vertex

for (int i = 0; i < V; i++)


color[i] = 0;

if (graphColoring(m, color, 0) == false) { //for vertex 0 check graph coloring


cout << "Solution does not exist.";
return false;
}
showColors(color);
return true;
}
 Complexity Analysis:

 Time Complexity: O(m^V).


There are total O(m^V) combination of colors. So time complexity is
O(m^V). The upperbound time complexity remains the same but the
average time taken will be less.
 Space Complexity: O(V).
Recursive Stack of graphColoring(…) function will require O(V) space.

OUTPUT-

You might also like