6.006- introduction to · the master method “one theorem to rule them all” (sort of) the master...

52
6.006- Introduction to Algorithms Lecture 8 Prof. Piotr Indyk

Upload: others

Post on 05-Aug-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 6.006- Introduction to · The master method “One theorem to rule them all” (sort of) The master method applies to recurrences of the form T(n) =sa T(n/b) + f (n) , where a ³

6.006- Introduction to

Algorithms

Lecture 8Prof. Piotr Indyk

Page 2: 6.006- Introduction to · The master method “One theorem to rule them all” (sort of) The master method applies to recurrences of the form T(n) =sa T(n/b) + f (n) , where a ³

Menu

• Sorting!– Insertion Sort

– Merge Sort

• Recurrences– Master theorem

Page 3: 6.006- Introduction to · The master method “One theorem to rule them all” (sort of) The master method applies to recurrences of the form T(n) =sa T(n/b) + f (n) , where a ³

The problem of sorting

Input: array A[1…n] of numbers.

Output: permutation B[1…n] of A such

that B[1] £ B[2] £ … £ B[n] .

How can we do it efficiently ?

e.g. A = [7, 2, 5, 5, 9.6] → B = [2, 5, 5, 7, 9.6]

Page 4: 6.006- Introduction to · The master method “One theorem to rule them all” (sort of) The master method applies to recurrences of the form T(n) =sa T(n/b) + f (n) , where a ³

A:1 n

Insertion sort

INSERTION-SORT (A, n) ⊳ A[1 . . n]

for j ← 2 to n

j

key

sorted

insert key A[j] into the (already sorted) sub-array A[1 .. j-1].

Illustration of iteration j

i

new location of key

by pairwise key-swaps down to its right position

Page 5: 6.006- Introduction to · The master method “One theorem to rule them all” (sort of) The master method applies to recurrences of the form T(n) =sa T(n/b) + f (n) , where a ³

Example of insertion sort

8 2 4 9 3 6

Page 6: 6.006- Introduction to · The master method “One theorem to rule them all” (sort of) The master method applies to recurrences of the form T(n) =sa T(n/b) + f (n) , where a ³

Example of insertion sort

8 2 4 9 3 6

Page 7: 6.006- Introduction to · The master method “One theorem to rule them all” (sort of) The master method applies to recurrences of the form T(n) =sa T(n/b) + f (n) , where a ³

Example of insertion sort

8 2 4 9 3 6

2 8 4 9 3 6

Page 8: 6.006- Introduction to · The master method “One theorem to rule them all” (sort of) The master method applies to recurrences of the form T(n) =sa T(n/b) + f (n) , where a ³

Example of insertion sort

8 2 4 9 3 6

2 8 4 9 3 6

Page 9: 6.006- Introduction to · The master method “One theorem to rule them all” (sort of) The master method applies to recurrences of the form T(n) =sa T(n/b) + f (n) , where a ³

Example of insertion sort

8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

Page 10: 6.006- Introduction to · The master method “One theorem to rule them all” (sort of) The master method applies to recurrences of the form T(n) =sa T(n/b) + f (n) , where a ³

Example of insertion sort

8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

Page 11: 6.006- Introduction to · The master method “One theorem to rule them all” (sort of) The master method applies to recurrences of the form T(n) =sa T(n/b) + f (n) , where a ³

Example of insertion sort

8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

2 4 8 9 3 6

Page 12: 6.006- Introduction to · The master method “One theorem to rule them all” (sort of) The master method applies to recurrences of the form T(n) =sa T(n/b) + f (n) , where a ³

Example of insertion sort

8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

2 4 8 9 3 6

Page 13: 6.006- Introduction to · The master method “One theorem to rule them all” (sort of) The master method applies to recurrences of the form T(n) =sa T(n/b) + f (n) , where a ³

Example of insertion sort

8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

2 4 8 9 3 6

2 3 4 8 9 6

