7/31/2019 Non Recursive
1/52
7/31/2019 Non Recursive
2/52
MaxElement
Finding the value of the largest element inthe list of n numbers
MaxElement(A[1..n])maxvalA[1]
for i 2 to n do
ifA[i] > maxval
maxvalA[i]
return maxval
7/31/2019 Non Recursive
3/52
Complexity of Algorithm The Loop is repeated n-1 times so
7/31/2019 Non Recursive
4/52
General Plan for Analyzing Timefficiency ofNonrecursive Algorithms
1. Decide on a parameter(or parameters) indicating an inputs size. 2.Identify the algorithms basic operation.(As a rule, it is located in its
innermost loop)
3.Check whether the number of times the basic operation is executed
depends only on the size of an input. 4.Set up a sum expressing the number of times the algorithms basic
operation is executed.
5.Using standard formulas and rules of sum manipulation, either find a
closed form formula for the count or, at the very least, establish its
order of growth.
7/31/2019 Non Recursive
5/52
UniqueElement
UniqueElement(A[1..n])
for i 1 to n-1 do
forj i+1 to n do{
ifA[i] = A[j]
return false}
return true
7/31/2019 Non Recursive
6/52
complexity
7/31/2019 Non Recursive
7/52
7/31/2019 Non Recursive
8/52
Fibonacci series
Algorithm Fibonacci(n)
{
if n
7/31/2019 Non Recursive
9/52
7/31/2019 Non Recursive
10/52
7/31/2019 Non Recursive
11/52
recursion What is recursion?When one function
calls ITSELF directly orindirectly.
Recursion means
defining something,such as a function, in
terms of itself
For example, letf(x) =x! We can definef(x) as
f(x) =x * f(x-1)
7/31/2019 Non Recursive
12/52
Recursion outside Computer Science
Names PHP : PHP Hypertext Preprocessor
GNU : GNU is Not Unix
Mathematics Definition of the set of non negative numbers N
0 is in N
If n is in N, then n+1 is in N.
Images
Fractals
7/31/2019 Non Recursive
13/52
13
Fractals
A fractal is a pattern that uses recursion The pattern itself repeats indefinitely
7/31/2019 Non Recursive
14/52
14
Fractals
7/31/2019 Non Recursive
15/52
Onion Model of RecursiveNaturals
0 s(0) = 1
s(s(0)) = 2s(s(s(0))) = 3
Every Onion is either
Base Case : The core
Recursive Case: A layer of skin around a smaller onion
(which may be the core, or some deeper layer)
The value of an onion is the number of layers of skin beyond the core
7/31/2019 Non Recursive
16/52
Meaning of recursion
To recur means to happen again. In computer science, we say that a
subroutine (function or procedure) is
recursive when it calls itself one or moretimes.
We call the programming technique by
using recursive subroutine(s) as recursion.
7/31/2019 Non Recursive
17/52
Recursive Function The recursive function is
a kind of function that calls itself, or a function that is part of a cycle in the sequence of
function calls.
f1 f1 f2 fn
7/31/2019 Non Recursive
18/52
Problems Suitable for RecursiveFunctions
One or more simple cases of the problem have a
straightforward solution. The other cases can be redefined in terms of
problems that are closer to the simple cases.
The problem can be reduced entirely to simplecases by calling the recursive function.
If this is a simple case
solve itelseredefine the problem using recursion
7/31/2019 Non Recursive
19/52
Splitting a Problem into SmallerProblems
Assume that the problem of size 1 can be solvedeasily (i.e., the simple case).
We can recursively split the problem into aproblem of size 1 and another problem of size n-1.
7/31/2019 Non Recursive
20/52
General Form of Recursive methodsSolve(Problem)
{
if (Problem is minimal/not decomposable: a base case)solve Problem directly; i.e., without recursion
else {
(1) Decompose Problem into one or more similar,
strictly smaller subproblems: SP1, SP2, ... , SPN(2) Recursively call Solve (this method) on each
subproblem: Solve(SP1), Solve(SP2),..., Solve(SPN)
(3) Combine the solutions to these subproblems into asolution that solves the original Problem}
}
7/31/2019 Non Recursive
21/52
Defining sets via recursion
Same two parts: Base case (or basis step)
Recursive step
Example: the set of positive integers
Basis step: 1 S Recursive step: ifx S, thenx+1 S
7/31/2019 Non Recursive
22/52
Defining sets via recursion
recursive definitions for:a) The set of odd positive integers
1 S
Ifx S, thenx+2 S
b) The set of positive integer powers of 3
3 S
Ifx S, then 3*x S
7/31/2019 Non Recursive
23/52
Recurrence in mathematical functions
Factorial
_
Permutation
_
Combination _
Pascal relationship
_
Integer power
_
)!1(! = nnn
rnrn CrnC 1)/( =
rnrnrn CCC 111 +=
rnrn PnP 1=
1=
nnxxx
7/31/2019 Non Recursive
24/52
Structure of recursion Similar to mathematic induction, recursion requires the
following two components Recurrence relation(s)
Base case(s)
Example
F(n)
IF (n=0) RETURN 1 //Base Case
ELSE RETURN n*F(n-1) //Recurrence Relation
7/31/2019 Non Recursive
25/52
Recurrence Relations
1. Base Case: the initial condition or basis which defines the first (or first few) elements of the
sequence
2. Inductive (Recursive) Case: an inductive step in which later terms in the sequence are defined interms of earlier terms.
A recurrence relation for a sequence a1, a2, a3, ... is a formula that relates each term a k to
certain of its predecessors ak-1, ak-2, ..., a k-i, where i is a fixed integer and kis any integer
greater than or equal to i. The initial conditions for such a recurrence relation specify the
values of a1, a2, a3, ..., ai-1.
7/31/2019 Non Recursive
26/52
Recursive Definition
We can also define the factorial function inthe following way:
7/31/2019 Non Recursive
27/52
Recursive Algorithm
factorial(n) {if (n = 0)
return 1
else
return n*factorial(n-1)
end if
}
7/31/2019 Non Recursive
28/52
How Recursion Works
To truly understand how recursion workswe need to first explore how any function
call works.
When a program calls a subroutine
(function) the current function must suspend
its processing.
The called function then takes over control
of the program.
7/31/2019 Non Recursive
29/52
How Recursion Works
When the function is finished, it needs toreturn to the function that called it.
The calling function thenwakes upand
continues processing.
7/31/2019 Non Recursive
30/52
Recursion
To see how the recursion works, lets break
down the factorial function to solve
factorial(3)
7/31/2019 Non Recursive
31/52
Breakdown
Here, we see that we start at the top level,factorial(3), and simplify the problem into3 x factorial(2).
Now, we have a slightly less complicated problemin factorial(2), and we simplify this problem into 2x factorial(1).s
7/31/2019 Non Recursive
32/52
Breakdown
We continue this process until we are able to reach aproblem that has a known solution.
In this case, that known solution is factorial(0) = 1.
The functions then return in reverse order to completethe solution.
7/31/2019 Non Recursive
33/52
The basic operation of the algorithm is
multiplication, whose number of executions
we denote M(n).
Since the function F(n) is computed
according to the formula
F(n)=F(n-1).n for n>0
Complexity O(n)
7/31/2019 Non Recursive
34/52
Fibonacci Numbers
The Fibonacci numbers, a famous sequence 0,1,1,2,3,5,8,13,21,34..
That can be defined by the simple recurrence
7/31/2019 Non Recursive
35/52
fibonacci
int Fibonacci (int n) {if ( (n == 1) || (n == 2) )
return 1;
else
return Fibonacci (n-1) +
Fibonacci (n-2);}
7/31/2019 Non Recursive
36/52
36
Recursive power example
Write method pow that takes integers x and y asparameters and returns xy.
xy = x * x * x * ... * x (y times, in total)
An iterative solution:
int pow(int x, int y) {int product = 1;for (int i = 0; i < y; i++)
product = product * x;return product;
}
7/31/2019 Non Recursive
37/52
37
Recursive power function
Another way to define the power function:
pow(x, 0) = 1pow(x, y) = x * pow(x, y-1), y > 0
int pow(int x, int y) {
if (y == 0)
return 1;
elsereturn x * pow(x, y - 1);
}
7/31/2019 Non Recursive
38/52
38
How recursion works
each call sets up a new instance of all the
parameters and the local variables
as always, when the method completes, controlreturns to the method that invoked it (which might
be another invocation of the same method)
pow(4, 3) = 4 *pow(4, 2)= 4 * 4 *pow(4, 1)= 4 * 4 * 4 *pow(4, 0)= 4 * 4 * 4 * 1
= 64
7/31/2019 Non Recursive
39/52
39
Infinite recursion
a definition with a missing or badly written base casecauses infinite recursion, similar to an infinite loop avoided by making sure that the recursive call gets closer to
the solution (moving toward the base case)
int pow(int x, int y) {
return x * pow(x, y - 1); // Oops! Forgot basecase
}
pow(4, 3) = 4 *pow(4, 2)= 4 * 4 *pow(4, 1)
= 4 * 4 * 4 *pow(4, 0)= 4 * 4 * 4 * 4 * pow(4, -1)= 4 * 4 * 4 * 4 * 4 * pow(4, -2)= ... crashes: Stack Overflow Error!
7/31/2019 Non Recursive
40/52
How C Maintains the RecursiveSteps
C keeps track of the values of variables by the
stack data structure. Recall that stack is a data structure where the last
item added is the first item processed.
There are two operations (push and pop) associatedwith stack.
ab
c
b
c
db
c
pop push d
7/31/2019 Non Recursive
41/52
How C Maintains the Recursive Steps
Each time a function is called, the executionstate of the caller function (e.g., parameters,
local variables, and memory address) are pushedonto the stack.
When the execution of the called function is
finished, the execution can be restored bypopping up the execution state from the stack.
This is sufficient to maintain the execution of the
recursive function. The execution state of each recursive step are stored
and kept in order in the stack.
7/31/2019 Non Recursive
42/52
42
Activation records
activation record: memory allocates to store
information about each running method return point ("RP"), argument values, local variablevalues
stacks up the records as methods are called; amethod's activation record exists until it returns
drawing the act. records helps us trace the behaviorof a recursive method_
| x = [ 4 ] y = [ 0 ] | pow(4, 0)| RP = [pow(4,1)] || x = [ 4 ] y = [ 1 ] | pow(4, 1)| RP = [pow(4,2)] || x = [ 4 ] y = [ 2 ] | pow(4, 2)| RP = [pow(4,3)] |
| x = [ 4 ] y = [ 3 ] | pow(4, 3)| RP = [main] || | main
7/31/2019 Non Recursive
43/52
Iterative vs. Recursive
Iterative 1 if n=0factorial(n) =
n x (n-1) x (n-2) x x 2 x 1 if n>0
Recursive
factorial(n) =
1 if n=0n x factorial(n-1) if n>0
Function calls itself
Function does NOT
calls itself
7/31/2019 Non Recursive
44/52
44
Recursion vs. iteration
every recursive solution has a correspondingiterative solution For example, N! can be calculated with a loop
Recursion
Uses more storage than iteration Runs slower due to runtime overhead
however, for some problems recursive solutions areoften more simple and elegant than iterative
solutions you must be able to determine when recursion is
appropriate
7/31/2019 Non Recursive
45/52
The main benefits of using recursion as aprogramming technique are these:
invariably recursive functions are clearer, simpler,
shorter, and easier to understand than their non-recursive counterparts.
the program directly reflects the abstract solutionstrategy (algorithm).
Recursion works the best when the algorithm and/ordata structure that is used naturally supports recursion.One such data structure is the tree
From a practical software engineering point ofview these are important benefits, greatlyenhancing the cost of maintaining the software.
7/31/2019 Non Recursive
46/52
Limitations of Recursion
In general, recursive algorithms run slowerthan their iterative counterparts.
Also, every time we make a call, we must
use some of the memory resources to makeroom for the stackframe.
7/31/2019 Non Recursive
47/52
7/31/2019 Non Recursive
48/52
Limitations of Recursion
Consider the recursive Fibonacci generator
How many recursive calls does it make? F(1): 1 F(2): 1 F(3): 3 F(4): 5 F(5): 9
F(10): 109 F(20): 13,529 F(30): 1,664,079 F(40): 204,668,309 F(50): 25,172,538,049 F(100): 708,449,696,358,523,830,149 7 * 1020
At 1 billion recursive calls per second (generous), this would take over22,000 years But that would also take well over 1012 Gb of memory!
7/31/2019 Non Recursive
49/52
Limitations of Recursion
Therefore, if the recursion is deep, say,factorial(1000), we may run out of memory.
Because of this, it is usually best to develop
iterative algorithms when we are workingwith large numbers.
7/31/2019 Non Recursive
50/52
Empirical Analysis of Algorithm
7/31/2019 Non Recursive
51/52
Empirical analysis of timeefficiency
Select a specific (typical) sample of inputs
Use physical unit of time (e.g.,
milliseconds)
or
Count actual number of basic operationsexecutions
7/31/2019 Non Recursive
52/52
Algorithm Visualization
Static Algorithm Visualization (series ofstill images)
Dynamic Algorithm Visualization
(Animation)