0% found this document useful (0 votes)
4 views

Design & Analysis of algorithm- 5

The document discusses algorithms for problem-solving in computer science, focusing on backtracking and branch-and-bound techniques. It explains how backtracking efficiently searches for solutions using a state space tree and depth-first search, while branch-and-bound optimizes solutions by breaking problems into subproblems and using bounding functions. Additionally, it compares the two techniques and illustrates the Traveling Salesman Problem as an example of branch-and-bound application.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Design & Analysis of algorithm- 5

The document discusses algorithms for problem-solving in computer science, focusing on backtracking and branch-and-bound techniques. It explains how backtracking efficiently searches for solutions using a state space tree and depth-first search, while branch-and-bound optimizes solutions by breaking problems into subproblems and using bounding functions. Additionally, it compares the two techniques and illustrates the Traveling Salesman Problem as an example of branch-and-bound application.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 82

Parul University

Session: July-Dec. 2024

05/28/2025 Department of Computer Science Slide No. 1


Parul University

203105374: Design & Analysis of


Algorithm

Date 05/28/2025 Department of Computer Science Slide No.


Unit-5
● Backtracking
● Branch and Bound
● Travelling Salesman Problem
● Graph Coloring
● n-Queen Problem
● Hamiltonian Cycles and
● Sum of subset

05/28/2025 Department of Computer Science Slide No.


Backtracking Technique
● Backtracking is the modified process of the brute force approach where the technique
efficiently searches for a solution to the problem among all available options.
● The brute force approach is nothing but it finds all possible solutions to make satisfaction
to the given problem.
● Example of Brute force approach −

05/28/2025 Department of Computer Science Slide No.


In the given figure there are three colors to fill into the cylinder by all possible solutions using
the brute force approach.

The following ways to fill the color into the cylinder are −
1. Blue, Orange, Yellow
2. Blue, Yellow, Orange
3. Orange, Blue, Yellow
4. Orange, Yellow, Blue
5. Yellow, Blue, Orange
6. Yellow, Orange, Blue

05/28/2025 Department of Computer Science Slide No.


● There are a total of 6 ways to fill the color into the cylinder and that is the way we can
modify the solution in the backtracking.
● The following important point related to backtracking −
● A backtracking solution can be represented in the form of a tree and it is also called a State
Space tree.
● Backtracking follow the Depth-first search(DFS).

05/28/2025 Department of Computer Science Slide No.


● In this given figure we made a tree solution approach using backtracking.

● Depending upon the constraints we solve the solution based on the constraints. For example,
we remove the orange from the end of every node and this shows the reduction of time
complexity of the problem.

● It involves a feasibility problem.

05/28/2025 Department of Computer Science Slide No.


Branch-N-Bound Technique
The branch-N-bound technique is an algorithm used to solve the problem of optimization by
breaking it down into smaller subproblems and by bounding function it eliminates the
subproblem that doesn’t contain an optimal solution.
Branch and bound search is a way to combine space saving of depth-first search with
heuristics information. The idea of branch and bound search is to maintain the lowest cost
path or minimal path.
This technique contains two parts −
● Branch − Several choices are found
● Bound − Setting bound on solution quality

The Branch-N-Bound follows the Breath First Search.


Now we are finding the route of R to G using BFS and creating the tree

05/28/2025 Department of Computer Science Slide No.


Example 1:

05/28/2025 Department of Computer Science Slide No.


● The following step to solve the BFS approach is given below −
● Step 1 − We know the source or root node is R.
● Step 2 − Visit the successor node of a root node i.e, A, B, and C.
● Step 3 − Expand the path node of A to D, B to D and B to G, and C to G.
● Step 4 − Now the complete structure becomes

05/28/2025 Department of Computer Science Slide No.


The leveling space partition of tree structure nodes are −
Level 0 − R
Level 1 − R -> A, R-> B, R -> C
Level 2 − R -> A -> D, R -> B -> D and R -> B -> G, R -> C -> G
Level 3 − Finally, the connecting path and calculation of node weight look like
R -> A -> D -> G( 3+3+2 = 8 ), R -> B -> D -> G ( 3+2+1 = 6 ) and R -> B -> G ( 9+1 = 10), R -> C ->
G( 5+3 = 8 )
We have the shortest route from node R – G is
R -> B -> D -> G = (3+2+1) = 6
So this way we solve the approach of breath first search.

05/28/2025 Department of Computer Science Slide No.


The following important point related to Branch-N-Bound −

○ This technique traverses in any manner DFS or BFS.

○ It involves a binding function.

○ It completely searches the state space tree by targeting the optimal solution.

05/28/2025 Department of Computer Science Slide No.


Difference between Backtracking VS Branch-N-Bound
technique
Parameters Backtracking Branch-N-Bound
Why it is used? This is used to solve the decision-based This is used to solve the
problem. optimization problem.

Nodes This is a state space tree where the node This explored the optimization
explored the depth-first search. problem.

Efficient More efficient. Less Efficient.