Page 14: 6.006- Introduction to · The master method “One theorem to rule them all” (sort of) The master method applies to recurrences of the form T(n) =sa T(n/b) + f (n) , where a ³

Example of insertion sort

8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

2 4 8 9 3 6

2 3 4 8 9 6

Page 15: 6.006- Introduction to · The master method “One theorem to rule them all” (sort of) The master method applies to recurrences of the form T(n) =sa T(n/b) + f (n) , where a ³

Example of insertion sort

8 2 4 9 3 6

2 8 4 9 3 6

2 4 8 9 3 6

2 4 8 9 3 6

2 3 4 8 9 6

2 3 4 6 8 9 done

Running time? Θ(n2)

e.g. when input is A = [n, n − 1, n − 2, . . . , 2, 1]

Page 16: 6.006- Introduction to · The master method “One theorem to rule them all” (sort of) The master method applies to recurrences of the form T(n) =sa T(n/b) + f (n) , where a ³

Meet Merge Sort

MERGE-SORT A[1 . . n]

1. If n = 1, done (nothing to sort).

Key subroutine: MERGE

divide and

conquer 2. Otherwise, recursively sort A[ 1 . . n/2 ] and A[ n/2+1 . . n ] .

3. “Merge” the two sorted sub-arrays.

Page 17: 6.006- Introduction to · The master method “One theorem to rule them all” (sort of) The master method applies to recurrences of the form T(n) =sa T(n/b) + f (n) , where a ³

20

13

7

2

12

11

9

1

Merging two sorted arrays

Page 18: 6.006- Introduction to · The master method “One theorem to rule them all” (sort of) The master method applies to recurrences of the form T(n) =sa T(n/b) + f (n) , where a ³

Merging two sorted arrays

20

13

7

2

12

11

9

1

1

Page 19: 6.006- Introduction to · The master method “One theorem to rule them all” (sort of) The master method applies to recurrences of the form T(n) =sa T(n/b) + f (n) , where a ³

Merging two sorted arrays

20

13

7

2

12

11

9

1

1

20

13

7

2

12

11

9

Page 20: 6.006- Introduction to · The master method “One theorem to rule them all” (sort of) The master method applies to recurrences of the form T(n) =sa T(n/b) + f (n) , where a ³

Merging two sorted arrays

20

13

7

2

12

11

9

1

1

20

13

7

2

12

11

9

2

Page 21: 6.006- Introduction to · The master method “One theorem to rule them all” (sort of) The master method applies to recurrences of the form T(n) =sa T(n/b) + f (n) , where a ³

Merging two sorted arrays

20

13

7

2

12

11

9

1

1

20

13

7

2

12

11

9

2

20

13

7

12

11

9

Page 22: 6.006- Introduction to · The master method “One theorem to rule them all” (sort of) The master method applies to recurrences of the form T(n) =sa T(n/b) + f (n) , where a ³

Merging two sorted arrays

20

13

7

2

12

11

9

1

1

20

13

7

2

12

11

9

2

20

13

7

12

11

9

7

Page 23: 6.006- Introduction to · The master method “One theorem to rule them all” (sort of) The master method applies to recurrences of the form T(n) =sa T(n/b) + f (n) , where a ³

Merging two sorted arrays

20

13

7

2

12

11

9

1

1

20

13

7

2

12

11

9

2

20

13

7

12

11

9

7

20

13

12

11

9

Page 24: 6.006- Introduction to · The master method “One theorem to rule them all” (sort of) The master method applies to recurrences of the form T(n) =sa T(n/b) + f (n) , where a ³

Merging two sorted arrays

20

13

7

2

12

11

9

1

1

20

13

7

2

12

11

9

2

20

13

7

12

11

9

7

20

13

12

11

9

9

Page 25: 6.006- Introduction to · The master method “One theorem to rule them all” (sort of) The master method applies to recurrences of the form T(n) =sa T(n/b) + f (n) , where a ³

