Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an...

34
Divide-and-Conquer UNC Chapel Hill Z. Guo

Transcript of Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an...

Page 1: Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.

Divide-and-Conquer

UNC Chapel Hill Z. Guo

Page 2: Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.

Divide-and-Conquer

• It’s a technique instead of an algorithm• Recursive in structure – Divide the problem into several smaller sub-

problems that are similar to the original but smaller in size

– Conquer the sub-problems by solving them recursively. If they are small enough, just solve them in a straightforward manner.

– Combine the solutions to create a solution to the original problem

UNC Chapel Hill Z. Guo

Page 3: Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.

Z. Guo

An Example: Merge SortSorting Problem: Sort a sequence or n elements into

non-decreasing order.

• Divide: Divide the n-element sequence to be sorted into two subsequences of n/2 elements each

• Conquer: Sort the two subsequences recursively using merge sort.

• Combine: Merge the two sorted subsequences to produce the sorted answer.

UNC Chapel Hill

Page 4: Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.

Z. Guo

Merge Sort – Example

18 26 32 6 43 15 9 1

18 26 32 6 43 15 9 1

18 26 32 6 43 15 9 1

2618 6 32 1543 1 9

18 26 32 6 43 15 9 1

18 26 32 6 43 15 9 1

18 26 32 6 15 43 1 9

6 18 26 32 1 9 15 43

1 6 9 15 18 26 32 43

18 26

18 26

18 26

32

32

6

6

32 6

18 26 32 6

43

43

15

15

43 15

9

9

1

1

9 1

43 15 9 1

18 26 32 6 43 15 9 1

18 26 6 32

6 26 3218

1543 1 9

1 9 15 43

1 6 9 1518 26 32 43

Original Sequence Sorted Sequence

UNC Chapel Hill

Page 5: Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.

Z. Guo

Merge-Sort (A, p, r)INPUT: a sequence of n numbers stored in array AOUTPUT: an ordered sequence of n numbers

MergeSort (A, p, r) // sort A[p..r] by divide & conquer

1 if p < r2 then q (p+r)/23 MergeSort (A, p, q)4 MergeSort (A, q+1, r)5 Merge (A, p, q, r) // merges A[p..q] with A[q+1..r]

Initial Call: MergeSort(A, 1, n)

UNC Chapel Hill

Page 6: Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.

Z. Guo

Procedure MergeMerge(A, p, q, r)1 n1 q – p + 12 n2 r – q3 for i 1 to n1 4 do L[i] A[p + i – 1]5 for j 1 to n2 6 do R[j] A[q + j]7 L[n1+1] 8 R[n2+1] 9 i 110 j 111 for k p to r12 do if L[i] R[j]13 then A[k] L[i]14 i i + 115 else A[k] R[j]16 j j + 1

Sentinels, to avoid having tocheck if either subarray isfully copied at each step.

Input: Array containing sorted subarrays A[p..q] and A[q+1..r].

Output: Merged sorted subarray in A[p..r].

UNC Chapel Hill

Page 7: Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.

Z. Guo

j

Merge – Example 6 8 26 32 1 9 42 43 … …A

k

6 8 26 32 1 9 42 43

k k k k k k k

i i i i

i j j j j

6 8 26 32 1 9 42 43

1 6 8 9 26 32 42 43

k

L R

UNC Chapel Hill

Page 8: Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.

Z. Guo

Correctness of MergeMerge(A, p, q, r)1 n1 q – p + 12 n2 r – q3 for i 1 to n1 4 do L[i] A[p + i – 1]5 for j 1 to n2 6 do R[j] A[q + j]7 L[n1+1] 8 R[n2+1] 9 i 110 j 111 for k p to r12 do if L[i] R[j]13 then A[k] L[i]14 i i + 115 else A[k] R[j]16 j j + 1

