SlideShare a Scribd company logo
1 of 54
What is
Recursion?
Hello!
I’m Esther
You can find me on Github at @mindplace
Defined in terms
of itself
Let’s see some
examples...
This sentence
has 26 letters in
it.
This sentence starts with
the word "this" and ends
with the word "word."
”
“Walking with me in the middle of town
he suddenly remarked to me, “Daddy,
not every boat has a lifeboat, has it?” I
said "How come?” “Well, the lifeboat
could have a smaller lifeboat, but then
that would be without one.”
- Edsger W. Dijkstra
In everyday life...
Cleaning the apartment:
◉ If this isn’t the last room in the apartment:
If this room is messy, clean it.
◉ Otherwise, move to next room.
Recursion and
programming
“Robot, walk iteratively”
◉ Calculate the number of steps between
you and the specified wall
◉ Walk that many steps
◉ Stop
“Robot, walk
recursively”◉ If there is a wall in front of you, stop.
◉ Otherwise, take a step forward.
◉ Go to the beginning of the instructions.
Qualities of a
recursive function
◉ Solves a big problem by solving smaller parts in
the same way
◉ Has a base case
◉ Calls itself
◉ Executes itself by adding those calls to the stack
Example:
factorials
2! => 2 * 1
3! => 3 * 2 * 1
3! => 3 * 2!
def factorial(n)
return 1 if n <= 1
n * factorial(n - 1)
end
?
1
2
2
6
Let’s see this
with a stack!
factorial(5)
factorial(5)
● Is 5 <= 1?
5 * factorial(4) - waiting...
factorial(5)
● Is 5 <= 1? No.
● Return value of
5 * factorial(4)
5 * factorial(4) - waiting...
factorial(5)
● Is 5 >= 1? No.
● Return value of
5 * factorial(4)
Is value of factorial(4)
known?
5 * factorial(4) - waiting...
factorial(5)
● Is 5 >= 1? No.
● Return value of
5 * factorial(4)
Is value of factorial(4)
known? No.
Wait until value is found...
5 * factorial(4) - waiting...
factorial(4)
● Is 4 <= 1?
5 * factorial(4) - waiting...
4 * factorial(3) ...
factorial(4)
● Is 4 <= 1? No.
And so on, until...
5 * factorial(4) - waiting...
4 * factorial(3) ...
factorial(1)
● Is 1 <= 1?
5 * factorial(4) - waiting...
4 * factorial(3) ...
3 * factorial(2) ...
2 * fact(1)...
fact(1)...
factorial(1)
● Is 1 <= 1? Yes!
5 * factorial(4) - waiting...
4 * factorial(3) ...
3 * factorial(2) ...
2 * fact(1)...
fact(1)...
factorial(1)
● Is 1 <= 1? Yes!
● Return 1
5 * factorial(4) - waiting...
4 * factorial(3) ...
3 * factorial(2) ...
2 * fact(1)...
fact(1)...
Now we know value of
factorial(1), can return
2 * factorial(1)
5 * factorial(4) - waiting...
4 * factorial(3) ...
3 * factorial(2) ...
2 * fact(1)...
fact(1)... => 1
Now we know value of
factorial(1), can return
2 * factorial(1)
This value gets returned to the
function that called it in the
first place...
5 * factorial(4) - waiting...
4 * factorial(3) ...
3 * factorial(2) ...
2 * fact(1)...
fact(1)... => 1
That function was factorial(2)!
5 * factorial(4) - waiting...
4 * factorial(3) ...
3 * factorial(2) ...
2 * fact(1)...
fact(1)... => 1
That function was factorial(2)!
Now that factorial(2) gets the
value of factorial(1), it can
return the value of
2 * factorial(1)...5 * factorial(4) - waiting...
4 * factorial(3) ...
3 * factorial(2) ...
2 * fact(1)...
fact(1)... => 1
5 * factorial(4) - waiting...
4 * factorial(3) ...
Which gets returned to
factorial(3), the function that
called it.
5 * factorial(4) - waiting...
4 * factorial(3) ...
3 * factorial(2) ...
2 * fact(1)...
fact(1)... => 1
5 * factorial(4) - waiting...
4 * factorial(3) ...
=> 2
This bubbling back of values
continues until the original
general case function gets
back the value it’s waiting for,
value of factorial(4)...
5 * factorial(4) - waiting...
4 * factorial(3) ...
3 * factorial(2) ...
2 * fact(1)...
fact(1)... => 1
5 * factorial(4) - waiting...
4 * factorial(3) ...
=> 2
...and finally returns the
answer:
120
5 * factorial(4) - waiting...
4 * factorial(3) ...
3 * factorial(2) ...
2 * fact(1)...
fact(1)... => 1
5 * factorial(4) - waiting...
4 * factorial(3) ...
=> 2
=> 6
=> 24
◉ Sudoku board solver: having to make a guess, then
another guess inside that guess case…
◉ Tree problems
◉ “...the solution to a problem depends on solutions to
smaller instances of the same problem”
Lots of problems can
be solved
recursively...
◉ Stack overflow
◉ Web development: waiting on a stack of calls
to resolve can create a slow user experience
◉ Can ‘feel’ complicated
… but… recursion isn’t
always the best choice.
Classic problems:
Factorial
3! => 3 * 2! ...
Fibonacci sequence
Each item is the sum
of the previous two
items
Sorting algorithms
Merge sort, quicksort
Palindrome
rotor == rotor
Recap:
a recursive function...
◉ Solves the big problem by solving small parts
◉ Has a base case
◉ Calls itself
◉ Executes itself by adding those calls to the stack
Thanks!
Any questions?
You can find me at @mindplace
Credits
◉ Presentation template by SlidesCarnival
◉ Plant illustrations from Köhler's Medizinal-Pflanzen in naturgetreuen at BHL
◉ Quora, “How should I explain recursion to a 4-year-old?”
◉ StackOverflow!
◉ YouTube, Computerphile: “What on Earth is Recursion?”
Go forth!

