SlideShare a Scribd company logo
1 of 20
CSG2A3
ALGORITMA dan STRUKTUR DATA
Recursive Algorithm
Sometimes, the best way to solve a problem is by
solving a smaller version of the exact same
problem first
Example problem:
– Try to tear a sheet of paper into the same 8 pieces
2
What is recursion?
To solve this, one solution is that we can just tear
it 7 times as follows:
That was an example of the application of looping
3
Tear paper into the same 8 pieces
1 2 3 4 5 6 7 8
Or we can tear it into 2, and repeat the process
for each pieces 2 times
That is an example of the application of recursive
4
Tear paper into the same 8 pieces
a b
Function that calls itself within the program text
5
Recursive Function
6
Have you seen this movie?
The movie tells about someone
who can dream inside a dream
If you think that the “dream” in
the movie is a function, then you’ll
find it really similar concept to the
recursive function
Recursion is a technique that solves a problem by
solving a smaller problem of the same type
Recursion is a principle closely related to
mathematical induction.
F(0) = 0
F(x) = F(x-1) + 2
7
Some Definitions
0
F(x) =
F(x-1) + 2
Example
Power of two
2n = 2 * 2n-1
20 = 1
Factorial
X! = X * (X-1)!
0! = 1
8
Careful when writing
If we use iteration, we must be careful
not to create an infinite loop by accident:
While ( result > 0 ) do
result ++
Oops!
Careful when writing
Similarly, if we use recursion we must be
careful not to create an infinite chain of
function calls
Remember the Rule!
An Algorithm must stop!
Define a rule that will stop the recursion
(initial set / base case)
– X! = X * (X-1)!
– 0! = 1
Define a rule to reach the next iteration
(construct new element / step)
11
Algorithm of the factorial function
Function Factorial(input : n : integer)
if (n == 0) then // base case
 1
else
 n * Factorial(n-1)
14
A famous example: The Fibonacci
numbers
f(0) = 0, f(1) = 1
f(n) = f(n – 1) + f(n - 2) f(0) = 0
f(1) = 1
f(2) = f(1) + f(0) = 1 + 0 = 1
f(3) = f(2) + f(1) = 1 + 1 = 2
f(4) = f(3) + f(2) = 2 + 1 = 3
f(5) = f(4) + f(3) = 3 + 2 = 5
f(6) = f(5) + f(4) = 5 + 3 = 8
Recursive Algorithm
An algorithm is called recursive if it solves a
problem by reducing it to an instance of the same
problem with smaller input
A recursive function must contain at least one
non-recursive branch.
The recursive calls must eventually lead to a non-
recursive branch
15
Recursion vs. iteration
For every recursive algorithm, there is an
equivalent iterative algorithm
Iteration can be used in place of recursion
–An iterative algorithm uses a looping construct
–A recursive algorithm uses a branching structure
16
Recursion vs. iteration
Recursive solutions are often less efficient, in
terms of both time and space, than iterative
solutions
Recursion can simplify the solution of a problem,
often resulting in shorter, more easily understood
source code
17
Question?
Write a recursive Algorithm
Binary Search
Reverse an Array
Counts number of zero
Checking a Palindrome
Drawing a triangle
Example :
*
**
***
****
19
THANK YOU
20

More Related Content

Similar to 10 - Recursive.pptx

Calculus - Functions Review
Calculus - Functions ReviewCalculus - Functions Review
Calculus - Functions Reviewhassaanciit
 
Lesson 4A - Inverses of Functions.ppt
Lesson 4A - Inverses of Functions.pptLesson 4A - Inverses of Functions.ppt
Lesson 4A - Inverses of Functions.pptssuser78a386
 
lecture4-recursion.pptx
lecture4-recursion.pptxlecture4-recursion.pptx
lecture4-recursion.pptxLizhen Shi
 
Calculus ii power series and functions
Calculus ii   power series and functionsCalculus ii   power series and functions
Calculus ii power series and functionsmeezanchand
 
