CS 162 Fall 2003 - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

CS 162 Fall 2003

Description:

We will see in a couple of weeks how hanoi is an exponential algorithm. ... Compare to Factorial and Hanoi. All use recursion, but use it in slightly different ways. ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 29
Provided by: webEngrOr
Category:
Tags: fall | hanoi

less

Transcript and Presenter's Notes

Title: CS 162 Fall 2003


1
CS 162 - Fall 2003
  • Chapter 17
  • Recursion (part 2)

2
Characteristics of Recursion
  • Remember the characteristics of a recursive
    solution
  • One or more base cases that solve a problem
    directly
  • Some way to "reduce" a more "complex" problem to
    a "simpler" one - one that eventually reaches the
    base case.

3
Factorial
  • Factorial problem
  • Base case is 1, since 1! 1
  • Recursive case, N! N (N-1)!
  • Here the recursive function makes one call on
    itself for the simpler version. There are
    problems where this is not the case

4
Towers of Hanoi
  • The Towers of Hanoi (Tower of Brahma or End of
    the World Puzzle). Invented by Edouard Lucas in
    1883. Legend tells of a Hindu temple where there
    is a pyramid of 64 gold disks, each different
    size. Goal is to move from one pole to another,
    using a third as assistance. One disk is moved at
    a time, no disk can be placed on a smaller one.
    When all disks are moved, the world ends.

5
Example of Solution
  • There is a link to a nice graphical version of
    the puzzle (with far fewer than 64 disks) on the
    links section of the course web page. Now would
    be a good time to go see this.
  • Somewhat difficult to solve iterative (you will
    have to trust me on this one)
  • Relatively easy to solve recursively.

6
Elements of Recursive Solution
  • Key to using recursion -- expressing the problem
    is a recursive fashion.
  • How to move a pile of N disks from pole A to B,
    using pole C as a temporary.

7
Recursive Algorithm
  • to Move(N, A, B, C)
  • Move a pile of N-1 from A to C, using B as
    temporary
  • Move a single disk from A to B
  • Move a pile of N-1 disks from C to B, using A as
    temporary
  • Question Does this satisfy our problem?

8
Base Case
  • What should be our base case? Two suggestions
  • if N 1, don't need recursion, can move just a
    single disk
  • Even better, if N 0, don't need to do anything
    at all! (As I've always said, nothing is easy).

9
Not a simple 1-1 recursive case
  • Notice that to perform the solution for case N,
    we make TWO recursive calls on the function.
  • While this can lead to a simple solution, also
    leads to exponential execution time. More on that
    in a couple of weeks.
  • Fortunate - remember the end of world?

10
Does it solve the problem?
  • Seems pretty clear that the recursive technique
    we have described will do the move OK, does it
    satisfy the no disk on top of another condition?
  • Key to proving recursion. Prove base case. Assume
    recursive calls work as advertised, and show that
    everything else is correct.

11
Proof. Base Case
  • Seems pretty clear that if we have not violated
    the "no disk on top of a smaller one" requirement
    up to some point, then moving (or not moving) a
    pile of size zero cannot cause us to break the
    rule.
  • Base case arguments are often pretty trivial.

12
Proof Induction Case
  • We assume the recursive calls will work as
    advertised. Need only consider the rest.
  • In this case, just the single disk move.
  • Must prove that no disk smaller than N is on pole
    B -- but this is easy, since all disks smaller
    than N are now on pole C.

13
The recursive solution
  • void hanoi (int N, int A, int B, int C)
  • if (N gt 0) // only induction case - base is
    nothing
  • hanoi(N-1, A, C, B)
  • system.out.println("Move " N " from "
    A " to " B)
  • hanoi(N-1, C, B, A)

14
Oh - that End of World thing?
  • Not to worry. We will see in a couple of weeks
    how hanoi is an exponential algorithm.
  • To move 64 disks would take 18,446,744,073,709,551
    ,615 moves. If they moved one per second, would
    be slightly more than 580 billion years.

15
Another Tricky Recursive Task
  • Another problem that is easier to do recursively
    than iteratively.
  • Take a word, and generate all permutations.
    Example from "eat" we have "eat", "eta", "aet",
    "ate", "tea", and "tae".
  • Part of problem - what does "generate" mean?

16
Generation or Enumeration
  • Common way of describing a sequence in Java and
    similar languages. Called the Enumeration
    pattern. Have a pair of functions
  • isThereMore - return true if more values
  • nextValue - return next value
  • Same idea, different names, used a lot.

17
A Enumeration Loop
  • The enumeration pattern makes it easy to write a
    loop that does something with all values
  • while (isThereMore())
  • do something with nextValue()

18
Class PermutationGenerator
  • class PermutationGenerator
  • public PermutationGenerator(String s)
  • public boolean hasMorePermutations()
  • public String nextPermutation()
  • uses the Enumeration Pattern different names

19
Solving the Problem Recursively
  • How do we solve this problem recursively?
  • One way. Consider each letter in turn "e", "a",
    "t".
  • Form word without that letter - get all
    permutations of that ("at", "ta"), and append e
    to each ("eat", "eta").
  • Do same for all letters.

20
Base Case
  • Obvious base cases - word of length 1 (only one
    instance) or word of length 0 (nothing)
  • Wait and see which one is easier to work with.

21
Tricky
  • Conceptually easy. Programming is tricky, because
    when we recurse we get a series of values. Need
    to enumerate over all values - then get next.
  • Need to remember current state - in this case,
    three values, current word, character position,
    and the enumerator for the recursive call.

22
Data Areas
  • class PermutationGenerator
  • ...
  • private String word
  • private int current
  • private PermutationGenerator tailGenerator

23
Initialization
  • class PermutationGenerator
  • public PermutationGenerator(String aWord)
  • word aWord
  • current 0
  • if (word.length() gt 1)
  • tailGenerator new
    PermutationGenerator(word.substring(1))

24
Handling Base Case
  • public String nextPermutation()
  • if (word.length() 1)
  • current
  • return word
  • ...

25
Handling Recursive Case
  • public String nextPermutation()
  • ...
  • String r word.charAt(current)
    tailGenerator.nextPermutation()
  • if (! tailGenerator.hasMorePermutations())
  • current
  • if (current lt word.length())
  • string tailString word.substr(0,
    current) word.substring(current1)
  • tailGenerator new permutationGenerator(
    tailString)

26
Compare this to others
  • Compare to Factorial and Hanoi
  • All use recursion, but use it in slightly
    different ways.
  • Key idea, base case, find a way to reduce a
    problem to another of same form, assume the
    recursive call works, and carry on.

27
Questions?
28
Ending Test
  • The ending test
  • public boolean hasMorePermutations()
  • return current lt word.length()
Write a Comment
User Comments (0)
About PowerShow.com