More Related Content

Recently uploaded

Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsRussian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingrknatarajan
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxpranjaldaimarysona
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college projectTonystark477637
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...Call Girls in Nagpur High Profile
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...ranjana rawat
 

Recently uploaded (20)

Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur EscortsRussian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
Russian Call Girls in Nagpur Grishma Call 7001035870 Meet With Nagpur Escorts
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and workingUNIT-V FMM.HYDRAULIC TURBINE - Construction and working
UNIT-V FMM.HYDRAULIC TURBINE - Construction and working
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Processing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptxProcessing & Properties of Floor and Wall Tiles.pptx
Processing & Properties of Floor and Wall Tiles.pptx
 
Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024Water Industry Process Automation & Control Monthly - April 2024
Water Industry Process Automation & Control Monthly - April 2024
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
result management system report for college project
result management system report for college projectresult management system report for college project
result management system report for college project
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
The Most Attractive Pune Call Girls Manchar 8250192130 Will You Miss This Cha...
 

Featured

Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsPixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

Featured (20)

Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 

Recursion

  • 2. Hello! I’m Esther You can find me on Github at @mindplace
  • 5. This sentence has 26 letters in it.
  • 6. This sentence starts with the word "this" and ends with the word "word."
  • 7.
  • 8.
  • 9. ” “Walking with me in the middle of town he suddenly remarked to me, “Daddy, not every boat has a lifeboat, has it?” I said "How come?” “Well, the lifeboat could have a smaller lifeboat, but then that would be without one.” - Edsger W. Dijkstra
  • 10.
  • 11.
  • 12. In everyday life... Cleaning the apartment: ◉ If this isn’t the last room in the apartment: If this room is messy, clean it. ◉ Otherwise, move to next room.
  • 14. “Robot, walk iteratively” ◉ Calculate the number of steps between you and the specified wall ◉ Walk that many steps ◉ Stop
  • 15. “Robot, walk recursively”◉ If there is a wall in front of you, stop. ◉ Otherwise, take a step forward. ◉ Go to the beginning of the instructions.
  • 16. Qualities of a recursive function ◉ Solves a big problem by solving smaller parts in the same way ◉ Has a base case ◉ Calls itself ◉ Executes itself by adding those calls to the stack
  • 18. 2! => 2 * 1
  • 19. 3! => 3 * 2 * 1
  • 20. 3! => 3 * 2!
  • 21. def factorial(n) return 1 if n <= 1 n * factorial(n - 1) end
  • 22.
  • 23. ?
  • 24.
  • 25.
  • 26. 1
  • 27. 2
  • 28. 2
  • 29. 6
  • 32. factorial(5) ● Is 5 <= 1? 5 * factorial(4) - waiting...
  • 33. factorial(5) ● Is 5 <= 1? No. ● Return value of 5 * factorial(4) 5 * factorial(4) - waiting...
  • 34. factorial(5) ● Is 5 >= 1? No. ● Return value of 5 * factorial(4) Is value of factorial(4) known? 5 * factorial(4) - waiting...
  • 35. factorial(5) ● Is 5 >= 1? No. ● Return value of 5 * factorial(4) Is value of factorial(4) known? No. Wait until value is found... 5 * factorial(4) - waiting...
  • 36. factorial(4) ● Is 4 <= 1? 5 * factorial(4) - waiting... 4 * factorial(3) ...
  • 37. factorial(4) ● Is 4 <= 1? No. And so on, until... 5 * factorial(4) - waiting... 4 * factorial(3) ...
  • 38. factorial(1) ● Is 1 <= 1? 5 * factorial(4) - waiting... 4 * factorial(3) ... 3 * factorial(2) ... 2 * fact(1)... fact(1)...
  • 39. factorial(1) ● Is 1 <= 1? Yes! 5 * factorial(4) - waiting... 4 * factorial(3) ... 3 * factorial(2) ... 2 * fact(1)... fact(1)...
  • 40. factorial(1) ● Is 1 <= 1? Yes! ● Return 1 5 * factorial(4) - waiting... 4 * factorial(3) ... 3 * factorial(2) ... 2 * fact(1)... fact(1)...
  • 41. Now we know value of factorial(1), can return 2 * factorial(1) 5 * factorial(4) - waiting... 4 * factorial(3) ... 3 * factorial(2) ... 2 * fact(1)... fact(1)... => 1
  • 42. Now we know value of factorial(1), can return 2 * factorial(1) This value gets returned to the function that called it in the first place... 5 * factorial(4) - waiting... 4 * factorial(3) ... 3 * factorial(2) ... 2 * fact(1)... fact(1)... => 1
  • 43. That function was factorial(2)! 5 * factorial(4) - waiting... 4 * factorial(3) ... 3 * factorial(2) ... 2 * fact(1)... fact(1)... => 1
  • 44. That function was factorial(2)! Now that factorial(2) gets the value of factorial(1), it can return the value of 2 * factorial(1)...5 * factorial(4) - waiting... 4 * factorial(3) ... 3 * factorial(2) ... 2 * fact(1)... fact(1)... => 1 5 * factorial(4) - waiting... 4 * factorial(3) ...
  • 45. Which gets returned to factorial(3), the function that called it. 5 * factorial(4) - waiting... 4 * factorial(3) ... 3 * factorial(2) ... 2 * fact(1)... fact(1)... => 1 5 * factorial(4) - waiting... 4 * factorial(3) ... => 2
  • 46. This bubbling back of values continues until the original general case function gets back the value it’s waiting for, value of factorial(4)... 5 * factorial(4) - waiting... 4 * factorial(3) ... 3 * factorial(2) ... 2 * fact(1)... fact(1)... => 1 5 * factorial(4) - waiting... 4 * factorial(3) ... => 2
  • 47. ...and finally returns the answer: 120 5 * factorial(4) - waiting... 4 * factorial(3) ... 3 * factorial(2) ... 2 * fact(1)... fact(1)... => 1 5 * factorial(4) - waiting... 4 * factorial(3) ... => 2 => 6 => 24
  • 48. ◉ Sudoku board solver: having to make a guess, then another guess inside that guess case… ◉ Tree problems ◉ “...the solution to a problem depends on solutions to smaller instances of the same problem” Lots of problems can be solved recursively...
  • 49. ◉ Stack overflow ◉ Web development: waiting on a stack of calls to resolve can create a slow user experience ◉ Can ‘feel’ complicated … but… recursion isn’t always the best choice.
  • 50. Classic problems: Factorial 3! => 3 * 2! ... Fibonacci sequence Each item is the sum of the previous two items Sorting algorithms Merge sort, quicksort Palindrome rotor == rotor
  • 51. Recap: a recursive function... ◉ Solves the big problem by solving small parts ◉ Has a base case ◉ Calls itself ◉ Executes itself by adding those calls to the stack
  • 52. Thanks! Any questions? You can find me at @mindplace
  • 53. Credits ◉ Presentation template by SlidesCarnival ◉ Plant illustrations from Köhler's Medizinal-Pflanzen in naturgetreuen at BHL ◉ Quora, “How should I explain recursion to a 4-year-old?” ◉ StackOverflow! ◉ YouTube, Computerphile: “What on Earth is Recursion?”