What Is Backtracking
What Is Backtracking
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.
How does Backtracking works?
As we know backtracking algorithm explores each and every possible path in order to find
a valid solution, this exploration of path can be easily understood via given images:
As shown in the image, “IS” represents the Initial State where the recursion call starts to
find a valid solution.
C : it represents different Checkpoints for recursive calls
TN: it represents the Terminal Nodes where no further recursive calls can be made, these
nodes act as base case of recursion and we determine whether the current solution is valid
or not at this state.
At each Checkpoint, our program makes some decisions and move to other checkpoints
untill it reaches a terminal Node, after determining whether a solution is valid or not, the
program starts to revert back to the checkpoints and try to explore other paths. For
example in the above image TN1…TN5 are the terminal node where the solution is not
acceptable, while TN6 is the state where we found a valid solution.
The back arrows in the images shows backtracking in actions, where we revert the
changes made by some checkpoint.
Solving problems by breaking them into Solving problems with multiple choices and
smaller, similar subproblems and solving them exploring options systematically, backtracking
recursively. when needed.
Controlled by function calls and call stack. Managed explicitly with loops and state.
Traversal, Towers of Hanoi, Divide and problem, Rat in a Maze problem, Knight’s
Conquer Algorithms, Merge Sort, Quick Sort, Tour Problem, Sudoku solver, and Graph
and Binary Search. coloring problems.
Applications of Backtracking
Creating smart bots to play Board Games such as Chess.
Solving mazes and puzzles such as N-Queen problem.
Network Routing and Congestion Control.
Decryption
Text Justification