RECURSION
Recursion is the process of defining
something in terms of itself. Recursive
functions are those functions that call itself.
3!= (3 - 1)! × 3
= 2! × 3
Factorial Example
We turn this problem into a
smaller problem of same kind.
This is called “decomposition.”
3!= (3 - 1)! × 3
= 2! × 3
Factorial Example
2! = (2 - 1)! × 2
= 1! × 2
3!= (3 - 1)! × 3
= 2! × 3
Factorial Example
2! = (2 - 1)! × 2
1! = (1 - 1)! × 1
= 0! X 1
= 1! × 2
3!= (3 - 1)! × 3
= 2! × 3
Factorial Example
2! = (2 - 1)! × 2
1! = (1 - 1)! × 1
= 0! X 1
= 1! × 2
0! = 1
3!= (3 - 1)! × 3
= 2! × 3
Factorial Example
2! = (2 - 1)! × 2
1! = (1 - 1)! × 1
= 0! X 1
= 1! × 2
0! = 1
T hi s i s c a l l e d t he “ba se c a se”
wher e t he f u n c ti o n d o e s n o t
call itself anymore
3!= (3 - 1)! × 3
= 2! × 3
Factorial Example
2! = (2 - 1)! × 2
1! = (1 - 1)! × 1
= 0! X 1
= 1! × 2
0! = 1
3!= (3 - 1)! × 3
= 2! × 3
Factorial Example
2! = (2 - 1)! × 2
= 1! × 2
1! = (1 - 1)! × 1
= 0! X 1
= 1x1=1
We use the result of smaller problemto
find the result of larger problem.
This is called“composition”.
3!= (3 - 1)! × 3
= 2! × 3
Factorial Example
2! = (2 - 1)! × 2
= 1! × 2
= 1 x 2 = 2
3!= (3 - 1)! × 3
= 2! × 3
= 2 × 3
= 6.
Factorial Example
deffac(n):
if n == 0:
return 1
else:
return fac(n-1)* n
print(fac(5)) 120
FACTORIAL E X A M P L E
RECURSION
The Two Rules of Recursion
•Must have a base case - There must be at least one base
criteria/condition, when such condition is met the function stops calling itself.
•Must move toward the base case - The recursive calls
should moves in such a way that each time it comes closer to the base criteria.
01
Recursive case
Part where the code calls it self
Base case
Base case is the condition that
stops recursion from continuing
on forever
RECURSION CONSIST OF
02
!
Recursion without
Basecasewill result
Recursion Error
When a recursion doesn’t have base
case, it will run infinitely and
reached maximum depth
1. Recursive functions make the code look clean and elegant.
2. A complex task can be broken down into simpler sub-problems using recursion.
3. Sequence generation is easier with recursion than using some nested iteration.
ADVANTAGES OF RECURSION
1. Sometimes the logic behind recursion is hard to follow through.
2. Recursive calls are expensive (inefficient) as they take up a lot of memory and time.
3. Recursive functions are hard to debug.
DISADVANTAGES OF RECURSION
HOW A PARTICULAR PROBLEM IS SOLVED USING RECURSION?
The idea is to represent a problem in terms of one or more smaller problems, and add
one or more base conditions that stop the recursion.
WHY RECURSION?
• Sometimes it’s easier to write and read code in recursive form.
• You can always convert an iterative algorithm to recursive and vice
versa.
(does not mean it’s easy)
REMEMBER?
• Decomposition
• Base Case
• Composition
THANK YOU
Presented by
KIRTI GUPTA
PGT CS
KV NTPC DADRI

Recursion part 1

  • 1.
  • 2.
    Recursion is theprocess of defining something in terms of itself. Recursive functions are those functions that call itself.
  • 3.
    3!= (3 -1)! × 3 = 2! × 3 Factorial Example We turn this problem into a smaller problem of same kind. This is called “decomposition.”
  • 4.
    3!= (3 -1)! × 3 = 2! × 3 Factorial Example 2! = (2 - 1)! × 2 = 1! × 2
  • 5.
    3!= (3 -1)! × 3 = 2! × 3 Factorial Example 2! = (2 - 1)! × 2 1! = (1 - 1)! × 1 = 0! X 1 = 1! × 2
  • 6.
    3!= (3 -1)! × 3 = 2! × 3 Factorial Example 2! = (2 - 1)! × 2 1! = (1 - 1)! × 1 = 0! X 1 = 1! × 2 0! = 1
  • 7.
    3!= (3 -1)! × 3 = 2! × 3 Factorial Example 2! = (2 - 1)! × 2 1! = (1 - 1)! × 1 = 0! X 1 = 1! × 2 0! = 1 T hi s i s c a l l e d t he “ba se c a se” wher e t he f u n c ti o n d o e s n o t call itself anymore
  • 8.
    3!= (3 -1)! × 3 = 2! × 3 Factorial Example 2! = (2 - 1)! × 2 1! = (1 - 1)! × 1 = 0! X 1 = 1! × 2 0! = 1
  • 9.
    3!= (3 -1)! × 3 = 2! × 3 Factorial Example 2! = (2 - 1)! × 2 = 1! × 2 1! = (1 - 1)! × 1 = 0! X 1 = 1x1=1 We use the result of smaller problemto find the result of larger problem. This is called“composition”.
  • 10.
    3!= (3 -1)! × 3 = 2! × 3 Factorial Example 2! = (2 - 1)! × 2 = 1! × 2 = 1 x 2 = 2
  • 11.
    3!= (3 -1)! × 3 = 2! × 3 = 2 × 3 = 6. Factorial Example
  • 12.
    deffac(n): if n ==0: return 1 else: return fac(n-1)* n print(fac(5)) 120 FACTORIAL E X A M P L E
  • 13.
    RECURSION The Two Rulesof Recursion •Must have a base case - There must be at least one base criteria/condition, when such condition is met the function stops calling itself. •Must move toward the base case - The recursive calls should moves in such a way that each time it comes closer to the base criteria.
  • 14.
    01 Recursive case Part wherethe code calls it self Base case Base case is the condition that stops recursion from continuing on forever RECURSION CONSIST OF 02 ! Recursion without Basecasewill result Recursion Error When a recursion doesn’t have base case, it will run infinitely and reached maximum depth
  • 15.
    1. Recursive functionsmake the code look clean and elegant. 2. A complex task can be broken down into simpler sub-problems using recursion. 3. Sequence generation is easier with recursion than using some nested iteration. ADVANTAGES OF RECURSION
  • 16.
    1. Sometimes thelogic behind recursion is hard to follow through. 2. Recursive calls are expensive (inefficient) as they take up a lot of memory and time. 3. Recursive functions are hard to debug. DISADVANTAGES OF RECURSION
  • 17.
    HOW A PARTICULARPROBLEM IS SOLVED USING RECURSION? The idea is to represent a problem in terms of one or more smaller problems, and add one or more base conditions that stop the recursion.
  • 18.
    WHY RECURSION? • Sometimesit’s easier to write and read code in recursive form. • You can always convert an iterative algorithm to recursive and vice versa. (does not mean it’s easy)
  • 19.
  • 20.
    THANK YOU Presented by KIRTIGUPTA PGT CS KV NTPC DADRI