Lesson 2 - Functions and their Graphs - NOTES.ppt
Lesson 2 - Functions and their Graphs - NOTES.pptLesson 2 - Functions and their Graphs - NOTES.ppt
Lesson 2 - Functions and their Graphs - NOTES.pptJaysonMagalong
 
Introduction to Recursion (Python)
Introduction to Recursion (Python)Introduction to Recursion (Python)
Introduction to Recursion (Python)Thai Pangsakulyanont
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programmingJay Nagar
 
Introduction to Artificial Neural Networks
Introduction to Artificial Neural NetworksIntroduction to Artificial Neural Networks
Introduction to Artificial Neural NetworksStratio
 
Numerical differentation with c
Numerical differentation with cNumerical differentation with c
Numerical differentation with cYagya Dev Bhardwaj
 
Integral Calculus Anti Derivatives reviewer
Integral Calculus Anti Derivatives reviewerIntegral Calculus Anti Derivatives reviewer
Integral Calculus Anti Derivatives reviewerJoshuaAgcopra
 
Algorithm in computer science
Algorithm in computer scienceAlgorithm in computer science
Algorithm in computer scienceRiazul Islam
 
FUZZY DIAGONAL OPTIMAL ALGORITHM TO SOLVE INTUITIONISTIC FUZZY ASSIGNMENT PRO...
FUZZY DIAGONAL OPTIMAL ALGORITHM TO SOLVE INTUITIONISTIC FUZZY ASSIGNMENT PRO...FUZZY DIAGONAL OPTIMAL ALGORITHM TO SOLVE INTUITIONISTIC FUZZY ASSIGNMENT PRO...
FUZZY DIAGONAL OPTIMAL ALGORITHM TO SOLVE INTUITIONISTIC FUZZY ASSIGNMENT PRO...IAEME Publication
 

Similar to 10 - Recursive.pptx (20)

Recursion DM
Recursion DMRecursion DM
Recursion DM
 
Calculus - Functions Review
Calculus - Functions ReviewCalculus - Functions Review
Calculus - Functions Review
 
Lesson 4A - Inverses of Functions.ppt
Lesson 4A - Inverses of Functions.pptLesson 4A - Inverses of Functions.ppt
Lesson 4A - Inverses of Functions.ppt
 
lecture4-recursion.pptx
lecture4-recursion.pptxlecture4-recursion.pptx
lecture4-recursion.pptx
 
Calculus ii power series and functions
Calculus ii   power series and functionsCalculus ii   power series and functions
Calculus ii power series and functions
 
Lesson 2 - Functions and their Graphs - NOTES.ppt
Lesson 2 - Functions and their Graphs - NOTES.pptLesson 2 - Functions and their Graphs - NOTES.ppt
Lesson 2 - Functions and their Graphs - NOTES.ppt
 
FUNDAMETAL ALG.ppt
FUNDAMETAL ALG.pptFUNDAMETAL ALG.ppt
FUNDAMETAL ALG.ppt
 
Introduction to Recursion (Python)
Introduction to Recursion (Python)Introduction to Recursion (Python)
Introduction to Recursion (Python)
 
ppt
pptppt
ppt
 
ppt
pptppt
ppt
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
02 Notes Divide and Conquer
02 Notes Divide and Conquer02 Notes Divide and Conquer
02 Notes Divide and Conquer
 
5 numerical analysis
5 numerical analysis5 numerical analysis
5 numerical analysis
 
recursion.ppt
recursion.pptrecursion.ppt
recursion.ppt
 
Introduction to Artificial Neural Networks
Introduction to Artificial Neural NetworksIntroduction to Artificial Neural Networks
Introduction to Artificial Neural Networks
 
Numerical differentation with c
Numerical differentation with cNumerical differentation with c
Numerical differentation with c
 
Integral Calculus Anti Derivatives reviewer
Integral Calculus Anti Derivatives reviewerIntegral Calculus Anti Derivatives reviewer
Integral Calculus Anti Derivatives reviewer
 
Algorithm Assignment Help
Algorithm Assignment HelpAlgorithm Assignment Help
Algorithm Assignment Help
 
Algorithm in computer science
Algorithm in computer scienceAlgorithm in computer science
Algorithm in computer science
 
