Introduction to Recursion - PowerPoint PPT Presentation

About This Presentation
Title:

Introduction to Recursion

Description:

Introduction to Recursion Intro to Computer Science CS1510 Dr. Sarah Diesburg Sorting Complexity Three sorting algorithms Bubble Sort Insertion Sort Merge Sort All ... – PowerPoint PPT presentation

Number of Views:111
Avg rating:3.0/5.0
Slides: 24
Provided by: SystemA384
Learn more at: http://www.cs.uni.edu
Category:

less

Transcript and Presenter's Notes

Title: Introduction to Recursion


1
Introduction to Recursion
  • Intro to Computer Science
  • CS1510
  • Dr. Sarah Diesburg

2
Sorting Complexity
  • Three sorting algorithms
  • Bubble Sort
  • Insertion Sort
  • Merge Sort
  • All these algorithms are approximately n2
  • Can we do better?

3
Sorting Programming Assignments
  • Lets say I have 16 programming assignments, and
    I need to sort them in alphabetical order
  • Im lazy, so I hand off half of the assignments
    to one student to sort, and the other half to
    another student to sort
  • How many comparisons do I have to make to merge
    both sorted paper stacks?

4
Sorting Programming Assignments
  • Lets say I have 16 programming assignments, and
    I need to sort them in alphabetical order
  • Im lazy, so I hand off half of the assignments
    to one student to sort, and the other half to
    another student to sort
  • How many comparisons do I have to make to merge
    both sorted paper stacks?
  • Worst case 1 pass, around n comparisons

5
What if everyone hands off the work?
A
16
B
C
6
What if everyone hands off the work?
A
16
B
C
8
8
D
E
F
G
7
What if everyone hands off the work?
A
16
B
C
8
8
D
E
F
G
4
4
4
4
H
2
I
1
8
What if everyone hands off the work?
How long does this take? 5 rows of 16 units of
work each 16580 Looks better than n-squared!
A
16
B
C
8
8
D
E
F
G
4
4
4
4
H
2
I
1
9
How can we build a Merge Sort?
  • We need to understand a new concept called
    recursion

10
A Function that Calls Itself
  • The very basic meaning of a recursive function is
    a function that calls itself.
  • However, when you first see it, it looks odd.
  • Look at the recursive function that calculates
    factorial (code listing 16-2).

11
Code Listing 16-2
  • def factorial(n)
  • """Recursive factorial."""
  • if n 1
  • return 1
  • else
  • return n factorial(n-1)

12
It Doesnt Do Anything!
  • This is a common complaint when one first sees a
    recursive function What exactly is it doing? It
    doesnt seem to do anything!
  • Our goal is to understand what it means to write
    a recursive function from a programmers and
    computers point of view.

13
Defining a Recursive Function
14
1) Divide and Conquer
  • Recursion is a natural outcome of a divide and
    conquer approach to problem solving.
  • A recursive function defines how to break a
    problem down (divide) and how to reassemble
    (conquer) the sub-solutions into an overall
    solution.

15
2) Base Case
  • Recursion is a process not unlike loop iteration.
  • You must define how long (how many iterations)
    recursion will proceed until it stops.
  • The base case defines this limit.
  • Without the base case, recursion will continue
    infinitely (just like an infinite loop).

16
Simple Example
  • Lao-Tzu A journey of 1000 miles begins with a
    single step.
  • def journey (steps)
  • the first step is easy (base case)
  • the nth step is easy having completed the
    previous n-1 steps (divide and conquer)

17
Code Listing 16-1
  • def takeStep(n)
  • if n 1 base case
  • return "Easy"
  • else
  • thisStep "step(" str(n) ")
  • divide step, recurse
  • previousSteps takeStep(n-1)
  • conquer step
  • return thisStep " " previousSteps

18
Factorial
  • You remember factorial?
  • Factorial(4) 4! 4 3 2 1
  • The result of the last step, multiply by 1, is
    defined based on all the previous results that
    have been calculated.

19
Code Listing 16-2
  • def factorial(n)
  • """Recursive factorial."""
  • if n 1
  • return 1
  • else
  • return n factorial(n-1)

20
Trace the Calls
  • 4! 4 3! start first function invocation
  • 3! 3! 2 start second function invocation
  • 2! 2 1! start third function invocation
  • 1! 1 fourth invocation, base case
  • 2! 2 1 2 third invocation finishes
  • 3! 3 2 6 second invocation finishes
  • 4! 4 6 24 first invocation finishes

21
Another Fibonacci
  • Depends on the Fibo results for the two previous
    values in the sequence.
  • The base values are Fibo(0) 1 and Fibo(1)
    1.
  • fibo (x) fibo(x-1) fibo(x-2)

22
Code Listing 16-3
  • def fibo(n)
  • """Recursive Fibonacci sequence."""
  • if n 0 or n 1 base case
  • return 1
  • else
  • divide and conquer
  • return fibonacci(n-1) fibonacci(n-2)

23
Trace
  • fibo(4) fibo(3) fibo(2)
  • fibo(3) fibo(2) fibo(1)
  • fibo(2) fibo(1) fibo(0) 2 base case
  • fibo(3) 2 fibo(1) 3 base case
  • fibo(4) 3 2 5
Write a Comment
User Comments (0)
About PowerShow.com