BACKTRACKING
[Link] Method
A backtracking algorithm is a problem-solving algorithm that uses
a brute force approach for finding the desired output.
The Brute force approach tries out all the possible solutions and chooses
the desired/best solutions.
The term backtracking suggests that if the current solution is not
suitable, then backtrack and try other solutions. Thus, recursion is used
in this approach.
This approach is used to solve problems that have multiple solutions. If
you want an optimal solution, you must go for dynamic programming.
State Space Tree: A space state tree is a tree representing all the
possible states (solution or non solution) of the problem from the root as
an initial state to the leaf as a terminal state.
State Space Tree
Backtracking Algorithm
Backtrack(x)
if x is not a solution
return false
if x is a new solution
add to list of solutions
backtrack(expand x)
Example Backtracking Approach
Problem: You want to find all the possible ways of arranging 2 boys
and 1 girl on 3 benches.
Constraint: Girl should not be on the middle bench.
Solution: There are a total of 3! = 6 possibilities. We will try all the
possibilities and get the possible solutions. We recursively try all the
possibilities.
All the possibilities are
All the possibilities
The following state space tree shows the possible solution
2. 8 Queens Problem:
Problem Statement
Given an 8x8 chess board, you must place 8 queens on the board so that
no two queens attack each other. Print all possible matrices satisfying
the conditions with positions with queens marked with '1' and empty
spaces with '0'. You must solve the 8 queens problem using
backtracking.
A queen can move vertically, horizontally and diagonally in any
number of steps.
You can also go through the N-Queen Problem for the general
approach to solving this problem.
Algorithm:
1. Initialization: Start with an empty 8x8 chessboard.
2. Row by Row Placement: Begin by trying to place a queen in the first
row (row 0).
3. Safety Check: For each potential position in a column, check if it's
"safe". A position is safe if no other queen can attack it horizontally,
vertically, or diagonally.
4. Recursive Placement:
o If a safe position is found, place the queen there.
o Then, recursively call the function to place a queen in the next row.
5. Backtracking:
o If no safe position can be found in the current row (meaning all columns
in that row are under attack), the algorithm "backtracks".
o It removes the queen from the previous row and tries the next available
column for that row.
6. Solution Found: If the algorithm successfully places queens in all eight
rows, a valid solution is found, and the board configuration is displayed.
State space tree of 4-queens is as below.
Time Complexity:
The time complexity of the 8 Queens problem (or more generally, the N-
Queens problem) using a backtracking algorithm is approximately
O(N!).
3. Sum of Subsets Problem
In the sum of subsets problem, there is a given set with some non-
negative integer elements. And another sum value is also provided, our
task is to find all possible subsets of the given set whose sum is the same
as the given sum value.
Set: In mathematical terms, a set is defined as a collection of similar
types of objects.
Subset: Suppose there are two sets namely set P and set Q. The set P is
said to be a subset of set Q, only if all the elements of set P also belong
to the set Q and vice-versa need not be true.
Backtracking Approach to solve Subset Sum Problem
In the naive method to solve a subset sum problem, the algorithm
generates all the possible permutations and then checks for a valid
solution one by one. Whenever a solution satisfies the constraints, mark
it as a part of the solution.
In solving the subset sum problem, the backtracking approach is used for
selecting 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.
Algorithm:
1. First, take an empty subset.
2. Include the next element, which is at index 0 to the empty set.
3. If the subset is equal to the sum value, mark it as a part of the
solution.
4. If the subset is not a solution and it is less than the sum value, add
next element to the subset until a valid solution is found.
5. Now, move to the next element in the set and check for another
solution until all combinations have been tried.
Input :
Suppose the given set and sum value is −
Set = {5,10,12,13,15,18}
sum value = 30
Here we get the solution is obtained by considering 1st value 5,2nd
value 10 and 5th value 15. so 5+10+15=30,our objective is reached.
All possible subsets of the given set, where sum of each element for
every subset is the same as the given sum value are given below −
output:
{5,10,15}
{5,12,13}
{12,18}
Here we find the solution by using state space tree representation
as follows
Time Complexity: O(2n) The above solution may try all subsets of the
given set in the worst case. Therefore time complexity of the above
solution is exponential.
Auxiliary Space: O(n) where n is recursion stack space.
4. Graph Coloring
Graph coloring refers to the problem of coloring vertices of a graph in
such a way that no two adjacent vertices have the same color. This is
also called the vertex coloring problem. If coloring is done using at
most m colors, it is called m-coloring.
Chromatic Number:
The minimum number of colors needed to color a graph is called its
chromatic number.
Algorithm:
Step 1 − Arrange the vertices of the graph in some order.
Step 2 − Choose the first vertex and color it with the first color.
Step 3 − Choose the next vertex and color it with the lowest numbered
color that has not been colored on any vertices adjacent to it. If all the
adjacent vertices are colored with this color, assign a new color to it.
Repeat this step until all the vertices are colored.
In the above figure, at first vertex a is colored red. As the adjacent
vertices of vertex a are vertex b and vertex d are colored with different
color, green and blue respectively. Then vertex c is colored as red as no
adjacent vertex of c is colored red. Hence, we could color the graph by 3
colors. Hence, the chromatic number of the graph is 3.
The state space tree of graph coloring problem is as follows.
Time Complexity: Time Complexity
In the backtracking approach of the graph coloring problem, the time
complexity is O(mV).Here m=No of colors,v=no of vertices.
Space Complexity
In the backtracking approach to the graph coloring problem, we are not
using any extra space but we are using the recursive stack for the
recursive function call. So, the space complexity is O(V).
5. 0/1 Knapsack Problem
Problem:
Given n items where each item has some weight and profit associated with it and
also given a bag with capacity W, [i.e., the bag can hold at most W weight in it].
The task is to put the items into the bag such that the sum of profits associated with
them is the maximum possible.
Input:W=4,profit[]=[1,23],weight[]=[4,5,1]
Output:3
Explanation: There are two items which have weight less than or equal to 4. If we
select the item with weight 4, the possible profit is 1. And if we select the item
with weight 1, the possible profit is 3. So the maximum possible profit is 3. Note
that we cannot put both the items with weight 4 and 1 together as the capacity of
the bag is 4.
Input:W=3,profit[]=[1,2,3],weight[]=[4,5,6]
Output: 0
Here our problem is items A,B,C are having profit/value=[1,7,11],weight[]=[1,2,3]
and W=5.
Here we draw state space tree diagram by selecting each weight and skipping and
we get the solution as weight=2+3=5 which is our constraint and value=7+11=18,
which is our maximum profit i.e our objective is reached.
The state space tree of 0/1 Knapsack problem is as follows
Time Complexity: The time complexity of the 0/1 Knapsack problem
using a naive backtracking approach is O(2n), where 'n' is the number of
items.