M.C. Escher: "Drawing Hands" (1948) - PowerPoint PPT Presentation

About This Presentation
Title:

M.C. Escher: "Drawing Hands" (1948)

Description:

QDQQDQDQ 8 7. QDQQDQDQQDQQD 13 It's the Fibonacci sequence! Fibonacci In the Fibonacci sequence, each term = sum of previous 2 terms Let fib(n) be the nth term ... – PowerPoint PPT presentation

Number of Views:254
Avg rating:3.0/5.0
Slides: 67
Provided by: Dave3173
Learn more at: http://www.cs.cmu.edu
Category:

less

Transcript and Presenter's Notes

Title: M.C. Escher: "Drawing Hands" (1948)


1
M.C. Escher"Drawing Hands" (1948)
2
Recursion
  • Dave Feinberg

3
Computation
  • All computation consists of chugging along from
    state to state to state ...
  • There is a set of rules that tell us, given the
    current state, which state to go to next.

4
Arithmetic as Rewrite Rules
  • 2 3 4
  • 5 4
  • 9
  • We stop when we reach a number.

5
Functions as New Rules
  • def square(n)
  • return n n
  • When we see square(something)
  • Rewrite it as something something

6
Functions as Rewrite Rules
  • def square(n)
  • return n n
  • square(3)
  • 3 3
  • 9

7
Piecewise Functions
  • f(n) 1 if n 1
  • n - 1 if n gt 1
  • f(4)
  • 4 - 1
  • 3

8
In Python
  • def f(n)
  • if n 1
  • return 1
  • else
  • return n - 1

9
This is just math, right?
  • Difference between mathematical functions and
    computation functions. Computation functions must
    be effective.
  • For example, we can define the square-root
    function asvx the y such that y 0 and y²
    x
  • This defines a valid mathematical function, but
    it doesn't tell us how to compute the square root
    of a given number.

10
Fancier Functions
  • def f(n)
  • return n (n - 1)
  • Find f(4)

11
Fancier Functions
  • def f(n)
  • return n (n - 1)
  • def g(n)
  • return n f(n - 1)
  • Find g(4)

12
Fancier Functions
  • def f(n)
  • return n (n - 1)
  • def g(n)
  • return n f(n - 1)
  • def h(n)
  • return n h(n - 1)
  • Find h(4)

13
Recursion
  • def h(n)
  • return n h(n - 1)
  • h is a recursive function,because it is defined
    in terms of itself.

14
Definition
  • Recursion
  • See "Recursion".

15
Recursion
  • def h(n)
  • return n h(n - 1)
  • h(4)
  • 4 h(3)
  • 4 3 h(2)
  • 4 3 2 h(1)
  • 4 3 2 1 h(0)
  • 4 3 2 1 0 h(-1)
  • 4 3 2 1 0 -1 h(-2)
  • ...
  • Evaluating h leads to an infinite loop!

16
What you are thinking
  • "Ok, recursion is bad.What's the big deal?"

17
Recursion
  • def f(n)
  • if n 1
  • return 1
  • else
  • return f(n - 1)
  • Find f(1)
  • Find f(2)
  • Find f(3)
  • Find f(100)

18
Recursion
  • def f(n)
  • if n 1
  • return 1
  • else
  • return f(n - 1)
  • f(3)
  • f(3 - 1)
  • f(2)
  • f(2 - 1)
  • f(1)
  • 1

19
Terminology
  • def f(n)
  • if n 1
  • return 1
  • else
  • return f(n - 1)
  • "Useful" recursive functions have
  • at least one recursive case
  • at least one base caseso that the computation
    terminates

base case
recursive case
20
Recursion
  • def f(n)
  • if n 1
  • return 1
  • else
  • return f(n 1)
  • Find f(5)
  • We have a base case and a recursive case. What's
    wrong?

21
Recursion
  • The recursive case
  • should call the function
  • on a simpler input,
  • bringing us closer and closer
  • to the base case.

22
Recursion
  • def f(n)
  • if n 0
  • return 0
  • else
  • return 1 f(n - 1)
  • Find f(0)
  • Find f(1)
  • Find f(2)
  • Find f(100)

23
Recursion
  • def f(n)
  • if n 0
  • return 0
  • else
  • return 1 f(n - 1)
  • f(3)
  • 1 f(2)
  • 1 1 f(1)
  • 1 1 1 f(0)
  • 1 1 1 0
  • 3

24
Factorial
  • 4! 4 3 2 1 24

25
Factorial
  • Does anyone know the value of 9!
  • 362,880
  • Does anyone know the value of 10!
  • How did you know?

26
Factorial
  • 9! 987654321
  • 10! 10 987654321
  • 10! 10 9!
  • n! n (n - 1)!
  • That's a recursive definition!

27
Factorial
  • def fact(n)
  • return n fact(n - 1)
  • fact(3)
  • 3 fact(2)
  • 3 2 fact(1)
  • 3 2 1 fact(0)
  • 3 2 1 0 fact(-1)
  • ...

28
Factorial
  • What did we do wrong?
  • What is the base case for factorial?

29
Factorial
  • n! of ways to seat n people in a room with n
    chairs.
  • How many ways are there to seat 3 people in a
    room with 3 chairs?

30
Factorial
  • There are 3! 3 2 1 6 waysto seat 3
    people in a room with 3 chairs.

A
B
C
A
C
B
B
A
C
B
C
A
C
A
B
C
B
A
31
Factorial
  • There are 2! 2 1 2 waysto seat 2 people in
    a room with 2 chairs.

A
B
B
A
32
Factorial
  • There is only 1! 1 wayto seat 1 person in a
    room with 1 chair.
  • How many ways are there to seat 0 people in a
    room with 0 chairs?