Function It involves the feasibility function. It involves the bounding function.

Traverse It traverses the tree by depth-first search It traverses the tree by breadth-first
search

Solves Backtracking can solve the game of chess and It doesn’t solve any game problem.
sudoku.

05/28/2025 Department of Computer Science Slide No.


Parameters Backtracking Branch-N-Bound

Application Used This application is used to solve This type of application


the N-queen problem, the is used to solve the
Hamilton cycle, and the problem problem based on the
based on graph coloring. Travelling salesman
problem.

05/28/2025 Department of Computer Science Slide No.


Traveling Salesperson problem using branch and bound
● Given a set of cities and distance between every pair of cities, the problem is to
find the shortest possible tour that visits every city exactly once and returns to
the starting point.
● For example, consider the graph shown in figure on right side. A TSP tour in the
graph is 0-1-3-2-0. The cost of the tour is 10+25+30+15 which is 80.

05/28/2025 Department of Computer Science Slide No.


Example

1 2 3 4 5
1 ∞ 20 30 10 11

2 15 ∞ 16 4 2

3 3 5 ∞ 2 4

4 19 6 18 ∞ 3

5 16 4 7 16 ∞

05/28/2025 Department of Computer Science Slide No.


State Space Tree

05/28/2025 Department of Computer Science Slide No.


Solution: Reduce the Row
● As we can observe in the above adjacent matrix that 10 is the minimum value in the first row, 2 is the
minimum value in the second row, 2 is the minimum value in the third row, 3 is the minimum value
in the third row, 3 is the minimum value in the fourth row, and 4 is the minimum value in the fifth
row.
● Now, we will reduce the matrix. We will subtract the minimum value with all the elements of a row.
First, we evaluate the first row. Let's assume two variables, i.e., i and j, where 'i' represents the rows,
and 'j' represents the columns.
● When i = 0, j =0
● M[0][0] = ∞-10= ∞
● When i = 0, j = 1
● M[0][1] = 20 - 10 = 10
● When i = 0, j = 2
● M[0][2] = 30 - 10 = 20

05/28/2025 Department of Computer Science Slide No.


When i = 0, j = 3 ● M[1][0] = 15-2= 13
M[0][3] = 10 - 10 = 0 ● When i = 1, j = 1
When i = 0, j = 4 ● M[1][1] = ∞ - 2= ∞
M[0][4] = 11 - 10 = 1 ● When i = 1, j = 2
The matrix is shown below after the ● M[1][2] = 16 - 2 = 14
evaluation of the first row: ● When i = 1, j = 3
● M[1][3] = 4 - 2 = 2
● When i = 1, j = 4
● M[1][4] = 2 - 2 = 0
● The matrix is shown below after the
evaluation of the second row:

● Consider the second row.


● When i = 1, j =0
05/28/2025 Department of Computer Science Slide No.
Now we reduce the matrix with respect to the
columns. Before reducing the matrix, we first find
The matrix is shown below after the evaluation of the minimum value of all the columns. The minimum
the fifth row: value of first column is 1, the minimum value of the
second column is 0, the minimum value of the third
column is 3, the minimum value of the fourth
column is 0, and the minimum value of the fifth
column is 0, as shown in the below matrix:

05/28/2025 Department of Computer Science Slide No.


Reduce the column
Now we reduce the matrix. the first column:
Consider the first column.
When i = 0, j =0
M[0][0] = ∞-1= ∞
When i = 1, j = 0
M[1][0] = 13 - 1= 12
When i = 2, j = 0
M[2][0] = 1 - 1 = 0
When i = 3, j = 0
M[3][0] = 16 - 1 = 15
When i = 4, j = 0
M[4][0] = 12 - 1 = 11
The matrix is shown below after the evaluation of
05/28/2025 Department of Computer Science Slide No.
Since the minimum value of the first and the third The matrix is shown below after the evaluation of
columns is non-zero, we will evaluate only first and the third column:
third columns. We have evaluated the first column.
Now we will evaluate the third column.
Consider the third column.
When i = 0, j =2
● M[0][2] = 20-3= 17
● When i = 1, j = 2
● M[1][2] = 13 - 1= 12
● When i = 2, j = 2
● M[2][2] = 1 - 1 = 0
● When i = 3, j = 2 The above is the reduced matrix. The minimum
● M[3][2] = 16 - 1 = 15 value of rows is 21, and the columns is 4. Therefore,
the total minimum value is (21 + 4) equals to 25.
● When i = 4, j = 2
● M[4][2] = 12 - 1 = 11

05/28/2025 Department of Computer Science Slide No.


● Let's understand that how to solve this problem using branch and bound with the
help of a state-space tree.
● To make a state-space tree, first, we consider node 1. From node 1, we can go either to
nodes 2, 3, 4, or 5 as shown in the below image. The cost of node 1 would be the cost which
we achieved in the above-reduced matrix, i.e.., 25. Here, we will also maintain the upper
bound. Initially, the upper bound would-be infinity.

05/28/2025 Department of Computer Science Slide No.