Merging two sorted arrays

20

13

7

2

12

11

9

1

1

20

13

7

2

12

11

9

2

20

13

7

12

11

9

7

20

13

12

11

9

9

20

13

12

11

Page 26: 6.006- Introduction to · The master method “One theorem to rule them all” (sort of) The master method applies to recurrences of the form T(n) =sa T(n/b) + f (n) , where a ³

Merging two sorted arrays

20

13

7

2

12

11

9

1

1

20

13

7

2

12

11

9

2

20

13

7

12

11

9

7

20

13

12

11

9

9

20

13

12

11

11

Page 27: 6.006- Introduction to · The master method “One theorem to rule them all” (sort of) The master method applies to recurrences of the form T(n) =sa T(n/b) + f (n) , where a ³

Merging two sorted arrays

20

13

7

2

12

11

9

1

1

20

13

7

2

12

11

9

2

20

13

7

12

11

9

7

20

13

12

11

9

9

20

13

12

11

11

20

13

12

Page 28: 6.006- Introduction to · The master method “One theorem to rule them all” (sort of) The master method applies to recurrences of the form T(n) =sa T(n/b) + f (n) , where a ³

Merging two sorted arrays

20

13

7

2

12

11

9

1

1

20

13

7

2

12

11

9

2

20

13

7

12

11

9

7

20

13

12

11

9

9

20

13

12

11

11

20

13

12

12

Page 29: 6.006- Introduction to · The master method “One theorem to rule them all” (sort of) The master method applies to recurrences of the form T(n) =sa T(n/b) + f (n) , where a ³

Merging two sorted arrays

20

13

7

2

12

11

9

1

1

20

13

7

2

12

11

9

2

20

13

7

12

11

9

7

20

13

12

11

9

9

20

13

12

11

11

20

13

12

12

Time = Q(n) to merge a total

of n elements (linear time).

Page 30: 6.006- Introduction to · The master method “One theorem to rule them all” (sort of) The master method applies to recurrences of the form T(n) =sa T(n/b) + f (n) , where a ³

Analyzing merge sort

MERGE-SORT A[1 . . n]

1. If n = 1, done.

2. Recursively sort A[ 1 . . n/2 ] and A[ n/2+1 . . n ] .

3. “Merge” the two sorted lists

T(n)

Q(1)

2T(n/2)

Q(n)

T(n) =Q(1) if n = 1;

2T(n/2) + Q(n) if n > 1.

T(n) = ?

Page 31: 6.006- Introduction to · The master method “One theorem to rule them all” (sort of) The master method applies to recurrences of the form T(n) =sa T(n/b) + f (n) , where a ³

Recurrence solving

Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.

Page 32: 6.006- Introduction to · The master method “One theorem to rule them all” (sort of) The master method applies to recurrences of the form T(n) =sa T(n/b) + f (n) , where a ³

Recursion tree

Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.

T(n)

Page 33: 6.006- Introduction to · The master method “One theorem to rule them all” (sort of) The master method applies to recurrences of the form T(n) =sa T(n/b) + f (n) , where a ³

Recursion tree

Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.

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

cn

Page 34: 6.006- Introduction to · The master method “One theorem to rule them all” (sort of) The master method applies to recurrences of the form T(n) =sa T(n/b) + f (n) , where a ³

Recursion tree

Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.

cn

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

cn/2 cn/2

Page 35: 6.006- Introduction to · The master method “One theorem to rule them all” (sort of) The master method applies to recurrences of the form T(n) =sa T(n/b) + f (n) , where a ³

Recursion tree

Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.

cn

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

cn/2 cn/2

Q(1)

Page 36: 6.006- Introduction to · The master method “One theorem to rule them all” (sort of) The master method applies to recurrences of the form T(n) =sa T(n/b) + f (n) , where a ³

Recursion tree

Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.

cn

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

cn/2 cn/2

Q(1)

h = lg n

