SlideShare a Scribd company logo
Python: Recursive Functions
Recursive Functions
Recall factorial function:
Iterative Algorithm
Loop construct (while)
can capture computation in a
set of state variables that
update on each iteration
through loop
Recursive Functions
Alternatively:
Consider
5! = 5x4x3x2x1
can be re-written as 5!=5x4!
In general n! = nx(n-1)!
factorial(n) = n * factorial(n-1)
Recursive Functions
Alternatively:
Consider
5! = 5x4x3x2x1
can be re-written as 5!=5x4!
In general n! = nx(n-1)!
factorial(n) = n * factorial(n-1)
Recursive Algorithm
function calling itself
Recursive Functions
Recursive Functions
Known
Base case
Recursive Functions
Base case
Recursive step
Recursive Functions
•  No computation in first
phase, only function calls
•  Deferred/Postponed
computation
– after function calls
terminate, computation
starts
•  Sequence of calls have
to be remembered Execution trace for n = 4
fact (4)
4 * fact (3)
4 * (3 * fact (2))
4 * (3 * (2 * fact (1)))
4 * (3 * (2 * (1 * fact (0))))
4 * (3 * (2 * (1 * 1)))
4 * (3 * (2 * 1))
4 * (3 * 2)
4 * 6
24
Courtesy Prof PR Panda CSE Department IIT Dehi
Another Example (Iterative)
Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-
programming-in-python-fall-2016/lecture-slides-code/
Iterative
Another Example (Recursive)
Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-
programming-in-python-fall-2016/lecture-slides-code/
Iterative Algorithm
Recursive
Recursive Functions
•  Size of the problem reduces at each step
•  The nature of the problem remains the same
•  There must be at least one terminating condition
•  Simpler, more intuitive
–  For inductively defined computation, recursive algorithm
may be natural
•  close to mathematical specification
•  Easy from programing point of view
•  May not efficient computation point of view
GCD Algorithm
gcd (a, b) =
b if a mod b = 0
gcd (b, a mod b) otherwise
Iterative
Algorithm
Recursive Algorithm
GCD Algorithm
gcd (a, b) =
b if a mod b = 0
gcd (b, a mod b) otherwise
gcd (6, 10)
gcd (10, 6)
gcd (6, 4)
gcd (4, 2)
2
2
2
2
Courtesy Prof PR Panda CSE Department IIT Dehi
Fibonacci Numbers
fib (n) =
0 n = 1
1 n = 2
fib (n-1) + fib (n-2) n > 2
Recursive Algorithm
Courtesy Prof PR Panda CSE Department IIT Dehi
Fibonacci Numbers
fib (6)
fib (5)
fib (4)
fib (3) fib (2)
fib (2) fib (1)
fib (3)
fib (2) fib (1)
fib (4)
fib (3) fib (2)
fib (2) fib (1)
Courtesy Prof PR Panda CSE Department IIT Dehi
Power Function
•  Write a function power (x,n) to compute the
nth power of x
power(x,n) =
1  if n = 0
x * power(x,n-1) otherwise
Power Function
•  Efficient power function
•  Fast Power
–  fpower(x,n) = 1 for n = 0
–  fpower(x,n) = x * (fpower(x, n/2))2 if n is odd
–  fpower(x,n) = (fpower(x, n/2))2 if n is even
Power Function
•  Efficient power function
Towers of Hanoi Problem
•  64 gold discs with different
diameters
•  Three poles: (Origin, Spare, Final)
•  Transfer all discs to final pole
from origin
–  one at a time
–  spare can be used to temporarily
store discs
–  no disk should be placed on a
smaller disk
•  Intial Arrangement:
–  all on origin pole, largest at bottom,
next above it, etc.
Origin Spare Final
Courtesy Prof PR Panda CSE Department IIT Dehi
3-Step Strategy
Origin Spare Final Origin Spare Final
Origin Spare Final Origin Spare Final
Solve for (n-1) disks. Move to Spare.
Use Final as Spare.
Move bottom disk to Final
Move (n-1) disks to Final.
Use Origin as Spare.
Courtesy Prof PR Panda CSE Department IIT Dehi
Recursive Solution
•  Use algorithm for (n-1) disks to solve n-disk problem
•  Use algorithm for (n-2) disks to solve (n-1) disk problem
•  Use algorithm for (n-3) disks to solve (n-2) disk problem
•  ...
•  Finally, solve 1-disk problem:
–  Just move the disk!
Courtesy Prof PR Panda CSE Department IIT Dehi
Recursive Solution
Source:https://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-0001-introduction-to-computer-science-and-
programming-in-python-fall-2016/lecture-slides-code/

More Related Content

