t to a man with a hammer ith h everything looks like a
play

"T "To a man with a hammer, ith h everything looks like - PowerPoint PPT Presentation

Topic 9 Introduction to Recursion I t d ti t R i Underneath the Hood. "T "To a man with a hammer, ith h everything looks like a nail everything looks like a nail" -Mark Twain Mark Twain CS 307 Fundamentals of CS 307


  1. Topic 9 Introduction to Recursion I t d ti t R i Underneath the Hood. "T "To a man with a hammer, ith h everything looks like a nail everything looks like a nail" -Mark Twain Mark Twain CS 307 Fundamentals of CS 307 Fundamentals of 1 2 Computer Science Computer Science Introduction to Recursion Introduction to Recursion The Program Stack Methods for Illustration 200 public void someFooMethod(int z) 200 bli id F M th d(i t ) � When you invoke a method in your code 201{ int x = 2 * z; what happens when that method is 202 202 System.out.println(x); i l ( ) completed? } FooObject f = new FooObject(); int x = 3; 300 public void someBarMethod(int y) f.someFooMethod(x); 301 { 301 { int x = 3 * y; i 3 * f.someBarMethod(x); 302 someFooMethod(x); � How does that happen? pp 303 System.out.println(x); � What makes it possible? } CS 307 Fundamentals of 3 CS 307 Fundamentals of 4 Computer Science Computer Science Introduction to Recursion Introduction to Recursion

  2. The Program Stack Basic CPU Operations � A CPU works via a fetch � A CPU k i f t h � When your program is executed on a command / execute command processor the commands are converted into loop and a program counter loop and a program counter another set of instructions and assigned � Instructions stored in memory memory locations. (Just like data!) (Just like data!) – normally a great deal of expansion takes place 101 FooObject f = new FooObject(); 101 FooObject f = new FooObject(); j j (); 102 int x = 3; 102 int x = 3; 103 f.someFooMethod(x); 103 f.someFooMethod(x); 104 f 104 f.someBarMethod(x); B M th d( ) 104 f 104 f.someBarMethod(x); h d( ) � What if someFooMethod is stored at � Von Neumann Architecture memory location 200? memory location 200? CS 307 Fundamentals of CS 307 Fundamentals of 5 6 Computer Science Computer Science Introduction to Recursion Introduction to Recursion Activation Records and the More on the Program Stack Program Stack Program Stack 101 FooObject f = new FooObject(); � When a method is invoked all the relevant 102 int x = 3; i f information about the current method ti b t th t th d 103 f.someFooMethod(x); (variables, values of variables, next line of 104 f.someBarMethod(x); code to be executed) is placed in an d t b t d) i l d i � Line 103 is really saying go to line 200 with f activation record as the implicit parameter and x as the explicit � The activation record is pushed onto the parameter program stack � When someFooMethod is done what happens? pp � A stack is a data structure with a single A. Program ends B. goes to line 103 access point, the top. p , p C Goes back to whatever method called it C. Goes back to whatever method called it CS 307 Fundamentals of 7 CS 307 Fundamentals of 8 Computer Science Computer Science Introduction to Recursion Introduction to Recursion

  3. The Program Stack � Data may either be added ( pushed) or removed (popped) from top a stack but it is always Using Recursion Using Recursion f from the top. – A stack of dishes – which dish do we have easy access to? CS 307 Fundamentals of CS 307 Fundamentals of 9 10 Computer Science Computer Science Introduction to Recursion Introduction to Recursion A Problem Attendance Question 2 � Write a method that determines how much space is take up � W it th d th t d t i h h i t k � How many levels of directories have to be by the files in a directory visited? � A directory can contain files and directories A directory can contain files and directories A. 0 � How many directories does our code have to examine? � How would you add up the space taken up by the files in a B. Unknown U o single directory C. Infinite – Hint: don't worry about any sub directories at first � Directory and File classes � Directory and File classes D. 1 D 1 � in the Directory class: E. 8 public File[] getFiles() public Directory[] getSubdirectories() � in the File class public int getSize() bli i t tSi () CS 307 Fundamentals of 11 CS 307 Fundamentals of 12 Computer Science Computer Science Introduction to Recursion Introduction to Recursion

  4. Sample Directory Structure Code for getDirectorySpace() public int getDirectorySpace(Directory d) bli i t tDi t S (Di t d) { int total = 0; scottm File[] fileList = d.getFiles(); e[] e st d.get es(); for(int i = 0; i < fileList.length; i++) AP cs307 total += fileList[i].getSize(); Directory[] dirList = d.getSubdirectories(); m2.txt for(int i = 0; i < dirList.length; i++) m1.txt A.pdf total += getDirectorySpace(dirList[i]); t t l + tDi t S (di Li t[i]) AB.pdf hw return total; } a1.htm a2.htm a3.htm a4.htm CS 307 Fundamentals of CS 307 Fundamentals of 13 14 Computer Science Computer Science Introduction to Recursion Introduction to Recursion Attendance Question 3 Iterative getDirectorySpace() public int getDirectorySpace(Directory d) public int getDirectorySpace(Directory d) � Is it possible to write a non recursive method { ArrayList dirs = new ArrayList(); to do this? File[] fileList; Directory[] dirList; y A. Yes dirs.add(d); Directory temp; B. No int total = 0; while( ! dirs.isEmpty() ) hil ( ! di i () ) { temp = (Directory)dirs.remove(0); fileList = temp.getFiles(); for(int i 0; i < fileList.length; i++) for(int i = 0; i < fileList length; i++) total += fileList[i].getSize(); dirList = temp.getSubdirectories(); for(int i =0; i < dirList.length; i++) dirs.add( dirList[i] ); } return total; } } CS 307 Fundamentals of 15 CS 307 Fundamentals of 16 Computer Science Computer Science Introduction to Recursion Introduction to Recursion

  5. Wisdom for Writing Recursive Simple Recursion Examples Simple Recursion Examples Methods CS 307 Fundamentals of CS 307 Fundamentals of 17 18 Computer Science Computer Science Introduction to Recursion Introduction to Recursion Writing Recursive Methods The 3 plus 1 rules of Recursion � Rules of Recursion � R l f R i 1. Know when to stop 1. Base Case: Always have at least one case that 2. Decide how to take one step can be solved without using recursion can be solved without using recursion 3. Break the journey down into that step and a 2. Make Progress: Any recursive call must s smaller journey a e jou ey progress toward a base case. progress toward a base case. 3. "You gotta believe." Always assume that the 4. Have faith recursive call works. (Of course you will have to design it and test it to see if it works or prove From Common Lisp: A Gentle that it always works.) Introduction to Introduction to A recursive solution solves a small part of A recursive solution solves a small part of Symbolic Computation the problem and leaves the rest of the by David Touretzky problem in the same form as the original problem in the same form as the original CS 307 Fundamentals of 19 CS 307 Fundamentals of 20 Computer Science Computer Science Introduction to Recursion Introduction to Recursion

  6. N! Factorial Recursively � the classic first recursion problem / example � Mathematical Definition of Factorial 0! = 1 � N! N! = N * (N - 1)! 5! = 5 * 4 * 3 * 2 * 1 = 120 The definition is recursive. int res = 1; // pre n >= 0 for(int i = 2; i <= n; i++) public int fact(int n) res *= i; { if(n == 0) { ( ) return 1; else return n * fact(n-1); return n * fact(n 1); } CS 307 Fundamentals of CS 307 Fundamentals of 21 22 Computer Science Computer Science Introduction to Recursion Introduction to Recursion Big O and Recursion Common Recurrence Relations � T(N) = T(N/2) + O(1) -> O(logN) � T(N) T(N/2) + O(1) > O(l N) � Determining the Big O of recursive methods – binary search can be tricky. � T(N) = T(N-1) + O(1) -> O(N) T(N) = T(N-1) + O(1) -> O(N) � A recurrence relation exits if the function is – sequential search, factorial defined recursively. � T(N) = T(N/2) + T(N/2) + O(1) -> O(N), ( ) ( ) ( ) ( ) ( ), � The T(N), actual running time, for N! is – tree traversal � T(N) = T(N-1) + O(N) -> O(N^2) recursive ecu s e – selection sort � T(N) fact = T(N-1) fact + O(1) � T(N) = T(N/2) + T(N/2) + O(N) -> O(NlogN) � This turns out to be O(N) � This turns out to be O(N) – merge sort t � T(N) = T(N-1) + T(N-1) + O(1) -> O(2^N) – There are N steps involved – Fibonacci – Fibonacci CS 307 Fundamentals of 23 CS 307 Fundamentals of 24 Computer Science Computer Science Introduction to Recursion Introduction to Recursion

  7. Tracing Fact With the Calling fact with 4 Program Stack Program Stack System.out.println( fact(4) ); System.out.println( fact(4) ); 4 4 in method fact in method fact n partial result = n * fact(n-1) top System.out.println( fact(4) ); top System.out.println( fact(4) ); CS 307 Fundamentals of CS 307 Fundamentals of 25 26 Computer Science Computer Science Introduction to Recursion Introduction to Recursion Calling fact with 3 Calling fact with 2 2 in method fact n partial result = n * fact(n-1) 3 in method fact 3 in method fact in method fact n n n n top top partial result = n * fact(n-1) partial result = n * fact(n-1) 4 4 in method fact i th d f t 4 4 in method fact i h d f n n top partial result = n * fact(n-1) partial result = n * fact(n-1) System.out.println( fact(4) ); System.out.println( fact(4) ); CS 307 Fundamentals of 27 CS 307 Fundamentals of 28 Computer Science Computer Science Introduction to Recursion Introduction to Recursion

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend