KEMBAR78
Recursion - Algorithms and Data Structures | PPT
Algorithms and Data Structures
Recursion
By- Priyanka
Basic Algorithms: Recursion
Basic Algorithms: Recursion
Basic Algorithms
 Write a function that calculate 3!
(factorial).
 Write a function that calculate n!
Recursion helps
 Avoidance of unnecessary calling of functions.
 A substitute for iteration where the iterative
solution is very complex.
 Extremely useful when applying the same solution.
Types of Recursion
Linear recursion : makes at most one recursive call each
time it is invoked.
Binary recursion : algorithm makes two recursive calls.
Multiple recursion : method may make (potentially more
than two) recursive calls.
Linear Recursion
Algorithm LinearSum(A, n)
if n = 1 then
return A[0]
else
return LinearSum(A, n-1) + A[n-1]
Linear Recursion
A={4, 3, 6, 2, 5}, n=5.
Student Exercise
Can you think of any drawback for recursive
functions?
Answer is related to computer memory
Binary Recursion
Algorithm BinarySum(A, i, n)
if n = 1 then
return A[i]
else
return BinarySum(A, i, [n/2])+
BinarySum (A, i+[n/2], [n/2])
Binary Recursion
Recursion trace for the execution of BinarySum(A, 0, 8) :
Multiple Recursion
Combinatorial puzzle:
pot + pan = bib
dog + cat = pig
boy + girl = baby
Puzzle Solve(3, S, U)
S is an empty sequence/string, and K=3
U = {a, b, c}
Example: Find a solution to cbb + ba = abc, where {a, b, c}
stands for {7, 8, 9}, K=3.
Soluti
on
Thank You

Recursion - Algorithms and Data Structures

Editor's Notes

  • #2 <number>
  • #4 Russian matryoshka doll Droste effect Barnsley fern <number>
  • #5 Show students that why we need a recursive function. <number>
  • #9 <number>