Reduction & Recursion Overview

Similar documents
Recursion. Chapter 5

Recursive Definitions

11/2/2017 RECURSION. Chapter 5. Recursive Thinking. Section 5.1

CMSC 150 LECTURE 7 RECURSION

Recursion. Tracing Method Calls via a Stack. Beyond this lecture...

CMSC 132: Object-Oriented Programming II. Recursive Algorithms. Department of Computer Science University of Maryland, College Park

Recursion: Factorial (1) Recursion. Recursion: Principle. Recursion: Factorial (2) Recall the formal definition of calculating the n factorial:

Reading 8 : Recursion

Recursion. EECS2030: Advanced Object Oriented Programming Fall 2017 CHEN-WEI WANG

2.3 Recursion. Overview. Mathematical Induction. What is recursion? When one function calls itself directly or indirectly.

Recursion vs Induction. CS3330: Algorithms The University of Iowa

Two Approaches to Algorithms An Example (1) Iteration (2) Recursion

Chapter 15: Recursion

Announcements. Recursion and why study it. Recursive programming. Recursion basic idea

OVERVIEW. Recursion is an algorithmic technique where a function calls itself directly or indirectly. Why learn recursion?

Overview. What is recursion? When one function calls itself directly or indirectly.

Chapter 10: Recursive Problem Solving

CS 3410 Ch 7 Recursion

Recursion vs Induction

COMP 202 Recursion. CONTENTS: Recursion. COMP Recursion 1

CS 310 Advanced Data Structures and Algorithms

Recursion Chapter 8. What is recursion? How can a function call itself? How can a function call itself?

Recursion Chapter 3.5

Recursion CSCI 136: Fundamentals of Computer Science II Keith Vertanen Copyright 2011

APCS-AB: Java. Recursion in Java December 12, week14 1

Recursion. Fundamentals of Computer Science

We cover recursion in 150. Why do it again in 151?

Recursion. Overview. Mathematical induction. Hello recursion. Recursion. Example applications. Goal: Compute factorial N! = 1 * 2 * 3...

recursive algorithms 1

Recursion. Let s start by looking at some problems that are nicely solved using recursion. First, let s look at generating The Fibonacci series.

Functions. CS10001: Programming & Data Structures. Sudeshna Sarkar Professor, Dept. of Computer Sc. & Engg., Indian Institute of Technology Kharagpur

Recursion. Chapter 17 CMPE13. Cyrus Bazeghi

Solutions to the Second Midterm Exam

Programming II (CS300)

Copyright The McGraw-Hill Companies, Inc. Persion required for reproduction or display. What is Recursion?

COMP-202. Recursion. COMP Recursion, 2011 Jörg Kienzle and others

11. Recursion. n (n 1)!, otherwise. Mathematical Recursion. Recursion in Java: Infinite Recursion. 1, if n 1. n! =

CSE 214 Computer Science II Recursion

CS/CE 2336 Computer Science II

12. Recursion. n (n 1)!, otherwise. Educational Objectives. Mathematical Recursion. Recursion in Java: 1, if n 1. n! =

Recursion Chapter 17. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Lecture 10: Recursion vs Iteration

Recursion Chapter 8. What is recursion? How can a function call itself? How can a function call itself? contains a reference to itself.

Lecture 6: Recursion RECURSION

What is Recursion? Chapter 17 Recursion. Executing RunningSum. High-Level Example: Binary Search

Programming II (CS300)

Recursion. Chapter 7

Chapter 17 Recursion

Chapter 6 Recursion. The Concept of Recursion

Recursion. Chapter 7. Copyright 2012 by Pearson Education, Inc. All rights reserved

Recursive Thinking. Chapter 8: Recursion. Recursive Definitions. Recursion. Java Software Solutions for AP* Computer Science A 2nd Edition

Complexity, Induction, and Recurrence Relations. CSE 373 Help Session 4/7/2016

q To develop recursive methods for recursive mathematical functions ( ).

SOFTWARE DEVELOPMENT 1. Recursion 2018W A. Ferscha (Institute of Pervasive Computing, JKU Linz)

q To develop recursive methods for recursive mathematical functions ( ).

Data Structures And Algorithms

Last week. Another example. More recursive examples. How about these functions? Recursive programs. CSC148 Intro. to Computer Science

COMP-202: Foundations of Programming. Lecture 13: Recursion Sandeep Manjanna, Summer 2015

