Backtracking DAA
Backtracking DAA
Backtracking DAA
Algorithms
ALGORITHM DESIGN TECHNIQUES
Greedy
Shortest path, minimum spanning tree, …
Divide and Conquer
Divide the problem into smaller subproblems,
solve them, and combine into the overall solution
Often done recursively
Quick sort, merge sort are great examples
Dynamic Programming
Brute force through all possible solutions, storing
solutions to subproblems to avoid repeat computation
Backtracking
A clever form of exhaustive search
BACKTRACKING
Suppose you have to make a series of
decisions, among various choices, where
You don’t have enough information to know
what to choose
Each decision leads to a new set of choices
Some sequence of choices (possibly more than
one) may be a solution to your problem
Backtracking is a methodical way of trying
out various sequences of decisions, until
you find the correct one that “works”.
BACKTRACKING
Backtracking is used to solve problems in which
a sequence of objects is chosen from a specified
set so that the sequence satisfies some
criterion.
Backtracking is a modified depth-first search of
a tree.
It is the procedure whereby, after determining
that a node can lead to nothing but dead nodes,
we go back (“backtrack”) to the node’s parent
and proceed with the search on the next child.
BACKTRACK ALGORITHM
Portion A
then you "backtrack" to
the junction.
dead end
?
dead end
dead end
?
start ? ?
dead end
dead end
success!
BACKTRACKING TECHNIQUE
Problem:
-- How do we represent the search
space?
State space tree to represent
N-Queens Problem
Place (k, i)
{
For j ← 1 to k - 1
(i+1, j-1)(i+1, j+1)
do if (x [j] = i)
or (Abs (x [j]) - i)) = (Abs (j
- k))
then return false;
return true;
}
x [] is a global array whose final k - 1 values have been set.
Abs (r) returns the absolute value of r.
Place (k, i) returns a Boolean value that is true if the kth queen
can be placed in column i. It tests both whether i is distinct
from all previous costs x1, x2,....xk-1 and whether there is no
other queen on the same diagonal.
Code Snippet of N-Queens
Problem
int canPlace(int k, int i){
void nQueens(int k, int n){ int j;
int i; for (j = 1;j <= k - 1;j++){
for (i=1;i <= n;i++){ if (arr[j] == i ||
(abs(arr[j]
if (canPlace(k, i))
- i) == abs(j - k)))
{ return 0;
arr[k] = i; }
if (k == n) return 1;
}
display(n);
else int main(){
int n=4;
nQueens(k + 1, n); nQueens(1,n);
} return 0;
}
}
}
What is the maximum number of
queens that can be placed on an n x n
chessboard such that no two attack
one another?
The answer is n queens, which gives
eight queens for the usual 8x8 board
POSSIBLE SOLUTION SET
Q Q Q Q Q
Q Q Q Q Q
Q Q Q Q Q
Q Q Q Q Q
Q Q Q Q Q
Q Q Q Q Q
Q Q Q Q Q
Q Q Q Q Q
Q Q Q Q Q
Q Q Q Q Q
Q Q Q Q Q
Q Q Q Q Q
Q Q Q Q Q
Q Q Q Q Q
Q Q Q Q Q
31
SOLUTION: BACKTRACKING
place a queen in the top row, then note the column and
diagonals it occupies.
then place a queen in the next row down, taking care not to
place it in the same column or diagonal. Keep track of the
occupied columns and diagonals and move on to the next
row.
If no position is open in the next row, we back track to the
previous row and move the queen over to the next
available spot in its row and the process starts over again.
THE "8 QUEENS" PROBLEM
Consider the problem of trying to place 8 queens on
a chess board such that no queen can attack
another queen.
Q
Q
What are the "choices"?
Q
How do we "make" or Q
"un-make" a choice?
Q
Q
NAIVE ALGORITHM
for (each square on board):
1 2 3 4 5 6 7 8
Place a queen there.
1 Q ... ... ... ... ... ... ...
Try to place the rest
2 ... ... ... ... ... ... ... ...
of the queens.
Un-place the queen.3 ...
5
How large is the 6
solution space for
this algorithm? 7
64 * 63 * 62 * ... 8
BETTER ALGORITHM IDEA
Observation: In a working
1 2 3 4 5 6 7 8
solution, exactly 1 queen
must appear in each
1 Q ... ...
4 ...
Redefine a "choice"
5 Q
to be valid placement
6
of a queen in a
7
particular column.
8
BACKTRACKING – EIGHT QUEENS
PROBLEM
Find an arrangement of 8
queens on a single chess
board such that no two
queens are attacking one
another.
It is an empty 8 x 8
chess board. We
have to place the
queens in this board.
BACKTRACKING EXAMPLE—8 QUEENS
PROBLEM(CONT…)
Then we have
placed the second
queen on the board.
The darken place
should not have the
queens because
they are horizontal,
vertical, diagonal to
the placed queens.
BACKTRACKING EXAMPLE—8 QUEENS
PROBLEM(CONT…)
• Map Coloring
• Scheduling the tasks
• Preparing Time Table
• Assignment
• Conflict Resolution
• Sudoku
Chromatic Number-
• Chromatic Number is the minimum number of
colors required to properly color any graph.
OR
• Chromatic Number is the minimum number of
colors required to color any graph such that no
• two adjacent vertices of it are assigned the same
color.
Chromatic Number
State space tree
Explore(choices):
if there are no more choices to make:
stop.
else, for each available choice C:
Choose C.
Explore the remaining choices.
Un-choose C, if necessary. (backtrack!)