Now, consider node 2. It means that we are moving value; therefore, we can say that above matrix has
from node 1 to node 2. Make the first row and been reduced. The cost of reduction of node 2 is c(1,
second column as infinity shown in the below 2) + r + r` = 10 + 25 + 0 = 35.
matrix: Once we move from the node 1 to node 3, we
Once we move from node 1 to node 2, we cannot cannot move back to the node 1. Therefore, we have
move back to node 1. Therefore, we have to make 2 to make 3 to 1 as infinity shown in the below matrix:
to 1 as infinity shown in the below matrix:
Since each row and column contains atleast one zero
value; therefore, we can say that above matrix has
been reduced. The cost of reduction of node 3 is c(1,
3) + r + r` = 17 + 25 + 11= 53.
Now, consider the node 4. It means that we are
moving from the node 1 to node 4. Make the first
row and forth column as infinity shown in the below
matrix:

Since each row and column contains atleast one zero


05/28/2025 Department of Computer Science Slide No.
Now, consider the node 5. It means that we are
moving from the node 1 to node 5. Make the first
row and fifth column as infinity shown in the below
matrix:
● Once we move from the node 1 to node 5, we
cannot move back to the node 1. Therefore, we
have to make 5 to 1 as infinity shown in the
below matrix:
● Since each row and column contains atleast
Once we move from the node 1 to node 4, we one zero value; therefore, we can say that
cannot move back to the node 1. Therefore, we above matrix has been reduced. In this case,
have to make 4 to 1 as infinity shown in the below second and third rows are non-zero. Therefore,
matrix: we have to first find the minimum values of
both the rows. The minimum value of second
Since each row and column contains atleast one row is 2; therefore, we subtract 2 from all the
zero value; therefore, we can say that above matrix elements of the second row. The elements of
has been reduced. The cost of reduction of node 4 second row would be:
is c(1, 4) + r + r` = 0 + 25 + 0 = 25.

05/28/2025 Department of Computer Science Slide No.


● A[1][0] = 12-2 = 10 cost of all the nodes.
● A[1][1] = ∞ First, we consider the path from the vertex 4 to the
● A[1][2] = 11 - 2 = 9 vertex 2. We make fourth row as ∞ and second
column as ∞. Since we cannot move back from 2 to
● A[1][3] = 2 - 2 = 0 1, so 1 to 2 is also infinity as shown in the below
● A[1][4] = ∞ - 2 = ∞ matrix:

As we can observe now that the second row Since all the rows and columns have atleast one
contains one zero value. zero value. Therefore, we can say that this matrix is
already reduced. So, there would be no reduction
The cost of reduction of node 5 is c(1, 5) + r + r` = 1 cost. The cost of reduction of node 2 is c(4, 2) + r +
+ 25 + 5 = 31 r` = 3 + 25 + 0 = 28
Since the node 4 has the minimum cost, i.e., 25. So
we will explore the node 4 first. From the vertex 4,
we can go either to the vertex 2, 3 or 5 as shown in
the below image:
Now we have to calculate the cost of the path from
the vertex 4 to 2, vertex 4 to 3, and vertex 4 to 5.
Here, we will use the matrix of node 4 to find the

05/28/2025 Department of Computer Science Slide No.


Now we have to calculate the cost of the path from ● A[2][4] = 2 - 2 = 0
the vertex 4 to the vertex 3. We make fourth row
and third column as infinity as shown in the below
matrix. Since we cannot move from the vertex 3 to 1, As we can observe now that the third row contains
so we make 3 to 1 as infinity shown in the below one zero value.
matrix: The first column does not contain the zero value. The
Now we will check whether each row and column minimum value of the first column is 11. We subtract
contain atleast one zero value or not. First, we 11 from all the elements of the first column. The
observe all the rows. Since the third row does not elements of first column would be:
have a zero value, so we first find the minimum ● A[0][0] = ∞ - 11 = ∞
value of the third row. The minimum value of the
third row is 2, so we subtract 2 from all the elements ● A[1][0] = 12 - 11 = 1
of the third row. The elements of third row would ● A[2][0] = ∞ - 11= ∞
be:
● A[3][0] = ∞ - 11= ∞
● A[2][0] = ∞ - 2 = ∞
● A[4][0] = 11 - 11 = 0
● A[2][1] = 3 - 2 = 1
● A[2][2] = ∞ - 2 = ∞
● A[2][3] = ∞ - 2 = ∞

05/28/2025 Department of Computer Science Slide No.


As we can observe now that the first column ● A[1][2] = 11 - 11 = 0
contains one zero value. The total minimum cost is ●
A[1][3] = ∞ - 11 = ∞
11 +2 equals to 13. The cost of reduction of node 3
is c(4, 3) + r + r` = 12 + 25 + 13 = 50. ● A[1][4] = ∞ - 11 = ∞
Now we will calculate the cost of the path from the As we can observe now that the second row
vertex 4 to 5. We make fourth row and fifth column contains one zero value. The cost of reduction of
as infinity. Since we cannot move back from the node 5 is c(4, 5) + r + r` = 0 + 25 + 11 = 36.
node 5 to 1, so we also make 1 to 5 as infinity shown
Now we will compare the cost of all the leaf nodes.
in the below matrix: The node with a cost 28 is minimum so we will
Now we will check whether each row and column explore this node. The node with a cost 28 can be
contain atleast one zero value or not. First, we further expanded to the nodes 3 and 5 as shown in
observe all the rows. The second row does not the below figure:
contain the zero value, so we find the minimum Now we have to calculate the cost of both the
value of the second row. The minimum value is 11 nodes, i.e., 3 and 5. First we consider the path from
so we subtract 11 from all the elements of the node 2 to node 3. Consider the matrix of node 2
second row. The elements of second row would be: which is shown below:
● A[1][0] = 12 - 11 = 1 We make second row and third column as infinity.
● A[1][1] = ∞ - 11 = ∞ Also, we cannot move back from the node 3 to node
1 so we make 3 to 1 as infinity as shown in the
below matrix:
05/28/2025 Department of Computer Science Slide No.
● A[2][2] = ∞ - 2 = ∞
● A[2][3] = ∞ - 2 = ∞
● A[2][4] = 2 - 2 = 0
Since fifth row does not contain any zero value so
we will find the minimum value of the fifth row.
The minimum value of the fifth row is 11 so we
subtract 11 from all the elements of the fifth row.
● A[4][0] = 11 - 11 = 0
Now we will check whether any row contains zero ● A[4][1] = ∞ - 11 = ∞
value or not. Since third row does not contain any ● A[4][2] = ∞ - 11 = ∞
zero value so we will find the minimum value of
the third row. The minimum value of the third row ● A[4][3] = ∞ - 11 = ∞
is 2 so we subtract 2 from all the elements of the ● A[4][4] = ∞ - 11 = ∞
third row. The elements of third row would be:
● A[2][0] = ∞ - 2 = ∞
● A[2][1] = ∞ - 2 = ∞

05/28/2025 Department of Computer Science Slide No.


The total minimum cost is (11 + 2) equals to 13. to the node 3 as shown in the below figure:
The cost of reduction of node 3 is c(2, 3) + r + r` =
11 + 28 + 13 = 52.
Consider the path from node 2 to node 5. Make
the fourth row and third column as infinity. Since
we cannot move back from the node 5 to node 1
so make 1 to 5 also as infinity as shown in the
below matrix:
Now we will check whether any row contains zero
value or not. Since every row and column contains
a zero value; therefore, the above matrix is the
reduced matrix.
The cost of reduction of node 5 is c(2, 5) + r + r` = 0
+ 28 + 0 = 28
Now we will find out the leaf node with a
minimum cost. The node 5 with a cost 28 is
minimum so we select node 5 for the further
exploration. The node 5 can be further expanded

05/28/2025 Department of Computer Science Slide No.


Here, we will use the matrix of node 5 having cost shown below:
28 as shown below:
The path of the tour would be 1->4->2->5->3.
Consider the path from node 5 to node 3. Make
the fifth row and third column as infinity. Since we
cannot move back from the node 3 to node 1 so
make 1 to 5 also as infinity as shown in the below
matrix:
Now we will check whether any row contains zero
value or not. Since every row and column contains
a zero value; therefore, the above matrix is the
reduced matrix.
The cost of reduction of node 3 is c(5, 3) + r + r` = 0
+ 28 + 0 = 28.
Finally, we traverse all the nodes. The upper value
is updated from infinity to 28. We will check
whether any leaf node has a value less than 28.
Since no leaf node contains the value less than 28
so we discard all the leaf nodes from the tree as