Page 37: 6.006- Introduction to · The master method “One theorem to rule them all” (sort of) The master method applies to recurrences of the form T(n) =sa T(n/b) + f (n) , where a ³

Recursion tree

Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.

cn

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

cn/2 cn/2

Q(1)

h = lg n

cn

Page 38: 6.006- Introduction to · The master method “One theorem to rule them all” (sort of) The master method applies to recurrences of the form T(n) =sa T(n/b) + f (n) , where a ³

Recursion tree

Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.

cn

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

cn/2 cn/2

Q(1)

h = lg n

cn

cn

Page 39: 6.006- Introduction to · The master method “One theorem to rule them all” (sort of) The master method applies to recurrences of the form T(n) =sa T(n/b) + f (n) , where a ³

Recursion tree

Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.

cn

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

cn/2 cn/2

Q(1)

h = lg n

cn

cn

cn

Page 40: 6.006- Introduction to · The master method “One theorem to rule them all” (sort of) The master method applies to recurrences of the form T(n) =sa T(n/b) + f (n) , where a ³

Recursion tree

Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.

cn

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

cn/2 cn/2

Q(1)

h = lg n

cn

cn

cn

#leaves = n Q(n)

Page 41: 6.006- Introduction to · The master method “One theorem to rule them all” (sort of) The master method applies to recurrences of the form T(n) =sa T(n/b) + f (n) , where a ³

Recursion tree

Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.

cn

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

cn/2 cn/2

Q(1)

h = lg n

cn

cn

cn

#leaves = n Q(n)

Total ?

Page 42: 6.006- Introduction to · The master method “One theorem to rule them all” (sort of) The master method applies to recurrences of the form T(n) =sa T(n/b) + f (n) , where a ³

Recursion tree

Solve T(n) = 2T(n/2) + cn, where c > 0 is constant.

cn

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

cn/2 cn/2

Q(1)

h = lg n

cn

cn

cn

#leaves = n Q(n)

Total = Q(n lg n)

Page 43: 6.006- Introduction to · The master method “One theorem to rule them all” (sort of) The master method applies to recurrences of the form T(n) =sa T(n/b) + f (n) , where a ³

The master method

“One theorem to rule them all” (sort of)The master method applies to recurrences of the form

T(n) = a T(n/b) + f (n) ,

where a ³ 1, b > 1, and f is positive.

e.g. Mergesort: a=2, b=2, f(n)=O(n)e.g.2 Binary Search: a=1, b=2, f(n)=O(1)Basic Idea: Compare f (n) with nlogba.

#subproblemssize of each

subproblemtime to split into

subproblems and

combine results

Page 44: 6.006- Introduction to · The master method “One theorem to rule them all” (sort of) The master method applies to recurrences of the form T(n) =sa T(n/b) + f (n) , where a ³

f (n/b)

Idea of master theorem

f (n/b) f (n/b)

T (1)

Recursion tree:

f (n) a

f (n/b2)f (n/b2) f (n/b2)… a

h = logbn

f (n)

a f (n/b)

a2 f (n/b2)

#leaves = ah

= alogbn

= nlogba

nlogbaT (1)

T(n) = a T(n/b) + f (n)

Page 45: 6.006- Introduction to · The master method “One theorem to rule them all” (sort of) The master method applies to recurrences of the form T(n) =sa T(n/b) + f (n) , where a ³

f (n/b)

Idea of master theorem

f (n/b) f (n/b)

T (1)

Recursion tree:

f (n) a

f (n/b2)f (n/b2) f (n/b2)… a

h = logbn

f (n)

a f (n/b)

a2 f (n/b2)

nlogbaT (1)

CASE 1: The weight increases geometrically from the root to the leaves. The leaves hold a constant fraction of the total weight. Q(nlogba)

T(n) = a T(n/b) + f (n)

Page 46: 6.006- Introduction to · The master method “One theorem to rule them all” (sort of) The master method applies to recurrences of the form T(n) =sa T(n/b) + f (n) , where a ³

