KEMBAR78
BacktrackingAlgorithms_data structure.pptx
BACKTRACKING
BACKTRACKING
 A backtracking algorithm is a way to solve
problems by trying out different options one by
one, and if an option doesn’t work, it "backtracks"
and tries the next option.
 It’s like exploring a maze: you try one path, and if
you hit a dead end, you go back and try a
different path until you find the exit.
 The goal is to explore all possible paths until you
find the correct solution.
BACKTRACKING
BACKTRACKING TERMINOLOGY
 State Space: This is the set of all possible states in our problem.
Think of it as all the possible configurations or situations we might
encounter while solving the problem.
 State Space Tree: If we were to draw the state space using a tree
structure, we would get a state space tree.
 Candidate: A candidate is a potential choice or element that we can
add to our current partial solution.
 Solution: A complete and valid answer to our problem. This is often
referred to as a "candidate solution" because it comprises the
candidates we've selected at each step of the backtracking process.
 Dead-end: A state where we can't proceed further towards a valid
solution.
 Terminal Condition: This is the condition that tells us when to stop
exploring a particular path. It could be because we've found a solution
or because we've reached a dead end.
TERMINOLOGY …
 A backtrack step: This is the process of
returning to a previous state in the algorithm's
execution path when a terminal condition is
reached to explore alternative possibilities.
 Pruning: This is a technique we use to eliminate
unnecessary exploration of the state space. It's
like taking shortcuts in our search by avoiding
paths we know won't lead to a solution.
HOW DOES A BACKTRACKING ALGORITHM
WORK?
1. Start at the Initial Position
 The algorithm begins at the initial position or the
root of the decision tree. This is the starting point
from where different paths will be explored.
2. Make a Decision
 At each step, the algorithm makes a decision that
moves it forward. This could be moving in a certain
direction in a maze or choosing a particular option
in a decision tree.
3. Check for Validity
 After making a decision, the algorithm checks if the
current path is valid or if it meets the problem’s
constraints. If the path is invalid or leads to a dead
end, the algorithm backtracks to the previous step.
HOW DOES A BACKTRACKING
ALGORITHM WORK?.....
4. Backtrack if Necessary
 If a dead end is reached or if the path doesn't lead to a
solution, the algorithm backtracks by undoing the last
decision. It then tries a different option from the
previous decision point.
5. Continue Exploring
 The algorithm continues to explore different paths,
making decisions, checking validity, and backtracking
when necessary. This process repeats until a solution is
found or all possible paths have been explored.
6. Find the Solution or Exhaust All Options
 The algorithm stops when it finds a valid solution or
when all possible paths have been explored and no
solution exists.
PERMUTATIONS
 One of the backtracking problems is
 finding all permutations of an array of numbers.
 For example:
 For the input [1, 2 ,3], all possible permutations are
 [[1, 2 ,3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]
THE STATE SPACE TREE
 The state space encompasses all possible paths we could take
to reach a solution, including both valid and invalid paths.
 In our permutation problem, this includes all possible
arrangements of the numbers, even those that repeat digits.
 To make this abstract concept more tangible, we visualize it as
a tree structure, which we call the state space tree.
CANDIDATES
 Candidates are all possible elements we could
add to our solution at each level of the tree.
 These are the choices available to us at any given
point in our backtracking process.
 At the root level, our candidates are
numbers 1, 2, and 3;
 we can start our permutation with any of these
numbers.
CANDIDATES….
 At the second level, for each branch from the root, we again have
three candidates to choose from. This results in nine total candidates
at the second level (three for each of the three first-level nodes).
 It's important to note that at this stage, we're considering all
possibilities, including invalid ones like choosing the same number
twice.
TERMINAL CONDITIONS AND
SOLUTIONS
 Terminal Conditions are reached at the leaf nodes of our state
space tree, representing the end points of our exploration paths.
 These conditions mark the moment when we can no longer
extend our current candidate solution and must make a decision.
 In our permutation problem, a terminal condition is reached
when we've selected three numbers, as this completes a
potential permutation.
 At this point, we need to evaluate the completed candidate
solution.
 Solutions are those terminal conditions that satisfy all the
constraints of our problem.
 In the context of our permutation task, a solution is a complete
arrangement of the numbers 1, 2, and 3 where each number is
used exactly once.
DEAD-ENDS
 Dead-ends (represented by X mark on the drawing), on the other
hand, are terminal conditions that violate one or more of our problem
constraints. In our permutation example, dead-ends would include
arrangements like [1,1,1], [1,2,2], or [2,3,3], where numbers are
repeated.
BACKTRACKING STEP
 A backtracking step occurs when the algorithm has
reached a terminal condition (either a solution or a dead-
end) and needs to return to a previous state to explore other
possibilities.
 It's the "back" in "backtracking" - the mechanism that allows
the algorithm to explore all potential solutions
systematically.
 Once we reach a terminal condition, we "backtrack" by
undoing the most recent choice (removing the last candidate
from the potential solution) and thus returning to the
previous state.
 Then, we move on to the next unexplored option at that
level.
 Once all options at that level are explored, we backtrack
further.
BACKTRACKING STEP….
 We start with [] as our initial state, represented by the root of the tree.
 From there, we add 1 to reach [1].
 We then add 1 again, reaching [1,1].
 In naive branching, we add 1 once more to get [1,1,1].
 At this point, we've reached a terminal condition (a complete but invalid
permutation), triggering our first backtrack.
 This backtrack is shown by the dashed arrow returning to [1,1].
 Next, we try our next option, reaching [1,1,2]. Since this is also a
