SlideShare a Scribd company logo
Python: Recursive Functions
Recursive Functions
Recall factorial function:
IterativeAlgorithm
Loop construct (while)
can capture computation in a
set of state variables that
update on each iteration
through loop
Recursive Functions
factorial(n) = n * factorial(n-1)
Alternatively:
Consider
5! = 5x4x3x2x1
can be re-written as 5!=5x4!
In general n! = nx(n-1)!
Recursive Functions
Alternatively:
Consider
5! = 5x4x3x2x1 Recursive Algorithm
can be re-written as 5!=5x4!
function calling itself
In general n! = nx(n-1)!
factorial(n) = n * factorial(n-1)
Recursive Functions
Recursive Functions
Known
Base case
Recursive Functions
Base case
Recursive step
Recursive Functions
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
• No computation in first
phase, only function calls
• Deferred/Postponed
computation
– after function calls
terminate, computation
starts
• Sequence of calls have
to be remembered
Courtesy Prof PR Panda CSE Department IIT Dehi
Another Example (Iterative)
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/
Another Example (Recursive)
IterativeAlgorithm
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/
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
RecursiveAlgorithm
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
1
n = 1
n = 2
fib (n-1) + fib (n-2) n > 2
RecursiveAlgorithm
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
Courtesy Prof PR Panda CSE Department IIT Dehi
Spare Final
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
Courtesy Prof PR Panda CSE Department IIT Dehi
• 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!
Problem solving with Top Down Design
 Top down design approach, also called as Stepwise refinement, is
essential to develop a well structured program.
 This approach is a problem solving technique that systematically breaks
a complicated problem into smaller, more manageable pieces.
 If any of these subproblems is not easier to solve, we further divide the
subproblem into smaller parts.
 We repeat the subdividing process until all small parts are not dividable.
 Then, we can use a few lines of code to solve every trivial problem. In the
end, we put all these little pieces together as a solution to the original
problem.
 This approach makes it easy to think about the problem, code the
solution read the code and identify bugs.
Steps to solve a problem using Top
Down Design:
1. Start with an initial problem statement.
2. Define subtask at the first level.
3. Divide subtasks at a higher level into
more specific tasks.
4. Repeat step (3) until each subtask is
trivial.
5. Refine the algorithm into real code.
Business requirement Analysis
Initial Problem statement
Read new emails from an email box and save email messages into a database table.
6-Python-Recursion PPT.pptx
6-Python-Recursion PPT.pptx
Case study: Gathering Information
from a file system
Import os
os.mkdir(“d:tempdir”)
FILES
Open( )
Syntax
file_object = (“filename.txt”, “Access Mode”)
File pointer
points to the content of file
Access Modes
r – read mode
only for reading the content
file pointer points at the beginning of the file
file should exist before opening in read mode

More Related Content

PPTX
LPG gas leekage dectection
PDF
Python recursion
PPTX
Sql subquery
PPT
Chapter 8
PPTX
BLAST (Basic local alignment search Tool)
PDF
Python final ppt
LPG gas leekage dectection
Python recursion
Sql subquery
Chapter 8
BLAST (Basic local alignment search Tool)
Python final ppt

What's hot (20)

PPT
RECURSION IN C
PPTX
Python Functions
ODP
Python Modules
PPTX
Regular expressions in Python
PPTX
Function in C program
PPTX
Functions in python
PPTX
Delegates and events in C#
PPS
Introduction to class in java
PPSX
Modules and packages in python
PPTX
Recursion
PPT
Shell programming
PPTX
Queue in Data Structure
PPTX
Recursive Function
PPSX
python Function
PPT
Input and output in C++
PDF
Python programming : Files
PDF
Python list
PDF
Methods in Java
PPTX
Functions in C
RECURSION IN C
Python Functions
Python Modules
Regular expressions in Python
Function in C program
Functions in python
Delegates and events in C#
Introduction to class in java
Modules and packages in python
Recursion
Shell programming
Queue in Data Structure
Recursive Function
python Function
Input and output in C++
Python programming : Files
Python list
Methods in Java
Functions in C
Ad

