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

11B Recursive Functions and Back Tracking

Uploaded by

Sophia Lindholm
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

11B Recursive Functions and Back Tracking

Uploaded by

Sophia Lindholm
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Recursive Functions and Back Tracking

Cosmin E. Oancea
[email protected]

Department of Computer Science (DIKU)


University of Copenhagen

December 2023 KOM-IT Lecture Slides


Today’s Lecture

We look at the Towers of Hanoi puzzle to demonstrate how


to (ideally) tackle a seemingly difficult problem:
I don’t plunge directly into writing code and hope that the
solution will materialize out of nowhere;
I instead reason about how the solution should look like,
perhaps in a divide and conquer manner;
I once you figure it out, implementation might be piece of cake;
I also reason about complexity so you do not kill your computer.

Then we examine the N-Queens problem as an example of


the backtracking technique:
I this is a brute force approach in which you examine the space
of all possible solutions;
I if you reach a dead-end, you backtrack one step and try again;
I until you succeed.
How does a Tower of Hanoi Looks Like?

Please notice the


beautiful pyramidal shape.
Poetic Intro to The Towers of Hanoi Puzzle
“Somewhere in a remote region of the world there is a monastery of a
very devout religious order. The monks have been charged with a
sacred task that keeps time for the universe.
At the beginning of all things, the monks were given a foundation
that supports three vertical posts. On the first post was a stack of 64
concentric, golden disks. The disks are of varying radii and stacked in
the shape of a beautiful pyramid.
The monks are charged with the task of moving the disks from the
first post to the third post. The disks are to be moved one at a time
and at every step, the beautiful pyramidal shape must be preserved
on all posts.
When the monks complete their task, all things will crumble to dust
and the universe will end.”

John Zell, Python Programming: An Introduction to Computer


Science (2004)
Great, a Puzzle, Let’s Play it like Humans
Great, a Puzzle, Let’s Play it like Humans

There is a mistake in
the left figure, where?
Reasoning out the Divide-and-Conquer Recursive Pattern

What is the base case?

How do we break this


problem into slightly
smaller sub-problems?

How should the


implementation look
like?
Reasoning out the Divide-and-Conquer Recursive Pattern

What is the base case?

How do we break this


problem into slightly
smaller sub-problems?

How should the


implementation look
like?
Reasoning out the Divide-and-Conquer Recursive Pattern
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?

264 ∼= 1.8447 × 1019 = 1.8447 × 10000000000000000000

Our universe is only about 13.77 billion years old, plus or minus
some 40 million years, i.e., about 14000000000 years

So, be nice to your computer, don’t kill it!


Backtracking Intuition
The resource for this lecture is this tutorial on recursion and
backtracking (click me).

Assume you are standing in front of three tunnels, one of which is


having a bag of gold at its end, but you don’t know which one.
How do you find the bag of gold?
Backtracking Intuition
The resource for this lecture is this tutorial on recursion and
backtracking (click me).

Assume you are standing in front of three tunnels, one of which is


having a bag of gold at its end, but you don’t know which one.
How do you find the bag of gold?

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.

So backtracking is a brute-force approach, in which we attempt


solving a sub-problem, and if this does not reach the desired
solution, i.e., reaches a dead-end, we undo whatever we did for
solving that sub-problem, and try solving another sub-problem.
N-Queens Problem
Given a chess board having N × N cells, we need to place N
queens in such a way that no queen is attacked by any other
queen. A queen can attack horizontally, vertically and diagonally.

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 )

i f NQueens ( board , i +1 ) i s true / / S o l v e s u b p r o b l e m


r e t u r n true / / I f s o l u t i o n i s fo u n d r e t u r n t r u e

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.

Printing the board can be achieved in a similar way to Task 2 of


week 9A, i.e., multiplication table and the table of prime numbers.

At the exercise session today, you may try to implement (as in the
slides) in console mode the Towers of Hanoi problem.

Solving N-Queen problem with visualization can possibly be your


choice for the fourth assignment. I can show you how it looks like
Summary of Today Lecture

Recursion: try to understand first the problem, and to reason


about how to break it in smaller problems of similar
shapes, and how to combine the results of the
smaller problems to obtain a solution for the bigger
problem. Do not rush to the implementation hoping
that a solution will materialize from thin air.
Once you gain the understanding, the solution
might prove to be “easy” to implement.

Backtracking: brute-force approach for searching a solution;


whenever we get stuck, we backtrack to a previous
other path to a potential solution, by undoing
whatever changes were made since that point, and
continue searching for a solution.
You try backtracking as a last resort, only when you
cannot apply any other more principled reasoning,
because it is typically computationally expensive.
Summary of the Past Four Weeks
We have studied at an intuitive level four important topics, each
one of them expanding to an entire field of study in CS:

map, filter, reduce: data-parallel programming;


array, dictionary, objects, trees: data structures and
algorithms;
complexity of algorithms;
recursion: functional programming.

So we have/will cover a long range of abstractions, the intent


being, to paraphrase Churchill, not to feed you, but rather to give
you a rod and teach you how to fish so you can feed yourself.
Another way to Summarize the Past Four Weeks

Many thanks to Ken Friis Larsen ([email protected]), whose slides from five years ago
have been a great source of inspiration, this picture included!

You might also like