terminal condition (dead-end), we backtrack to [1,1] and try the third
and final number 3.
 However, [1,1,3] is also a dead-end, so we backtrack twice, popping two
values from the candidate array until we reach [1]. Then we add the
number 2 and get [1,2], and subsequently try the next number 1, giving
us [1,2,1].
 This is not a valid solution, so we backtrack again.
 This process continues until we explore all possible options,
systematically generating all valid permutations.
PRUNING
 Pruning is an optimization technique used in backtracking
algorithms to eliminate branches of the state space tree that are
guaranteed not to lead to a valid solution.
 Unlike naive branching logic, pruning allows us to skip exploring
certain paths based on problem-specific knowledge or constraints.
PRUNING ….
 In this pruned tree, we can see how the algorithm
avoids unnecessary exploration.
 The 'X' marks indicate branches that are pruned and
not explored further.
 These pruned branches represent paths where a
number would be repeated in the permutation,
which we know cannot lead to a valid solution.
 For instance, under the root node 1, we immediately
prune the left branch, as it would lead to [1,1], an
invalid permutation.
 We only explore paths that don't repeat numbers,
significantly reducing the number of branches we
must traverse
WHEN TO USE A BACKTRACKING
ALGORITHM?
Backtracking algorithms are best used to solve
problems that have the following characteristics:
 There are multiple possible solutions to the
problem.
 The problem can be broken down into smaller
subproblems.
 The subproblems can be solved independently.
APPLICATIONS OF BACKTRACKING
ALGORITHM
 Puzzle Solving: Used in solving puzzles like Sudoku, crosswords, and
the N-Queens problem.
 Combinatorial Optimization: Generates all permutations,
combinations, and subsets of a given set.
 Constraint Satisfaction Problems: Applied in problems like graph
coloring, scheduling, and job assignment where constraints must be met.
 Pathfinding: Solves mazes and other pathfinding problems, such as the
Rat in a Maze problem.
 Game Solving: Used in strategy games to explore possible moves, such
as in the Knight’s Tour problem.
 Decision-Making: Helps in making optimal decisions in situations
where multiple choices are possible, like in the Subset Sum problem.
 String Processing: Generates valid strings that meet certain criteria,
such as generating balanced parentheses.
 Optimization Problems: Finds solutions to complex optimization
problems, such as maximizing profits or minimizing costs under certain
constraints.
N-QUEENS PROBLEM
Using Backtracking
N-QUEENS PROBLEM
 Given an integer n, place n queens on an n × n
chessboard such that no two queens attack each
other.
 A queen can attack another queen if they are placed
in the same row, the same column, or on the
same diagonal.
 Find all possible distinct arrangements of the
queens on the board that satisfy these conditions.
 The output should be an array of solutions, where
each solution is represented as an array of integers
of size n, and the i-th integer denotes the column
position of the queen in the i-th row.
 If no solution exists, return an empty array.
N-QUEENS PROBLEM ….
 Input: n = 4
Output: [[2, 4, 1, 3], [3, 1, 4, 2]]
Explanation: Below is Solution for 4 queen
problem
 Input: n = 3
Output: []
Explanation: There are no possible solutions for
n = 3
[NAIVE APPROACH] - USING BACKTRACKING
 The idea is to use backtracking to place queens
on an n × n chessboard. We can proceed either
row by row or column by column. For each
row (or column), try placing a queen in every
column (or row) and check if it is safe (i.e., no
other queen in the same column, row, or
diagonals). If safe, place the queen and move to
the next row/column. If no valid position exists,
backtrack to the previous step and try a different
position. Continue until all queens are placed or
all possibilities are explored.
STATE SPACE TREE FOR N-QUEEN’S PROBLEM
 Time Complexity: O(n!)
For the first queen, we have n columns to choose
from. Each subsequent queen has fewer valid
positions because previous queens block their
columns and diagonals, roughly reducing choices
to n−2, n−4, and so on, giving an approximate
O(n!) time.

