11B Recursive Functions and Back Tracking
11B Recursive Functions and Back Tracking
Cosmin E. Oancea
[email protected]
There is a mistake in
the left figure, where?
Reasoning out the Divide-and-Conquer Recursive Pattern
/ / Move n d i s k s fr o m t h e s o u r c e p o s t
/ / ( s r c ) to the d e s t i n a t i o n post ( dst ) ,
/ / u s i n g t h e a u x i l i a r y p o s t ( aux )
f u n c t i o n moveTower ( n , s r c , d s t , aux ) {
/ / base case
i f ( n === 1 )
r e t u r n ”<p>Move d i s k from ” + s r c
+ ” t o ” + d s t + ” </p>” ;
/ / d i v i d e and co n q u e r / r e c u r s i v e c a s e
l e t f s t =moveTower ( n −1 , s r c , aux , d s t ) ;
l e t snd =moveTower ( 1 , s r c , d s t , aux ) ;
l e t thd =moveTower ( n −1 , aux , d s t , s r c ) ;
r e t u r n f s t + snd + thd ;
}
/ / n i s t h e number o f d i s k s
f u n c t i o n t o we r s O f H a n o i ( n ) {
r e t u r n moveTower ( n , ” A ” , ” C ” , ”B ” ) ;
}
Reasoning out the Complexity
/ / Move n d i s k s fr o m t h e s o u r c e p o s t
/ / ( s r c ) to the d e s t i n a t i o n post ( dst ) ,
/ / u s i n g t h e a u x i l i a r y p o s t ( aux )
f u n c t i o n moveTower ( n , s r c , d s t , aux ) {
/ / base case
i f ( n === 1 )
r e t u r n ”<p>Move d i s k from ” + s r c
+ ” t o ” + d s t + ” </p>” ;
/ / d i v i d e and co n q u e r / r e c u r s i v e c a s e
l e t f s t = moveTower ( n −1 , s r c , aux , d s t ) ;
l e t snd = moveTower ( 1 , s r c , d s t , aux ) ;
l e t thd = moveTower ( n −1 , aux , d s t , s r c ) ;
r e t u r n f s t + snd + thd ;
}
/ / n i s t h e number o f d i s k s
f u n c t i o n t o we r s O f H a n o i ( n ) {
r e t u r n moveTower ( n , ” A ” , ” C ” , ”B ” ) ;
}
Reasoning out the Complexity
/ / Move n d i s k s fr o m t h e s o u r c e p o s t
/ / ( s r c ) to the d e s t i n a t i o n post ( dst ) ,
For every problem
/ / u s i n g t h e a u x i l i a r y p o s t ( aux )
f u n c t i o n moveTower ( n , s r c , d s t , aux ) { of some size n, we
/ / base case spawn two
i f ( n === 1 ) subproblems of
r e t u r n ”<p>Move d i s k from ” + s r c size n − 1;
+ ” t o ” + d s t + ” </p>” ;
/ / d i v i d e and co n q u e r / r e c u r s i v e c a s e this smells like
l e t f s t = moveTower ( n −1 , s r c , aux , d s t ) ; exponential
l e t snd = moveTower ( 1 , s r c , d s t , aux ) ; complexity:
l e t thd = moveTower ( n −1 , aux , d s t , s r c ) ; towersOfHanoi(n)
r e t u r n f s t + snd + thd ; takes order
} 2n operations;
/ / n i s t h e number o f d i s k s
f u n c t i o n t o we r s O f H a n o i ( n ) { tree demonstration
r e t u r n moveTower ( n , ” A ” , ” C ” , ”B ” ) ; on the blackboard;
}
The number of leaves in a balanced binary tree of height n is ???
Coming Back to the Poetic Intro of The Towers of Hanoi
“...On the first post was a stack of 64 concentric, golden disks.
... When the monks complete their task, all things will crumble to dust
and the universe will end.”
Why will all things crumble to dust and the universe will end?
Coming Back to the Poetic Intro of The Towers of Hanoi
“...On the first post was a stack of 64 concentric, golden disks.
... When the monks complete their task, all things will crumble to dust
and the universe will end.”
Why will all things crumble to dust and the universe will end?
Our universe is only about 13.77 billion years old, plus or minus
some 40 million years, i.e., about 14000000000 years
You go on the first tunnel, reach its end, and if it is not there
then backtrack and
go down the second tunnel, reach its end, and if it is not
there backtrack again and
go down the third tunnel.
Backtracking Intuition
In recursion we break down our problem, say A into sub-problems,
say B, C, D.
But it might be the case that the solution to A, does not depend
on all three sub-problems, in fact we do not know on which one it
depends.
Intuitive Solution:
Let’s place the first queen at some cell (i, j).
Now the number of unattacked cells is reduced, and the
number of queens to be placed is N − 1 (smaller subproblem).
Place the next queen at some unattacked cell.
Continue doing this as long as:
I The number of unattacked cells is not zero, AND
I The number of queens to be placed is not zero.
If the number of queens to be placed becomes zero:
Hurray, we have a solution!
If the number of unattacked cells becomes zero, then we
need to backtrack:
I remove the last-placed queen from its current cell, try to
place it in some other cell and so on.
N-Queens Problem Demonstration
We can do this systematically by
placing the first queen first at
(0,0), then if a solution is not
found for this subproblem at
(0,1), and so on until we find a
solution or exhaust the space of
all solutions (which is huge).
A possible solution is:
Pseudocode for a Solution of N-Queens Problem
We need a function, named isAttacked that takes as
arguments a position and a board configuration, and computes
whether it is safe (legal) to place a queen at that position:
function i s At t a cke d ( x , y , board ) {
/ / c h e c k i n g f o r row and column
i f any c e l l i n xt h row i s 1
r e t u r n true
i f any c e l l i n yt h column i s 1
r e t u r n true
/ / checking f o r diagonals
i f any c e l l ( p , q ) h a vi n g p+q = x+ y i s 1
r e t u r n true
i f any c e l l ( p , q ) h a vi n g p−q = x−y i s 1
r e t u r n true
r e t u r n false
}
Pseudocode for a Solution of N-Queens Problem
Initially the board is filled with 0, and i=0, i.e., no queens are
placed. N-Queens returns a boolean, i.e., success or failed.
If success, the board contains the solution; one may print it!
f u n c t i o n NQueens ( board , i ) {
l e t N = board . l e n g t h ;
i f ( i i s e q u a l t o N ) r e t u r n true ;
/ / ˆ H u r r a y , Long L i v e t h e Queens !
/ / ( A l l q u e e n s h a ve been p l a c e d ! )
f o r j = 0 t o N−1 {
i f i s A t t a c k e d ( i , j , board ) i s false {
board [ i ] [ j ] = 1 / / P l a c e c u r r e n t queen a t ( i , j )
board [ i ] [ j ] = 0 / / i f a s o l u t i o n was n o t fo u n d t h e n
} / / undo w h a t e ve r ch a n g e s were made ,
} / / i . e . , remove t h e c u r r e n t queen fr o m ( i , j )
r e t u r n false
}
Pseudocode for a Solution of N-Queens Problem
The N-Queens implementation on the previous slide terminates
after finding one solution, but many solutions exist.
At the exercise session today, you may try to implement (as in the
slides) in console mode the Towers of Hanoi problem.
Many thanks to Ken Friis Larsen ([email protected]), whose slides from five years ago
have been a great source of inspiration, this picture included!