chapter 2 recursive algorithms. 2 chapter outline analyzing recursive algorithms recurrence...

46
Chapter 2 Recursive Algorithms

Upload: randell-hudson

Post on 15-Jan-2016

274 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chapter 2 Recursive Algorithms. 2 Chapter Outline Analyzing recursive algorithms Recurrence relations Closest pair algorithms Convex hull algorithms Generating

Chapter 2

Recursive Algorithms

Page 2: Chapter 2 Recursive Algorithms. 2 Chapter Outline Analyzing recursive algorithms Recurrence relations Closest pair algorithms Convex hull algorithms Generating

2

Chapter Outline

• Analyzing recursive algorithms

• Recurrence relations

• Closest pair algorithms

• Convex hull algorithms

• Generating permutations

• Recursion and stacks

Page 3: Chapter 2 Recursive Algorithms. 2 Chapter Outline Analyzing recursive algorithms Recurrence relations Closest pair algorithms Convex hull algorithms Generating

3

Prerequisites

• Before beginning this chapter, you should be able to:– Read and create recursive algorithms– Identify comparison and arithmetic

operations– Use basic algebra

Page 4: Chapter 2 Recursive Algorithms. 2 Chapter Outline Analyzing recursive algorithms Recurrence relations Closest pair algorithms Convex hull algorithms Generating

4

Goals

• At the end of this chapter you should be able to:– Create the recurrence relation for a

recursive algorithm– Convert a simple recurrence relation into

closed form– Explain the closest pair algorithm

Page 5: Chapter 2 Recursive Algorithms. 2 Chapter Outline Analyzing recursive algorithms Recurrence relations Closest pair algorithms Convex hull algorithms Generating

5

Goals (continued)

– Explain the convex hull algorithm– Generate permutations both recursively

and iteratively– Explain the relationship between recursion

and stacks

Page 6: Chapter 2 Recursive Algorithms. 2 Chapter Outline Analyzing recursive algorithms Recurrence relations Closest pair algorithms Convex hull algorithms Generating

6

Recursive Algorithms

• Recursive algorithms solve the problem by solving smaller versions of the problem– If the smaller versions are only a little

smaller, the algorithm can be called a reduce and conquer algorithm

– If the smaller versions are about half the size of the original, the algorithm can be called a divide and conquer algorithm

Page 7: Chapter 2 Recursive Algorithms. 2 Chapter Outline Analyzing recursive algorithms Recurrence relations Closest pair algorithms Convex hull algorithms Generating

7

Recursive Algorithm Analysis

• The analysis depends on– the preparation work to divide the input– the size of the smaller pieces– the number of recursive calls– the concluding work to combine the results

of the recursive calls

Page 8: Chapter 2 Recursive Algorithms. 2 Chapter Outline Analyzing recursive algorithms Recurrence relations Closest pair algorithms Convex hull algorithms Generating

8

Generic Recursive Algorithm

Recurse( data, N, solution )// data a set of input values// N the number of values in the set// solution the solution to this problemif N ≤ SizeLimit then

DirectSolution( data, N, solution )else

DivideInput( data, N, smallerSets, smallerSizes, numberSmaller )

for i = 1 to numberSmaller do Recurse(smallerSets[i], smallerSizes[i],

smallSolution[i]) end for CombineSolutions(smallSolution,

numberSmaller, solution)end if

Page 9: Chapter 2 Recursive Algorithms. 2 Chapter Outline Analyzing recursive algorithms Recurrence relations Closest pair algorithms Convex hull algorithms Generating

9

Generic Recursive Algorithm Analysis

• The recursive algorithm does the following amount of work:

• Where– DIR(N) is the amount of work done by the direct solution– DIV(N) is the amount of work done by the division of the

problem– COM(N) is the amount of work done to combine the

solutions

Page 10: Chapter 2 Recursive Algorithms. 2 Chapter Outline Analyzing recursive algorithms Recurrence relations Closest pair algorithms Convex hull algorithms Generating

10

Divide and Conquer ExampleLargest( list, start, end )if start ≥ end then return list[end]

end ifmiddle = (start + end) / 2first = Largest( list, start, middle )second = Largest( list, middle+1, end )if first > second thenreturn first

elsereturn second

end if

Page 11: Chapter 2 Recursive Algorithms. 2 Chapter Outline Analyzing recursive algorithms Recurrence relations Closest pair algorithms Convex hull algorithms Generating

11

Divide and Conquer Example Analysis

• This algorithm does:– One addition and division to calculate

middle– Two recursive calls with lists about half the

original size– One comparison to decide between the

largest of the first half and the largest of the second half

