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

Slides:



Advertisements
Similar presentations
Recursion.
Advertisements

Towers of Hanoi Move n (4) disks from pole A to pole C such that a disk is never put on a smaller disk A BC ABC.
Recursion Chapter 14. Overview Base case and general case of recursion. A recursion is a method that calls itself. That simplifies the problem. The simpler.
COSC 2006 Data Structures I Recursion III
Recursion Ellen Walker CPSC 201 Data Structures Hiram College.
ITEC200 – Week07 Recursion. 2 Learning Objectives – Week07 Recursion (Ch 07) Students can: Design recursive algorithms to solve.
Circular Arrays Neat trick: use a circular array to insert and remove items from a queue in constant time The idea of a circular array is that the end.
Lesson 19 Recursion CS1 -- John Cole1. Recursion 1. (n) The act of cursing again. 2. see recursion 3. The concept of functions which can call themselves.
Recursion COMP171 Fall Recursion / Slide 2 Recursion * In some problems, it may be natural to define the problem in terms of the problem itself.
Recursion Chapter 7. Spring 2010CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn how.
1 CSCD 300 Data Structures Recursion. 2 Proof by Induction Introduction only - topic will be covered in detail in CS 320 Prove: N   i = N ( N + 1.
Fall 2007CS 2251 Recursion Chapter 7. Fall 2007CS 2252 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method.
Recursion Road Map Introduction to Recursion Recursion Example #1: World’s Simplest Recursion Program Visualizing Recursion –Using Stacks Recursion Example.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Topic 7 – Recursion (A Very Quick Look). CISC 105 – Topic 7 What is Recursion? A recursive function is a function that calls itself. Recursive functions.
CS102 – Recursion David Davenport Bilkent University Ankara – Turkey Spring 2003.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java: Early Objects Third Edition by Tony Gaddis Chapter.
Recursion Chapter 7. Chapter 7: Recursion2 Chapter Objectives To understand how to think recursively To learn how to trace a recursive method To learn.
1 Algorithm Design Techniques Greedy algorithms Divide and conquer Dynamic programming Randomized algorithms Backtracking.
Algorithm Cost Algorithm Complexity. Algorithm Cost.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 19: Recursion.
Recursion Chapter 7. Chapter Objectives  To understand how to think recursively  To learn how to trace a recursive method  To learn how to write recursive.
CS-2852 Data Structures LECTURE 12B Andrew J. Wozniewicz Image copyright © 2010 andyjphoto.com.
Recursion l Powerful Tool l Useful in simplifying a problem (hides details of a problem) l The ability of a function to call itself l A recursive call.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 15: Recursion Starting Out with Java: From Control Structures.
Copyright © 2011 Pearson Education, Inc. Starting Out with Java: Early Objects Fourth Edition by Tony Gaddis Chapter 14: Recursion.
15-1 Chapter-18: Recursive Methods –Introduction to Recursion –Solving Problems with Recursion –Examples of Recursive Methods.
Chapter 9: Recursion1 CHAPTER 9 RECURSION. Recursion  Concept of recursion  A recursive: Benefit and Cost  Comparison : Iterative and recursive functions.
Data Structures and Algorithms Stacks. Stacks are a special form of collection with LIFO semantics Two methods int push( Stack s, void *item ); - add.
Sudeshna Sarkar, IIT Kharagpur 1 Functions : Recursion Lecture
Data Structures Recursion Phil Tayco Slide version 1.0 Mar. 8, 2015.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Chapter 14 Recursion. Chapter Objectives Learn about recursive definitions Explore the base case and the general case of a recursive definition Learn.
CSC 221: Recursion. Recursion: Definition Function that solves a problem by relying on itself to compute the correct solution for a smaller version of.
Recursion. Math Review Given the following sequence: a 1 = 1 a n = 2*a n-1 OR a n+1 = 2*a n What are the values of the following? a 2 = a 3 = a 4 =
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
Basic Mathematics Chapter 1 (1.2 and 1.3) Weiss. Recursion / Slide 2 Logarithms * Definition: if and only if * Theorem 1.1: n Proof: apply the definition.
ISOM MIS 215 Module 4 – Recursion. ISOM Where are we? 2 Intro to Java, Course Java lang. basics Arrays Introduction NewbieProgrammersDevelopersProfessionalsDesigners.
Sudeshna Sarkar, IIT Kharagpur 1 Functions : Recursion Lecture
1 Recursion n what is it? n how to build recursive algorithms n recursion analysis n tracing simple recursive functions n hands on attempts at writing.
Lecture 2 Basic Data Structures and Recursion Review.
1 Chapter 8 Recursion. 2 Objectives  To know what is a recursive function and the benefits of using recursive functions (§8.1).  To determine the base.
Programming With Java ICS201 University Of Ha’il1 Chapter 11 Recursion.
CS2852 Week 6, Class 2 Today Class exercise: Implementing a recursive method Binary Search Trees Tomorrow: Quiz at start of lab Implementing a recursive.
Recursion. What is Recursion? Method of solving problems Alternative to iteration All recursive solutions can be implemented iteratively Characteristic...
1 Recursion Recursive function: a function that calls itself (directly or indirectly). Recursion is often a good alternative to iteration (loops). Its.
Chapter 6 (Lafore’s Book) Recursion Hwajung Lee.  Definition: An algorithmic technique. To solve a problem on an instance of size n, the instance is:
1 Data Structures CSCI 132, Spring 2016 Notes 16 Tail Recursion.
Chapter Topics Chapter 16 discusses the following main topics:
Chapter 19: Recursion.
Recursion Salim Arfaoui.
Recursion what is it? how to build recursive algorithms
Recursion A problem solving technique where an algorithm is defined in terms of itself A recursive method is a method that calls itself A recursive algorithm.
Computer Science 4 Mr. Gerb
Fibonacci Fibonacci series 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 Definition:
Recursion &Faster Sorting
Introduction to Computer Science - Alice
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Ch 12 Recursion Mr. Dave Clausen La Cañada High School
Chapter 12 Recursion (methods calling themselves)
Data Structures and Algorithms
Chapter 12 Supplement: Recursion with Java 1.5
Recursion Chapter 18.
Chapter 17 Recursion.
Chapter 18 Recursion.
Lecture 11 Algorithm Design
Chapter 19: Recursion.
ITEC324 Principle of CS III
Ch 12 Recursion Mr. Dave Clausen La Cañada High School
Presentation transcript:

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

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 Ouch. Is there a nice solution for large cases?

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

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

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

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); }

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

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); }

Working the Crowd: The Need for Audience Participation in University Teaching Date: June 7 th, 2002 Author: Steve Engels, University of Waterloo

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

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

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

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

Other Techniques Every little bit helps Examples: –Names –Feedback –Sugar –Projectiles