05/28/2025 Department of Computer Science Slide No.


Graph Coloring-
● Graph Coloring is a process of assigning colors to the vertices of a graph such that no two adjacent
vertices of it are assigned the same color.
● Graph Coloring is also called as Vertex Coloring.
● It ensures that there exists no edge in the graph whose end vertices are colored with the same color.
● Such a graph is called as a Properly colored graph.

● In this graph,
● No two adjacent vertices are colored with the same color.
● Therefore, it is a properly colored graph.

05/28/2025 Department of Computer Science Slide No.


Chromatic Number-
● Chromatic Number is the minimum graph = 3.
number of colors required to properly ● We can not properly color this graph with
color any graph. less than 3 colors.
● OR
● Chromatic Number is the minimum
number of colors required to color any
graph
● such that no two adjacent vertices of it
are assigned the same color.
● In this graph, No two adjacent vertices are
colored with the same color.
● Minimum number of colors required to
properly color the vertices = 3.
● Therefore, Chromatic number of this
05/28/2025 Department of Computer Science Slide No.
m Coloring Problem using Backtracking:
● Assign colors one by one to different vertices, starting from vertex 0. Before assigning a
color, check for safety by considering already assigned colors to the adjacent vertices i.e
check if the adjacent vertices have the same color or not. If there is any color assignment
that does not violate the conditions, mark the color assignment as part of the solution. If
no assignment of color is possible then backtrack and return false
Follow the given steps to solve the problem:
● Create a recursive function that takes the graph, current index, number of vertices, and
color array.
● If the current index is equal to the number of vertices. Print the color configuration in the
color array.
● Assign a color to a vertex from the range (1 to m).
● For every assigned color, check if the configuration is safe, (i.e. check if the adjacent
vertices do not have the same color) and recursively call the function with the next index
and number of vertices otherwise, return false

