Professional Documents
Culture Documents
}
2
RECURSION IN ALGORITHMS
WHAT IS RECURSION?
A Definition Is Recursive If It Is Defined In
Terms Of Itself
a noun
Descendants
WHAT IS RECURSION?
Think
self-referential definition
if y is 0, then 1
otherwise
its x * (x raised to the y-1 power)
St
ro
ng
ly
Di
s
is
a
ag
re
e
gr
ee
gr
ee
15%
4.
28%
25%
Ag
re
e
3.
33%
ly
2.
Strongly Agree
Agree
Disagree
Strongly Disagree
St
ro
ng
1.
RECURSIVE FACTORIAL
RECURSIVE DESIGN
Recursive methods/functions require:
1) One or more (non-recursive) base cases
that will cause the recursion to end.
if (n == 1) return 1;
2) One or more recursive cases that operate
on smaller problems and get you closer to the
base case.
return n * factorial(n-1);
Note: The base case(s) should always be
checked before the recursive call.
11
exponentiation (x,y)
12
DESIGNING ALGORITHMS
RECURSION
A recursive method is a method that contains a call
to itself
Technically:
Conceptually:
This allows programming in a style that reflects divide-nconquer algorithmic thinking
At the beginning recursive programs are confusing
after a while they become clearer than non-recursive
variants
FACTORIAL
public static void Factorial {
public static void main() {
System.out.println(5!= + factorial(5));
}
public static long factorial(int n){
if (n == 0)
return 1;
else
return n * factorial(n-1);
}
if (n==0) return 1;
In our case:
ANALYSIS
n
x
Theorem: For any x and positive integer n, the power
method returns .
Proof: by complete induction on n.
Basis: For n=0, we return 1.
If n is even, we return power(x,n/2)*power(x,n/2).
By then / 2 2
n/2
x ) xn
induction hypothesis power(x,n/2) x
returns
, so we (return
.
If n is odd, we return x*power(x,n-1).
By the induction
n 1
n
n 1
x
x
x
hypothesis power(x,n-1) returns
, so we return
.
public
class Reverse {
EVERSE
static InputRequestor in = new InputRequestor():
public static void main(String[] args) {
printReverse();
}
public static void printReverse() {
int j = in.requestInt(Enter another Number+
(0 for end of list):);
if (j!=0){
printReverse();
System.out.println(j);
}
}
}
RECURSIVE DEFINITIONS
Many things are defined recursively.
Fibonaci Numbers: 1, 1, 2, 3, 5, 8, 13, 21,
fn = fn-1 + fn-2
Fractals
long fib(int n) {
if ( n == 0 ) return 1;
if ( n == 1 ) return 1;
return fib(n-1) + fib(n-2);
}
Lets start to trace it for fib(6)
25
TRACE OF FIB(5)
26
27
NON-RECURSIVE FIBONACCI
28
ITERATIVE FIBONACCI
long fib(int n) {
if ( n < 2 ) return 1;
long answer;
long prevFib=1, prev2Fib=1; // fib(0) & fib(1)
for (int k = 2; k <= n; ++k) {
answer = prevFib + prev2Fib;
prev2Fib = prevFib;
prevFib = answer;
}
return answer;
}
29
Its form
Examples:
Binary Search
Merge Sort
30
FIBONACI NUMBERS
public class Fibonaci {
public static void main(String[] args)
for(int j = 1 ; j<20 ; j++)
System.out.println(fib(j));
}
public static int fib (int n) {
if (n <= 1) return 1;
return fib(n-1) + fib(n-2);
}
}
TURTLEFRACTAL
public class TurtleFractal {
static Turtle turtle = new Turtle();
public static void main(String[] args)
turtle.tailDown();
drawFractal(500,4);
}
public static void drawFractal(int
if (level==0)
turtle.moveForward(length);
else {
drawFractal(length/3, level-1)
turtle.turnLeft(60);
drawFractal(length/3, level-1)
turtle.turnRight(120);
drawFractal(length/3, level-1)
turtle.turnLeft(60);
drawFractal(length/3, level-1)
}
}}
;
;
;
;
FRACTALTURTLE OUTPUT
34
35
36
Factorial
Binary
search
Fibonacci
This
else
divide I into I1, , Ik.
for each i in {1, , k}
Si = solve(Ii);
solution = combine(S1, , Sk);
return solution;
39
But,
We
TOP-DOWN STRATEGY
Divide
Most
41
first
mid
last
Strategy
Find the midpoint of the array
Is target equal to the item at midpoint?
If smaller, look in the first half
If larger, look in second half
42
45
ALGORITHM: MERGESORT
Specification:
Algorithm:
return;
46
A = {8, 3, 2, 9, 7, 1, 5, 4};
47
EFFICIENCY OF MERGESORT
Wait
The
Given two sequences A and B sorted in nondecreasing order, merge them to create one sorted
sequence C
Input size: C has n items, and A and B have n/2
Strategy:
ALGORITHM: MERGE
merge(A, B, C)
if (A is empty)
append whats left in B to C
else if (B is empty)
append whats left in A to C
return
50
51
Rules
52
Class
exercise:
53