4.4 recursive algorithms a recursive algorithm is one which calls itself to solve “smaller”...

13
4.4 Recursive Algorithms • A recursive algorithm is one which calls itself to solve “smaller” versions of an input problem. • How it works: – The current status of the algorithm is placed on a stack. •A stack is a data structure from which entries can be added and deleted only from one end. 1

Upload: donald-griffin

Post on 31-Dec-2015

226 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 4.4 Recursive Algorithms A recursive algorithm is one which calls itself to solve “smaller” versions of an input problem. How it works: – The current status

4.4 Recursive Algorithms• A recursive algorithm is one which calls itself

to solve “smaller” versions of an input problem.

• How it works:– The current status of the algorithm is placed on a

stack.• A stack is a data structure from which entries

can be added and deleted only from one end.

1

Page 2: 4.4 Recursive Algorithms A recursive algorithm is one which calls itself to solve “smaller” versions of an input problem. How it works: – The current status

• like the plates in a cafeteria:• PUSH: put a 'plate' on the stack.• POP: take a 'plate' off the stack.

• When an algorithm calls itself, the current activation is suspended in time and its parameters are PUSHed on a stack.

• The set of parameters need to restore the algorithm to its current activation is called an activation record.

2

Page 3: 4.4 Recursive Algorithms A recursive algorithm is one which calls itself to solve “smaller” versions of an input problem. How it works: – The current status

• Example:procedure factorial (n)/* make the procedure idiot proof */

if n < 0 return 'error'if n = 0 then return 1

elsereturn n factorial (n-1)

• Algorithm 1 A Recursive Algorithm for Computing n! procedure factorial (n: nonnegative integers) if n=0 then factorial(n) :=1 else factorial(n) := n . factorial(n-1)

3

Page 4: 4.4 Recursive Algorithms A recursive algorithm is one which calls itself to solve “smaller” versions of an input problem. How it works: – The current status

The operating system supplies all the necessary facilities to produce:factorial (3): PUSH 3 on stack and call

factorial (2): PUSH 2 on stack and callfactorial (1): PUSH 1 on stack and call

factorial (0): return 1POP 1 from stack and return (1) (1)

POP 2 from the stack and return (2) [(1) (1)]POP 3 from the stack and return (3) [(2) [(1) (1)]]

• Complexity:• Let f(n) be the number of multiplications required to

compute factorial (n).f(0) = 0: the initial conditionf(n) = 1 + f(n-1): the recurrence equation

4

Page 5: 4.4 Recursive Algorithms A recursive algorithm is one which calls itself to solve “smaller” versions of an input problem. How it works: – The current status

• Hw :example 2 (p.312) Give a recursive algorithm for computing an, where a is a nonzero real number and n is a nonnegative integer.

• Example:• A recursive procedure to find the max of a nonvoid

list.• Assume we have a built-in functions called– Length which returns the number of elements in a

list– Max which returns the larger of two values– Listhead which returns the first element in a list– Remain which returns the remainder of the list

• Max requires one comparison.

5

Page 6: 4.4 Recursive Algorithms A recursive algorithm is one which calls itself to solve “smaller” versions of an input problem. How it works: – The current status

procedure maxlist (list)/* strip off head of list and pass the remainder */if Length(list) = 1 then

return Listhead(list)else

return Max( Listhead(list), maxlist(Remain (list)))

• The recurrence equation for the number of comparisons required for a list of length n, f(n), is

f(1) = 0f(n) = 1 + f(n-1)

6

Page 7: 4.4 Recursive Algorithms A recursive algorithm is one which calls itself to solve “smaller” versions of an input problem. How it works: – The current status

• Example: If we assume the length is a power of 2:– We divide the list in half and find the maximum of

each half.– Then find the Max of the maximum of the two

halves.procedure maxlist2 (list)/* a divide and conquer approach */

if Length (list) = 1 thenreturn Listhead(list)

elsea = maxlist2 (fist half of list)b = maxlist2 (second half of list)

return Max{a, b}7

Page 8: 4.4 Recursive Algorithms A recursive algorithm is one which calls itself to solve “smaller” versions of an input problem. How it works: – The current status

• Recurrence equation for the number of comparisons required for a list of length n, f(n), isf(1) = 0f(n) = 2 f(n/2) + 1

• There are two calls to maxlist each of which requires f(n/2) operations to find the max.

• There is one comparison required by the Max function.

8

Page 9: 4.4 Recursive Algorithms A recursive algorithm is one which calls itself to solve “smaller” versions of an input problem. How it works: – The current status

Recursion and Iteration• Algorithm 7 A Recursive

Algorithm for Fibonacc Numbers. procedure fibonacci (n:

nonnegative integers) if n=0 then

fibonacci(0) :=0 else if n=1 then

fibonacci(1) :=1 else fibonacci(n) :=

fibonacci(n-1) + fibonacci(n-2)

• Algorithm 8 An Iterative Algorithm for Computing Fibonacci Numbers.

procedure iterative fibonacci (n: nonnegative integers)

if n=0 then y :=0 else begin x :=0 y :=1 for i :=2 to n begin z :=x+y x :=y y :=z endEnd{z is the nth Fibonacci number}

9

Page 10: 4.4 Recursive Algorithms A recursive algorithm is one which calls itself to solve “smaller” versions of an input problem. How it works: – The current status

Recursive Algorithm for Fibonacci Number

10

Page 11: 4.4 Recursive Algorithms A recursive algorithm is one which calls itself to solve “smaller” versions of an input problem. How it works: – The current status

The Merge Sort• Example 9: Sort the list 8, 2, 4, 6, 9, 7, 10, 1, 5, 3 using

the merge sort.• Algorithm 9 A Recursive Merge Sort.Procedure mergesort(L= a1, . . . , an)If n=1 then return LIf n> 1 then Begin m :=n/2 L1 := a1, a2, . . . ,am

L2 := am+1, am+2, . . ., an

return merge(mergesort(L1), mergesort(L2)) End{The return value is now sorted into nondecreasing order}

11

Page 12: 4.4 Recursive Algorithms A recursive algorithm is one which calls itself to solve “smaller” versions of an input problem. How it works: – The current status

FIGURE 2 The Merge Sort of 8,2,4,6,9,7,10,1,5,3.

12

Page 13: 4.4 Recursive Algorithms A recursive algorithm is one which calls itself to solve “smaller” versions of an input problem. How it works: – The current status

13