Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introducing Recursion Date: June 7 th, 2002 Author: Steve Engels, University of Waterloo.

Similar presentations


Presentation on theme: "Introducing Recursion Date: June 7 th, 2002 Author: Steve Engels, University of Waterloo."— Presentation transcript:

1 Introducing Recursion Date: June 7 th, 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 10 19 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! = 1if n=0 (base case) –n! = n(n-1)!if n>0 (recursive case) Example #2: Fibonacci numbers –f 0 = f 1 = 1(base case) –f n = f n-1 + f n-2 if n≥2 (recursive case) Example #3: Binary trees

6 Recursive Programs Recall Fibonacci numbers. How would you calculate f n ? 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 static public int fib (int n) { if (n < 2) return 1; else return fib(n-1) + fib(n-2); }

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. public void Hanoi (int numDisks, int source, int dest, int helper) { if (numDisks == 1) moveDisk(numDisks, first, last); else { Hanoi(numDisks-1, source, helper, dest); moveDisk(1, source, dest); Hanoi(numDisks-1, helper, dest, source); }

9 Working the Crowd: The Need for Audience Participation in University Teaching Date: June 7 th, 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


Download ppt "Introducing Recursion Date: June 7 th, 2002 Author: Steve Engels, University of Waterloo."

Similar presentations


Ads by Google