lecture 10
lecture 10
4 4
4 4 4 4
2 3 3 3 3 3
1 1 1 1 1 1 1 1
2 4 4
From decimal to binary number
• To convert a decimal number to a binary number
• An initial approach
n = 29 – For a given number n, repeat the process of
n/2 remainder • Find the remainder by dividing the number by 2
14 1 • Output the remainder
7 0
• Update n to n/2 (using integer division)
3 1
1 1 • Example: for n = 29
0 1
– If the remainders are output in the order they were
computed: 10111
– The correct answer: 11101
– Push the remainders onto a stack and then output the
result by removing the entry on top of the stack
Java Program
import java.util.Stack;
public class ToBinary {
Final states
Backtracking
• Backtracking is a general problem strategy for
searching systematically for a solution to a
problem among all possible options.
• Stacks are often used in the implementation
of backtracking algorithms.
• Example:
• N-Queen problem
• Aim: Place N queens on an NxN
checkerboard so that no two queens can
attack each other.
N-Queen Problem
N=5 • Aim: Place N queens on an NxN
checkerboard so that no two queens
can attack each other.
• That is, no two queens can be on the
same:
– row
– column
– diagonal
N-Queen Problem
• Aim: Place N queens on an NxN
N=5 checkerboard so that no two queens
can attack each other.
row = 0
row = 1
row = 2
row = 3
row = 4
N-Queen Problem
N=5 • Aim: Place N queens on an NxN
checkerboard so that no two queens
can attack each other.
• That is, no two queens can be on the
same:
– row
– column
– diagonal
N-Queen Problem
1st Solution
3
1
4
2
0
Stack
N-Queen Problem
3
1
4
2
3
0
Stack
N-Queen Problem
2nd Solution
2
4
1
2
3
0
Stack
Backtracking Algorithm
• For each row on the checkerboard
– Try placing a queen in a column that doesn’t have any conflict
– If there is no conflict, add the move to a stack
else shift the queen on the next column
– Check if a solution is found, if yes, output the solution and
backtrack to find the next solution.
// backtracking can be accomplished by popping the stack
– If there is no more room to shift the queen,
backtrack to the previous row
– After backtracked to the previous row, shift the queen from
the previous column to the next column.
Java Program
import java.util.Stack;
public static void solve(int n) { //finds all solutions to the n-queen problem
int row = 0;
int col = 0;
2
0
Stack
N-Queen Problem
while ( row < n ) { row = 2
while ( col < n ) { 0
2
3
col = 4
1
if ( isConflict(row, col) == false ) { false
true
s.push(col); push(4)
break;
}
else col++;
}
… row++; col = 0;
}
4
2
0
Stack
Java Program
if (s.empty() == true) break; // either no solution or all solutions have been found
1
4
2
3
0
Stack
Java Program
if (s.empty() == true) break; // either no solution or all solutions have been found
3 1: [0, 2, 4, 1, 3]
1
4
2
0
Stack
Java Program
public static boolean isConflict(int row, int col) {
int diff = row-col;
int sum = row+col;
for (int i = 0; i < row; i++) {
int t = s.get(i);
if (t==col || i-t == diff || i+t == sum) return true;
}
return false;
}
N-Queen Problem
• How to determine if there is a conflict?
col = 2 N=5
row – col = 0
(0,1)
row – col = 1
(1,2) row = 1
(2,3)
row + col = 4
(0,3)
row + col = 5
row = 1 (1,2)
(2,1)
4
2
0
2
3
1
Stack
N-Queen Problem
4th Solution
3
0
2
2
4
1
Stack
N-Queen Problem
5th Solution
4
1
3
2
0
2
Stack
N-Queen Problem
6th Solution
0
3
1
2
4
2
Stack
N-Queen Problem
7th Solution
1
4
2
2
0
3
Stack
N-Queen Problem
8th Solution
0
2
4
2
1
3
Stack
N-Queen Problem
9th Solution
2
0
3
2
1
4
Stack
N-Queen Problem
10th Solution
1
3
0
2
4
Stack
Square Apple Problem
Rules:
The worm can only move
into another cell that
shares a common wall; and
a cell that has not been
previously visited.
2D Square Apple Problem