RECURSION. Data Structures & SWU Rachel Cardell- Oliver

Programming II (CS300)

What is recursion? Recursion. How can a function call itself? Recursive message() modified. Week 10. contains a reference to itself.

Chapter 17 Recursion

Recursion. notes Chapter 8

CmpSci 187: Programming with Data Structures Spring 2015

RECURSION. Many Slides from Ken Birman, Cornell University

Identify recursive algorithms Write simple recursive algorithms Understand recursive function calling

C22a: Problem Solving using Recursion

COMP-202. Recursion. COMP Recursion, 2013 Jörg Kienzle and others

Recursive Definitions Structural Induction Recursive Algorithms

CS 112 Introduction to Programming

Fundamental mathematical techniques reviewed: Mathematical induction Recursion. Typically taught in courses such as Calculus and Discrete Mathematics.

CISC 3115 TY3. C21a: Recursion. Hui Chen Department of Computer & Information Science CUNY Brooklyn College. 11/6/2018 CUNY Brooklyn College

NCS 301 DATA STRUCTURE USING C

VTU NOTES QUESTION PAPERS NEWS RESULTS FORUMS

Standard Version of Starting Out with C++, 4th Edition. Chapter 19 Recursion. Copyright 2003 Scott/Jones Publishing

Recursion defining an object (or function, algorithm, etc.) in terms of itself. Recursion can be used to define sequences

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May

Chapter Summary. Mathematical Induction Recursive Definitions Structural Induction Recursive Algorithms

What is recursion? Recursion. How can a function call itself? Recursive message() modified. Week 10. contains a reference to itself. Gaddis:

EE 368. Weeks 4 (Notes)

Unit #3: Recursion, Induction, and Loop Invariants

Department of Computer Science Yale University Office: 314 Watson Closely related to mathematical induction.

Objectives. Recursion. One Possible Way. How do you look up a name in the phone book? Recursive Methods Must Eventually Terminate.

Algorithm Analysis. CENG 707 Data Structures and Algorithms

CMPSCI 250: Introduction to Computation. Lecture #14: Induction and Recursion (Still More Induction) David Mix Barrington 14 March 2013

Recursion CS GMU

STUDENT LESSON A9 Recursion

COMP-202 Unit 8: Recursion. CONTENTS: Recursion in Java More complex examples

What is recursion? Recursion. How can a function call itself? Recursive message() modified. contains a reference to itself. Week 7. Gaddis:

Recursion. Example R1

Chapter 12 Supplement: Recursion with Java 1.5. Mr. Dave Clausen La Cañada High School

COMP 250 Fall Recursive algorithms 1 Oct. 2, 2017

What is recursion? Recursion. Recursive message() modified. How can a function call itself? contains a reference to itself. Week 10. Gaddis:

COE428 Lecture Notes Week 1 (Week of January 9, 2017)

8/5/10 TODAY'S OUTLINE. Recursion COMP 10 EXPLORING COMPUTER SCIENCE. Revisit search and sorting using recursion. Recursion WHAT DOES THIS CODE DO?

! New mode of thinking. ! Powerful programming paradigm. ! Mergesort, FFT, gcd. ! Linked data structures.

Lesson 12: Recursion, Complexity, Searching and Sorting. Modifications By Mr. Dave Clausen Updated for Java 1_5

CSE 143 Lecture 11. Decimal Numbers

9/16/14. Overview references to sections in text RECURSION. What does generic mean? A little about generics used in A3

The divide and conquer strategy has three basic parts. For a given problem of size n,

Transcription:

Reduction & Recursion Overview Reduction definition Reduction techniques Recursion definition Recursive thinking (Many) recursion examples Indirect recursion Runtime stack Factorial isnumericstring add + mult in recursion Fibonacci Binary Enumeration GCD Partial Sum Catalan Numbers Towers of Hanoi Recursive directory search Depth first search Fractals: Hilbert Curve 23 December 2007 Ariel Shamir 1 Reduction Reduction is a technique for solving problems by rephrasing the problem in terms of another problem whose solution is known. We say that we reduce the problem into another known problem. Examples for reduction in mathematics. Reduction as a programming technique. 23 December 2007 Ariel Shamir 2 1

