Recursion: Function and Programming - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Recursion: Function and Programming

Description:

Evolutionary Approach (Present = Past + Span) Let s learn from math ... the past is the key to the present Recurring back: If complicated, ... – PowerPoint PPT presentation

Number of Views:31
Avg rating:3.0/5.0
Slides: 17
Provided by: shel103
Category:

less

Transcript and Presenter's Notes

Title: Recursion: Function and Programming


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
August 15, 2015
Azusa Pacific University, Azusa, CA 91702, Tel
(800) 825-5278 Department of Computer
Science, http//www.apu.edu/clas/computerscience/
2
Software Engineering at Azusa Pacific University
Recursion Function and Programming
? Evolutionary Approach (Present Past Span)
Lets learn from math (break-down / gather-up)
Gather-up
Break-down
2
August 15, 2015
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
August 15, 2015
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
August 15, 2015
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
August 15, 2015
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 i0 I lt 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
August 15, 2015
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
August 15, 2015
Azusa Pacific University, Azusa, CA 91702, Tel
(800) 825-5278 Department of Computer
Science, http//www.apu.edu/clas/computerscience/
8
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
August 15, 2015
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
August 15, 2015
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 (i8
I gt 0 i--) // descending to past
push (I, stack) For (i8 i gt 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
August 15, 2015
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
August 15, 2015
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
August 15, 2015
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
August 15, 2015
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
August 15, 2015
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
August 15, 2015
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
August 15, 2015
Azusa Pacific University, Azusa, CA 91702, Tel
(800) 825-5278 Department of Computer
Science, http//www.apu.edu/clas/computerscience/
Write a Comment
User Comments (0)
About PowerShow.com