1 / 16

Recursion: Function and Programming

Software Engineering at Azusa Pacific University. Recursion: Function and Programming. This is an example in regard to programming and algorithm. Here I took part of material from my previous teaching.  Evolutionary Approach  Examples and Algorithms  Programming in C++

nehru-garza
Download Presentation

Recursion: Function and Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Software Engineering at Azusa Pacific University Recursion: Function and Programming This is an example in regard to programming and algorithm. Here I took part of material from my previous teaching. Evolutionary Approach Examples and Algorithms Programming in C++ Recursion and Iteration Summary 1 November 21, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/

  2. = = = = + + Span Past Present Solvable Unsolved = = + Software Engineering at Azusa Pacific University Recursion: Function and Programming Evolutionary Approach (Present = Past + Span) Let’s learn from math (break-down / gather-up) Gather-up Break-down 2 November 21, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/

  3. Software Engineering at Azusa Pacific University Recursion: Function and Programming Examples and Algorithms (typical pattern) A function calls to itself (directly or indirectly) funcT RunBack (T timeTag) { if (bEarlyEnough(timeTag)) // termination return Answer; else // recursive approach return RunBack (DeTimeTag) + Span (timeTag); } With recursively thinking and programming, we do break-down-into-past first, then gather-up-to-present 3 November 21, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/

  4. Software Engineering at Azusa Pacific University Recursion: Function and Programming Examples and Algorithms (Factorial) Assume we need to do factorial. We use the number as time tag, so that i refers to the present while i-1 to the previous (or past). The recursive function calling itself fulfils recursion /* Fact = 1 * 2 * (i-1) * i * … 8 * * Fact (i) = Fact(i-1) * i * * Past Span *==== How the recursion works is described as follows ===== * Fact = Fact(7) * 8 * └ fact(6) * 7 * └  Fact(5) * 6 * … …. * └ fact(0) *1 Break-down Gather-up  8! = (7!)*8 = 40320  7! = (6!)*7 = 5040  6! = (5!)*4 =720  1 */ 4 November 21, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/

  5. Software Engineering at Azusa Pacific University Recursion: Function and Programming Evolutionary Approach Examples and Algorithms Programming in C++ Recursion and Iteration Summary 5 November 21, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/

  6. Software Engineering at Azusa Pacific University Recursion: Function and Programming Programming in C++ (Factorial) Recall that the Fibonacci numbers are defined recursively: However, the algorithm used in left program  is non-recursive --it is iterative, while in right program we use the definition of Fibonacci numbers to implement directly a recursive algorithm. /*===== iterative implementation === * ?? Where is intuitive definition?? */ int IterativeFact (int n) { int previous = -1, result = 0; for (int i=0; I <= n; i++) { int const sum = result + previous; previous = result; result = sum; } return result; } /* ==== recursive function ======= * !!What a intuitive mapping !! * Fn= Fn-1 * n * * (1) (2) (3) */ long Fact (int n) // n is time tag { if (n == 1) // Termination return n; else // recurring back return Fact (n-1) * n; } 6 November 21, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/

  7. Software Engineering at Azusa Pacific University Recursion: Function and Programming Evolutionary Approach Examples and Algorithms Programming in C++ Recursion and Iteration Summary 7 November 21, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/

  8. /* ======Recursive Process=====*/ int main () { int ret = Fact(8); // Initialization } long Factint n) // n is time tag { if (i == 1) // Termination return 1; else return Fact(n-1) * n; // Decrease } /* ===== Iterative Process======*/ int main () { long ret= 1; // Initialization while (n>0) // Termination { ret *= n; n--; // Decrease } } Software Engineering at Azusa Pacific University Recursion: Function and Programming Recursion and Iteration • Life cycle issues (logic equivalence)  Tree elements for both iteration and recursion • Initialization / Termination / Decrease 8 November 21, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/

  9. Software Engineering at Azusa Pacific University Recursion: Function and Programming Recursion and Iteration • Life cycle issues (logic equivalence)  Logic equivalent solution (I/T/D) /* General recursive function * Fn = Fn-1 + Sn * *========= * Fact = Fact(7) * 8 * └ fact(6) * 7 * └  Fact(5) * 6 * └ Fact(4) * 5 * └ Fact(3) * 4 * └ Fact(2) * 3 * └ Fact(1) * 2 * └ 1 *1 Break down to past Gather up to present = 40320 = 5040 = 720 = 120 = 24 = 6 = 2 = 1 2 8 7 3 6 4 5 5 4 6 3 7 2 8 9 November 21, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/

  10. Software Engineering at Azusa Pacific University Recursion: Function and Programming Recursion and Iteration • Life cycle issues (logic equivalence)  Logic equivalent solution (I/T/D) /* General recursive function * Fn = Fn-1 + Sn * ========= Long ret = 1 for (i=8; I > 0; i--) { // descending to past push (I, stack) } For (i=8; i > 0; i --) { // ascending to present ret *= pop (stack) } = 40320 = 5040 = 720 = 120 = 24 = 6 = 2 = 1 8 2 7 3 6 4 5 5 4 6 3 2 7 8 10 November 21, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/

  11. Software Engineering at Azusa Pacific University Recursion: Function and Programming Recursion and Iteration • Life cycle issues (logic equivalence)  Logic equivalent solution (I/T/D) By means of stack, we can always find a iterative equivalence to any recursive algorithm. However some iterative solution will be very difficult to understand, because it loses straightforwardness 11 November 21, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/

  12. Software Engineering at Azusa Pacific University Recursion: Function and Programming Recursion and Iteration • Life cycle issues (Initialization / Termination / Decrease) • Tricky exercise between recursion and iteration  Iteration provides plane algorithm  Complicated thinking thread  Efficient running (just iteratively going through)  Non-straightforward in description (twisty)  Recursion provides solid algorithm  Simplified thought (KISS)  Inefficient running (procedural call)  Straightforward (intuitive in math) 12 November 21, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/

  13. Software Engineering at Azusa Pacific University Recursion: Function and Programming Recursion and Iteration • Life cycle issues (Initialization / Termination / Decrease) • Tricky exercise between recursion and iteration • Mechanical conversion from recursion to iteration  Converted by compilers and algorithm designer  Keep it simplified (for man to write)  Auto complicated (for machine to convert)  Efficiency improved (for application) Go do right thing with recursive design because keeping it simple is really wise, while taking advantage of computer for conversion if necessary 13 November 21, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/

  14. Software Engineering at Azusa Pacific University Recursion: Function and Programming Evolutionary Approach Examples and Algorithms Programming in C++ Recursion and Iteration Summary 14 November 21, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/

  15. Software Engineering at Azusa Pacific University Recursion: Function and Programming Summary of Recursion • Recursive thinking:  Mathematic intuition (straightforwardness)  Be wise: looking back prior to looking ahead • Recursive programming  Function calls itself, directly or indirectly  Remember this: the past is the key to the present • Recurring back:  If complicated, break down to past  Conquer the parts, then gather up to present 15 November 21, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/

  16. Software Engineering at Azusa Pacific University 16 November 21, 2014 Azusa Pacific University, Azusa, CA 91702,Tel: (800) 825-5278 Department of Computer Science,http://www.apu.edu/clas/computerscience/

More Related