Loop Invariant for the for loopAt the start of each iteration of the for loop: Subarray A[p..k – 1] contains the k – p smallest elementsof L and R in sorted order. L[i] and R[j] are the smallest elements of L and R that have not been copied back into A.

Initialization:Before the first iteration: •A[p..k – 1] is empty.•i = j = 1.•L[1] and R[1] are the smallest elements of L and R not copied to A.

UNC Chapel Hill

Page 9: Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.

Z. Guo

Correctness of MergeMerge(A, p, q, r)1 n1 q – p + 12 n2 r – q3 for i 1 to n1 4 do L[i] A[p + i – 1]5 for j 1 to n2 6 do R[j] A[q + j]7 L[n1+1] 8 R[n2+1] 9 i 110 j 111 for k p to r12 do if L[i] R[j]13 then A[k] L[i]14 i i + 115 else A[k] R[j]16 j j + 1

Maintenance:

Case 1: L[i] R[j]•By LI, A contains p – k smallest elements of L and R in sorted order.•By LI, L[i] and R[j] are the smallest elements of L and R not yet copied into A.•Line 13 results in A containing p – k + 1 smallest elements (again in sorted order).Incrementing i and k reestablishes the LI for the next iteration.Similarly for L[i] > R[j].

Termination:•On termination, k = r + 1.•By LI, A contains r – p + 1 smallest elements of L and R in sorted order.•L and R together contain r – p + 3 elements. All but the two sentinels have been copied back into A.

UNC Chapel Hill

Page 10: Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.

Z. Guo

Analysis of Merge Sort• Running time T(n) of Merge Sort:• Divide: computing the middle takes (1) • Conquer: solving 2 subproblems takes 2T(n/2) • Combine: merging n elements takes (n) • Total: T(n) = (1) if n = 1

T(n) = 2T(n/2) + (n) if n > 1

T(n) = (n lg n) (CLRS, Chapter 4)

UNC Chapel Hill

MergeSort (A, p, r) // sort A[p..r] by divide & conquer1 if p < r2 then q (p+r)/23 MergeSort (A, p, q)4 MergeSort (A, q+1, r)5 Merge (A, p, q, r) // merges A[p..q] with A[q+1..r]

Page 11: Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.

Recurrence Relations• Recurrences (Chapter 4)– Substitution Method– Iteration Method– Master Method

• Arising from Divide and Conquer (e.g. MERGE-SORT)

T(n) = (1) if n cT(n) = a T(n/b) + D(n) + C(n) otherwise

UNC Chapel Hill Z. Guo

Page 12: Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.

Substitution Method

• Guessing the form of the solutions, then using mathematical induction to find the constants and show the solution works.

• It works well when it is easy to guess. But, there is no general way to guess the correct solution.

UNC Chapel Hill Z. Guo

Page 13: Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.

An Example

• Solve: T(n) = 3T(n/3) + n

T(n) =

=

c n lg n

UNC Chapel Hill Z. Guo

GUESSGUESS

Page 14: Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.

An Example

• Solve: T(n) = 3T(n/3) + n

T(n) 3c n/3 lg n/3 + n

c n lg (n/3) + n

= c n lg n - c n lg3 + n

= c n lg n - n (c lg 3 - 1)

c n lg n

* The last step is true for c 1 / lg3.

UNC Chapel Hill Z. Guo

Page 15: Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.

Making a Good Guess

• Guessing a similar solution to the one that you have seen before– T(n) = 3T(n/3 + 5) + n similar to T(n) = 3T(n/3) + n

when n is large, the difference between n/3 and (n/3 + 5) is insignificant

• Another way is to prove loose upper and lower bounds on recurrence and then reduce the range of uncertainty.– Start with T(n) = (n) & T(n) = O(n2) T(n) = (n log n)

UNC Chapel Hill Z. Guo

Page 16: Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.

Changing Variables

• Use algebraic manipulation to turn an unknown recurrence similar to what you have seen before. – Consider T(n) = 2T(n1/2) + lg n

