updates hw#1 has been delayed until next monday. there were two errors in the assignment. 2-1. merge...

23
Updates • HW#1 has been delayed until next MONDAY. There were two errors in the assignment. • 2-1. Merge sort runs in Θ(n log n). Insertion sort runs in Θ(n2). Consider a modification to Merge sort where n/k sublists of length k are sorted using Insertion sort and then merged by Merge sort.

Post on 21-Dec-2015

214 views

Category:

Documents


1 download

TRANSCRIPT

Updates

• HW#1 has been delayed until next MONDAY. There were two errors in the assignment.

• 2-1. Merge sort runs in Θ(n log n). Insertion sort runs in Θ(n2). Consider a modification to Merge sort where n/k sublists of length k are sorted using Insertion sort and then merged by Merge sort.

Updates

• A) Show each n/k sublist can be sorted by Insertion Sort in Θ(n/k) worst case time.

• B) Show that the sublists can be merged in Θ(n lg(n/k)) worst case time.

• C) Skip.

• D) How should k be chosen in practice? (hint: philosophical)

Lecture 3: Solving Recurrences

Recurrences

• Recurrence: An equation that describes a function in terms of its value on smaller inputs.

• Example (MergeSort):T(n) = Θ(n) if n = 1,

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

Recurrences

• We will study three techniques:1. The substitution method

2. The recursion-tree method

3. The master method

• Sometimes more than one works well, sometimes none work well.

The Substitution Method

The most general method:1. Guess the form of the solution.

2. Verify using induction.

3. Solve for the constants.

Substitution Method: Example 1

• Example: T(n) = 4T(n/2) + 100n

• Guess: T(n) = O(n3)

Substitution Method: Example 1• Example: T(n) = 4T(n/2) + 100n• Proof:

1. Assume that T(1) = Θ(1).2. Guess T(n) = O(n3). We want to prove T(n) < cn3 for some c>0.3. Verify by substituting into the recurrence.

We assume this holds for T(n/2) < c (n/2)3, thusT(n) < 4c(n/2)3 + 100n = cn3/2 + 100n= (c/2)n3 + 100n= cn3 - (c/2)n3 + 100n= cn3 - ((c/2)n3 - 100n) “residual”< cn3

when (c/2)n3 - 100n > 0, that is solving for constants, when c > 200 and n > 1.

Always verify the base case(s) as well. Here Θ(1) < cn3 for sufficiently large c.

A Tighter Bound?

• Example: T(n) = 4T(n/2) + 100n

• Guess: T(n) = O(n2)

A Tighter Bound?

• Example: T(n) = 4T(n/2) + 100n• Proof:

1. Assume that T(1) = Θ(1).2. Guess T(n) = O(n2). We want to prove T(n) < cn2 for some c>0.3. Verify by substituting into the recurrence.

We assume this holds for T(n/2) < c (n/2)2, thusT(n) < 4c(n/2)2 + 100n = cn2 + 100n= cn2 – (-100n) residual?< cn2

when -100n > 0. No valid assignment of c and n > 0 can be made! We have made a bad guess.We MUST prove the EXACT form of the inductive hypothesis (our guess).

Subtleties..

• Example: T(n) = 4T(n/2) + 100n

• Guess T(n) = O(n2 -n).

Subtleties..

• Example: T(n) = 4T(n/2) + 100n• Proof:

1.Assume that T(1) = Θ(1).

2.Guess T(n) = O(n2 -n). We want to prove T(n) < c1n2 -c2 for some c1>0 and c2>0.

3.Verify by substituting into the recurrence.We assume this holds for T(n/2) < c1(n/2)2-c2n/2, thusT(n) < 4c1(n/2)2 - 4c2n/2 + 100n = c1n2 - 2c2n + 100n= c1n2 - c2n - (c2n - 100n) residual< c1n2 - c2n when c2n-100n > 0, that is, c2>100.

Recursion Tree Method

• The Recursion Tree Method doesn’t necc. build a proof, but a good guess. You might use this method with the substitution method.

• The idea is to draw the tree and do the “accounting” the way we did with MergeSort last time.

• This can be unreliable, but it provides good intuition.

Example of recursion tree

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

Example of recursion tree

T(n)

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

Example of recursion tree

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

n2

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

Example of recursion tree

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

n2

(n/4)2 (n/2)2

T(n/16) T(n/8) T(n/8) T(n/4)

Example of recursion tree

(n/16)2 (n/8)2 (n/8)2 (n/4)2

(n/4)2 (n/2)2

(1)

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

n2

Example of recursion tree

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

(n/16)2 (n/8)2 (n/8)2 (n/4)2

(n/4)2 (n/2)2

(1)

2nn2

Example of recursion tree

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

(n/16)2 (n/8)2 (n/8)2 (n/4)2

(n/4)2 (n/2)2

(1)

2165 n

2nn2

Example of recursion tree

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

(n/16)2 (n/8)2 (n/8)2 (n/4)2

(n/4)2

(1)

2165 n

2n

225625 n

n2

(n/2)2

Example of recursion tree

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

(n/16)2 (n/8)2 (n/8)2 (n/4)2

(n/4)2

(1)

2165 n

2n

225625 n

13

1652

165

1652 n

Total == (n2)

n2

(n/2)2

geometric series

The Master Method

• The Master Theorem: Let a > 1 and b > 1 be constants. Let f(n) be a function and T(n) be defined on the non-negative integers as,T(n) = a T(n/b) + f(n).

• T(n) can be asymptotically bounded as follows:1. If f(n) = O(nlogba-ε), then T(n) = Θ(nlogba)

2. If f(n) = Θ(nlogba), then T(n) = Θ(nlogbalg n)

3. If f(n) = Ω(nlogba+ ε) and if af(n/b)<cf(n) for c < 1 and all sufficiently large n, then T(n) = Θ(f(n)).