What's hot (20)

PPT
how to calclute time complexity of algortihm
Sajid Marwat
 
PPT
Recursion
James Wong
 
PPTX
Recursion
Nalin Adhikari
 
PPT
Recursion
Malainine Zaid
 
PPTX
Computational Complexity
Kasun Ranga Wijeweera
 
PPT
CS8451 - Design and Analysis of Algorithms
Krishnan MuthuManickam
 
PPT
Ch10 Recursion
leminhvuong
 
PPT
Time andspacecomplexity
LAKSHMITHARUN PONNAM
 
PPTX
Complexity analysis - The Big O Notation
Jawad Khan
 
PPTX
Lecture09 recursion
Hariz Mustafa
 
PPTX
Recursion(Advanced data structure)
kurubameena1
 
PPTX
Complexity analysis in Algorithms
Daffodil International University
 
PPT
Data Structures- Part2 analysis tools
Abdullah Al-hazmy
 
PDF
Lec10
Nikhil Chilwant
 
PPT
Matrix transposition
동호 이
 
PPTX
Analysis of Algorithm (Bubblesort and Quicksort)
Flynce Miguel
 
PPTX
asymptotic analysis and insertion sort analysis
Anindita Kundu
 
PDF
CPSC 125 Ch 2 Sec 4
David Wood
 
PPTX
Algorithm Complexity and Main Concepts
Adelina Ahadova
 
PDF
Anlysis and design of algorithms part 1
Deepak John
 
how to calclute time complexity of algortihm
Sajid Marwat
 
Recursion
James Wong
 
Recursion
Nalin Adhikari
 
Recursion
Malainine Zaid
 
Computational Complexity
Kasun Ranga Wijeweera
 
CS8451 - Design and Analysis of Algorithms
Krishnan MuthuManickam
 
Ch10 Recursion
leminhvuong
 
Time andspacecomplexity
LAKSHMITHARUN PONNAM
 
Complexity analysis - The Big O Notation
Jawad Khan
 
Lecture09 recursion
Hariz Mustafa
 
Recursion(Advanced data structure)
kurubameena1
 
Complexity analysis in Algorithms
Daffodil International University
 
Data Structures- Part2 analysis tools
Abdullah Al-hazmy
 
Matrix transposition
동호 이
 
Analysis of Algorithm (Bubblesort and Quicksort)
Flynce Miguel
 
asymptotic analysis and insertion sort analysis
Anindita Kundu
 
CPSC 125 Ch 2 Sec 4
David Wood
 
Algorithm Complexity and Main Concepts
Adelina Ahadova
 
Anlysis and design of algorithms part 1
Deepak John
 

Similar to 6-Python-Recursion.pdf (20)

PPTX
6-Python-Recursion PPT.pptx
Venkateswara Babu Ravipati
 
PDF
Python Programming unit5 (1).pdf
jamvantsolanki
 
PDF
Python recursion
Prof. Dr. K. Adisesha
 
PPTX
Recursion
Esther Leytush
 
PPTX
DSA. Data structure algorithm dRecursion.pptx
siddiqsid0006
 
PPTX
lecture4-recursion.pptx
Lizhen Shi
 
PPTX
Recursive Algorithms with their types and implementation
Ahmad177077
 
PPTX
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
15AnasKhan
 
PPT
Lecture9 recursion
Muhammad Zubair
 
PPTX
Python recursion
ToniyaP1
 
PPT
14 recursion
Himadri Sen Gupta
 
PPTX
Recursion part 2
Keerty Smile
 
PDF
Introduction to Recursion (Python)
Thai Pangsakulyanont
 
PPT
Ch4-recursion.ppt
BasheerBasheer13
 
PPTX
Chapter 02 functions -class xii
Praveen M Jigajinni
 
PPTX
Recursion with Python [Rev]
Dennis Walangadi
 
PPTX
6 Recursion Using Python 1.pptx6 Recursion Using Python 1.pptx
usha raj
 
PDF
Copy of y16 02-2119divide-and-conquer
Joepang2015
 
PPT
Lec-6 Recursion of Data Structures & Algorithms
haseebanjum2611
 
PDF
Chapter VII RECURSION.pdf algor and data structure
benyakoubrania53
 
6-Python-Recursion PPT.pptx
Venkateswara Babu Ravipati
 
Python Programming unit5 (1).pdf
jamvantsolanki
 
Python recursion
Prof. Dr. K. Adisesha
 
Recursion
Esther Leytush
 
DSA. Data structure algorithm dRecursion.pptx
siddiqsid0006
 
lecture4-recursion.pptx
Lizhen Shi
 
Recursive Algorithms with their types and implementation
Ahmad177077
 