Reduction Examples P(x) Q 1 (x), Q 2 (x), Q k (x) Where Q i (x) ) is simpler than P(x) Sorting Finding minimum (selection sort) Polynomial multiplication Integer multiplication and addition More 23 December 2007 Ariel Shamir 3 Recursion As Reduction P(x) P(x 1 ), P(x 2 ), P(x k ) When x i << x (x i is smaller than x) Recursion is a fundamental programming technique that can provide an elegant solution to certain kinds of problems. In recursion we solve a problem by reducing it to a similar, smaller problem whose input might be different. 23 December 2007 Ariel Shamir 4 2

Recursion In Programs public static void f() { f(); public static void f(int n) { f(n-1); // reduction! public static void f(int n) { if (n == 0) // base case! return; f(n-1); // reduction! 23 December 2007 Ariel Shamir 5 Effective Recursion Rules 1. Self call P P 2. Reduction of some sort and merging P(n) P(x 1 ) P(x 2 ) P(x k ) 3. Base case (recursion halt) P(0) or P(1) etc. 23 December 2007 Ariel Shamir 6 3

Recursive Methods A method in Java that invokes itself is called a recursive method. An effective recursive method has a base case which solve the task without a recursive call and the recursive part which solve the task by reducing it and using a recursive call. If there is no base case and sometimes if there is no reduction, the sequence will not come to an end. We call this case an infinite recursion. 23 December 2007 Ariel Shamir 7 Factorial Example If n is a positive integer, we define n! - the factorial of n - as follows: n!=1*2*3* *n *n A simple implementation for a method that computes the factorial of a number would be with a loop that goes over the expression and keep track of the partial multiplication (next slide) 23 December 2007 Ariel Shamir 8 4

Factorial Code (Loop) /** * Computes the factorial of a number. * @param n The given number. * @return n! - The factorial of n. */ public static long factorial(long n) { long fact = 1; for (int i=1; i<=n; i++) { fact *= i; return fact; 23 December 2007 Ariel Shamir 9 Factorial Reduction The previous solution is simple, but we want to find a recursive solution: For every integer n, such that n>1 : n! = 1*2*...n = (1*2*...n-1)*n 1)*n = (n-1)! * n So we have: 1! = 1 n! = (n-1)!*n, for n>1 23 December 2007 Ariel Shamir 10 5

Factorial Recursive Solution Recursion: n! (? )! Reduction: n n-1 Merge: use * operator Base: if n == 1 then 1! = 1 23 December 2007 Ariel Shamir 11 From (n-1)! to n! We can use these observations to derive a new implementation for computing n! If n is 1, there is no problem, we know that 1!=1. If n>1 we can t t say immediately the result, but if we only knew how to compute (n-1)! we could compute n! using the fact that n!=(n-1)!*n. 23 December 2007 Ariel Shamir 12 6

How to find (n-1)!? Note, that we can make the reduction for any arbitrary n, such that n>1. So we can reduce the problem from n! to (n-1)! to (n-2)! etc. until we will face the problem of computing 1! which is known. 23 December 2007 Ariel Shamir 13 Factorial Recursive Computation 5! 5 * 4! 4 * 3! 3 * 2! 120 24 2 * 1! 6 1! 2 1 23 December 2007 Ariel Shamir 14 7

Factorial Recursive Code /** * Computes the factorial of a number. * @param n A positive integer. * @return n! - The factorial of n. */ public static long factorial(long n) { if (n==1) { return 1; return n*factorial(n-1); 23 December 2007 Ariel Shamir 15 Recursive Factorial Code (Version 2) /** * Computes the factorial of a number. * @param n A positive integer. * @return n! - The factorial of n. */ public static long factorial(long n) { return ((n==1)? 1 : n*factorial(n-1)); 23 December 2007 Ariel Shamir 16 8

Factorial Code Proof Usually recursive algorithms are proved using using mathematical induction. In our implementation of factorial we have to show the following: Factorial give the correct result for n=1. If factorial returns the correct answer for an arbitrary number n such that n>1, then factorial returns the correct answer for n+1. 23 December 2007 Ariel Shamir 17 Recursive Complexity? Factorial Recursion: T(N) = T(N-1) + C 0 T(N-1) = T(N-2) + C 0 T(N) =(N-1)C 0 +C 1 ~= O(N) T(1) = C 1 measuring complexity: Non-recursive programs counting loops Recursive programs solving recursion! 23 December 2007 Ariel Shamir 18 9

isnumeric() Example Consider the task of checking if a given string consist only of digits. We want to write a method isnumeric() that for a given string would return true if and only if all the characters in the string are from 0, 1,,..., 9. A simple implementation would loop on the characters of the string and return false if one found that is not a digit (next slide). 23 December 2007 Ariel Shamir 19 isnumeric() Code /** * Checks if a given string consists only * from digit symbols. */ public static boolean isnumeric(string s) { for (int i=0; i<s.length(); i++) { char c = s.charat(i); if (c< 0 c> 9 ) { return false; return true; 23 December 2007 Ariel Shamir 20 10

IsNumeric Recursive Solution Recursion: isnum(s) isnum(???) Reduction: S S.substring(1); Merge: use && operator Base: if S is empty then it is numeric (because of the && operator) 23 December 2007 Ariel Shamir 21 Recursive isnumeric() Observe the following facts: An empty string doesn t t contain non-digit characters, therefore the method should return true for the empty string. If the first character of s is non-digit then the method should return false. If the first character of s is a digit symbol, then the s is numeric if and only if the rest of s (without the first character) is numeric. 23 December 2007 Ariel Shamir 22 11

isnumeric() Recursive Code /** * Checks if a given string consists only * from digit symbols. */ public static boolean isnumeric(string s) { if (s.length()==0) { return true; char c = s.charat(0); return (c>= 0 && c<= 9 && isnumeric(s.substring(1)); 23 December 2007 Ariel Shamir 23 Recursive Programming Note that just because we can use recursion to solve a problem, it doesn't mean we should. For instance, we usually would not use recursion to solve the previous problem. However, for some problems, recursion provides an elegant solution, often cleaner than an iterative version. 23 December 2007 Ariel Shamir 24 12

Fibonacci Example There are many cases where a recursive solution is very appropriate. Fibonacci series is a series of integers f 0, f 1, f 2,... that is defined as follows: f 0 = 1 f 1 = 1 f n = f n-1 +f n-2 for n>1 A recursive solution seems very suitable. 23 December 2007 Ariel Shamir 25 Fibonacci Recursive Solution Recursion: Fib(n) Fib(???) Reduction: n n-1,n-2 Merge: use + operator Base: if n == 0 or n == 1 then we have 1 23 December 2007 Ariel Shamir 26 13

Fibonacci Code 1 1 2 3 5 8 13 21 34 55 89 144... /** * Returns the n number in fibonacci series */ public static int fibonacci(int n) { if (n<=1) { return 1; return fibonacci(n-1)+fibonacci(n-2); 23 December 2007 Ariel Shamir 27 Fibonacci Computation F(4) F(3) F(2) F(2) F(1) F(1) F(0) F(1) F(0) 23 December 2007 Ariel Shamir 28 14

Fibonacci Complexity f n = f n-1 +f n-2 T(n) = T(n-1) + T(n-2) + C 0 T(n) = f n +(f n 1) C 0 (Induction) T(n) = (C 0 +1) f n C 0 ~= O(f n ) f n =~ (0.5(1+ 5)) 5)) n = O(φ n ) Exponential! 23 December 2007 Ariel Shamir 29 Can We Do Better? Trivially: just sum up from f 0 and f 1 Where is the price of recursion? Doing redundant work: To compute f n we need f n-1 and f n-2 but when computing f n-1 we didn t t use f n-2 but computed it again from scratch! 23 December 2007 Ariel Shamir 30 15

Binary Enumeration Print all possible combinations of 0 and 1 in a given range: 000 001 010 011 100 101 110 111 23 December 2007 Ariel Shamir 31 Non-recursive int[] a = new int[n]; for (a[0]= 0 ; a[0] < 2 ; a[0]++){ for (a[1]= 0 ; a[1] < 2 ; a[1]++){...// n-2 2 loops come here printarray(a); 23 December 2007 Ariel Shamir 32 16

Enumeration Recursive Solution Recursion: enum(n) enum(???) Reduction: n n-1,n-1 Merge: use concatenation Base: if n == 0 then we print New: we send something (the array) through the recursion 23 December 2007 Ariel Shamir 33 Recursive void printcombrec(int[] a,int i) { if (i == a.length) ) { printarray(a); return; a[i] ] = 0; printcombrec(a,i+1); a[i] ] = 1; printcombrec(a,i+1); 23 December 2007 Ariel Shamir 34 17

GCD Example The Greatest Common Divisor of two integers a,b is defined as the largest integer that divides both a and b. We denote the greatest common divisor of a and b by gcd(a,b). 23 December 2007 Ariel Shamir 35 Finding the GCD Suppose a>b. If b divides a, then gcd(a,b)=b (because b also divides itself and there is no greater integer that divides b). This will be the base case. If b doesn t t divide a, let m be the reminder of the division of a by b (m( = a%b). We can write: a = k*b+m, m<b 23 December 2007 Ariel Shamir 36 18

Finding the GCD (Cont.) Denote x = gcd(a,b). x must also divide m (since a = kb + m). Then x divides a,b,m! Denote y = gcd(b,m). y must also divide a (since m = a - kb). Then y divides a,b,m! Since y divides a and b, y gcd(a,b) ) = x Since x divides m and b, x gcd(b,m) ) = y y x y gcd(a,b) ) = x = y = gcd(b,m)! Therefore, gcd(a,b) ) = b if b divides a or else gdc(a,b) ) = gcd(b,a%b). 23 December 2007 Ariel Shamir 37 Recursive gcd() Code /** * Finds the greatest common divisor of * two input numbers. */ public static long gcd(long a,long b) { if (b < a) { long m = a%b; return (m == 0? b : gcd(b,m)); else { long m = b%a; return (m == 0? a : gcd(a,m)); 23 December 2007 Ariel Shamir 38 19

GCD Complexity Assume we have a > b. b If b < a/2 then a%b < b < a/2 If b > a/2 then a%b = a-b a b < a/2 So after 2 GCD steps we replace a with a number smaller than a/2. At most after 2log 2 a we get a number smaller than 2. So GCD is better than O(log 2 a) 23 December 2007 Ariel Shamir 39 GCD Worst Case Complexity Fibonacci consecutive pair: f n /f n-1 = (f n-1 +f n-2 ) /f n-1 = 1 + f n-2 /f n-1 i.e. f n %f n-1 = f n-2 Therefore we move from (f n, f n-1 ) to (f n-1, f n-2 ). 23 December 2007 Ariel Shamir 40 20

Partial Sum Example Problem: given a series of integers a 1, a 2,..., a n and a number s,, we want to determine weather there is a partial series whose elements sum up to s. 13,5,10,3,12,7 Sum = 21? 13+5+3 = 21! 23 December 2007 Ariel Shamir 41 Non Recursive Solution Sort? Start with minimum? Start with maximum? Loop? Search for all possible sub-sums! sums! How many sub-sums? sums? 2 N Why? 2 2 = 4, 2 3 = 8, 2 10 = 1024, 2 20 > 1000000 23 December 2007 Ariel Shamir 42 21

Partial Sum Solution If the series is empty the answer is true iff s is zero. Otherwise, we should check the following two options: The last element is not included in the partial series - in this case we reduce the problem to finding a partial sum s in the rest of the series. The last element is included - we reduce the problem to finding a partial sum s- a n in the rest. 23 December 2007 Ariel Shamir 43 haspartialsum() Example /** * Checks if a given series of integers has a partial * series whose sum of elements is equal to a given * integer. * @param elements The series of integers. * @param sum The requested sum */ public static boolean haspartialsum( int[] series, int sum) { return haspartialsum(series, sum, series.length); 23 December 2007 Ariel Shamir 44 22

Partial Sum Code // Checks for a partial sum in a given series // length indicates the length of the series private static boolean haspartialsum( int[] series, int sum, int length) { if (length==0) return (sum ==0); return haspartialsum(series, sum, length-1) haspartialsum(series, sum-series[length-1],length-1); 23 December 2007 Ariel Shamir 45 Partial Sum Call Graph F(S,x 1..x n ) F(S,x 1..x n-1 ) F(S-x n,x 1..x n-1 ) F(S,x 1..x n-2 ) F(S-x n,x 1..x n-2 ) F(S-x n-1,x 1..x n-2 ) F(S-x n -x n-1,x 1..x n-2 ) 23 December 2007 Ariel Shamir 46 23

Evaluation Order // Checks for a partial sum in a given series // length indicates the length of the series private static boolean haspartialsum( int[] series, int sum, int length) { if (length==0) return (sum ==0); return haspartialsum(series, sum, length-1) haspartialsum(series, sum-series[length-1],length-1); 23 December 2007 Ariel Shamir 47 Depth First Evaluation haspartialsum(series, sum, length-1) haspartialsum(series, sum, length-2) haspartialsum(series, sum, length-3)... haspartialsum(series, sum, 1) haspartialsum(series, sum, 0) 23 December 2007 Ariel Shamir 48 24

Following the Path F(S,x 1..x n ) F(S,x 1..x n-1 ) F(S-x n,x 1..x n-1 ) F(S,x 1..x n-2 ) F(S-x n,x 1..x n-2 ) F(S-x n-1,x 1..x n-2 ) F(S-x n -x n-1,x 1..x n-2 ) 23 December 2007 Ariel Shamir 49 Stopping the Path // Checks for a partial sum in a given series // length indicates the length of the series private static boolean haspartialsum( int[] series, int sum, int length) { if (sum == 0) return true; if (length==0 sum < 0) return false; return haspartialsum(series, sum, length-1) haspartialsum(series, sum-series[length-1],length-1); 23 December 2007 Ariel Shamir 50 25

Catalan Numbers Example The n th Catalan number C n Comes up in many problems. The number of valid strings of length 2n consisting of n '(' symbols and n ')' symbols, which satisfy that in every prefix of the string the number of '(' symbols is greater than or equal to the number of ')' symbols. For example "(()())"" " is a valid string, while "())(()"" " is not. 23 December 2007 Ariel Shamir 51 Enumeration of Valid Strings 1: () 2: ()() (()) 3: ((())) (()()) (())() ()(()) ()()() 4: (((()))) ((()())) ((())()) ((()))() (()(())) (()()()) (()())() (())()() ()((())) ()(()()) ()(())() ()()(()) ()(())() ()()()() 23 December 2007 Ariel Shamir 52 26

Number of (N+2) Polygon Triangulations 23 December 2007 Ariel Shamir 53 Number of Groupings for Multiplication 1: (a b) 2: ((a b)c) (a(b c)) 3: (a(b(c d))) ((a(b c))d) (a((b c)d)) ((a b)(c d)) 4: (a (b (c (d e)))) (a (b ((c d) e))) (a ((b c) (d e))) (a ((b (c d)) e)) (a (((b c) d) e)) ((a b) (c (d e))) ((a b) ((c d) e)) ((a (b c)) (d e)) ((a (b (c d))) e) ((a ((b c) d)) e) (((a b) c) (d e)) (((a b) (c d)) e) (((a (b c)) d) e) ((((a b) c) d) e) 23 December 2007 Ariel Shamir 54 27

Catalan Numbers Recursion The base case is C 0 =1 For n>0 we have C n =Σ n k=1n (C k-1 *C n-k ) i.e. loop and sum previous Catalan numbers. 23 December 2007 Ariel Shamir 55 Catalan Example /** * Returns the n'th Catalan number. */ public static int catalan(int n) { if (n==0) return 1; int sum = 0; for (int k=1; k<=n; k++) { sum += catalan(k-1)*catalan(n-k); return sum; 23 December 2007 Ariel Shamir 56 28

Towers of Hanoi Example 23 December 2007 Ariel Shamir 57 Solution for 3 Rings 23 December 2007 Ariel Shamir 58 29

Recursive Solution a b c n-1: a b (using c) a b c a b c 1: a c (using b) n-1: b c (using a) 23 December 2007 Ariel Shamir 59 Towers of Hanoi Program // Simulates the Towers of Hanoi problem for a // given number of disks. private static void solve( Pile from, Pile to, Pile using, int n) { if (n==1) { to.adddisk(from.removedisk()); return; solve(from, using, to, n-1); solve(from, to, using, 1); solve(using, to, from, n-1); 23 December 2007 Ariel Shamir 60 30

Listing Files Example Suppose we want to list all the files under a given folder. We can do it as follows: List folders under folder f: Let L be the list of all files and folders directly under f. Recursion For each element fi in L : If fi is a folder : list folders under fi. Otherwise : print the name of fi. 23 December 2007 Ariel Shamir 61 Listing File Code import java.io.file; // List all the files under a given folder // Usage: java ListFiles <folder-name> class ListFiles { /** * List of the files under a given folder * given as an argument. */ public static void main(string[] args) { listfiles(new File(args[0])); 23 December 2007 Ariel Shamir 62 31

ListFiles Recursion Code // List all files under a given folder private static void listfiles(file folder) { String[] files = folder.list(); for (int i=0; i<files.length; i++) { File file = new File(folder, files[i]); if (file.isdirectory()) { listfiles(file); else { System.out.println(file); 23 December 2007 Ariel Shamir 63 Depth First Search Listing files is just an example of a well known algorithm for search on a graph, which is called depth first search. DFS first follows path up to their end (deep search) and then continues to other paths (breadth search). 23 December 2007 Ariel Shamir 64 32

DFS Example 23 December 2007 Ariel Shamir 65 Other Search Possibility? BFS: Breadth First Search 23 December 2007 Ariel Shamir 66 33

Indirect Recursion A method invoking itself is considered to be direct recursion. A method m1 could invoke another method m2, which invokes another m3, etc., until eventually the original method m1 is invoked again. This is called indirect recursion, and requires all the same care as direct recursion. It is often more difficult to trace and debug. 23 December 2007 Ariel Shamir 67 Fractals Hint 23 December 2007 Ariel Shamir 68 34

Hilbert Curve: Space Filling Curves X (count > 0? YFXFXFY : return ) Y (count > 0? XFYFYFX : return ) F (count > 0? F : draw) 23 December 2007 Ariel Shamir 69 How Does Recursion Work? If we examine again the implementation of factorial, the non- recursive solution describes the whole process of the computation. Instead of using variables inside the method, the return values are accumulating the product. 23 December 2007 Ariel Shamir 70 35

How Is 5! Computed? We call factorial(4) to compute 4! when the computation is finished the result (return value of factorial(4) ) is multiplied by 5 and is returned. How do we compute factorial(4) recursively you know the answer! 23 December 2007 Ariel Shamir 71 Recursive Solutions When trying to understand a recursive solution, or when seeking a recursive solution to a problem try to avoid thinking of the problem in this way. Assume that the method works for smaller inputs and try to understand how it uses this assumption to solve the problem with the current input. 23 December 2007 Ariel Shamir 72 36

Recursion Execution However, we do explain the process by which the computer executes a recursive method. This will reveal us more about the process of executing a program in general. 23 December 2007 Ariel Shamir 73 Executing a Program When we executes a Java program, the virtual machine creates a thread that processes the program code. The thread begins in the first statement of the main() method of our program, and advances according to the flow of the program until it reaches the end of main(). 23 December 2007 Ariel Shamir 74 37

Loading a Program Disk Program Code Memory 23 December 2007 Ariel Shamir 75 Java Program Execution Model Memory Garbage Collection Code Segment Heap Runtime Stack Commands Objects Functions Frames Program executing in memory 23 December 2007 Ariel Shamir 76 38

The Runtime Stack The thread uses a section of memory that is called its Runtime Stack. At the beginning the Runtime stack is empty. 23 December 2007 Ariel Shamir 77 Method Frames As the thread enters the main() method, it allocates a frame on the stack for the execution of main. The frame stores all the local variables defined in main etc. Main() 23 December 2007 Ariel Shamir 78 39

A Method Call If the thread encounters a method call, say to foo(),, it pushes a new frame on the stack for the variables of foo(). foo() Main() 23 December 2007 Ariel Shamir 79 A Method Return When the execution of the method foo()is finished (the method returns) the frame that was created for it is removed from the stack. foo() Main() 23 December 2007 Ariel Shamir 80 40

Many Method Calls If main calls m1 which calls m2 which calls m3, all three frames will be accumulated one on top of the other in the stack. m3() m2() m1() Main() 23 December 2007 Ariel Shamir 81 Other Method Calls m3 returns and m4 is called from m2 m4() m3() m2() m1() Main() 23 December 2007 Ariel Shamir 82 41

The Runtime Stack in Recursion public static long factorial(int n) { // return factorial(n-1)*n; The Runtime stack is filled by the frames of the same function called with different parameters factorial(n-2) factorial(n-1) factorial(n) Main() 23 December 2007 Ariel Shamir 83. Folding Back the Stack When the recursion reaches the base state (here n==1), The stack begins to unfold back by popping the frames out.. factorial(1) factorial(n-1) factorial(n) Main() 23 December 2007 Ariel Shamir 84 42

End of Recursion When it reaches the first call, all the calculations have been accumulated and the last call returns the result. factorial(n-1) factorial(n) Main() 23 December 2007 Ariel Shamir 85 43