05/28/2025 Department of Computer Science Slide No.


● If any recursive function returns true then return true
● If no recursive function returns true then return false

Illustration:
● To color the graph, color each node one by one.
● To color the first node there are 3 choices of colors Red, Green and Blue, so lets take the red
color for first node.
● After Red color for first node is fixed then we have made choice for second node in similar
manner as we did for first node, then for 3rd node and so on.
● There is one important point to remember. while choosing color for the node, it should not
be same as the color of the adjacent node.

05/28/2025 Department of Computer Science Slide No.


05/28/2025 Department of Computer Science Slide No.
05/28/2025 Department of Computer Science Slide No.
05/28/2025 Department of Computer Science Slide No.
Algorithm
● #include<iostream> ● } bool isValid(int v,int color[], int c) { //check
● #define V 4 whether putting a color valid for v

● using namespace std;


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

● bool graph[V][V] = { ● if (graph[v][i] && c == color[i])


● return false;
● {0, 1, 1, 1},
● return true;
● {1, 0, 1, 0},
● }
● {1, 1, 0, 1},
● bool graphColoring(int colors, int color[], int
● {1, 0, 1, 0},
vertex) {
● };
● if (vertex == V) //when all vertices are
● void showColors(int color[]) { considered
● cout << "Assigned Colors are: " <<endl; ● return true;
● for (int i = 0; i < V; i++)
● cout << color[i] << " ";
● cout << endl;
05/28/2025 Department of Computer Science Slide No.
● for (int col = 1; col <= colors; col++) { ● }
● if (isValid(vertex,color, col)) { //check
whether color col is valid or not ● bool checkSolution(int m) {
● color[vertex] = col; ● int *color = new int[V]; //make color
● if (graphColoring (colors, color, matrix for each vertex
vertex+1) == true) //go for additional ● for (int i = 0; i < V; i++)
vertices
● color[i] = 0; //initially set to 0
● return true;

● if (graphColoring(m, color, 0) == false) {
● color[vertex] = 0; //for vertex 0 check graph coloring
● } ● cout << "Solution does not exist.";
● } ● return false;
● return false; //when no colors can be ● }
assigned

05/28/2025 Department of Computer Science Slide No.


● showColors(color);
● return true;
● }

● int main() {
● int colors = 3; // Number of colors
● checkSolution (colors);
● }

05/28/2025 Department of Computer Science Slide No.


● Complexity Analysis
● In the brute force approach to the graph coloring problem, the time complexity
is O(mV)
● In the backtracking approach of the graph coloring problem, the time
complexity is O(mV). In the backtracking approach, the average time complexity
is less than O(mV).

05/28/2025 Department of Computer Science Slide No.


N-Queen Problem:
● The N Queen is the problem of placing N chess queens on an N×N chessboard so that no
two queens attack each other. For example, the following is a solution for the 4 Queen
problem.

05/28/2025 Department of Computer Science Slide No.


Backtracking Algorithm by placing queens in columns:
● The idea is to place queens one by one in different columns, starting from the leftmost
column. When we place a queen in a column, we check for clashes with already placed
queens. In the current column, if we find a row for which there is no clash, we mark this
row and column as part of the solution. If we do not find such a row due to clashes, then
we backtrack and return false.
Follow the steps mentioned below to implement the idea:
● Start in the leftmost column
● If all queens are placed return true
● Try all rows in the current column. Do the following for every tried row.
● If the queen can be placed safely in this row
● Then mark this [row, column] as part of the solution and recursively check if placing
queen here leads to a solution.
● If placing the queen in [row, column] leads to a solution then return true. ing.

05/28/2025 Department of Computer Science Slide No.


● If placing queen doesn’t lead to a solution then unmark this [row, column] and track back
and try other rows.
● If all rows have been tried and nothing worked return false to trigger backtrack

● Time Complexity: O(N!)


● Auxiliary Space: O(N2)

05/28/2025 Department of Computer Science Slide No.


Backtracking Algorithm by placing queens in rows:
● The idea is to place queens one by one in different rows, starting from the topmost row.
When we place a queen in a row, we check for clashes with already placed queens. In the
current column, if we find a row for which there is no clash, we mark this row and column
as part of the solution. If we do not find such a row due to clashes, then we backtrack
and return false
Follow the steps mentioned below to implement the idea:
1. Make a recursive function that takes the state of the board and the current row number
as its parameter.
2. Start in the topmost row.
3. If all queens are placed return true
4. Try all columns in the current row. Do the following for every tried column.
5. If the queen can be placed safely in this column

05/28/2025 Department of Computer Science Slide No.


6. Then mark this [row, column] as part of the solution and recursively check if placing
queen here leads to a solution.
7. If placing the queen in [row, column] leads to a solution then return true.
8. If placing queen doesn’t lead to a solution then unmark this [row, column] and track back
and try other columns.
9. If all columns have been tried and nothing worked return false to trigger backtracking.

Time Complexity: O(N!)


Auxiliary Space: O(N2)

05/28/2025 Department of Computer Science Slide No.


Example
● Now, we place queen q1 in the very first (1, 2) and then all other queens are
acceptable position (1, 1). Next, we put placed safely by moving q2 to (2, 4), q3 to
queen q2 so that both these queens do (3, 1) and q4 to (4, 3). That is, we get the
not attack each other. We find that if we solution (2, 4, 1, 3). This is one possible
place q2 in column 1 and 2, then the dead solution for the 4-queens problem. For
end is encountered. Thus the first another possible solution, the whole
acceptable position for q2 in column 3, method is repeated for all partial
i.e. (2, 3) but then no position is left for solutions. The other solutions for 4 -
placing queen 'q3' safely. So we backtrack queens problems is (3, 1, 4, 2) i.e.
one step and place the queen 'q2' in (2,
4), the next best possible solution. Then
we obtain the position for placing 'q3'
which is (3, 2). But later this position also
leads to a dead end, and no place is found
where 'q4' can be placed safely. Then we
have to backtrack till 'q1' and place it to

05/28/2025 Department of Computer Science Slide No.


05/28/2025 Department of Computer Science Slide No.
05/28/2025 Department of Computer Science Slide No.
● Fig shows the complete state space for 4 - queens problem. But we can use backtracking
method to generate the necessary node and stop if the next node violates the rule, i.e., if
two queens are attacking.

05/28/2025 Department of Computer Science Slide No.


● It can be seen that all the solutions to the 4 queens problem can be represented as 4 -
tuples (x1, x2, x3, x4) where xi represents the column on which queen "qi" is placed.
● One possible solution for 8 queens problem is shown in fig:

05/28/2025 Department of Computer Science Slide No.


● Thus, the solution for 8 -queen problem same diagonal.
for (4, 6, 8, 2, 7, 1, 3, 5). ● Place (k, i) return true if a queen can be
● If two queens are placed at position (i, j) placed in the kth row and ith column
and (k, l). otherwise return is false.
● Then they are on same diagonal only if (i -
j) = k - l or i + j = k + l. ● x [] is a global array whose final k - 1
● The first equation implies that j - l = i - k. values have been set. Abs (r) returns the
● The second equation implies that j - l = k - absolute value of r.
i.
● Therefore, two queens lie on the
duplicate diagonal if and only if |j-l|=|i-k|
● Place (k, i) returns a Boolean value that is
true if the kth queen can be placed in
column i. It tests both whether i is distinct
from all previous costs x1, x2,....xk-1 and
whether there is no other queen on the
05/28/2025 Department of Computer Science Slide No.
● N - Queens (k, n)
● {
● For i ← 1 to n
● do if Place (k, i) then
● {
● x [k] ← i;
● if (k ==n) then
● write (x [1....n));
● else
● N - Queens (k + 1, n);
● }
● }

05/28/2025 Department of Computer Science Slide No.


Hamiltonian Cycle
● The Hamiltonian cycle of undirected graph G <= V , E> is the cycle containing each
vertex in V. -If graph contains a Hamiltonian cycle, it is called Hamiltonian graph
otherwise it is non-Hamiltonian.
● Finding a Hamiltonian cycle in a graph is a well-known problem with many real-world
applications, such as in network routing and scheduling.
● Hamiltonian Path in an undirected graph is a path that visits each vertex exactly once. A
Hamiltonian cycle (or Hamiltonian circuit) is a Hamiltonian Path such that there is an edge
(in the graph) from the last vertex to the first vertex of the Hamiltonian Path. Determine
whether a given graph contains Hamiltonian Cycle or not. If it contains, then prints the
path. Following are the input and output of the required function.
● Input:
● A 2D array graph[V][V] where V is the number of vertices in graph and graph[V][V] is
adjacency matrix representation of the graph. A value graph[i][j] is 1 if there is a direct
edge from i to j, otherwise graph[i][j] is 0.

05/28/2025 Department of Computer Science Slide No.


● Output: An array path[V] that should And the following graph doesn’t contain
contain the Hamiltonian Path. path[i] any Hamiltonian Cycle.
should represent the ith vertex in the
Hamiltonian Path. The code should also (0)--(1)--(2)
return false if there is no Hamiltonian
Cycle in the graph. | /\ |
● For example, a Hamiltonian Cycle in the | / \ |
following graph is {0, 1, 2, 4, 3, 0}.
|/ \|
(0)--(1)--(2)
(3) (4)
| /\ |

| / \ |

|/ \|

(3)-------(4)

05/28/2025 Department of Computer Science Slide No.


Backtracking Algorithm
● Create an empty path array and add vertex 0 to it. Add other vertices, starting from the
vertex 1. Before adding a vertex, check for whether it is adjacent to the previously added
vertex and not already added. If we find such a vertex, we add the vertex as part of the
solution. If we do not find a vertex then we return false.
● Given a graph G = (V, E) we have to find the Hamiltonian Circuit using Backtracking
approach. We start our search from any arbitrary vertex say 'a.' This vertex 'a' becomes
the root of our implicit tree. The first element of our partial solution is the first
intermediate vertex of the Hamiltonian Cycle that is to be constructed. The next adjacent
vertex is selected by alphabetical order. If at any stage any arbitrary vertex makes a cycle
with any vertex other than vertex 'a' then we say that dead end is reached. In this case,
we backtrack one step, and again the search begins by selecting another vertex and
backtrack the element from the partial; solution must be removed. The search using
backtracking is successful if a Hamiltonian Cycle is obtained.
● Time Complexity : O(N!), where N is number of vertices.
● Auxiliary Space : O(1), since no extra space used.
05/28/2025 Department of Computer Science Slide No.
Example:
● Consider a graph G = (V, E) shown in fig. vertex 'a.' this vertex 'a' becomes the root
we have to find a Hamiltonian circuit of our implicit tree.
using Backtracking method. ●
Next, we choose vertex 'b' adjacent to 'a'
as it comes first in lexicographical order
(b, c, d).

● Solution: Firstly, we start our search with

05/28/2025 Department of Computer Science Slide No.


● Next, we select 'c' adjacent to 'b.‘

● Next, we select 'd' adjacent to 'c.‘

● Next, we select 'e' adjacent to 'd.


05/28/2025 Department of Computer Science Slide No.
● Next, we select vertex 'f' adjacent to 'e.'
The vertex adjacent to 'f' is d and e, but
they have already visited. Thus, we get
the dead end, and we backtrack one step
and remove the vertex 'f' from partial
solution.

05/28/2025 Department of Computer Science Slide No.


● From backtracking, the vertex adjacent
to 'e' is b, c, d, and f from which vertex 'f'
has already been checked, and b, c, d
have already visited. So, again we
backtrack one step. Now, the vertex
adjacent to d are e, f from which e has
already been checked, and adjacent of 'f'
are d and e. If 'e' vertex, revisited them
we get a dead state. So again we
backtrack one step.
● Now, adjacent to c is 'e' and adjacent to
'e' is 'f' and adjacent to 'f' is 'd' and
adjacent to 'd' is 'a.' Here, we get the
Hamiltonian Cycle as all the vertex other
than the start vertex 'a' is visited only
once. (a - b - c - e - f -d - a).

05/28/2025 Department of Computer Science Slide No.


● Again Backtrack
05/28/2025 Department of Computer Science Slide No.
● Here we have generated one Hamiltonian circuit, but another Hamiltonian circuit can
also be obtained by considering another vertex.

05/28/2025 Department of Computer Science Slide No.


Subset Sum Problem using Backtracking
● Given a set[] of non-negative integers and a value sum, the task is to print the subset of
the given set whose sum is equal to the given sum.
● Examples:
1. Input: set[] = {1,2,1}, sum = 3

Output: [1,2],[2,1]

Explanation: There are subsets [1,2],[2,1] with sum 3.

2. Input: set[] = {3, 34, 4, 12, 5, 2}, sum = 30

Output: []

Explanation: There is no subset that add up to 30.

05/28/2025 Department of Computer Science Slide No.


Backtracking Approach to solve
● Before knowing about the Backtracking approach, we should know about two things in a
subset which are given below:
● Include: Here include means that we are selecting the element from the array.
● Exclude: Here, exclude means that we are rejecting the element from the array.
● To implement the recursive approach, we consider the following two cases:
● Now we consider the first element and now the required sum is equal to the difference
between the target sum and value of first element. The number of elements is equal to
the difference between the total elements and 1.
● Leave the 'first' element and now the required sum = target sum. The number of
elements is equal to the difference between the total elements and 1.
● Let's understand that how can we solve the problem using recursion. Consider the array
which is given below:

05/28/2025 Department of Computer Science Slide No.


Algorithm
● def subset_sum(arr, res, sum) ● reject = subset_sum(arr, res, sum)
● if sum ==0 ● return reject or sum
● return true
● if sum < 0
● return false
● if len(arr) == 0 and sum!= 0
● return false
● arr.pop(0);
● if len(arr) > 0
● res.append(arr[0])
● select = subset_sum(arr, sum-arr[0], res)

05/28/2025 Department of Computer Science Slide No.


● arr = [3, 4, 5, 2]
● sum = 9
● result = []

● In the above example, we have taken an array, and the empty array named result that
stores all the values whose resultant sum is equal to 9.
● First element in an array is 3. There are two scenarios:
● First scenario is select. The sum is equal to the target sum - value of first element, i.e., 9 -
3 = 6 and the first element, i.e., 3 gets stored in the result array, i.e., result[].
● Second scenario is reject. The array arr contains the elements 4, 5, 2, i.e., arr = [4, 5, 2]
and sum would be same as 9 as we are rejecting the element 3. The result[] array would
remain empty.

05/28/2025 Department of Computer Science Slide No.


● Now we perform the same select and
reject operation on element 4 as it is the
first element of the array now.
● Select the element 4 from the array.
Since we are selecting 4 from the array
so array arr would contain the elements
5, 2, i.e., arr = [5, 2]. The sum is equal to
the 6-4 = 2 and the element 4 gets stored
in the result arr. The result[] = {3, 4}.
● Reject the element 4 from the array.
Since we are rejecting the 4 from the
array so array arr would contain the
elements 5, 2, i.e., arr = [5, 2]. The sum
would remain same as 6 and the result
array would be same as previous, i.e.,
{3}.

05/28/2025 Department of Computer Science Slide No.


● Now we perform the select and reject
operation on element 5.
● Select the element 5 from the array. Since
we are selecting 5 from the array so array
arr would contain the elements 2, i.e., arr
= [2]. The sum is equal to the 2 - 5 equals
to -3 and the element 5 gets stored in the
result arr. The result[] = {3, 4, 5}.
● Reject the element 5 from the array. Since
we are rejecting 5 from the array so array
arr would contain the element 2, i.e., arr =
[2]. The sum would remain same as
previous, i.e., 6 and the result array would
be same as previous, i.e., {3, 4}.

05/28/2025 Department of Computer Science Slide No.


● If we observe S-5, we can see that the sum
is negative that returns false. It means that
there is no further subset available in the
set.
● Consider R-5. It also has two scenarios:
● Select the element 2 from the array. Once
the element 2 gets selected, the array
becomes empty, i.e., arr[] = " ". The sum
would be 2-2 equals to 0 and the element
2 gets stored in the result array. The
result[] = [3, 4, 2].
● Reject the element 2 from the array. Once
the element 2 gets rejected, the array
becomes empty, i.e., arr[] = " ". The sum
would be same as previous, i.e., 2 and the
result array would also be same as
previous, i.e., [3, 4].

05/28/2025 Department of Computer Science Slide No.


● Consider R-4. It has two scenarios:

● Select the element 5 from the array. Since


we are selecting 5 from the array so array
arr would contain the elements 2, i.e., arr
= [2]. The sum would be 6-5 equals to 1
and the element 5 gets stored in the result
array. The result[] = [3, 5].
● Reject the element 5 from the array. Since
we are rejecting 5 from the array so array
arr would contain the element 2, i.e., arr =
[2]. The sum would remain same as
previous, i.e., 6 and the result array would
be same as previous, i.e., {3}.

05/28/2025 Department of Computer Science Slide No.


● Consider S-5. It has two scenarios:

● Select the element 2 from the array. Since


we are selecting 2 from the array so array
arr would be empty, i.e., arr = " ". The
sum would be 1-2 equals to -1 and the
element 2 gets stored in the result array.
The result[] = [3, 5, 2].
● Reject the element 2 from the array. Since
we are rejecting 2 from the array so array
arr would become empty. The sum would
remain same as previous, i.e., 1 and the
result array would be same as previous,
i.e., {3, 5}.

05/28/2025 Department of Computer Science Slide No.


● Consider R-5. It has two scenarios:
● Select the element 2 from the array. Since
we are selecting 2 from the array so array
arr would be empty, i.e., arr = " ". The
sum would be 6-2 equals to 4 and the
element 2 gets stored in the result array.
The result[] = [3, 2].
● Reject the element 2 from the array. Since
we are rejecting 2 from the array so array
arr would become empty. The sum would
remain same as previous, i.e., 6 and the
result array would be same as previous,
i.e., {3}.

05/28/2025 Department of Computer Science Slide No.


● Similarly, we get the reject case, i.e., R-3 as shown as below:

05/28/2025 Department of Computer Science Slide No.


● When we apply the base conditions on the above tree, then we will find two subsets given
below:
● S1 = {3, 4, 2}
● S2 = {4, 5}

05/28/2025 Department of Computer Science Slide No.


05/28/2025 Department of Computer Science Slide No.
05/28/2025 Department of Computer Science Slide No.
<Session Name>: Source
https://www.geeksforgeeks.org/

05/28/2025 Department of Computer Science Slide No.


Time for a Break !

05/28/2025 Department of Computer Science Slide No.


Any Doubts/Questions

05/28/2025 Department of Computer Science Slide No.


Thank You

05/28/2025 Department of Computer Science 81


Icons To Be Used (Suggestions Only)

Hands on
Doubts/ Tools Exercise
Questions

Coding Test Your


Reference
Standards Understanding

A Welcome Contacts
Demonstration Break

05/28/2025 Department of Computer Science Slide No.

You might also like