35000120060_Nitesh Modi_CSE Presentation on recursion.pptx
15AnasKhan
 
Lecture9 recursion
Muhammad Zubair
 
Python recursion
ToniyaP1
 
14 recursion
Himadri Sen Gupta
 
Recursion part 2
Keerty Smile
 
Introduction to Recursion (Python)
Thai Pangsakulyanont
 
Ch4-recursion.ppt
BasheerBasheer13
 
Chapter 02 functions -class xii
Praveen M Jigajinni
 
Recursion with Python [Rev]
Dennis Walangadi
 
6 Recursion Using Python 1.pptx6 Recursion Using Python 1.pptx
usha raj
 
Copy of y16 02-2119divide-and-conquer
Joepang2015
 
Lec-6 Recursion of Data Structures & Algorithms
haseebanjum2611
 
Chapter VII RECURSION.pdf algor and data structure
benyakoubrania53
 
Ad

Recently uploaded (20)

PPTX
UNIT DAA PPT cover all topics 2021 regulation
archu26
 
PPTX
Presentation on Foundation Design for Civil Engineers.pptx
KamalKhan563106
 
PPTX
Thermal runway and thermal stability.pptx
godow93766
 
PDF
UNIT-4-FEEDBACK AMPLIFIERS AND OSCILLATORS (1).pdf
Sridhar191373
 
PPTX
原版一样(Acadia毕业证书)加拿大阿卡迪亚大学毕业证办理方法
Taqyea
 
PPTX
Electron Beam Machining for Production Process
Rajshahi University of Engineering & Technology(RUET), Bangladesh
 
PPTX
Hashing Introduction , hash functions and techniques
sailajam21
 
PPTX
site survey architecture student B.arch.
sri02032006
 
PPTX
Green Building & Energy Conservation ppt
Sagar Sarangi
 
PDF
Additional Information in midterm CPE024 (1).pdf
abolisojoy
 
PPTX
drones for disaster prevention response.pptx
NawrasShatnawi1
 
PPTX
MPMC_Module-2 xxxxxxxxxxxxxxxxxxxxx.pptx
ShivanshVaidya5
 
PDF
PRIZ Academy - Change Flow Thinking Master Change with Confidence.pdf
PRIZ Guru
 
PDF
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
PDF
BioSensors glucose monitoring, cholestrol
nabeehasahar1
 
PPTX
Pharmaceuticals and fine chemicals.pptxx
jaypa242004
 
PPT
inherently safer design for engineering.ppt
DhavalShah616893
 
PPTX
Introduction to Neural Networks and Perceptron Learning Algorithm.pptx
Kayalvizhi A
 
PDF
A presentation on the Urban Heat Island Effect
studyfor7hrs
 
PDF
ARC--BUILDING-UTILITIES-2-PART-2 (1).pdf
IzzyBaniquedBusto
 
UNIT DAA PPT cover all topics 2021 regulation
archu26
 
Presentation on Foundation Design for Civil Engineers.pptx
KamalKhan563106
 
Thermal runway and thermal stability.pptx
godow93766
 
UNIT-4-FEEDBACK AMPLIFIERS AND OSCILLATORS (1).pdf
Sridhar191373
 
原版一样(Acadia毕业证书)加拿大阿卡迪亚大学毕业证办理方法
Taqyea
 
Electron Beam Machining for Production Process
Rajshahi University of Engineering & Technology(RUET), Bangladesh
 
Hashing Introduction , hash functions and techniques
sailajam21
 
site survey architecture student B.arch.
sri02032006
 
Green Building & Energy Conservation ppt
Sagar Sarangi
 
Additional Information in midterm CPE024 (1).pdf
abolisojoy
 
drones for disaster prevention response.pptx
NawrasShatnawi1
 
MPMC_Module-2 xxxxxxxxxxxxxxxxxxxxx.pptx
ShivanshVaidya5
 
PRIZ Academy - Change Flow Thinking Master Change with Confidence.pdf
PRIZ Guru
 
Set Relation Function Practice session 24.05.2025.pdf
DrStephenStrange4
 
BioSensors glucose monitoring, cholestrol
nabeehasahar1
 
Pharmaceuticals and fine chemicals.pptxx
jaypa242004
 
inherently safer design for engineering.ppt
DhavalShah616893
 
Introduction to Neural Networks and Perceptron Learning Algorithm.pptx
Kayalvizhi A
 
A presentation on the Urban Heat Island Effect
studyfor7hrs
 
ARC--BUILDING-UTILITIES-2-PART-2 (1).pdf
IzzyBaniquedBusto
 
Ad

6-Python-Recursion.pdf