UNC Chapel Hill Z. Guo

Page 17: Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.

Changing Variables

• Use algebraic manipulation to turn an unknown recurrence similar to what you have seen before. – Consider T(n) = 2T(n1/2) + lg n – Rename m = lg n and we have T(2m) = 2T(2m/2) + m – Set S(m) = T(2m) and we have S(m) = 2S(m/2) + m S(m) = O(m lg m)– Changing back from S(m) to T(n), we have T(n) = T(2m) = S(m) = O(m lg m) = O(lg n lg lg n)

UNC Chapel Hill Z. Guo

Page 18: Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.

Avoiding Pitfalls

• Be careful not to misuse asymptotic notation. For example:

– We can falsely prove T(n) = O(n) by guessing T(n) c n for T(n) = 2T(n/2) + n

T(n) 2c n/2 + n

c n + n

= O(n) Wrong!– The err is that we haven’t proved T(n) c n

UNC Chapel Hill Z. Guo

Page 19: Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.

Exercises

• Solution of T(n) = T(n/2) + 1 is O(lg n)

• Solution of T(n) = 2T(n/2 + 17) + n is O(n lg n)

• Solve T(n) = 2T(n1/2) + 1 by making a change of variables. Don’t worry whether values are integral.

UNC Chapel Hill Z. Guo

Page 20: Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.

Comp 550

Recursion-tree Method• Making a good guess is sometimes difficult ...• Use recursion trees to devise good guesses.• Recursion Trees– Show successive expansions of recurrences using

trees.– Keep track of the time spent on the subproblems of

a divide and conquer algorithm.– Help organize the algebraic bookkeeping necessary

to solve a recurrence.

Page 21: Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.

Comp 550

Recursion Tree – Example

• Running time of Merge Sort:T(n) = (1) if n = 1T(n) = 2T(n/2) + (n) if n > 1

• Rewrite the recurrence asT(n) = c if n = 1

T(n) = 2T(n/2) + cn if n > 1c > 0: Running time for the base case and time per array element for the divide and combine steps.

Page 22: Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.

Comp 550

Recursion Tree for Merge SortFor the original problem, we have a cost of cn, plus two subproblems each of size (n/2) and running time T(n/2).

cn

T(n/2) T(n/2)

Each of the size n/2 problems has a cost of cn/2 plus two subproblems, each costing T(n/4).

cn

cn/2 cn/2

T(n/4) T(n/4) T(n/4) T(n/4)

Cost of divide and merge.

Cost of sorting subproblems.

T(n) = 2T(n/2) + cn

Page 23: Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.

Comp 550

Recursion Tree for Merge SortContinue expanding until the problem size reduces to 1.

cn

cn/2 cn/2

cn/4 cn/4 cn/4 cn/4

c c c cc c

lg n

cn

cn

cn

cn

Total : cnlgn+cn

Page 24: Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.

Comp 550

Recursion Tree for Merge SortContinue expanding until the problem size reduces to 1.

cn

cn/2 cn/2

cn/4 cn/4 cn/4 cn/4

c c c cc c

•Each level has total cost cn.•Each time we go down one level, the number of subproblems doubles, but the cost per subproblem halves cost per level remains the same.•There are lg n + 1 levels, height is lg n. (Assuming n is a power of 2.)

•Can be proved by induction.•Total cost = sum of costs at each level = (lg n + 1)cn = cnlgn + cn = (n lgn).

Page 25: Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.

UNC Chapel Hill Z. Guo

Recursion Trees and Recurrences• Useful even when a specific algorithm is not specified– For T(n) = 2T(n/2) + n2, we have

Page 26: Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.

UNC Chapel Hill Z. Guo

Recursion Trees

T(n) = (n2)

Page 27: Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.

Comp 550

Recursion Trees – Caution Note

• Recursion trees only generate guesses.– Verify guesses using substitution method.

• If careful when drawing out a recursion tree and summing the costs, can be used as direct proof.

