Backtracking
Backtracking
Backtracking is a problem-solving technique that involves trying different options until the right
solution is found. When faced with a complex problem or puzzle, backtracking allows us to explore
various paths and make informed choices.Backtracking is an important tool for solving constraint
satisfaction problems, such as crosswords, verbal arithmetic, Sudoku, and many other puzzles.A space
state tree is a tree representing all the possible states (solution or nonsolution) of the problem from the
This paper represents an overview of two different types of backtracking techniques. There are:
Sum of Subsets
The "sum of subsets" refers to finding all possible combinations of elements in a set
such that the sum of the elements in each combination matches a specific target sum.
• Graph Coloring
Graph coloring in backtracking is a problem-solving technique where the goal is to
assign colors to the vertices of a graph such that no two adjacent vertices share the same color .
INTRODUCTION
The term "backtrack" was coined by American mathematician D. H. Lehmer in the 1950s. The
pioneer string-processing language SNOBOL (1962) may have been the first to provide a built-in
general backtracking facility.
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 (decision problems). If you want an optimal solution, you
A backtracking algorithm uses the ‘depth-first search’ method. When it starts exploring the
solutions, a bounding function is applied so that the algorithm can check if the so-far built solution
satisfies the constraints. If it does, it continues searching. If it doesn’t, the branch would be
eliminated, and the algorithm goes back to the level before.
Backtracking
We will consider here will have three numbers as input. The goal is to generate all possible
combinations of the numbers. So if we have the numbers 1, 2, & 3 as inputs, the output should contain
the numbers 123, 132, 213, 231, 312, and 321. Now let’s look at the state-space tree for this example.
Now suppose we add one condition to the problem. And the condition is that the numbers 1 & 2
cannot be adjacent to one another. In that case, outputs 123, 213, 312, & 321 will be automatically
rejected or eliminated from the output. And the all that remains in the final output will be 132 & 231.
Sum of subset
Subset sum problem is the problem of finding a subset such that the sum of
elements equal a given number. The backtracking approach generates all
permutations in the worst case but in general, performs better than the recursive
approach towards subset sum problem.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.
if i == n:
# Return if we have reached the end of the array
return
# Driver code
if __name__ == "__main__":
# Test case 1
set_1 = [1, 2, 1]
sum_1 = 3
n_1 = len(set_1)
subset_1 = []
print("Output 1:")
print_subset_sum(0, n_1, set_1, sum_1, subset_1)
print()
flag = False
# Test case 2
set_2 = [3, 34, 4, 12, 5, 2]
sum_2 = 30
n_2 = len(set_2)
subset_2 = []
print("Output 2:")
print_subset_sum(0, n_2, set_2, sum_2, subset_2)
if not flag:
print("There is no such subset")
output:
Output 1:
[21][12]
Output 2:
There is no such subset