Self-Reference - Recursion Cmput 115 - Lecture 6 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this lecture is.

Slides:



Advertisements
Similar presentations
MATH 224 – Discrete Mathematics
Advertisements

Fundamentals of Computer Science Lecture 14: Recursion Instructor: Evan Korth New York University.
Cmput Lecture 8 Department of Computing Science University of Alberta ©Duane Szafron 2000 Revised 1/26/00 The Java Memory Model.
Binary Search Trees Cmput Lecture 23 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Sorting - Selection Sort Cmput Lecture 10 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is.
Doubly-Linked Lists Cmput Lecture 16 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Ordered Containers Cmput Lecture 21 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Computer Science II Recursion Professor: Evan Korth New York University.
Container Traversal Cmput Lecture 20 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Self-Reference - Induction Cmput Lecture 7 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this lecture is.
Data Abstraction - Interfaces and Implementations Cmput Lecture 1 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some.
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.
Stacks Cmput Lecture 18 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code from.
More on Recursive Recursion vs. Iteration Why Recursion?
Circularly-Linked Lists Cmput Lecture 17 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Sorting - Merge Sort Cmput Lecture 12 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
Recursion CS-240/CS341. What is recursion? a function calls itself –direct recursion a function calls its invoker –indirect recursion f f1 f2.
Object (Data and Algorithm) Analysis Cmput Lecture 5 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this.
Recursion Road Map Introduction to Recursion Recursion Example #1: World’s Simplest Recursion Program Visualizing Recursion –Using Stacks Recursion Example.
Chapter 10 Recursion. Copyright © 2005 Pearson Addison-Wesley. All rights reserved Chapter Objectives Explain the underlying concepts of recursion.
1 Chapter 18 Recursion Dale/Weems/Headington. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions.
Recursive Algorithms Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Revision Using Recursion1 Recursion. Revision Using Recursion2 Recall the Recursion Pattern Recursion: when a method calls itself Classic example--the.
Sorting - Insertion Sort Cmput Lecture 11 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is.
Quick Sort Cmput Lecture 13 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code.
Vectors Cmput Lecture 4 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this lecture is based on code from.
Code Clarity - Comments, Preconditions and Postconditions Cmput Lecture 2 Department of Computing Science University of Alberta ©Duane Szafron 1999.
Cmput Lecture 15 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code from the book:
Queues Cmput Lecture 19 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based on code from.
The List Interface Cmput Lecture 14 Department of Computing Science University of Alberta ©Duane Szafron 2000 Some code in this lecture is based.
CHAPTER 10 Recursion. 2 Recursive Thinking Recursion is a programming technique in which a method can call itself to solve a problem A recursive definition.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
1 Chapter 18-1 Recursion Dale/Weems. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing.
A Review of Recursion Dr. Jicheng Fu Department of Computer Science University of Central Oklahoma.
Recursion Chapter 7 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
1 Recursion Dr. Bernard Chen Ph.D. University of Central Arkansas.
Lecture 8. How to Form Recursive relations 1. Recap Asymptotic analysis helps to highlight the order of growth of functions to compare algorithms Common.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12: Recursion Problem Solving, Abstraction, and Design using C++
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
1 Lecture 14 Chapter 18 - Recursion. 2 Chapter 18 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing.
1 Chapter 13 Recursion. 2 Chapter 13 Topics l Meaning of Recursion l Base Case and General Case in Recursive Function Definitions l Writing Recursive.
Recursion Concepts Implementation Data Structures and Algorithms in Java, Third EditionCh05 – 1.
Recursion Textbook chapter Recursive Function Call a recursive call is a function call in which the called function is the same as the one making.
Computer Science Department Data Structure & Algorithms Lecture 8 Recursion.
CS212: DATASTRUCTURES Lecture 3: Recursion 1. Lecture Contents 2  The Concept of Recursion  Why recursion?  Factorial – A case study  Content of a.
Chapter 8 Recursion Modified.
Chapter 4 Recursion. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Explain the underlying concepts of recursion.
Ordered Containers CMPUT Lecture 19 Department of Computing Science University of Alberta ©Duane Szafron 2003 Some code in this lecture is based.
Recursion. What is recursion? Rules of recursion Mathematical induction The Fibonacci sequence Summary Outline.
Dale Roberts CSCI N305 Functions Recursion Department of Computer and Information Science, School of Science, IUPUI.
Java Programming: Guided Learning with Early Objects Chapter 11 Recursion.
1 CompSci 105 SS 2005 Principles of Computer Science Lecture 6: Recursion Lecturer: Santokh Singh Assignment 1 due tomorrow. Should have started working.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 8: Recursion Presentation slides for Java Software Solutions for AP* Computer Science 3rd.
1 Recursion Recursion is a powerful programming technique that provides elegant solutions to certain problems. Chapter 11 focuses on explaining the underlying.
1 Recursion. 2 Chapter 15 Topics  Meaning of Recursion  Base Case and General Case in Recursive Function Definitions  Writing Recursive Functions with.
Recursion Chapter 11. How it works “A recursive computation solves a problem by using the solution of the same problem with simpler inputs” Big Java,
Recursion A function is said to be recursive if it calls itself, either directly or indirectly. void repeat( int n ) { cout
Recursion. Recursive Methods  A recursive method is a method that calls itself.  General Form of Simple Recursive Methods  Every recursive method has.
C++ Programming Lecture 12 Functions – Part IV
1 Chapter 8 Recursion. 2 Recursive Function Call a recursion function is a function that either directly or indirectly makes a call to itself. but we.
Chapter 15 Running Time Analysis. Topics Orders of Magnitude and Big-Oh Notation Running Time Analysis of Algorithms –Counting Statements –Evaluating.
Nyhoff, ADTs, Data Structures and Problem Solving with C++, Second Edition, © 2005 Pearson Education, Inc. All rights reserved Recursion,
More on Recursive Recursion vs. Iteration Why Recursion?
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Java Software Structures: John Lewis & Joseph Chase
Loops in C C has three loop statements: the while, the for, and the do…while. The first two are pretest loops, and the the third is a post-test loop. We.
Announcements Final Exam on August 17th Wednesday at 16:00.
CS201: Data Structures and Discrete Mathematics I
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
Presentation transcript:

Self-Reference - Recursion Cmput Lecture 6 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this lecture is based on code from the book: Java Structures by Duane A. Bailey or the companion structure package Revised 1/2/00

©Duane Szafron About This Lecture In this lecture we will learn about self- reference. This powerful idea allows us to: –Write self-referencing algorithms. –Construct self-referencing objects. –Prove properties about algorithms using mathematical induction. In this lecture we will focus on self- referencing Algorithms

©Duane Szafron Outline Self-reference Recursive methods Computing the sum … n recursively Inserting an element in a vector recursively Stack frames A trace of recursion

©Duane Szafron Self-Reference Self-reference occurs when object refers to itself or more generally to another object from the same class. Self-reference also occurs when a method (or algorithm) calls itself. Such a method is called a recursive method. Self-reference also occurs when the proof of a theorem relies on the application of the same theorem to a simpler case. This situation is called mathematical induction.

©Duane Szafron Recursive Methods Recursion occurs when a method calls itself, either directly or indirectly. For recursion to terminate, two conditions must be met: –the recursive call must somehow be simpler than the original call. –there must be one or more simple cases that do not make recursive calls (basis steps).

©Duane Szafron Recursive Sum Example Write a method that computes the sum of the integers from 1 to n. Note that: … + n = ( … + n-1) + n public static int sum(int n) { // post: return the sum of ints from 1 to the given value if (n < 1) return 0; else return sum(n - 1) + n; } code based on Bailey pg. 59

©Duane Szafron Vector Element Insertion Recall the iterative implementation : public void insertElementAt(Object object, int index) { //pre: 0 <= index <= size() //post: inserts the given object at the given index, // moving elements from index to size()-1 to the right int i; this.ensureCapacity(this.elementCount + 1); for (i = this.elementCount; i > index; i--) this.elementData[i] = this.elementData[i - 1]; this.elementData[index] = object; this.elementCount++; } code based on Bailey pg. 39 index

©Duane Szafron Recursive Element Insertion Here is a recursive implementation: public void insertElementAt(Object object, int index) { //pre: 0 <= index <= size() //post: inserts the given object at the given index, // moving elements from index to size()-1 to the right Object previous; if (index >= this.size()) this.addElement(object); else { previous = this.elementAt(index); this.insertElementAt(previous, index + 1); setElementAt(object, index); } code based on Bailey pg. 60 Basis step Recurring step

©Duane Szafron Trace of Recursive Insert index=3 ins 2.5 this index=4 ins 3 this ins index=5 this (index=size(this)=5) add index=5 this 5 set 4 index= this 5 index=4 set this ’ 5 set this 5 index=3

©Duane Szafron Direct References in Methods When a method is executing it can access some objects and some values. The receiver object can be referenced directly using the pseudo-variable this. Other objects and values can be referenced directly using method parameters and local variables. Still other objects and values can only be accessed indirectly by sending messages that return references to them.

©Duane Szafron Method Activations and Frames A method can only access objects while it is executing or active. The collection of all direct references in a method is called the frame or stack frame of a method. The frame is created when the method is invoked, and destroyed when the method finishes. If a method is invoked again, a new frame is created for it.

©Duane Szafron Multiple Activations of a Method When we invoke a recursive method on an object, the method becomes active. Before it is finished, it makes a recursive call to the same method. This means that when recursion is used, there is more than one copy of the same method active at once. Therefore, each active method has its own frame which contains independent copies of its direct references.

©Duane Szafron Stack Frames for InsertElementAt Each frame has its own pseudo-variable, this, bound to a different receiver object. Each frame has two parameters, object and index. Each frame has its local variable, previous, bound to a different object. These frames all exist at the same time.

©Duane Szafron Recursive Vector Insertion Example For example, insert “Wilma” at index 1. “Wilma” aVector elementData elementCount anArray 0123 “Fred”“Barney”“Betty”null 3

©Duane Szafron Calling insertElementAt(“Wilma”, 1) if (index >= this.size()) this.addElement(object); else { previous = this.elementAt(index); this.insertElementAt(previous, index + 1);....} this object index previous aVector elementData elementCount anArray 0123 “Fred”“Barney”“Betty”null 3 “Wilma” 1 code based on Bailey pg. 60

©Duane Szafron Calling insertElementAt(“Barney”, 2) this object index previous aVector elementData elementCount anArray 0123 “Fred”“Barney”“Betty”null 3 “Wilma” 1 this object index previous 2 code based on Bailey pg. 60 if (index >= this.size()) this.addElement(object); else { previous = this.elementAt(index); this.insertElementAt(previous, index + 1);....}

©Duane Szafron Calling insertElementAt(“Betty”, 3) this object index previous aVector elementData elementCount anArray 0123 “Fred”“Barney”“Betty”null 3 “Wilma” 1 2 this object index previous this object index previous 3 4 code based on Bailey pg. 60 if (index >= this.size()) this.addElement(object); else { previous = this.elementAt(index); this.insertElementAt(previous, index + 1);....}

©Duane Szafron Returning insertElementAt(“Betty”, 3) if (index >= this.size()) this.addElement(object); else { previous = this.elementAt(index); this.insertElementAt(previous, index + 1); this object index previous aVector elementData elementCount anArray 0123 “Fred”“Barney”“Betty” 4 “Wilma” 1 2 this object index previous this object index previous 3 code based on Bailey pg. 60

©Duane Szafron Returning insertElementAt(“Barney”, 2) if (index >= this.size()) this.addElement(object); else { previous = this.elementAt(index); this.insertElementAt(previous, index + 1); setElementAt(object, index); } this object index previous aVector elementData elementCount anArray 0123 “Fred”“Barney”“Betty” 4 “Wilma” 1 this object index previous 2 code based on Bailey pg. 60

©Duane Szafron Returning insertElementAt(“Wilma”, 1) this object index previous aVector elementData elementCount anArray 0123 “Fred”“Barney”“Betty” 4 “Wilma” 1 code based on Bailey pg. 60 if (index >= this.size()) this.addElement(object); else { previous = this.elementAt(index); this.insertElementAt(previous, index + 1); setElementAt(object, index); }

©Duane Szafron Recursive Vector Insertion Done After, inserting “Wilma” at index 1. “Wilma” aVector elementData elementCount anArray 0123 “Fred”“Barney”“Betty” 4 “Wilma”

©Duane Szafron Some Principles from the Textbook 7. Recursive structures must make “progress” towards a principles from Bailey ch. 4 “base case”.