Title: LEARN TO PROGRAM WITH ALICE
1LEARN TO PROGRAM WITH ALICE
- Xiannong Meng
- Computer Science Department
- Bucknell University
2Lecture Eight
3What is Recursion
- Recursion is a form of repetition in which a
method (or a question) calls itself - A very powerful problem solving method
- Dont know how many times the repetition will
take place before hand - Recursion stops when the set conditions are met
- A recursive method has two parts
- Recursion call with smaller size of the problem
- Base condition(s) where the recursion stops
4Simple Examples of Recursion
- Summation S 1 2 3 n
- Multiplication (factorial) F 1 2 3 n
- While the above problems can do without
recursion, many other problems become very hard
without it, e.g. chess-playing
Sum(n) Sum(n-1) n Sum(0) 0
Fact(n) Fact(n-1) n Sum(0) 1
5Example of HorseRaceNon-recursive Version
Race If one of the horses has won the winner
says I won!!! Else randomly choose one
horse and move it forward a small amount do
everything again (repeating)
6Example of HorseRaceRecursive Version
Race If one of the horses has won the winner
says I won!!! Else randomly choose one
horse and move it forward a small amount
call the Race method
7Towers of Hanoi
- There 64 disks (could be any number) and three
towers. At the beginning all 64 disks are on one
of the towers. The goal is to move all the disks
to one of the other towers, using a third tower
as a spare, obeying these two rules - Only one disk may be moved at a time
- A larger disk may never be placed on top of a
smaller one
8Complexity
- If we actually solve the problem with 64 disks on
a computer, it may take centuries to run! - Illustrate the problem with 4 disks
- Two requirements
- Recursion with a smaller problem
- Base case(s)
9An Example of Four-Disks
- Move the top three disks from cone1 to cone2
- Move the last disk (the largest one) from cone1
to cone3 - Move the three disks from cone2 to cone3
10Towers of Hanoi
World.towers Parameters howMany, source, target,
spare If howMany is equal to 1 move it from
the source to target Else (1) call towers to
move howMany-1 disks from source to spare
using target as spare (2) move it (disk
howMany) from source to target (3) call
towers to move howMany-1 disks from spare to
target using source as spare
11Towers of Hanoi in Emacs
- By default it plays with three disks only
- It may also play a 32-disk or 64-disk demo, but
it starts with a portion of the disks in place
because the limited visual space on a screen - Try it out emacs M-x hanoi (for 3-disk) or M-x
hanoi- (for 32-disk) or M-x hanoi-unix-64 (for
64-disk) - Hit the space bar to stop it when youve had
enough
12Complexity (Big-O Notation)
- Take the Tower-Of-Hanoi as an example
- T(n) 2T(n-1) c
- T(n) 22T(n-2) c c 22T(n-2) 2c
- T(n) 2(n-1)T(1) (n-1)c
- Where n is the size of the problem, c is a
constant
13Complexity (Big-O Notation)continue
- We call this type of solution of exponential
complexity O(2n) - For a problem of size 64, a processor of 4 B
instructions/second, it would take
264/(4109360024365) 146 years