Recursion Tessema M. Mengistu Department of Computer Science - - PowerPoint PPT Presentation

recursion
SMART_READER_LITE
LIVE PREVIEW

Recursion Tessema M. Mengistu Department of Computer Science - - PowerPoint PPT Presentation

Recursion Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale tessema.mengistu@siu.edu Room - 3131 1 Outline What Is Recursion? Tracing a Recursive Method Recursive Methods that Return a


slide-1
SLIDE 1

Recursion

1

Tessema M. Mengistu Department of Computer Science Southern Illinois University Carbondale tessema.mengistu@siu.edu Room - 3131

slide-2
SLIDE 2

Outline

  • What Is Recursion?
  • Tracing a Recursive Method
  • Recursive Methods that Return a Value
  • Recursively Processing an Array
  • Recursively Processing a Linked List
  • The Time Efficiency of Recursive Methods

2

slide-3
SLIDE 3

Recursion

  • Repetition is a major feature of many algorithms

– Iterative – Recursive

  • We often solve a problem by breaking it into

smaller problems – divide and conquer

  • When the smaller problems are identical (except

for size)

– This is called “recursion”

  • Repeated smaller problems

– Until problem with known solution is reached

3

slide-4
SLIDE 4

Recursion

  • Example – factorial of a positive number n

fact(4) = 4 * fact(3) = 24 3* fact(2) = 6 2 *fact(1) = 2 fact(1) =1

  • In general

fact(n) = n* fact(n-1) n-1*fact(n-2) . . . 2*fact(1)

4

slide-5
SLIDE 5

Recursion

  • Recursion is a problem solving process that

breaks a problem into identical but smaller problems.

  • A method that calls itself is a recursive

method.

  • The invocation is a recursive call or recursive

invocation.

5

slide-6
SLIDE 6

Recursion

  • Example

6

slide-7
SLIDE 7

Design of Recursive Solution

  • What

part

  • f

solution can you contribute directly?

  • What smaller (identical) problem has solution

that …

– When taken with your contribution – Provides the solution to the original problem

  • When does process end?

– What smaller but identical problem has known solution – Have you reached this problem, or base case?

7

slide-8
SLIDE 8

Design Guidelines

  • Method must receive input value
  • Must contain logic that involves this input

value and leads to different cases

  • One or more cases should provide solution

that does not require recursion

– Base case or stopping case

  • One or more cases must include recursive

invocation of method – recursion call

8

slide-9
SLIDE 9

Recursive Method the returns a value

  • Example

– Write a recursive method that displays the sum of the first n positive integers – Compute the sum 1 + 2 + . . . + n

9

slide-10
SLIDE 10

Design Guidelines

  • A recursive method that does not check for a

base case, or that misses the base case is a logic error

– Infinite recursion

10

slide-11
SLIDE 11

Tracing Recursive Method

  • Given recursive countDown method
  • For 3

11

slide-12
SLIDE 12

12

slide-13
SLIDE 13

Tracing Recursive Method

13

slide-14
SLIDE 14

Tracing Recursive Method

  • A recursive method uses more memory than

an iterative method, in general, because each recursive call generates an activation record.

– Results in “stack overflow” – Infinite recursion or large-size problems are the likely causes of this error

14

slide-15
SLIDE 15

Display an Array Recursively

  • Using first element
  • Using last element

15

slide-16
SLIDE 16

Display an Array Recursively

  • Using the middle element

16

slide-17
SLIDE 17

Displaying a Bag Recursively

  • When implemented with an array

17

slide-18
SLIDE 18

Recursively Processing a Linked Chain

  • Method display, implemented recursively

18

slide-19
SLIDE 19

Recursively Processing a Linked Chain

  • Recursive method to display the linked chain

in reverse order

19

slide-20
SLIDE 20

Time Efficiency of Recursive Methods

  • Consider

the method countDown() that displays all positive numbers up to n

  • It has time complexity of

20

slide-21
SLIDE 21

Time Efficiency of Recursive Methods

  • The equation for t(n) is called a recurrence

relation

– The definition of the function t contains an

  • ccurrence of itself
  • How can we proof t(n) = n?

– Proof by induction

  • Assume t(n-1) = n-1 for n>1
  • t(n) = 1+t(n-1) = 1 +n-1 =n
  • Therefore t(n) = n for n>1

– So countDown() is O(n)

21

slide-22
SLIDE 22

Time Efficiency of Computing xn

  • Recursive calculation for xn
  • Can be shown
  • Thus efficiency is O(log n)
  • Proof – Reading Assignment

22

slide-23
SLIDE 23

Simple Solution to a Difficult Problem

  • Consider Towers of Hanoi puzzle

23

slide-24
SLIDE 24

Towers of Hanoi

  • Rules
  • 1. Move one disk at a time. Each disk you move

must be a topmost disk.

  • 2. No disk may rest on top of a disk smaller than

itself.

  • 3. You can store disks on the second pole

temporarily, as long as you observe the previous two rules.

24

slide-25
SLIDE 25

25

slide-26
SLIDE 26

26

slide-27
SLIDE 27

Solution

  • Move a disk from pole 1 to pole 3
  • Move a disk from pole 1 to pole 2
  • Move a disk from pole 3 to pole 2
  • Move a disk from pole 1 to pole 3
  • Move a disk from pole 2 to pole 1
  • Move a disk from pole 2 to pole 3
  • Move a disk from pole 1 to pole 3

27

slide-28
SLIDE 28

Recursive Solution

  • To solve for n disks …

– Ask friend to solve for n – 1 disks – He in turn asks another friend to solve for n – 2 – Etc. – Each one lets previous friend know when their simpler task is finished

28

slide-29
SLIDE 29

3

29

slide-30
SLIDE 30

Towers of Hanoi

30

slide-31
SLIDE 31

Towers of Hanoi

31

slide-32
SLIDE 32

Algorithm Efficiency

  • Moves required for n disks
  • We note and conjecture

(proved by induction)

32

slide-33
SLIDE 33

Algorithm Efficiency

  • The number of moves required to solve the

Towers of Hanoi problem grows exponentially with the number of disks n.

– m(n) = O(2n)

33

slide-34
SLIDE 34

Poor Solution to a Simple Problem

  • Fibonacci sequence

1, 1, 2, 3, 5, 8, 13, …

34

slide-35
SLIDE 35

Poor Solution to a Simple Problem

  • A recursive solution

Note the two recursive calls

35

slide-36
SLIDE 36

36

slide-37
SLIDE 37

Tail Recursion

  • When the last action performed by a recursive

method is a recursive call

  • Example:
  • Repeats call with change in parameter or variable
slide-38
SLIDE 38

Indirect Recursion

  • Consider chain of events

– Method A calls Method B – Method B calls Method C – and Method C calls Method A

  • Mutual recursion

– Method A calls Method B – Method B calls Method A

38