FUZZY DIAGONAL OPTIMAL ALGORITHM TO SOLVE INTUITIONISTIC FUZZY ASSIGNMENT PRO...
FUZZY DIAGONAL OPTIMAL ALGORITHM TO SOLVE INTUITIONISTIC FUZZY ASSIGNMENT PRO...FUZZY DIAGONAL OPTIMAL ALGORITHM TO SOLVE INTUITIONISTIC FUZZY ASSIGNMENT PRO...
FUZZY DIAGONAL OPTIMAL ALGORITHM TO SOLVE INTUITIONISTIC FUZZY ASSIGNMENT PRO...
 

Recently uploaded

TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontologyjohnbeverley2021
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...apidays
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Bhuvaneswari Subramani
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityWSO2
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2
 

Recently uploaded (20)

TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 

10 - Recursive.pptx

  • 1. CSG2A3 ALGORITMA dan STRUKTUR DATA Recursive Algorithm
  • 2. Sometimes, the best way to solve a problem is by solving a smaller version of the exact same problem first Example problem: – Try to tear a sheet of paper into the same 8 pieces 2 What is recursion?
  • 3. To solve this, one solution is that we can just tear it 7 times as follows: That was an example of the application of looping 3 Tear paper into the same 8 pieces 1 2 3 4 5 6 7 8
  • 4. Or we can tear it into 2, and repeat the process for each pieces 2 times That is an example of the application of recursive 4 Tear paper into the same 8 pieces a b
  • 5. Function that calls itself within the program text 5 Recursive Function
  • 6. 6 Have you seen this movie? The movie tells about someone who can dream inside a dream If you think that the “dream” in the movie is a function, then you’ll find it really similar concept to the recursive function
  • 7. Recursion is a technique that solves a problem by solving a smaller problem of the same type Recursion is a principle closely related to mathematical induction. F(0) = 0 F(x) = F(x-1) + 2 7 Some Definitions 0 F(x) = F(x-1) + 2
  • 8. Example Power of two 2n = 2 * 2n-1 20 = 1 Factorial X! = X * (X-1)! 0! = 1 8
  • 9. Careful when writing If we use iteration, we must be careful not to create an infinite loop by accident: While ( result > 0 ) do result ++ Oops!
  • 10. Careful when writing Similarly, if we use recursion we must be careful not to create an infinite chain of function calls
  • 11. Remember the Rule! An Algorithm must stop! Define a rule that will stop the recursion (initial set / base case) – X! = X * (X-1)! – 0! = 1 Define a rule to reach the next iteration (construct new element / step) 11
  • 12. Algorithm of the factorial function Function Factorial(input : n : integer) if (n == 0) then // base case  1 else  n * Factorial(n-1)
  • 13.
  • 14. 14 A famous example: The Fibonacci numbers f(0) = 0, f(1) = 1 f(n) = f(n – 1) + f(n - 2) f(0) = 0 f(1) = 1 f(2) = f(1) + f(0) = 1 + 0 = 1 f(3) = f(2) + f(1) = 1 + 1 = 2 f(4) = f(3) + f(2) = 2 + 1 = 3 f(5) = f(4) + f(3) = 3 + 2 = 5 f(6) = f(5) + f(4) = 5 + 3 = 8
  • 15. Recursive Algorithm An algorithm is called recursive if it solves a problem by reducing it to an instance of the same problem with smaller input A recursive function must contain at least one non-recursive branch. The recursive calls must eventually lead to a non- recursive branch 15
  • 16. Recursion vs. iteration For every recursive algorithm, there is an equivalent iterative algorithm Iteration can be used in place of recursion –An iterative algorithm uses a looping construct –A recursive algorithm uses a branching structure 16
  • 17. Recursion vs. iteration Recursive solutions are often less efficient, in terms of both time and space, than iterative solutions Recursion can simplify the solution of a problem, often resulting in shorter, more easily understood source code 17
  • 19. Write a recursive Algorithm Binary Search Reverse an Array Counts number of zero Checking a Palindrome Drawing a triangle Example : * ** *** **** 19