A
33
Factorial
  • There is 1 way to seat 0 people in a room with 0
    chairs.
  • This is why 0! is defined to be 1.
  • That's our base case!

34
Factorial
  • def fact(n)
  • if n 0
  • return 1
  • else
  • return n fact(n - 1)
  • fact(3)
  • 3 fact(2)
  • 3 2 fact(1)
  • 3 2 1 fact(0)
  • 3 2 1 1
  • 6

35
Writing Recursive Functions
  • Write if. Why?
  • Base case Test for and handle simplest case.
  • Recursive case Call function on simpler input.
  • Assume the recursive call works. How does it
    help us?

36
Honey Bees
  • Drone son of queen
  • Queen daughter of queen drone
  • (Worker daughter of queen drone)

37
Honey Bees
  • 1. D individual
  • 2. Q parent
  • 3. QD grandparents
  • 4. QDQ great grandparents
  • Rewrite rules D ? Q
  • Q ? QD

38
Honey Bees
  • 1. D individual
  • 2. Q parent
  • 3. QD grandparents
  • 4. QDQ great grandparents
  • 5. QDQQD
  • 6. QDQQDQDQ
  • 7. QDQQDQDQQDQQD
  • How many in each generation?

39
Honey Bees
  • 1. D 1
  • 2. Q 1
  • 3. QD 2
  • 4. QDQ 3
  • 5. QDQQD 5
  • 6. QDQQDQDQ 8
  • 7. QDQQDQDQQDQQD 13
  • It's the Fibonacci sequence!

40
Fibonacci
  • In the Fibonacci sequence,each term sum of
    previous 2 terms
  • Let fib(n) be the nth term.
  • Then, fib(n) fib(n - 1) fib(n - 2)
  • The sequence is defined recursively!

41
Fibonacci
  • def fib(n)
  • return fib(n - 1) fib(n - 2)
  • Find fib(1)
  • We need a base case!

42
Fibonacci
  • def fib(n)
  • if n 1
  • return 1
  • else
  • return fib(n - 1) fib(n - 2)
  • Find fib(1)
  • Find fib(2)
  • How do we fix our function?

43
Fibonacci
  • def fib(n)
  • if n lt 2
  • return 1
  • else
  • return fib(n - 1) fib(n - 2)
  • Find fib(1)
  • Find fib(2)
  • Find fib(3)

44
Tiling Squares
  • Rewrite rule Add square to long side.

45
Tiling Squares
What is the side length of each square?
46
Tiling Squares
5
8
21
1
1
3
2
13
47
Spiral
48
Fibonacci
  • 1 1 1
  • 2 1 2
  • 3 2 1.5
  • 5 3 1.666...
  • 8 5 1.6
  • 13 8 1.625
  • 21 13 1.615...
  • 34 21 1.619...

49
Limit
fib(n) fib(n - 1)
  • What is the limit ofas n approaches infinity?
  • 1.6180339887498948482...
  • What's that called?

50
The Golden Ratio
  • The proportions of a rectangle that,when a
    square is added to itresults in a rectanglewith
    the same proportions.

Square
Square


51
The Golden Ratio
f2 - f - 1 0
f 1.618...
52
Fibonacci
  • fib(n) 1 n 1, 2
  • fib(n-1) fib(n-2) n gt 2
  • fib(n)

fn - (1 - f)n v5
53
Top-Down Computation
fib(5)
fib(4)
fib(3)
fib(3)
fib(2)
fib(2)
fib(1)
fib(2)
fib(1)
54
Overlapping Subproblems
fib(5)
fib(4)
fib(3)
fib(3)
fib(2)
fib(2)
fib(1)
fib(2)
fib(1)
fib(5)
1 time
fib(4)
1 time
fib(3)
2 times
fib(2)
3 times
55
Bottom-Up Computation
  • def fib(n)
  • if n lt 2
  • return 1
  • else
  • return fib(n - 1) fib(n - 2)
  • To find fib(5), first find fib(1) ...

56
Bottom-Up Computation
  • def fib(n)
  • if n lt 2
  • return 1
  • else
  • return fib(n - 1) fib(n - 2)

n
fib(n)
1
1
2
1
3
2
4
3
5
5
57
Worksheet!
  • Yay.

58
Why Recursion?
  • Why not just use for/while loops, instead of
    recursion?
  • 1. It's good to know that you don't need
    for/while to program loops.
  • 2. Many problems are easier to solve recursively
    than with for/while loops.

59
Merge Sort
  • If only 1 element
  • Do nothing.
  • Otherwise
  • Split the list in half.
  • Merge sort each half.
  • Merge the sorted halves.

60
Forestry
  • (Demo)

61
Forestry
  • Problem Identify contiguous green squares on
    the grid, and change their color.
  • Story We're determining which trees get burned
    down if a given tree catches fire.

62
Related Problems
  • using the "paint" tool to fill a region
  • clearing out the 0's in Minesweeper

63
Forest Fire
  • Let's Code!

64
Solving Recursively
  • Key Idea Change color of current location.
    Then recursively burn the 4 neighboring trees.
  • Base Case location is outside grid, or not green

65
The Code
  • def burn(grid, row, col)
  • if isvalid(grid, row, col)
  • if getcolor(grid, row, col) "green"
  • setcolor(grid, row, col, "red")
  • burn(grid, row - 1, col)
  • burn(grid, row, col - 1)
  • burn(grid, row, col 1)
  • burn(grid, row 1, col)

66
Big Ideas
  • Comp Sci focuses on how to compute.
  • Computation involves chugging along from state to
    state.
  • Rewrite rules tell us what state to go to next.
  • Recursive functions are defined in terms of
    themselves.
  • Useful recursive functions have a base case(s)
    and a recursive case(s).
Write a Comment
User Comments (0)
About PowerShow.com