Lecture 07 - Recursion
Lecture 07 - Recursion
International University
School of Computer Science and Engineering
T UE SD AY , 2 2 O CT O BE R 2 0 24 2
Objectives
T UE SD AY , O CT O BE R 2 2 , 2 02 4 D A TA ST R U C T U R E S A N D A L G O RI T H M S IN JA V A 3
T UE SD AY , 2 2 O CT O BE R 2 0 24 4
Triangle Numbers: Examples
T UE SD AY , 2 2 O CT O BE R 2 0 24 5
Finding nth
Term using
a Loop
T UE SD AY , O CT O BE R 2 2 , 2 02 4 6
T UE SD AY , 2 2 O CT O BE R 2 0 24 7
Finding nth Term using Recursion
• Value of the nth term is the SUM of:
• The first column (row): n
• The SUM of the rest columns (rows)
T UE SD AY , 2 2 O CT O BE R 2 0 24 8
Recursive method
T UE SD AY , 2 2 O CT O BE R 2 0 24 9
Recursive method
T UE SD AY , 2 2 O CT O BE R 2 0 24 10
Definition
T UE SD AY , 2 2 O CT O BE R 2 0 24 D A TA ST R U C T U R E S A N D A L G O RI T H M S IN JA V A 11
Characteristics of Recursive Program/ Algorithms
T UE SD AY , 2 2 O CT O BE R 2 0 24 D A TA ST R U C T U R E S A N D A L G O RI T H M S IN JA V A 12
Recursion Characteristics
• There is some version of the problem that is simple enough that the
routine can solve it, and return, without calling itself
• Is recursion efficient?
• No
• Address of calling methods must be remembered (in stack)
• Intermediate arguments must also be stored
T UE SD AY , 2 2 O CT O BE R 2 0 24 13
Example: Factorials
T UE SD AY , 2 2 O CT O BE R 2 0 24 14
Computing factorial by simple iteration
• See Factorial1.java
T UE SD AY , 2 2 O CT O BE R 2 0 24 15
Factorials
T UE SD AY , 2 2 O CT O BE R 2 0 24 16
Computing factorial by recursion
• See Factorial2.java
• Computing factorial by simulating recursion using a stack
• Factorial3.java
T UE SD AY , 2 2 O CT O BE R 2 0 24 17
Recursion & stack
T UE SD AY , 2 2 O CT O BE R 2 0 24 18
Method calls and recursion implementation
T UE SD AY , 2 2 O CT O BE R 2 0 24 19
Method calls and recursion implementation
• Each new activation record is placed on the top of the run-time stack
• When a method terminates, its activation record is removed from the
top of the run-time stack
• Thus, the first AR placed onto the stack is the last one removed.
T UE SD AY , 2 2 O CT O BE R 2 0 24 20
TUESDAY , OCTOBER 22 ,
202 4 D A TA ST R U C T U R E S A N D A L G O RI T H M S IN JA V A 21
Anatomy of a recursive call
T UE SD AY , 2 2 O CT O BE R 2 0 24 22
Binary Search: Recursion vs. Loop
T UE SD AY , 2 2 O CT O BE R 2 0 24 23
Binary Search: Recursion vs.
Loop
T UE SD AY , 2 2 O CT O BE R 2 0 24 24
Recursive binary search implementation
• BinarySearchApp.java
• Trace the recursion by printing lowerBound and upperBound at each
call and exit
T UE SD AY , 2 2 O CT O BE R 2 0 24 25
Divide-and-conquer
T UE SD AY , 2 2 O CT O BE R 2 0 24 26
Towers of Hanoi
T UE SD AY , 2 2 O CT O BE R 2 0 24 27
Algorithm
T UE SD AY , 2 2 O CT O BE R 2 0 24 29
Implementation
T UE SD AY , 2 2 O CT O BE R 2 0 24 30
Implementation
• TowersApp.java
• Include a counter and print the number of recursive steps for
different number of disks
T UE SD AY , 2 2 O CT O BE R 2 0 24 31
Classification of recursive functions by number
of recursive calls
▪ Considering the maximum number of recursive calls that may be
started from within the body of a single activation:
▪ Linear recursion: Only 1 recursive call (to itself) inside the recursive
function (e.g., binary search, factorial).
▪ Binary recursion: There exactly 2 recursive calls (to itself) inside the
recursive function (e.g., Fibonacci number) .
▪ Multiple recursion: There are 3 or more recursive calls (to itself)
inside the recursive function (e.g., "Sierpinski triangle").
T UE SD AY , 2 2 O CT O BE R 2 0 24 32
Tail recursion
T UE SD AY , 2 2 O CT O BE R 2 0 24 33
Indirect recursion
public class IndirectRecursionExample {
▪ If f() calls itself, it is direct public static void functionA(int n) {
recursive
if (n > 0) {
System.out.print(n + " ");
functionB(n - 1);
}
▪ If f() calls g(), and g() calls f(). It is }
T UE SD AY , 2 2 O CT O BE R 2 0 24 34
Nested recursion
▪ A function is not only defined in terms of itself but also is used as one
of the parameters
▪ Examples: Ackermann function
▪ A(0, y) = y + 1
▪ A(x, 0) = A(x - 1, 1)
▪ A(x, y) = A(x - 1, A(x, y - 1))
This function is interesting because of its value
grows rapidly, even for small inputs.
A(3,1) = 24 - 3
A(4,1) = 265536 - 3
T UE SD AY , 2 2 O CT O BE R 2 0 24 35
Eliminate recursion
T UE SD AY , 2 2 O CT O BE R 2 0 24 36
Example: Fibonacci sequence
T UE SD AY , 2 2 O CT O BE R 2 0 24 D A TA ST R U C T U R E S A N D A L G O RI T H M S IN JA V A 37
1. public class FibonacciSequence {
Example: Fibonacci
2.
3.
public static int fibonacci(int n) {
if (n <= 0) {
sequence
4. return 0; Execution:
5. } else if (n == 1) {
6. return 1;
fibonacci(0): 0
7. } else {
fibonacci(1): 1
8. return fibonacci(n - 1) + fibonacci(n - 2);
fibonacci(2): 1
9. }
10. }
fibonacci(3): 2
15. } fibonacci(8): 21
16. } fibonacci(9): 34
17. }
fibonacci(10): 55
T UE SD AY , 2 2 O CT O BE R 2 0 24 D A TA ST R U C T U R E S A N D A L G O RI T H M S IN JA V A 38
Excessive recursion
• Some recursive methods repeats the computations for some parameters, which
results in long computation time even for simple cases.
• For example, consider the Fibonacci sequence.
• In Java it can be implemented recursively as:
T UE SD AY , 2 2 O CT O BE R 2 0 24 39
Excessive recursion
The tree of calls for fibo(4)
T UE SD AY , 2 2 O CT O BE R 2 0 24 40
Excessive recursion
T UE SD AY , 2 2 O CT O BE R 2 0 24 D A TA ST R U C T U R E S A N D A L G O RI T H M S IN JA V A 42
More Examples – Drawing fractals
T UE SD AY , 2 2 O CT O BE R 2 0 24 D A TA ST R U C T U R E S A N D A L G O RI T H M S IN JA V A 43
More Examples – Von Knoch snowflakes
T UE SD AY , 2 2 O CT O BE R 2 0 24 D A TA ST R U C T U R E S A N D A L G O RI T H M S IN JA V A 44
More Examples – Sierpinski Triangle
T UE SD AY , 2 2 O CT O BE R 2 0 24 D A TA ST R U C T U R E S A N D A L G O RI T H M S IN JA V A 45
Recursion vs. Iteration
T UE SD AY , 2 2 O CT O BE R 2 0 24 D A TA ST R U C T U R E S A N D A L G O RI T H M S IN JA V A 46
Merge Sort
T UE SD AY , 2 2 O CT O BE R 2 0 24 47
Merge Sort
T UE SD AY , 2 2 O CT O BE R 2 0 24 48
Merging two sorted
arrays
• Given two sorted arrays (A, B)
• Creating sorted array C
containing all elements of A, B
T UE SD AY , 2 2 O CT O BE R 2 0 24 49
Merge Sort
T UE SD AY , 2 2 O CT O BE R 2 0 24 50
Merge Sort
T UE SD AY , 2 2 O CT O BE R 2 0 24 51
Merge Sort
• Array size not a power of 2
T UE SD AY , 2 2 O CT O BE R 2 0 24 52
Merge sort - algorithm
T UE SD AY , 2 2 O CT O BE R 2 0 24 53
Implementation
T UE SD AY , 2 2 O CT O BE R 2 0 24 54
Merge sort – Merging Example
• We compare 3 and 7
• Copy 3 down
• Increment the corresponding indices
Merging Example
• We compare 5 and 7
• Copy 5 down
• Increment the appropriate indices
Merging Example
• We compare 18 and 7
• Copy 7 down
• Increment...
Merging Example
• We compare 18 and 12
• Copy 12 down
• Increment...
Merging Example
• We compare 18 and 16
• Copy 16 down
• Increment...
Merging Example
• We compare 18 and 33
• Copy 18 down
• Increment...
Merging Example
• We compare 21 and 33
• Copy 21 down
• Increment...
Merging Example
• We compare 24 and 33
• Copy 24 down
• Increment...
Merging Example
T UE SD AY , 2 2 O CT O BE R 2 0 24 66
Implementation
• MergeSortApp.java
• Modify the code to print out the output after each recursive step
• Count the number of recursive calls. Estimate the relationship
between the number of calls and the number of elements.
T UE SD AY , 2 2 O CT O BE R 2 0 24 67
Efficiency of Merge
Sort: O(NlogN)
T UE SD AY , 2 2 O CT O BE R 2 0 24 68
Summary
T UE SD AY , 2 2 O CT O BE R 2 0 24 D A TA ST R U C T U R E S A N D A L G O RI T H M S IN JA V A 69
Vietnam National University of HCMC
International University
School of Computer Science and Engineering
THANK YOU