f (n/b)

Idea of master theorem

f (n/b) f (n/b)

T (1)

Recursion tree:

f (n) a

f (n/b2)f (n/b2) f (n/b2)… a

h = logbn

f (n)

a f (n/b)

a2 f (n/b2)

nlogbaT (1)CASE 2: (k = 0) The weight is approximately the same on each of the logbn levels.

Q(nlogbalg n)

T(n) = a T(n/b) + f (n)

Page 47: 6.006- Introduction to · The master method “One theorem to rule them all” (sort of) The master method applies to recurrences of the form T(n) =sa T(n/b) + f (n) , where a ³

f (n/b)

Idea of master theorem

f (n/b) f (n/b)

T (1)

Recursion tree:

f (n) a

f (n/b2)f (n/b2) f (n/b2)… a

h = logbn

f (n)

a f (n/b)

a2 f (n/b2)

nlogbaT (1)

CASE 3: The weight decreases geometrically from the root to the leaves. The root holds a constant fraction of the total weight. Q( f (n))

T(n) = a T(n/b) + f (n)

Page 48: 6.006- Introduction to · The master method “One theorem to rule them all” (sort of) The master method applies to recurrences of the form T(n) =sa T(n/b) + f (n) , where a ³

Three common cases

Compare f (n) with nlogba:

1. f (n) = Θ(nlogba – e) for some constant e > 0.• f (n) grows polynomially slower than nlogba

(by an ne factor).

cost of level i = ai f (n/bi) = Q(nlogba-e

⋅bi e) so geometric increase of cost as we go deeper in the tree

Solution: T(n) = Q(nlogba) .

hence, leaf level cost dominates!

Page 49: 6.006- Introduction to · The master method “One theorem to rule them all” (sort of) The master method applies to recurrences of the form T(n) =sa T(n/b) + f (n) , where a ³

Three common cases (cont.)

Compare f (n) with nlogba:

2. f (n) = Q(nlogba logkn) for some constant k ³ 0.• f (n) and nlogba grow at similar rates.

Solution: T(n) = Q(nlogba logk+1n) .

(cost of level i ) = ai f (n/bi) = Q(nlogba

⋅logk(n/bi)) so all levels have about the same cost

Page 50: 6.006- Introduction to · The master method “One theorem to rule them all” (sort of) The master method applies to recurrences of the form T(n) =sa T(n/b) + f (n) , where a ³

Three common cases (cont.)

Compare f (n) with nlogba:

3. f (n) = Θ(nlogba + e) for some constant e > 0.• f (n) grows polynomially faster than nlogba (by

an ne factor).

(cost of level i ) = ai f (n/bi) = Q(nlogba+e

⋅b-i e) so geometric decrease of cost as we go deeper in the tree

Solution: T(n) = Q( f (n) ) .

hence, root cost dominates!

Page 51: 6.006- Introduction to · The master method “One theorem to rule them all” (sort of) The master method applies to recurrences of the form T(n) =sa T(n/b) + f (n) , where a ³

Examples

Ex. T(n) = 2T(n/2) + 1

a = 2, b = 2 ⇒ nlogba =n; f (n) = 1.

CASE 1: f (n) = O(n1 – e) for e = 1.

∴ T(n) = Q(n).

Ex. T(n) = 2T(n/2) + n

a = 2, b = 2 ⇒ nlogba = n; f (n) = n.

CASE 2: f (n) = Q(nlg0n), that is, k = 0.

∴ T(n) = Q(nlg n).

Page 52: 6.006- Introduction to · The master method “One theorem to rule them all” (sort of) The master method applies to recurrences of the form T(n) =sa T(n/b) + f (n) , where a ³

Examples

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

a = 4, b = 2 ⇒ nlogba = n2; f (n) = n3.

CASE 3: f (n) = W(n2 + e) for e = 1.

∴ T(n) = Q(n3).