SlideShare a Scribd company logo
Recursion
What is recursion
• Looping without a loop statement

• A function that is part of its own
  definition

• Can only work if there’s a terminating
  condition, otherwise it goes forever
  (the base case)
A recursive function
• N Factorial
• 1!   =   1
  2!   =   1   x   2   =   2
  3!   =   1   x   2   x   3 = 2! x 3 = 6
  N!   =   1   x   2   x   3 x .... (N-2) x (N-1) x N = (N-1)! x N
How recursion is handled
• Every time a function is called, the
  function values, local variables,
  parameters and return addresses are
  pushed onto the stack.

• Over and Over again
• You might run out!
Dry-running a recursive call
Procedure Printsequence(n)
    n <- n-1
    if n > 1 then
       Printsequence(n)
    endif
    output(n)
EndProcedure
Dry-running a recursive call
Procedure Printsequence(n)
    n <- n-1
    if n > 1 then
       output(n)
       Printsequence(n)
    endif

EndProcedure
How to write a recursive
         procedure
• Code the general case
  Result = n * Factorial(n-1)
• Code the base case
  Result = 1
• Ensure that the base case is reached
  after a finite number of recursive calls

More Related Content

PPTX
Algorithm analysis in fundamentals of data structure
PPTX
Traveling salesman problem
PPTX
Stacks IN DATA STRUCTURES
PPTX
Moore and mealy machine
PDF
Algorithms Lecture 2: Analysis of Algorithms I
DOCX
Os lab file c programs
PPTX
Asymptotic Notations
PDF
Automata
Algorithm analysis in fundamentals of data structure
Traveling salesman problem
Stacks IN DATA STRUCTURES
Moore and mealy machine
Algorithms Lecture 2: Analysis of Algorithms I
Os lab file c programs
Asymptotic Notations
Automata

What's hot (20)

PPT
Complexity of Algorithm
PDF
Recurrence relation solutions
PPT
Time complexity
PPT
Divide and conquer
PPT
Fuzzy c-means clustering for image segmentation
PPTX
Bruteforce algorithm
PPTX
Traveling salesman problem
PPTX
Top down parsering and bottom up parsering.pptx
PPTX
Back tracking and branch and bound class 20
PDF
DAA Notes.pdf
PPTX
Asymptotic notations
PPT
Abstract data types
PPTX
Back patching
PPT
01 knapsack using backtracking
PPT
how to calclute time complexity of algortihm
PPTX
Array Introduction One-dimensional array Multidimensional array
PPTX
Turing machine by_deep
PPTX
PPTX
DFS and BFS
PPT
Data structure
Complexity of Algorithm
Recurrence relation solutions
Time complexity
Divide and conquer
Fuzzy c-means clustering for image segmentation
Bruteforce algorithm
Traveling salesman problem
Top down parsering and bottom up parsering.pptx
Back tracking and branch and bound class 20
DAA Notes.pdf
Asymptotic notations
Abstract data types
Back patching
01 knapsack using backtracking
how to calclute time complexity of algortihm
Array Introduction One-dimensional array Multidimensional array
Turing machine by_deep
DFS and BFS
Data structure
Ad

Similar to Recursion (20)

PDF
6-Python-Recursion.pdf
PPTX
Recursion
PPTX
6-Python-Recursion PPT.pptx
PPTX
Recurrent Neural Networks (RNNs)
PPTX
RECURSION.pptx
PPT
3. Recursion and Recurrences.ppt detail about recursive learning
PPT
RecursionWeek8.ppt
PPTX
recurrent nural networks in best teaching way
PPTX
Recursion
KEY
Playing Go with Clojure
PPS
Presentation of GetTogether on Functional Programming
PPTX
Searching using Quantum Rules
PPTX
Advanced functions ppt (Chapter 1) part i
PPTX
Advanced functions part i
PPT
cos323_s06_lecture03_optimization.ppt
PDF
PowerPoint Presentation
PDF
PowerPoint Presentation
PPTX
PPTX
Pseudo and Quasi Random Number Generation
PDF
Optim_methods.pdf
6-Python-Recursion.pdf
Recursion
6-Python-Recursion PPT.pptx
Recurrent Neural Networks (RNNs)
RECURSION.pptx
3. Recursion and Recurrences.ppt detail about recursive learning
RecursionWeek8.ppt
recurrent nural networks in best teaching way
Recursion
Playing Go with Clojure
Presentation of GetTogether on Functional Programming
Searching using Quantum Rules
Advanced functions ppt (Chapter 1) part i
Advanced functions part i
cos323_s06_lecture03_optimization.ppt
PowerPoint Presentation
PowerPoint Presentation
Pseudo and Quasi Random Number Generation
Optim_methods.pdf
Ad

More from grahamwell (20)

PPTX
Pseudocode
PPT
Excel =if function
PPT
Excel Min max-average
PPTX
What is binary and why do we use it?
PPTX
Introduction to touch develop
PPTX
Introduction to touch develop
PPTX
The software story
PPTX
Turtle graphics
PPTX
Database field types
PPTX
Databases 101
PPTX
Kodu controls
PPT
Pascal names and types
PPT
Python part two names and types
PPTX
Abstraction - Year 9
PPTX
Thinking about your project
PPTX
The rail fence
PPT
Lesson 1
PPT
Rsa encryption
PPT
Server side scripts
PPTX
Revision topic 1 sensors and control
Pseudocode
Excel =if function
Excel Min max-average
What is binary and why do we use it?
Introduction to touch develop
Introduction to touch develop
The software story
Turtle graphics
Database field types
Databases 101
Kodu controls
Pascal names and types
Python part two names and types
Abstraction - Year 9
Thinking about your project
The rail fence
Lesson 1
Rsa encryption
Server side scripts
Revision topic 1 sensors and control

Recursion

  • 2. What is recursion • Looping without a loop statement • A function that is part of its own definition • Can only work if there’s a terminating condition, otherwise it goes forever (the base case)
  • 3. A recursive function • N Factorial • 1! = 1 2! = 1 x 2 = 2 3! = 1 x 2 x 3 = 2! x 3 = 6 N! = 1 x 2 x 3 x .... (N-2) x (N-1) x N = (N-1)! x N
  • 4. How recursion is handled • Every time a function is called, the function values, local variables, parameters and return addresses are pushed onto the stack. • Over and Over again • You might run out!
  • 5. Dry-running a recursive call Procedure Printsequence(n) n <- n-1 if n > 1 then Printsequence(n) endif output(n) EndProcedure
  • 6. Dry-running a recursive call Procedure Printsequence(n) n <- n-1 if n > 1 then output(n) Printsequence(n) endif EndProcedure
  • 7. How to write a recursive procedure • Code the general case Result = n * Factorial(n-1) • Code the base case Result = 1 • Ensure that the base case is reached after a finite number of recursive calls