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

Backtracking - Introduction

Backtracking is an algorithmic technique for solving problems recursively by incrementally building candidates to solutions and abandoning partial candidates ("backtracking") that cannot be completed to a valid solution due to problem constraints. It is a refined brute force approach that considers all possible combinations and chooses the best solutions. It is often faster than brute force as it can eliminate many candidates with a single test.

Uploaded by

rajkumar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
93 views

Backtracking - Introduction

Backtracking is an algorithmic technique for solving problems recursively by incrementally building candidates to solutions and abandoning partial candidates ("backtracking") that cannot be completed to a valid solution due to problem constraints. It is a refined brute force approach that considers all possible combinations and chooses the best solutions. It is often faster than brute force as it can eliminate many candidates with a single test.

Uploaded by

rajkumar
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Backtracking - Introduction

By
Dr.V.Venkateswara Rao
Backtracking -Introduction
Backtracking is an algorithmic-technique for solving problems recursively by
trying to build a solution incrementally, one piece at a time
( One input at a time),
removing those solutions that fail to satisfy the constraints of the problem
at any point of time.

Backtracking can be defined as a general algorithmic technique


that considers searching every possible combination in order to solve a
computational problem. 

The Backtracking is an algorithmic-method to solve a problem with an


additional way. It uses a recursive approach to explain the problems. We c
say that the backtracking is needed to find all possible combination to sol
optimization problem.
Backtracking is an algorithmic technique that considers searching
in every possible combination for solving a computational
problem.
It is known for solving problems recursively one step at a time and
removing those solutions that that do not satisfy the problem
constraints at any point of time.
It is a refined brute force approach that tries out all the possible
solutions and chooses the best possible ones out of them.
The backtracking approach is generally used in the cases where
there are possibilities of multiple solutions.
Backtracking is a general algorithm for finding all (or some) solutions to some
computational problems, notably constraint satisfaction problems, that incrementally
builds candidates to the solutions, and abandons each partial candidate `c` (“backtracks”)
as soon as it determines that `c` cannot possibly be completed to a valid solution.

Backtracking can be applied only for problems that admit the concept of a “partial
candidate solution” and a relatively quick test of whether it can be completed to a valid
solution.

Backtracking is often much faster than brute force enumeration of all candidates since it
can eliminate a large number of candidates with a single test.
Backtracking - How it works?
In any backtracking problems, the algorithm tries to find a path to the feasible
solution which has some intermediary checkpoints. In case they don’t lead to the
feasible solution, the problem can backtrack from the checkpoints and take another
path in search of the solution.
Here S is the starting point of the problem. We start from S, we go to find solution S1 via
the intermediate point I1. But we find that the solution S1 is not a feasible solution to
our problem.
Hence, we backtrack (go back) from S1, go back to I1, go back to S and then check for the
feasible solution S2. This process happens till we arrive at a feasible solution.
Here, S1 and S2 are not the feasible solutions.
Only S3 is a feasible solution as per our example.
When we look at this example, we can see that we traverse through all possible
combinations, till we arrive at the feasible solution.

This is why, we say that backtracking is a brute-force algorithmic technique.


The above tree representation of a problem is called as a “space state tree”. It
represents all possible states (solution or non-solution) of that given problem.
The final algorithm can be summarised as:
Step 1 − if current point is a feasible solution, return success
Step 2 − else if all paths are exhausted (i.e current point is an end point), return
failure, since we have no feasible solution.
Step 3 − else if current point is not an end point, backtrack and explore other points
and repeat above steps.
Decision Problem – In this, we search for a feasible solution.
Optimization Problem – In this, we search for the best solution.
Enumeration Problem – In this, we find all feasible solutions
Permutations and combinations are examples of Enumeration Problems

Difference between Recursion and Backtracking:


In recursion, the function calls itself until it reaches a base case. In
backtracking, we use recursion to explore all the possibilities until we get
the best result for the problem.
Recursive backtracking solution. 
void findSolutions(n, other params) :
if (found a solution) :
solutionsFound = solutionsFound + 1;
displaySolution();
if (solutionsFound >= solutionTarget) :
System.exit(0);
return
for (val = first to last) :
if (isValid(val, n)) :
applyValue(val, n);
findSolutions(n+1, other params);
removeValue(val, n);
Permutation Problems:
1.Given a collection of numbers, return all possible permutations.
2. Given a Set(Array) Find all possible subsets
3. Given A set(Array) find Power Set
4. Given Array(Set) Find number of Subsets(Count of Subsets)
5. Given a Set(Array) Find all possible subsets of Length 2
6. Given Array(Set) Find number of Subsets(Count of Subsets) of Length 2
7. Given a Set(Array) Find all possible subsets of Length 3
8. Given Array(Set) Find number of Subsets(Count of Subsets) of Length 3
9. Given a Set(Array) Find all possible subsets of Length k
10. Given Array(Set) Find number of Subsets(Count of Subsets) of Length k
11. Subset Sum Problem
12. Subsets of length with sum equal to K
13. program to print all permutations of a given string
14. Given a Set(Array) Find all possible subsets with duplicates
15. program to print all permutations of a given string with duplicates
Print all possible solutions to N–Queens problem
Print all possible Knight’s tours on a chessboard
Find the shortest path in a maze (Rat in Maze)
Find the longest possible route in a matrix
Find the path from source to destination in a matrix that satisfies given constraints
Find the total number of unique paths in a maze from source to destination
Find all combinations of elements satisfying given constraints
K–Partition Problem | Printing all partitions
Magnet Puzzle
Find all paths from the first cell to the last cell of a matrix
Print all shortest routes in a rectangular grid
m Coloring Problem
Hamiltonian Cycle
Sudoku
Solving Cryptarithmetic Puzzles

Number of ways to reach source to destination


Number of jumps required

Length of Longest Common Subsequence

Length of Longest Increasing Subsequence

Length of Longest Fibonacci Sequence

You might also like