0% found this document useful (0 votes)
12 views7 pages

Backtracking

This document provides an overview of backtracking as a problem-solving technique, particularly for constraint satisfaction problems like Sudoku and graph coloring. It discusses two specific backtracking techniques: the Sum of Subsets and Graph Coloring, detailing their applications and algorithms. Additionally, it explains key terminologies and the structure of a state-space tree used in backtracking algorithms.

Uploaded by

vidyas.bsccs2020
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)
12 views7 pages

Backtracking

This document provides an overview of backtracking as a problem-solving technique, particularly for constraint satisfaction problems like Sudoku and graph coloring. It discusses two specific backtracking techniques: the Sum of Subsets and Graph Coloring, detailing their applications and algorithms. Additionally, it explains key terminologies and the structure of a state-space tree used in backtracking algorithms.

Uploaded by

vidyas.bsccs2020
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/ 7

ABSTRACT

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

root as an initial state to the leaf as a terminal state.

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

must go for dynamic programming.

Backtracking is an algorithmic technique that incrementally builds and explores possible


solutions to a problem. It starts with an empty solution, makes a move, checks constraints,
and either recurses or backtracks based on the validity of the partial solution. It's particularly
useful for solving problems involving permutations, combinations, subsets, and certain graph
or puzzle scenarios. Backtracking efficiently explores solution spaces, pruning paths that
cannot lead to valid outcomes, but its effectiveness depends on the problem and how well
pruning can be implemented.
It uses ‘recursive calling’to find a solution set by building a solution step by step, increasing
levels with time. In order to find these solutions, a search tree named ‘state space tree’ is used. In a
state-space tree, each branch is a variable, and each level represents a solution.

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

Backtracking is a class of algorithms for finding solutions to some computational problems,


notably constraint satisfaction problems, that incrementally builds candidates to the solutions, and
abandons a candidate as soon as it determines that the candidate cannot possibly be completed to a
valid solution. 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
Basic Terminologies
 Candidate: A candidate is a potential choice or element that can be added to the current
solution.
 Solution: The solution is a valid and complete configuration that satisfies all problem
constraints.
 Partial Solution: A partial solution is an intermediate or incomplete configuration being
constructed during the backtracking process.
 Decision Space: The decision space is the set of all possible candidates or choices at each
decision point.
 Decision Point: A decision point is a specific step in the algorithm where a candidate is
chosen and added to the partial solution.
 Feasible Solution: A feasible solution is a partial or complete solution that adheres to all
constraints.
 Dead End: A dead end occurs when a partial solution cannot be extended without violating
constraints.
 Backtrack: Backtracking involves undoing previous decisions and returning to a prior
decision point.
 Search Space: The search space includes all possible combinations of candidates and choices.
 Optimal Solution: In optimization problems, the optimal solution is the best possible
solution.
Types of Backtracking Problems
Problems associated with backtracking can be categorized into 3 categories:
 Decision Problems: Here, we search for a feasible solution.
 Optimization Problems: For this type, we search for the best solution.
 Enumeration Problems: We find set of all possible feasible solutions to the problems of this
type.
State-Space Tree
A space state tree is a tree that represents all of the possible states of the problem, from the root as an
initial state to the leaf as a terminal state.

An Example of Backtracking Algorithm

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.

# Print all subsets if there is at least one subset of set[]


# with a sum equal to the given sum
flag = False

def print_subset_sum(i, n, _set, target_sum, subset):


global flag
# If targetSum is zero, then there exists a subset
if target_sum == 0:
# Prints valid subset
flag = True
print("[", end=" ")
for element in subset:
print(element, end=" ")
print("]", end=" ")
return

if i == n:
# Return if we have reached the end of the array
return

# Not considering the current element


print_subset_sum(i + 1, n, _set, target_sum, subset)

# Consider the current element if it is less than or equal to targetSum


if _set[i] <= target_sum:
# Push the current element into the subset
subset.append(_set[i])

# Recursive call for considering the current element


print_subset_sum(i + 1, n, _set, target_sum - _set[i], subset)

# Remove the last element after recursive call to restore subset's


original configuration
subset.pop()

# 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

You might also like