BacktrackingAlgorithms_data structure.pptx

  • 1.
  • 2.
    BACKTRACKING  A backtrackingalgorithm is a way to solve problems by trying out different options one by one, and if an option doesn’t work, it "backtracks" and tries the next option.  It’s like exploring a maze: you try one path, and if you hit a dead end, you go back and try a different path until you find the exit.  The goal is to explore all possible paths until you find the correct solution.
  • 3.
  • 4.
    BACKTRACKING TERMINOLOGY  StateSpace: This is the set of all possible states in our problem. Think of it as all the possible configurations or situations we might encounter while solving the problem.  State Space Tree: If we were to draw the state space using a tree structure, we would get a state space tree.  Candidate: A candidate is a potential choice or element that we can add to our current partial solution.  Solution: A complete and valid answer to our problem. This is often referred to as a "candidate solution" because it comprises the candidates we've selected at each step of the backtracking process.  Dead-end: A state where we can't proceed further towards a valid solution.  Terminal Condition: This is the condition that tells us when to stop exploring a particular path. It could be because we've found a solution or because we've reached a dead end.
  • 5.
    TERMINOLOGY …  Abacktrack step: This is the process of returning to a previous state in the algorithm's execution path when a terminal condition is reached to explore alternative possibilities.  Pruning: This is a technique we use to eliminate unnecessary exploration of the state space. It's like taking shortcuts in our search by avoiding paths we know won't lead to a solution.
  • 6.
    HOW DOES ABACKTRACKING ALGORITHM WORK? 1. Start at the Initial Position  The algorithm begins at the initial position or the root of the decision tree. This is the starting point from where different paths will be explored. 2. Make a Decision  At each step, the algorithm makes a decision that moves it forward. This could be moving in a certain direction in a maze or choosing a particular option in a decision tree. 3. Check for Validity  After making a decision, the algorithm checks if the current path is valid or if it meets the problem’s constraints. If the path is invalid or leads to a dead end, the algorithm backtracks to the previous step.
  • 7.
    HOW DOES ABACKTRACKING ALGORITHM WORK?..... 4. Backtrack if Necessary  If a dead end is reached or if the path doesn't lead to a solution, the algorithm backtracks by undoing the last decision. It then tries a different option from the previous decision point. 5. Continue Exploring  The algorithm continues to explore different paths, making decisions, checking validity, and backtracking when necessary. This process repeats until a solution is found or all possible paths have been explored. 6. Find the Solution or Exhaust All Options  The algorithm stops when it finds a valid solution or when all possible paths have been explored and no solution exists.
  • 8.
    PERMUTATIONS  One ofthe backtracking problems is  finding all permutations of an array of numbers.  For example:  For the input [1, 2 ,3], all possible permutations are  [[1, 2 ,3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]
  • 9.
    THE STATE SPACETREE  The state space encompasses all possible paths we could take to reach a solution, including both valid and invalid paths.  In our permutation problem, this includes all possible arrangements of the numbers, even those that repeat digits.  To make this abstract concept more tangible, we visualize it as a tree structure, which we call the state space tree.
  • 10.
    CANDIDATES  Candidates areall possible elements we could add to our solution at each level of the tree.  These are the choices available to us at any given point in our backtracking process.  At the root level, our candidates are numbers 1, 2, and 3;  we can start our permutation with any of these numbers.
  • 11.
    CANDIDATES….  At thesecond level, for each branch from the root, we again have three candidates to choose from. This results in nine total candidates at the second level (three for each of the three first-level nodes).  It's important to note that at this stage, we're considering all possibilities, including invalid ones like choosing the same number twice.
  • 12.
    TERMINAL CONDITIONS AND SOLUTIONS Terminal Conditions are reached at the leaf nodes of our state space tree, representing the end points of our exploration paths.  These conditions mark the moment when we can no longer extend our current candidate solution and must make a decision.  In our permutation problem, a terminal condition is reached when we've selected three numbers, as this completes a potential permutation.  At this point, we need to evaluate the completed candidate solution.  Solutions are those terminal conditions that satisfy all the constraints of our problem.  In the context of our permutation task, a solution is a complete arrangement of the numbers 1, 2, and 3 where each number is used exactly once.
  • 13.
    DEAD-ENDS  Dead-ends (representedby X mark on the drawing), on the other hand, are terminal conditions that violate one or more of our problem constraints. In our permutation example, dead-ends would include arrangements like [1,1,1], [1,2,2], or [2,3,3], where numbers are repeated.
  • 14.
    BACKTRACKING STEP  Abacktracking step occurs when the algorithm has reached a terminal condition (either a solution or a dead- end) and needs to return to a previous state to explore other possibilities.  It's the "back" in "backtracking" - the mechanism that allows the algorithm to explore all potential solutions systematically.  Once we reach a terminal condition, we "backtrack" by undoing the most recent choice (removing the last candidate from the potential solution) and thus returning to the previous state.  Then, we move on to the next unexplored option at that level.  Once all options at that level are explored, we backtrack further.
  • 15.
    BACKTRACKING STEP….  Westart with [] as our initial state, represented by the root of the tree.  From there, we add 1 to reach [1].  We then add 1 again, reaching [1,1].  In naive branching, we add 1 once more to get [1,1,1].  At this point, we've reached a terminal condition (a complete but invalid permutation), triggering our first backtrack.  This backtrack is shown by the dashed arrow returning to [1,1].  Next, we try our next option, reaching [1,1,2]. Since this is also a terminal condition (dead-end), we backtrack to [1,1] and try the third and final number 3.  However, [1,1,3] is also a dead-end, so we backtrack twice, popping two values from the candidate array until we reach [1]. Then we add the number 2 and get [1,2], and subsequently try the next number 1, giving us [1,2,1].  This is not a valid solution, so we backtrack again.  This process continues until we explore all possible options, systematically generating all valid permutations.
  • 17.
    PRUNING  Pruning isan optimization technique used in backtracking algorithms to eliminate branches of the state space tree that are guaranteed not to lead to a valid solution.  Unlike naive branching logic, pruning allows us to skip exploring certain paths based on problem-specific knowledge or constraints.
  • 18.
    PRUNING ….  Inthis pruned tree, we can see how the algorithm avoids unnecessary exploration.  The 'X' marks indicate branches that are pruned and not explored further.  These pruned branches represent paths where a number would be repeated in the permutation, which we know cannot lead to a valid solution.  For instance, under the root node 1, we immediately prune the left branch, as it would lead to [1,1], an invalid permutation.  We only explore paths that don't repeat numbers, significantly reducing the number of branches we must traverse
  • 19.
    WHEN TO USEA BACKTRACKING ALGORITHM? Backtracking algorithms are best used to solve problems that have the following characteristics:  There are multiple possible solutions to the problem.  The problem can be broken down into smaller subproblems.  The subproblems can be solved independently.
  • 20.
    APPLICATIONS OF BACKTRACKING ALGORITHM Puzzle Solving: Used in solving puzzles like Sudoku, crosswords, and the N-Queens problem.  Combinatorial Optimization: Generates all permutations, combinations, and subsets of a given set.  Constraint Satisfaction Problems: Applied in problems like graph coloring, scheduling, and job assignment where constraints must be met.  Pathfinding: Solves mazes and other pathfinding problems, such as the Rat in a Maze problem.  Game Solving: Used in strategy games to explore possible moves, such as in the Knight’s Tour problem.  Decision-Making: Helps in making optimal decisions in situations where multiple choices are possible, like in the Subset Sum problem.  String Processing: Generates valid strings that meet certain criteria, such as generating balanced parentheses.  Optimization Problems: Finds solutions to complex optimization problems, such as maximizing profits or minimizing costs under certain constraints.
  • 21.
  • 22.
    N-QUEENS PROBLEM  Givenan integer n, place n queens on an n × n chessboard such that no two queens attack each other.  A queen can attack another queen if they are placed in the same row, the same column, or on the same diagonal.  Find all possible distinct arrangements of the queens on the board that satisfy these conditions.  The output should be an array of solutions, where each solution is represented as an array of integers of size n, and the i-th integer denotes the column position of the queen in the i-th row.  If no solution exists, return an empty array.
  • 23.
    N-QUEENS PROBLEM …. Input: n = 4 Output: [[2, 4, 1, 3], [3, 1, 4, 2]] Explanation: Below is Solution for 4 queen problem
  • 24.
     Input: n= 3 Output: [] Explanation: There are no possible solutions for n = 3
  • 25.
    [NAIVE APPROACH] -USING BACKTRACKING  The idea is to use backtracking to place queens on an n × n chessboard. We can proceed either row by row or column by column. For each row (or column), try placing a queen in every column (or row) and check if it is safe (i.e., no other queen in the same column, row, or diagonals). If safe, place the queen and move to the next row/column. If no valid position exists, backtrack to the previous step and try a different position. Continue until all queens are placed or all possibilities are explored.
  • 26.
    STATE SPACE TREEFOR N-QUEEN’S PROBLEM
  • 27.
     Time Complexity:O(n!) For the first queen, we have n columns to choose from. Each subsequent queen has fewer valid positions because previous queens block their columns and diagonals, roughly reducing choices to n−2, n−4, and so on, giving an approximate O(n!) time.