Introducing Recursion - PowerPoint PPT Presentation

About This Presentation
Title:

Introducing Recursion

Description:

Towers of Hanoi. Moving small towers is easy. 1 disc: 1 move. 2 discs: 3 moves. 3 discs: 7 moves ... Hanoi(numDisks-1, helper, dest, source); Working the Crowd: ... – PowerPoint PPT presentation

Number of Views:77
Avg rating:3.0/5.0
Slides: 15
Provided by: csTor
Category:

less

Transcript and Presenter's Notes

Title: Introducing Recursion


1
Introducing Recursion
  • Date June 7th, 2002
  • Author Steve Engels,
  • University of Waterloo

2
Introduction
  • Towers of Hanoi
  • Moving small towers is easy
  • 1 disc 1 move
  • 2 discs 3 moves
  • 3 discs 7 moves
  • 64 discs 1.8 x 1019
  • Ouch. Is there a nice solution for large cases?

3
Induction
  • First, look at induction
  • Inductive vs deductive proofs
  • Deductive proof based on a series of logical
    steps
  • Inductive proof of base case, then proof of
    general case (i.e. all cases bigger than base
    case)
  • Intuition behind induction
  • Show that the smallest case is true
  • Show that each big case is true if the case
    smaller than it is true
  • Therefore, if the smallest case is true, all
    cases bigger than it must also be true

4
Induction Example
  • Thm Given N people in a room, where each person
    shakes hands with every other person exactly
    once, show that the number of handshakes is
    n(n-1)/2
  • Proof
  • Induction involves a different kind of thinking
    breaking up large problems into smaller
    problems to solve them

5
Recursive Definitions
  • A recursive definition defines an object in terms
    of smaller objects of the same type.
  • Includes base (degenerate) cases and recursive
    cases
  • Example 1 Factorial function
  • n! 1 if n0 (base case)
  • n! n(n-1)! if ngt0 (recursive case)
  • Example 2 Fibonacci numbers
  • f0 f1 1 (base case)
  • fn fn-1 fn-2 if n2 (recursive case)
  • Example 3 Binary trees

6
Recursive Programs
  • Recall Fibonacci numbers. How would you
    calculate fn?
  • Solution is defined in terms of solutions to
    smaller problems of the same type
  • Example
  • Recursive and iterative programs are related in
    the same way as inductive and deductive reasoning
    ? different kind of thinking

7
Recursive Programs
  • How do you build a recursive method?
  • 5 steps
  • Step 0 Analyse problem. See how to break up
    large problem into smaller problems
  • Step 1 Base case
  • Step 2 Break large case into smaller case(s)
  • Step 3 Call method recursively on smaller
    case(s)
  • Step 4 Use result of recursive call(s) to
    produce result for large case
  • Step 5 Return result for large case

8
Hanoi Revisited
  • So how do we solve the Towers of Hanoi elegantly?
  • Note Some solutions can be implemented both
    recursively and iteratively.

9
Working the CrowdThe Need for Audience
Participation in University Teaching
  • Date June 7th, 2002
  • Author Steve Engels,
  • University of Waterloo

10
How to Keep Your Audience Awake
  • Important factors in students
  • Interest
  • Awareness
  • Consciousness
  • Techniques for stimulating student interest
  • Props
  • Demonstrations
  • Exercise
  • Other techniques

11
Using Props
  • Replace verbal descriptions with tangible
    examples
  • Useful when describing objects, structures, data,
    etc.
  • Examples
  • Arrays
  • Stacks
  • Queues
  • Linked lists
  • Circular
  • Sublist
  • Trees

12
Using Demonstrations
  • Replace descriptions of an action or process with
    a visual demonstration
  • Illustrates motion, action, operation of
    props/objects
  • Examples
  • Linked list operations
  • Activation stacks
  • Binary search
  • Tree traversals
  • Sorting

13
Using Exercises
  • Test understanding of material through practice
  • Ensure that material is understood, and reinforce
    applications of lesson
  • Examples
  • Reading parts of lecture material
  • Polling for solution to problem
  • Random questions
  • Group work

14
Other Techniques
  • Every little bit helps
  • Examples
  • Names
  • Feedback
  • Sugar
  • Projectiles
Write a Comment
User Comments (0)
About PowerShow.com