BacktrackingAlgorithms_data structure.pptx

swatiganar1 0 views 27 slides Oct 10, 2025
Slide 1
Slide 1 of 27
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27

About This Presentation

Backtracking is a general algorithmic technique used to solve problems recursively by systematically trying out various possible solutions and then undoing them if they do not lead to a valid solution. It operates on the principle of exploring all potential combinations or paths in a problem space, ...


Slide Content

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.
Tags