Chapter-1-Recursive-and-Itration
Chapter-1-Recursive-and-Itration
Recursion
o Recursion is one way to decompose a task into smaller
subtasks. At least one of the subtasks is a smaller
example of the same task.
• The smallest example of the same task has a non-
recursive solution.
Example: The factorial function
n! = n * (n-1)! and 1! = 1
5/25/2015 UOG, Computer science 3
Recursion
• In general, There are two approaches to write repetitive
algorithm. One uses iteration; the other uses recursion.
• Recursion is a repetitive process in which an algorithm calls
itself or in other words “Recursion is a process in which
function calls itself”, and such functions are called as
recursive function.
Factorial – A Case Study:
1 if n=0
Factorial(n)=
n*(n-1)*(n-2)*…*3*2*1 if n>0
5/25/2015 UOG, Computer science 4
factorial(4) =4*3*2*1 24
= 4 * factorial(3)
6
= 3* factorial(2)
2
= 2*factorial(1)
1
= 1*factorial(0)
If (n==0 || n==1)
return(1)
5/25/2015 UOG, Computer science 5
Recursive Algorithm
• recursiveFactorial(n)
• if(n equals 0)
• return 1
• else
• return( n * recursiveFactorial(n-1))
• end if
• end recursiveFactorial
1 if n=0
Factorial(n)=
n* factorial(F-1) if n>0
5/25/2015 UOG, Computer science 7
Recursive
a
Multiplication- A Case
if b=1
Study
Mult(a,b) =
mult (a * (b-1)) + a if b>1
15
mult(5,4) + 5= mult(5 * 3) + 5
= mult(5 * 2) +5 10
=mult(5,1) +5 5
5/25/2015 UOG, Computer science 8
Algorithm
• mult(n,m)
• Input n,m
• if(m==1)
• return(n)
• else
• return( mult(n, m-1) + n)
• end if
• end
5/25/2015 UOG, Computer science 9
Fibonacci series
• Fibonacci numbers are named after Leonardo Fibonacci, an
italian mathematician, who lived in the early thirteen century. In
this series each number is the sum of the previous numbers.
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, …..
0 if n=0
Fibonacci(n)= 1 if n=1
Fibonacci(n-1)+Fibonacci(n-2) otherwise
2 Fib(3) Fib(2) 1
1
1
Fib(2) Fib(1) Fib(1) Fib(0)
0
1
Fib(1) Fib(0)
1
0
UOG, Computer science 11
Binary Search
• The sequential search algorithm is very slow. If we have an
array of 1 million elements, we must do 1 million
comparisons in the worst case.
• It is suggested that you consider binary searches whenever
the list contains more than 50 elements.
• The Binary Search is applied only on sorted List, So if we
want to search an element form the List (Array), Then we
will have to first sort the Array then search it using the binary
search.
• Binary search starts by testing the data in the element at the
middle of the array. This determines if the target is in the first
half or the second half of the list.
UOG, Computer science 12
Index 0 1 2 3 4 5 6 7
23 45 46 58 78 81 85 92
85 > 58
First mid last
Target =85
4 5 7
Index 0 1 2 3 4 5 6 7
23 45 46 58 78 81 85 92
85 > 81
First mid last
Target =85
6 6 7
Index 0 1 2 3 4 5 6 7
23 45 46 58 78 81 85 92
85 ==85
UOG, Computer science 14
0 1 2 3 4 5 6 7
23 45 46 58 78 81 85 92
85 > 58
• Line 1: if (first > last) ? It is not true, so execute Line 3.
• Line 3: Mid=(0+7)/2=3
• Line 4: is x==a[3] ? 58!= 85, so execute Line 6.
• Line 6: if x<a[3] ? 85<58 it is not true, so per form else clause
at line 8
• Repeat the algorithm with first=mid+1=4 and last=7 i.e. search
the upper half of the array.
UOG, Computer science 16
First mid last
Target =85
4 5 7
0 1 2 3 4 5 6 7
23 45 46 58 78 81 85 92
0 1 2 3 4 5 6 7
23 45 46 58 78 81 85 92
Types of Recursion
• Direct recursion
• a function calls itself
• Indirect recursion
• function A calls function B, and function B calls function A. Or,
• function A calls function B, which calls …, which calls function A
5/25/2015 UOG, Computer science 14-19
Recursion
We have to pay a price for recursion:
• calling a function consumes more time and memory than
adjusting a loop counter.
• high performance applications (graphic action games,
simulations of nuclear explosions) hardly ever use recursion.
In less demanding applications recursion is an
attractive alternative for iteration (for the right
problems!)
5/25/2015 UOG, Computer science 21
Assignment
• Define n! by recursion
• Define GCD by recursion
• Write in brief about the recursion behavior of the
Fibonacci Series
• Define Recursion and recurrence.
• Differentiate between Recursion and the Iteration.