Topic 2: Recursion: Data Structures & Algorithms Analysis
Topic 2: Recursion: Data Structures & Algorithms Analysis
1
Chapter Contents
What Is Recursion?
Tracing a Recursive Method
Recursive Methods That Return a Value
Recursively Processing an Array
Recursively Processing a Linked Chain
A Simple Solution to a Difficult Problem
A Poor Solution to a Simple Problem
Calling Trees
Indirect Recursion
Tail Recursion
Learning Objectives
At the end of this lesson, the student
should be able to:
identify program with recursive method,
trace recursive method,
write recursive method, and
identify tail recursion and replace it with
iteration.
What Is Recursion?
It is a problem-solving process that can be used to
generate simple solutions (design of algorithm).
Technique related to repetition. It is an alternative way
of solving repetition problem.
Breaking a problem into identical but smaller
problems. The smaller problems are identical except
for their size.
Eventually you reach a smallest problem
Answer is obvious or trivial
Using that solution enables you to solve the previous
problems
Eventually the original problem is solved 4
Applications of Recursive
Algorithms
The fields of Artificial Intelligent – games, proving
mathematical theorems, recognizing patterns, etc.
Mathematical operations – computing factorial, greatest
common divisors, exponential functions, printing
permutations, etc.
Operations on data structures – String, array, list, tree,
graph, etc.
To design very efficient searching techniques - linear
and binary search.
Divide and conquer algorithms – sorting techniques,
etc.
Backtracking algorithms – puzzle, nonlinear data
structures traversal, etc.
What Is Recursion?
The invocation is a
Recursive call, or
Recursive invocation
Recursive Methods
A recursive method is one that calls itself. Each
time it is called, the recursive method can operate
on different arguments.
Generally, a recursive solution is slightly less
efficient, in terms of computer time, than an
iterative one because of the overhead for the extra
method calls.
In many instances, however, recursion enables us
to specify a natural, simple solution to a problem
that otherwise would be difficult to solve.
When Designing Recursive
Solution
Recognize the base case and provide solution
to it.
Devise a strategy to split the problem into
smaller versions of itself. Each recursive case
must make progress towards the base case.
Combine the solutions to the smaller problem in
such a way that each larger problem is solve
correctly.
When Designing Recursive
Solution
Requirements of recursive method:
Must have one (or more) base cases
The general case (recursive case) must
eventually be reduced to the base case.
When Designing Recursive
Solution
Method definition must provide parameter (s)
Leads to different cases
Typically includes an if or a switch statement
One or more of these cases should provide a
non recursive solution
The base or stopping case
One or more cases includes recursive
invocation
Takes a step towards the base case
Two forms for recursive
algorithms
if the stopping case is reached
Solve it.
else
Split the problem into simpler cases using
recursion.
} // end sumOf
Recursive Methods That Return
a Value
fac(4) // 24
return 4* fac(3) // 4 * 6 = 24
return 3* fac(2) // 3 * 2 = 6
return 2 * fac(1) // 2 * 1= 2
return 1
Recursive Tree (Calling Tree)
Given a function definition of f(n) :
n if n<=1
f(n) = n + f(½n) if n is even number, and n>1
f(½ (n+1)) + f(½ (n-1)) if n is odd number, and n>1
If n=-5
F (-5)
-5
Recursive Tree (Calling Tree)
If n= 10
n if n<=1
f(n) = n + f(½n) if n is even number, and n>1
f(½ (n+1)) + f(½ (n-1)) if n is odd number, and n>1
F (10)
10+7=17
10 F (5)4+3=7
F (3) F (2)
F (1) 2+1=3
F (2) F (1)
3+1=4
2
2+1=3 F (1) 1 1
2
1 1
1
1
Recursively Processing an Array
Some of the more powerful searching and
sorting algorithms often are stated
recursively (usually is implemented using
an array)
When processing array recursively, divide
it into two pieces, for example;
Last element one piece, and the rest of the
array could be the other piece, or
First element one piece, and the rest of the
array could be the other piece, or
Divide the array into two halves
Recursively Processing an Array
public static void displayArray(int array[], int first, int
last)
{
System.out.print(array[first] + “ ”);//start from 1st element
if (first< last)
displayArray(array, first+1, last);
} // end display Array
Disadvantage:
Use more memory (activation records)
Conclusion
Q & A Session
References
Data Structures and Abstractions with Java . Authors:
Frank M. Carrano & Walter Savitch . Chapter 10
Data structures with Java. Authors : Hubbard J.R. &
Huray A. Chapter 10
40