Similar to 6-Python-Recursion PPT.pptx (20)

PDF
6-Python-Recursion.pdf
PPT
Dynamic pgmming
PPT
M251_Meeting 9 (Recursion_AdvancedJava).ppt
PPTX
Recursion
PPTX
lecture4-recursion.pptx
PPT
dynamic programming Rod cutting class
PPTX
6 Recursion Using Python 1.pptx6 Recursion Using Python 1.pptx
PPT
14 recursion
PPT
Data Structures- Part5 recursion
PPT
Dynamicpgmming
PDF
Sure interview algorithm-1103
PDF
Programming workshop
PDF
BCS401 ADA First IA Test Question Bank.pdf
PPT
Lecture 1 and 2 of Data Structures & Algorithms
PDF
Fibonacci Function Gallery - Part 2 - One in a series
PDF
RNN sharing at Trend Micro
PDF
Mit6 094 iap10_lec03
PPT
FUNDAMETAL ALG.ppt
PDF
Neural Network as a function
PPT
LECTURE 5-Function in Matlab how to use mathlab
6-Python-Recursion.pdf
Dynamic pgmming
M251_Meeting 9 (Recursion_AdvancedJava).ppt
Recursion
lecture4-recursion.pptx
dynamic programming Rod cutting class
6 Recursion Using Python 1.pptx6 Recursion Using Python 1.pptx
14 recursion
Data Structures- Part5 recursion
Dynamicpgmming
Sure interview algorithm-1103
Programming workshop
BCS401 ADA First IA Test Question Bank.pdf
Lecture 1 and 2 of Data Structures & Algorithms
Fibonacci Function Gallery - Part 2 - One in a series
RNN sharing at Trend Micro
Mit6 094 iap10_lec03
FUNDAMETAL ALG.ppt
Neural Network as a function
LECTURE 5-Function in Matlab how to use mathlab
Ad

More from Venkateswara Babu Ravipati (14)

PPTX
JNTUK python programming python unit 3.pptx
PPTX
PPT JNTUK python programming unit 2.pptx
PPTX
EG-Unit-2- Points.pptx
PPTX
PPTX
PPTX
inputoutput.pptx
PPTX
PP ECE A Sec UNIT-1.pptx
PPTX
python unit 2.pptx
PDF
PPTX
cad cam and robotics.pptx
PPTX
Le and ne algorithms
JNTUK python programming python unit 3.pptx
PPT JNTUK python programming unit 2.pptx
EG-Unit-2- Points.pptx
inputoutput.pptx
PP ECE A Sec UNIT-1.pptx
python unit 2.pptx
cad cam and robotics.pptx
Le and ne algorithms

Recently uploaded (20)

PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PDF
PPT on Performance Review to get promotions
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
Sustainable Sites - Green Building Construction
PPTX
web development for engineering and engineering
PDF
Arduino robotics embedded978-1-4302-3184-4.pdf
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PDF
Digital Logic Computer Design lecture notes
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
Welding lecture in detail for understanding
PPT
Project quality management in manufacturing
PDF
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PDF
Well-logging-methods_new................
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
OOP with Java - Java Introduction (Basics)
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Internet of Things (IOT) - A guide to understanding
Evaluating the Democratization of the Turkish Armed Forces from a Normative P...
PPT on Performance Review to get promotions
bas. eng. economics group 4 presentation 1.pptx
Sustainable Sites - Green Building Construction
web development for engineering and engineering
Arduino robotics embedded978-1-4302-3184-4.pdf
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Digital Logic Computer Design lecture notes
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Welding lecture in detail for understanding
Project quality management in manufacturing
July 2025 - Top 10 Read Articles in International Journal of Software Enginee...
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Well-logging-methods_new................

6-Python-Recursion PPT.pptx