Page 12: Chapter 2 Recursive Algorithms. 2 Chapter Outline Analyzing recursive algorithms Recurrence relations Closest pair algorithms Convex hull algorithms Generating

12

Divide and Conquer Example Analysis

• The number of comparisons of list values done by this example is given by:

1 for1 2C2

1 for0 )(C

nn

nn

Page 13: Chapter 2 Recursive Algorithms. 2 Chapter Outline Analyzing recursive algorithms Recurrence relations Closest pair algorithms Convex hull algorithms Generating

13

Recurrence Relation Form

• A recurrence relation is a recursive form of an equation, for example:

• A recurrence relation can be put into an equivalent closed form without the recursion

2 )1(T )(T

3 )1(T

nn

Page 14: Chapter 2 Recursive Algorithms. 2 Chapter Outline Analyzing recursive algorithms Recurrence relations Closest pair algorithms Convex hull algorithms Generating

14

Converting Recurrence Relations

• Begin by looking at a series of equations with decreasing values of n:

2 )5(T )4(T

2 )4(T )3(T

2 )3(T )2(T

2 )2(T )1(T

2 )1(T )(T

nn

nn

nn

nn

nn

Page 15: Chapter 2 Recursive Algorithms. 2 Chapter Outline Analyzing recursive algorithms Recurrence relations Closest pair algorithms Convex hull algorithms Generating

15

Converting Recurrence Relations

• Now, we substitute back into the first equation:

2 )2 )2 )2 )2 )5(T(((( )(T

2 )2 )2 )2 )4(T((( )(T

2 )2 )2 )3(T(( )(T

2 )2 )2(T( )(T

2 )1(T )(T

nn

nn

nn

nn

nn

Page 16: Chapter 2 Recursive Algorithms. 2 Chapter Outline Analyzing recursive algorithms Recurrence relations Closest pair algorithms Convex hull algorithms Generating

16

Converting Recurrence Relations

• We stop when we get to T(1):

• How many “+ 2” terms are there? Notice we increase them with each substitution.

2 )2 )2 )2 )1(T((( )(T

2 )2 )2(T( )(T

2 )1(T )(T

n

nn

nn

Page 17: Chapter 2 Recursive Algorithms. 2 Chapter Outline Analyzing recursive algorithms Recurrence relations Closest pair algorithms Convex hull algorithms Generating

17

Converting Recurrence Relations

• We must have n – 1 of the “+ 2” terms because there was one at the start and we did n – 2 substitutions:

• So, the closed form of the equation is:

1

12 )1(T )(T

n

in

)1(2 3 )(T nn

Page 18: Chapter 2 Recursive Algorithms. 2 Chapter Outline Analyzing recursive algorithms Recurrence relations Closest pair algorithms Convex hull algorithms Generating

18

Approximating Recurrence Relations

• For recurrence relations of the formT(N) = a * T(N/b) + f(N)

where f(N) = Θ(Nd), the closed form can be approximated by:

da

dd

dd

baN

baNN

baN

N

b if

iflg

if

)(T

log

Page 19: Chapter 2 Recursive Algorithms. 2 Chapter Outline Analyzing recursive algorithms Recurrence relations Closest pair algorithms Convex hull algorithms Generating

19

Closest Pair Problem

• Given a set of N points in space, which two are the closest?

• A brute force method calculates the distance between every pair of points using:

• But this does (N2 – N)/2 distance calculations

2122

1221,d yyxxpp

Page 20: Chapter 2 Recursive Algorithms. 2 Chapter Outline Analyzing recursive algorithms Recurrence relations Closest pair algorithms Convex hull algorithms Generating

20

Brute Force Algorithm

smallDist = ∞for i = 1 to N do

for j = i+1 to N dodist = sqrt( (xi – xj)2 + (yi –

yj)2)if dist < smallDist then

smallDist = distfirst = isecond = j

end ifend for

end for

Page 21: Chapter 2 Recursive Algorithms. 2 Chapter Outline Analyzing recursive algorithms Recurrence relations Closest pair algorithms Convex hull algorithms Generating

21

A Divide and Conquer Solution

• Divide the set of points in into a right and left half

• Find the closest pair in the right and left half (recursively)

• Determine (ds) the shortest of these two distances

• Check if there is a pair of points within ds units but on opposite sides of the dividing line that are closer

Page 22: Chapter 2 Recursive Algorithms. 2 Chapter Outline Analyzing recursive algorithms Recurrence relations Closest pair algorithms Convex hull algorithms Generating

22

Divide and Conquer Example

Page 23: Chapter 2 Recursive Algorithms. 2 Chapter Outline Analyzing recursive algorithms Recurrence relations Closest pair algorithms Convex hull algorithms Generating

23

Divide and Conquer Example

Page 24: Chapter 2 Recursive Algorithms. 2 Chapter Outline Analyzing recursive algorithms Recurrence relations Closest pair algorithms Convex hull algorithms Generating

24

Divide and Conquer Example

Page 25: Chapter 2 Recursive Algorithms. 2 Chapter Outline Analyzing recursive algorithms Recurrence relations Closest pair algorithms Convex hull algorithms Generating

25

Divide and Conquer Example

Page 26: Chapter 2 Recursive Algorithms. 2 Chapter Outline Analyzing recursive algorithms Recurrence relations Closest pair algorithms Convex hull algorithms Generating

26

Divide and Conquer Closest PairPart 1

ClosestPair( Px, Py, d, p1, p2)// Px the points sorted by x coordinate// Py the points sorted by y coordinate// d the shortest distance between points p1 and p2// p1 the first point// p2 the second point

if sizeOf(Px) 3 thenfind d, p1, and p2 by brute forcereturn

end if

construct Lx, Ly, Rx, RyClosestPair(Lx, Ly, dl, L1, L2)ClosestPair(Rx, Ry, dr, R1, R2)

Page 27: Chapter 2 Recursive Algorithms. 2 Chapter Outline Analyzing recursive algorithms Recurrence relations Closest pair algorithms Convex hull algorithms Generating

27

Divide and Conquer ClosestPair 2

d = minimum(dl, dr)if d == dl then

p1 = L1p2 = L2

elsep1 = R1p2 = R2

end if

construct Mx and Myfor i = 1 to sizeOf(My)-1 do

for j = i+1 to i+7 (with j sizeOf(My)) dotemp = distance(Myi, Myj)if temp < d then

d = tempp1 = Myip2 = Myj

end ifend for

end for

Page 28: Chapter 2 Recursive Algorithms. 2 Chapter Outline Analyzing recursive algorithms Recurrence relations Closest pair algorithms Convex hull algorithms Generating

28

Divide and Conquer Closest Point Analysis

• The combination step looks at the seven points closest to those points within ds of the dividing line

• The points are divided into two halves

• Therefore, the recurrence relation isT(N) = 2 * T(N/2) + (N)

• This algorithm is (N lg N)

Page 29: Chapter 2 Recursive Algorithms. 2 Chapter Outline Analyzing recursive algorithms Recurrence relations Closest pair algorithms Convex hull algorithms Generating

29

Convex Regions

• A region is convex if a line connecting every pair of points in the region lies entirely within the region

Page 30: Chapter 2 Recursive Algorithms. 2 Chapter Outline Analyzing recursive algorithms Recurrence relations Closest pair algorithms Convex hull algorithms Generating

30

Convex Hull

• A convex hull for a set of points is the smallest convex region including all of the points

• All of the points lie on one side of each edge of the convex hull

Page 31: Chapter 2 Recursive Algorithms. 2 Chapter Outline Analyzing recursive algorithms Recurrence relations Closest pair algorithms Convex hull algorithms Generating

31

Brute Force Convex Hull

• Consider the line defined by the first pair of points

• If all of the other points lie on one side of that line, the line is part of the convex hull

• Repeat this process for every other pair of points

• This process is O(N3)

Page 32: Chapter 2 Recursive Algorithms. 2 Chapter Outline Analyzing recursive algorithms Recurrence relations Closest pair algorithms Convex hull algorithms Generating

32

A Divide and Conquer Solution

• The leftmost and rightmost points are on the convex hull

• Use the line between these points to divide the set of points into an upper and lower set

• Find the convex hull of the two parts• The convex hull of the entire set is the

convex hulls of these two parts without the dividing line

Page 33: Chapter 2 Recursive Algorithms. 2 Chapter Outline Analyzing recursive algorithms Recurrence relations Closest pair algorithms Convex hull algorithms Generating

33

A Divide and Conquer Solution

• To find the convex hull of each part– Find the point farthest away from the

dividing line– If the points are inside the triangle formed

with this point, this is the convex hull– If there are points outside one of these two

edges, recursively find the convex hull of those points

– If necessary, repeat for the other edge

Page 34: Chapter 2 Recursive Algorithms. 2 Chapter Outline Analyzing recursive algorithms Recurrence relations Closest pair algorithms Convex hull algorithms Generating

34

Divide and Conquer Example

Page 35: Chapter 2 Recursive Algorithms. 2 Chapter Outline Analyzing recursive algorithms Recurrence relations Closest pair algorithms Convex hull algorithms Generating

35

Divide and Conquer Example

Page 36: Chapter 2 Recursive Algorithms. 2 Chapter Outline Analyzing recursive algorithms Recurrence relations Closest pair algorithms Convex hull algorithms Generating

36

Divide and Conquer Example

Page 37: Chapter 2 Recursive Algorithms. 2 Chapter Outline Analyzing recursive algorithms Recurrence relations Closest pair algorithms Convex hull algorithms Generating

37

Divide and Conquer Convex Hull

quickHull(S, ps, pe)if S == {} then

return [] // the empty listelse

pf = point of S farthest from the dividing linePR = set of points to the right of the line

between ps and pfPL = set of points to the right of the line

between pf and pereturn quickHull(PR, ps, pf) + [pf]

+ quickHull(PL, pf, pe)end if

Page 38: Chapter 2 Recursive Algorithms. 2 Chapter Outline Analyzing recursive algorithms Recurrence relations Closest pair algorithms Convex hull algorithms Generating

38

Divide and Conquer Convex Hull Analysis

• If the algorithm divides the points into two halves, the recurrence relation isT(N) = 2 * T(N/2) + (N) and the closed form is (N lg N)

• In the worst case, all the points wind up on one side of each dividing line and the algorithm is then O(N2)

Page 39: Chapter 2 Recursive Algorithms. 2 Chapter Outline Analyzing recursive algorithms Recurrence relations Closest pair algorithms Convex hull algorithms Generating

39

Permutations

• The permutation of a set of elements is an ordering of those elements

• The permutations of the numbers from 1 to 3 are [1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], and [3, 2, 1]

• If there are N elements in the set, there are N! permutations

Page 40: Chapter 2 Recursive Algorithms. 2 Chapter Outline Analyzing recursive algorithms Recurrence relations Closest pair algorithms Convex hull algorithms Generating

40

Recursive Permutations

• Swap pairs of list values during the recursion

• Output the list when the “bottom” of the recursion is reached

• The algorithm by Heap will permute the elements at the front of the list and will then swap in the next element

• It then permutes the front elements again

Page 41: Chapter 2 Recursive Algorithms. 2 Chapter Outline Analyzing recursive algorithms Recurrence relations Closest pair algorithms Convex hull algorithms Generating

41

Recursive Permutations Algorithm

heapPermute(n)// list is a global variableif n == 0 then

output listelse

for i = 1 to n doheapPermute(n-1)if n is odd then

swap list[1] and list[n]else

swap list[i] and list[n]end if

end forend if

Page 42: Chapter 2 Recursive Algorithms. 2 Chapter Outline Analyzing recursive algorithms Recurrence relations Closest pair algorithms Convex hull algorithms Generating

42

Iterative Permutations

• Permutations can be listed in lexical order

• The position within this list is a permutation’s rank in the range [0, N! – 1]

• It is possible to iteratively generate the permutation from this rank number by using the factorial of the values from 1 to N

Page 43: Chapter 2 Recursive Algorithms. 2 Chapter Outline Analyzing recursive algorithms Recurrence relations Closest pair algorithms Convex hull algorithms Generating

43

Iterative Permutations Algorithm

list[size] = 1for j = 1 to size – 1 dod = (rank mod f[j + 1]) / f[j]rank = rank – d * f[j]list[size – j] = d+1for i = size – j + 1 to size do

if list[i] > d thenlist[i] = list[i] + 1

end ifend for

end for

Page 44: Chapter 2 Recursive Algorithms. 2 Chapter Outline Analyzing recursive algorithms Recurrence relations Closest pair algorithms Convex hull algorithms Generating

44

Recursion and Stacks

• Every subprogram has an Activation Record (AR) that includes the space needed for parameters, local variables, a return value, and a place to return control when done

• Every time a subprogram is called during execution, an instance of this AR is created and placed on the system stack

Page 45: Chapter 2 Recursive Algorithms. 2 Chapter Outline Analyzing recursive algorithms Recurrence relations Closest pair algorithms Convex hull algorithms Generating

45

Recursion and Stacks

• When a subprogram completes, its AR instance (ARI) is removed from the system stack

• For recursive subprograms, there will be multiple ARIs on the stack – one for each call

Page 46: Chapter 2 Recursive Algorithms. 2 Chapter Outline Analyzing recursive algorithms Recurrence relations Closest pair algorithms Convex hull algorithms Generating

46

Recursion and Stacks

• A recursive subprogram could be rewritten to remove recursion by using a programmer created stack to keep track of what would be on the system stack

• Tracing recursive subprograms is easier if you simulate the system stack by drawing boxes when subprograms are called and crossing them out when the subprogram finishes