LEARN TO PROGRAM WITH ALICE - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

LEARN TO PROGRAM WITH ALICE

Description:

Only one disk may be moved at a time. A larger disk may never be placed on top of a smaller one ... howMany, source, target, spare. If howMany is equal ... – PowerPoint PPT presentation

Number of Views:53
Avg rating:3.0/5.0
Slides: 14
Provided by: xianno
Category:
Tags: alice | learn | program | with

less

Transcript and Presenter's Notes

Title: LEARN TO PROGRAM WITH ALICE


1
LEARN TO PROGRAM WITH ALICE
  • Xiannong Meng
  • Computer Science Department
  • Bucknell University

2
Lecture Eight
  • Recursion

3
What 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

4
Simple 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
5
Example 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)
6
Example 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
7
Towers 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

8
Complexity
  • 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)

9
An 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

10
Towers 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
11
Towers 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

12
Complexity (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

13
Complexity (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
Write a Comment
User Comments (0)
About PowerShow.com