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

DAA - Backtracking.pptx

Backtracking is an algorithmic technique that incrementally builds solutions to computational problems by exploring all possible combinations. It involves recursive decision-making, where the algorithm evaluates alternatives and backtracks when a sub-solution fails. Common applications include solving puzzles like Sudoku and the NQueens problem, where the goal is to find feasible or optimal solutions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

DAA - Backtracking.pptx

Backtracking is an algorithmic technique that incrementally builds solutions to computational problems by exploring all possible combinations. It involves recursive decision-making, where the algorithm evaluates alternatives and backtracks when a sub-solution fails. Common applications include solving puzzles like Sudoku and the NQueens problem, where the goal is to find feasible or optimal solutions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Design and Analysis of

Algorithms

-Backtracking
What is Backtracking?
Backtracking can be defined as a general algorithmic technique that considers searching
every possible combination in order to solve a computational problem.
Backtracking is finding the solution of a problem whereby the solution depends on the
previous steps taken.
For example,
A maze problem
Assembling Lego pieces
Sudoku
NQueens Problem
Subset Sum Problem
Knapsack Problem
Tower of Hanoi
What is Backtracking?
A backtracking algorithm tries to construct a solution to a computational problem
incrementally, one sub solution at a time.

Whenever the algorithm needs to decide between multiple alternatives to the next component
of the solution, it recursively evaluates every alternative and then chooses the best one.
What is Backtracking?
A backtracking algorithm tries to construct a solution to a computational problem
incrementally, one sub solution at a time.

Whenever the algorithm needs to decide between multiple alternatives to the next component
of the solution, it recursively evaluates every alternative and then chooses the best one.
Steps of Backtracking Algorithm:
The general steps of backtracking are:

● start with a sub-solution

● check if this sub-solution will lead to the solution or not

● If not, then come back and change the sub-solution and continue again
Why Backtracking?

1. Decision Problem – a feasible solution.


2. Optimization Problem – best solution.
3. Enumeration Problem – all feasible solutions.
Backtracking uses Recursion

● When a function calls itself, its called Recursion.

● The process in which a function calls itself directly or indirectly is called

recursion and the corresponding function is called as recursive function.


Recursion

function factorial (x)

if x is 0 // base case

return 1

return x * factorial (x-1) // break into smaller problem(s)

Base Case: Any recursive method must have a terminating condition. Terminating condition
is one for which the answer is already known and we just need to return that. For example for
the factorial problem, we know that factorial(0)=1, so when x is 0 we simply return 1,
otherwise we break into smaller problem i.e. find factorial of x−1
.
Recursion
Backtracking - Algorithmic Design Technique

Backtracking is a form of recursion.

● At each stage, there will be a number of options, choose one of these option.

● Then will get a new set of options based on the choice selected.

● This procedure is repeated over and over until a final state is reached.

● If a good sequence of choices are made, will reach a final state (a goal state);

if didn't, it isn't.
Backtracking
Example
Backtracking
Example

1. Starting at Root, options are A and B. choose A.


2. At A, options are C and D. choose C.
3. C is bad. Go back to A.
4. At A, we have already tried C, and it failed. Try D.
5. D is bad. Go back to A.
6. At A, there are no options left to try. Go back to
Root.
7. At Root, we have already tried A. Try B.
8. At B, options are E and F. Try E.
9. E is good. Congratulations!
Backtracking
Example
The tree is an abstract
model of the possible sequences
of choices that can be made.
STATE SPACE TREE
Backtracking Algorithm
boolean solve(Node n) else {
{ for each child c of n
if n is a leaf node {
{ if solve(c) succeeds, return true
if the leaf is a goal node, return true }
else return false return false
} }
}
NQueens Problem
Given - A chess board having N×N

Objective : To place N Queens in NxN chess board so that no queen is attacked by any other

queen. No Queen must be in the same row, column or diagonal

Constraint : 1<=N<=10
NQueens Problem
NQueens Problem
NQueens Problem
NQueens Problem
NQueens Problem
NQueens Problem
NQueens Problem
NQueens Problem
NQueens Problem
NQueens Problem
NQueens Problem
NQueens Problem
NQueens Problem
NQueens Problem
NQueens Problem
NQueens Problem
NQueens Problem
NQueens Problem - Pseudocode
Sample Input : 4

N-QUEEN(row, n, N, board) Sample Output :


if n==0
return TRUE 0 1 0 0

for j in 1 to N 0 0 0 1
if ! IS-ATTACK (row, j, board, N)
board[row][j] = 1 1 0 0 0

if N-QUEEN (row+1, n-1, N, board) 0 0 1 0


return TRUE

board[row][j] = 0 //backtracking, changing current


decision
NQueens Algorithm - Time Complexity

T(n) = O(N!)
N- No. of Queens to be placed

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy