Recursion. Slides. Programming in C++ Computer Science Dept Va Tech Aug., 2001. 1995-2001 Barnette ND, McQuain WD



Similar documents
Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

Section IV.1: Recursive Algorithms and Recursion Trees

16. Recursion. COMP 110 Prasun Dewan 1. Developing a Recursive Solution

What Is Recursion? Recursion. Binary search example postponed to end of lecture

Analysis of Binary Search algorithm and Selection Sort algorithm

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...

Chapter 5. Recursion. Data Structures and Algorithms in Java

Lecture Notes on Linear Search

recursion here it is in C++ power function cis15 advanced programming techniques, using c++ fall 2007 lecture # VI.1

Stacks. Linear data structures

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Functions Recursion. C++ functions. Declare/prototype. Define. Call. int myfunction (int ); int myfunction (int x){ int y = x*x; return y; }

Software Testing. Definition: Testing is a process of executing a program with data, with the sole intention of finding errors in the program.

CSCI 123 INTRODUCTION TO PROGRAMMING CONCEPTS IN C++

10CS35: Data Structures Using C

Recursion. Definition: o A procedure or function that calls itself, directly or indirectly, is said to be recursive.

csci 210: Data Structures Recursion

Recursive Algorithms. Recursion. Motivating Example Factorial Recall the factorial function. { 1 if n = 1 n! = n (n 1)! if n > 1

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

6. Standard Algorithms

Sample Questions Csci 1112 A. Bellaachia

Data Structures, Practice Homework 3, with Solutions (not to be handed in)

Data Structure with C

Sorting Algorithms. Nelson Padua-Perez Bill Pugh. Department of Computer Science University of Maryland, College Park

Common Data Structures

PES Institute of Technology-BSC QUESTION BANK

CSC148 Lecture 8. Algorithm Analysis Binary Search Sorting

IS0020 Program Design and Software Tools Midterm, Feb 24, Instruction

While Loop. 6. Iteration

Pseudo code Tutorial and Exercises Teacher s Version

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

Recursion and Recursive Backtracking

Course: Programming II - Abstract Data Types. The ADT Stack. A stack. The ADT Stack and Recursion Slide Number 1

C++FA 5.1 PRACTICE MID-TERM EXAM

Sources: On the Web: Slides will be available on:

Introduction to Programming (in C++) Loops. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

Example. Introduction to Programming (in C++) Loops. The while statement. Write the numbers 1 N. Assume the following specification:

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015

1 Description of The Simpletron

Moving from CS 61A Scheme to CS 61B Java

Data Structure and Algorithm I Midterm Examination 120 points Time: 9:10am-12:10pm (180 minutes), Friday, November 12, 2010

Mathematical Induction. Lecture 10-11

Algorithms and Data Structures Exercise for the Final Exam (17 June 2014) Stack, Queue, Lists, Trees, Heap

Moving from C++ to VBA

Computer Science 210: Data Structures. Searching

50 Computer Science MI-SG-FLD050-02

Converting a Number from Decimal to Binary

Binary search algorithm

Answers to Review Questions Chapter 7

DATA STRUCTURES USING C

6.2 Permutations continued

6. Control Structures

COMPUTER SCIENCE. Paper 1 (THEORY)

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

COMP 250 Fall 2012 lecture 2 binary representations Sept. 11, 2012

Chapter 5 Functions. Introducing Functions

Binary Heap Algorithms

Functional Programming in C++11

Introduction to data structures

For the next three questions, consider the class declaration: Member function implementations put inline to save space.

12-6 Write a recursive definition of a valid Java identifier (see chapter 2).

Module 2 Stacks and Queues: Abstract Data Types

Arrays. number: Motivation. Prof. Stewart Weiss. Software Design Lecture Notes Arrays

Quiz 4 Solutions EECS 211: FUNDAMENTALS OF COMPUTER PROGRAMMING II. 1 Q u i z 4 S o l u t i o n s

Regression Verification: Status Report

Lecture Notes on Binary Search Trees

Passing 1D arrays to functions.

Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

Algorithm Design and Recursion

You are to simulate the process by making a record of the balls chosen, in the sequence in which they are chosen. Typical output for a run would be:

COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms.

Lecture 12 Doubly Linked Lists (with Recursion)

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

What Is Recursion? 5/12/10 1. CMPSC 24: Lecture 13 Recursion. Lecture Plan. Divyakant Agrawal Department of Computer Science UC Santa Barbara

I PUC - Computer Science. Practical s Syllabus. Contents

Solving Problems Recursively

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

Organization of Programming Languages CS320/520N. Lecture 05. Razvan C. Bunescu School of Electrical Engineering and Computer Science

CS 2412 Data Structures. Chapter 2 Stacks and recursion

Data Structures. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

Data Structures. Algorithm Performance and Big O Analysis

(IALC, Chapters 8 and 9) Introduction to Turing s life, Turing machines, universal machines, unsolvable problems.

MAX = 5 Current = 0 'This will declare an array with 5 elements. Inserting a Value onto the Stack (Push)

C++FA 3.1 OPTIMIZING C++

Curriculum Map. Discipline: Computer Science Course: C++

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

Introduction to Programming (in C++) Sorting. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

Keil C51 Cross Compiler

Hash Tables. Computer Science E-119 Harvard Extension School Fall 2012 David G. Sullivan, Ph.D. Data Dictionary Revisited

13 Classes & Objects with Constructors/Destructors

Transcription:

1 Slides 1. Table of Contents 2. Definitions 3. Simple 4. Recursive Execution Trace 5. Attributes 6. Recursive Array Summation 7. Recursive Array Summation Trace 8. Coding Recursively 9. Recursive Design 10. Avoiding Pitfalls 11. Middle Decomposition 12. Call Tree Traces 13. Edges & Center Decomposition 14. Recursive Sorting 15. Backtracking 16. Knapsack Solution 17. Runtime Stack 18. Knap Runtime Trace Snapshot 19. Storage Organization

Definitions 2 see a process in which the result of each repetition is dependent upon the result of the next repetition. Simplifies program structure at a cost of function calls Hofstadter's Law It always takes longer than you expect, even when you take into account Hofstadter's Law. Sesquipedalian a person who uses words like sesquipedalian. Yogi Berra Its déjà vu all over again.

Simple 3 A procedure or function which calls itself is a recursive routine. Consider the following function, which computes N! 1 * 2 *... * N int int Factorial(int Factorial(int n n int int Product Product 1, 1, 2 2 while while ( ( < < n n Product Product Product Product * * + + 1 1 return return (Product (Product Now consider a recursive version of Factorial: int int Factorial(int Factorial(int n n if if ( ( n n > > 1 1 return( return( n n * * Factorial Factorial (n-1 (n-1 else else return(1 return(1

Recursive Execution Trace 4 Factorial (5 return 120... and then the return sequence 5 * Factorial (4 return 24 4 * Factorial (3 return 6 3 * Factorial (2 return 2 First the recursive descent... 2 * Factorial (1 1 return 1

Attributes 5 Every recursive algorithm can be implemented nonrecursively. recursion <> iteration Eventually, the routine must not call itself, allowing the code to "back out". Recursive routines that call themselves continuously are termed: infinite recursion <> infinite loop Problem with this recursive factorial implementation? Negative numbers! is inefficient at runtime.

Recursive Array Summation 6 Here is a recursive function that takes an array of integers and computes the sum of the elements: // X[] array of integers to be summed // Start start summing at this index... // Stop... and stop summing at this index // int SumArray(const int X[], int Start, int Stop // error check if (Start > Stop Start < 0 Stop < 0 return 0 else if (Start Stop // base case return X[Stop] else // recursion return (X[Start] + SumArray(X, Start + 1, Stop

Recursive Array Summation Trace 7 The call: const int Size 5 int X[Size] 37, 14, 22, 42, 19 SumArray(X,0,Size- 1// note Stop is last valid index would result in the recursive trace: // return values: SumArray(X, 0, 4 // 134 return(x[0]+sumarray(x,1,4 // 37 + 97 return(x[1]+sumarray(x,2,4 // 14 + 83 return(x[2]+sumarray(x,3,4 // 22 + 61 return(x[3]+sumarray(x,4,4 // 42 + 19 return X[4] // 19

Coding Recursively 8 Mathematical Induction Model Solve the trivial "base" case(s. Restate general case in 'simpler' or 'smaller' terms of itself. Array Sum Example Determine the size of a single linked list. Base Case : array size 1, sum the element General Case : first element + Sum(Rest of Array // X[] array of integers to be summed // Start start summing at this index... // Stop... and stop summing at this index // int SumArray(const int X[], int Start, int Stop // error check if (Start > Stop Start < 0 Stop < 0 return 0 else if (Start Stop // base case return X[Stop] else // recursion return (X[Start] + SumArray(X, Start + 1, Stop Example Example of of tail tail recursion recursion (going (going up up recursion. recursion. Tail Tail recursive recursive functions functions are are characterized characterized by by the the recursive recursive call call being being the the last last statement statement in in the the function, function, (can (can easily easily be be replaced replaced by by a a loop. loop.

Recursive Design 9 Problem: Code a function void intcomma( long that outputs the integer argument comma separated : e.g., the call: intcomma ( 123456789 displays: 123,456,789 Top-Down Design Code void intcomma ( long num if (num is less than 1000 display num else display comma separated digits above 1000 display comma display digits below 1000 void intcomma ( long num if (num < 1000 cout << setw(3 << num else intcomma(num / 1000 cout << ',' << setw(3 << num % 1000 Consider: Consider: intcomma( intcomma( 123456789 123456789 intcomma( intcomma( 1001 1001

Avoiding Pitfalls General Solution void intcomma ( long num 10 if (num < 0 cout << '-' num -num if (num < 1000 cout << setw(3 << num else intcomma(num / 1000 cout << ',' num num % 1000 cout << (num / 100 num num % 100 cout << (num / 10 << (num % 10 Trace intcomma(9087605430 intcomma(9087605430 intcomma(9087605 and intcomma(9087 and intcomma(9 and intcomma(9 9 // display sign for negatives Example Example of of going going down down (head (head recursion recursion // display digits // separately // for zeroes output alternatively string prefix (num < 10? "00" : (num < 100? "0" : "" cout << prefix << num 9,087 9,087,605 9,087,605,430

Middle Decomposition 11 Problem: Given an array of integers of n+1 elements code a function to return the index of the maximum value in the array. Solution: Check if the middle element is the largest if so return its index otherwise return the index of either the largest element in the lower half or the largest element in the upper half, whichever is the larger of the two. int rmax(const int ray[], int start, int end const int Unknown -1 Example Example of of int mid, h1max, h2max splitting splitting into if (end < start return Unknown into halves halves recursion recursion mid (start + end / 2 h1max rmax(ray, start, mid-1//left half if (h1max Unknown h1max start h2max rmax(ray, mid+1, end //right half if (h2max Unknown h2max end if ( (ray[mid] > ray[h1max] && (ray[mid] > ray[h2max] return mid else return( (ray[h1max] > ray[h2max]? h1max : h2max Unknown checks ensure that indices are within array subscript range

Call Tree Traces Given: 12 ray [0] [1] [2] [3] [4] 56 23 66 44 78 Call Tree Trace of h1max0 0 rmax(ray, 0, 4 mid2 h2max4 4 4 h1max0 unknown rmax(ray, 0, 1 mid0 rmax(ray, 0, -1 h1max1 unknown h2max1 1 rmax(ray, 1, 1 mid1 h2max1 unknown h1max3 unknown rmax(ray, 3, 2 rmax(ray, 3, 4 mid3 4 h1max4 unknown h2max4 h2max 4 rmax(ray, 4, 4 mid4 unknown rmax(ray, 1, 0 rmax(ray, 2, 1 rmax(ray, 4, 3 rmax(ray, 5, 4 Middle Middle decomposition decomposition (splitting (splitting problem problem into into halves, halves, recursive recursive functions functions are are best best traced traced with with tree tree diagrams diagrams

Edges & Center Decomposition 13 Problem: sort a subset, (m:n, of an array of integers (ascending order Solution: Find the smallest and largest values in the subset of the array (m:n and swap the smallest with the m th element and swap the largest with the n th element, (i.e. order the edges. Sort the center of the array (m+1: n-1. Solution Trace unsorted array m [0] [1] [2] [3] [4] [5] [6] [7] [8] n [9] 56 23 66 44 78 99 30 82 17 36 after call#1 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] 17 23 66 44 78 36 30 82 56 99 after call#3 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] 17 23 30 44 56 36 66 78 82 99 Variation Variation of of the the selection selection sort sort algorithm algorithm

Recursive Sorting 14 void duplexselection(int ray[], int start, int end int mini start, maxi end if (start < end //startend > 1 element to sort findminimaxi( ray, start, end, mini, maxi swapedges(ray, start, end, mini, maxi duplexselection( ray, start+1, end-1 void findminimaxi( const int ray[], int start, int end, int& mini, int& maxi if (start < end //subset to search exists if (ray[start] < ray[mini] mini start else if (ray[start] > ray[maxi] maxi start findminimaxi( ray, start+1, end, mini, maxi void swapedges(int ray[], int start, int end, int mini, int maxi //check for swap interference if ( (mini end && (maxi start swap( ray[start], ray[end] //check for low 1/2 interference else if (maxi start swap( ray[maxi], ray[end] swap( ray[mini], ray[start] // (mini end no interference else swap( ray[mini], ray[start] void void swap swap swap(ray[maxi], ray[end] ( ( int& int& x, x, int& int& y y int int tmp tmp x x x x y y y y tmp tmp

Backtracking 15 Knapsack Problem (very weak form Given an integer total, and an integer array, determine if any collection of array elements within a subset of the array sum up to total. Assume the array contains only positive integers. Special Base Cases total 0 : solution: the collection of no elements adds up to 0. total < 0 : solution: no collection adds to sum. start of subset index > end of subset index : solution: no such collection can exist. Inductive Step Check if a collection exists containing the first subset element. Does a collection exist for total - ray[ subset start ] from subset start + 1 to end of subset? If no collection exists containing ray[ subset start ] check for a collection for total from subset start + 1 to the end of the subset. Backtracking Backtracking step. step. Function Function searches searches for for alternative alternative solution solution undoing undoing previous previous possible possible solution solution search search work. work.

Knapsack Solution Knap backtracking function 16 bool Knap (const int ray[], int total, int start, int end if (total 0 // empty collection adds up to 0 return true if ( (total < 0 (start > end //no such return false //collection exists //check for collection containing ray[start] if (Knap(ray, total-ray[start], start+1, end return true // check for collection w/o ray[start] return (Knap(ray, total, start+1, end Trace Knap(ray, 100, 0, 4 TRUE Knap(ray, 50, 1, 4 TRUE Knap(ray, 30, 2, 4 ray [0] [1] [2] [3] [4] 50 20 40 60 30 FALSE TRUE Knap(ray, -10, 3, 4 Knap(ray, 30, 3, 4 FALSE TRUE Knap(ray, -30, 4, 4 Knap(ray, 30, 4, 4 Backtracking Backtracking steps steps TRUE Knap(ray, 0, 5, 4

Runtime Stack 17 Underpinnings Every instance of a function execution (call creates an Activation Record, (frame for the function. Activation records hold required execution information for functions: Return value for the function Pointer to activation record of calling function Return memory address, (calling instruction address Parameter storage Local variable storage Runtime Stack Activation records are created and stored in an area of memory termed the runtime stack. Fny( activation record Fnx( activation record main( activation record local storage Parameter storage Return address Pointer previous act. rec. Fn return value local storage Parameter storage Return address Pointer previous act. rec. Fn return value local storage Parameter storage Return address Pointer previous act. rec. Fn return value

Knap Runtime Trace Snapshot 18 First backtrack step(during fourth call Let first recursive call in knap be at address α Let second recursive call in knap be at address β Activation record destroyed as third recursive call completes execution second recursive call first recursive call external call ray -10 3 4 α? ray 30 2 4 α? ray 50 1 4 α? ray 100 0 4 Ψ? local storage Parameter storage Return address Previous act. rec. Fn return value local storage Parameter storage Return address Previous act. rec. Fn return value local storage Parameter storage Return address Previous act. rec. Fn return value local storage Parameter storage Return address Previous act. rec. Fn return value

Storage Organization 19 Typical C++ program execution memory model System privileges (not accessible to the C program Binary Code (text segment Lowest memory addresss Static Data (data segment Runtime Stack Function activation record management Dynamic memory structure management Storage Corruption Heap Highest memory addresss Infinite regression results in a collision between the runtime stack & heap termed a run-time stack overflow error. Illegal pointer de-references (garbage, dangling-references often result in memory references outside the operating system allocated partition, (segment for the C program resulting in a segmentation error (GPF - access violation and core dump.