Page 28: Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.

UNC Chapel Hill Z. Guo

Recursion Trees• Exercise T(n) = T(n/2) + T(n/4) + n2

Page 29: Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.

Comp 550

The Master Method• Based on the Master theorem.• “Cookbook” approach for solving recurrences

of the form T(n) = aT(n/b) + f(n)• a 1, b > 1 are constants.• f(n) is asymptotically positive.• T(n) is defined for nonnegative integers• n/b may not be an integer, but we ignore floors and

ceilings. Why?

Page 30: Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.

Comp 550

The Master TheoremTheorem 4.1Let a 1 and b > 1 be constants, let f(n) be a function, and

Let T(n) be defined on nonnegative integers by the recurrence T(n) = aT(n/b) + f(n), where we can replace n/b by n/b or n/b. T(n) can be bounded asymptotically in three cases:

1. If f(n) = O(nlogba–) for some constant > 0, then T(n) = (nlogba).

2. If f(n) = (nlogba), then T(n) = (nlogbalg n).3. If f(n) = (nlogba+) for some constant > 0,

and if, for some constant c < 1 and all sufficiently large n, we have a·f(n/b) c f(n), then T(n) = (f(n)).

Theorem 4.1Let a 1 and b > 1 be constants, let f(n) be a function, and

Let T(n) be defined on nonnegative integers by the recurrence T(n) = aT(n/b) + f(n), where we can replace n/b by n/b or n/b. T(n) can be bounded asymptotically in three cases:

1. If f(n) = O(nlogba–) for some constant > 0, then T(n) = (nlogba).

2. If f(n) = (nlogba), then T(n) = (nlogbalg n).3. If f(n) = (nlogba+) for some constant > 0,

and if, for some constant c < 1 and all sufficiently large n, we have a·f(n/b) c f(n), then T(n) = (f(n)).

We’ll return to recurrences as we need them…

If none of these three cases apply, you’re on your own.

Page 31: Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.

UNC Chapel Hill Z. Guo

Master Theorem

Let a 1 and b > 1 be constants and let T(n) be the recurrence

T(n) = a T(n/b) + c nk

defined for n 0. 1. If a > bk, then T(n) = ( nlogba ).

2. If a = bk, then T(n) = ( nk lg n ).

3. If a < bk, then T(n) = ( nk ).

Page 32: Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.

UNC Chapel Hill Z. Guo

Examples

• T(n) = 16T(n/4) + n– a = 16, b = 4, thus nlogba = nlog416 = (n2)– f(n) = n = O(nlog416 - ) where = 1 case 1.– Therefore, T(n) = (nlogba ) = (n2)

• T(n) = T(3n/7) + 1– a = 1, b=7/3, and nlogba = nlog 7/3 1 = n0 = 1– f(n) = 1 = (nlogba) case 2.– Therefore, T(n) = (nlogba lg n) = (lg n)

Page 33: Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.

UNC Chapel Hill Z. Guo

Examples (Cont.)• T(n) = 3T(n/4) + n lg n– a = 3, b=4, thus nlogba = nlog43 = O(n0.793)– f(n) = n lg n = (nlog43 + ) where 0.2 case 3.– Therefore, T(n) = (f(n)) = (n lg n)

• T(n) = 2T(n/2) + n lg n– a = 2, b=2, f(n) = n lg n, and nlogba = nlog22 = n– f(n) is asymptotically larger than nlogba, but not

polynomially larger. The ratio lg n is asymptotically less than n for any positive . Thus, the Master Theorem doesn’t apply here.

Page 34: Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.

UNC Chapel Hill Z. Guo

Exercises

• Use the Master Method to solve the following: T(n) = 4T(n/2) + n

T(n) = 4T(n/2) + n2

T(n) = 4T(n/2) + n3

• Try this if you finished the ones above: T(n) = 4T(n/2) + n2/lgn