+ All Categories
Home > Documents > CS 332: Algorithms

CS 332: Algorithms

Date post: 06-Feb-2016
Category:
Upload: fabian
View: 29 times
Download: 0 times
Share this document with a friend
Description:
CS 332: Algorithms. Merge Sort Solving Recurrences The Master Theorem. Administrative Question. Who here cannot make Monday-Wednesday office hours at 10 AM? If nobody, should we change class time?. Homework 1. Homework 1 will be posted later today - PowerPoint PPT Presentation
Popular Tags:
27
David Luebke 1 06/18/22 CS 332: Algorithms Merge Sort Solving Recurrences The Master Theorem
Transcript
Page 1: CS 332: Algorithms

David Luebke 1 04/22/23

CS 332: Algorithms

Merge Sort

Solving Recurrences

The Master Theorem

Page 2: CS 332: Algorithms

David Luebke 2 04/22/23

Administrative Question

Who here cannot make Monday-Wednesday office hours at 10 AM?

If nobody, should we change class time?

Page 3: CS 332: Algorithms

David Luebke 3 04/22/23

Homework 1

Homework 1 will be posted later today (Problem with the exercise numbering, sorry) Due Monday Jan 28 at 9 AM Should be a fairly simple warm-up problem set

Page 4: CS 332: Algorithms

David Luebke 4 04/22/23

Review: Asymptotic Notation

Upper Bound Notation: f(n) is O(g(n)) if there exist positive constants c

and n0 such that f(n) c g(n) for all n n0

Formally, O(g(n)) = { f(n): positive constants c and n0 such that f(n) c g(n) n n0

Big O fact: A polynomial of degree k is O(nk)

Page 5: CS 332: Algorithms

David Luebke 5 04/22/23

Review: Asymptotic Notation

Asymptotic lower bound: f(n) is (g(n)) if positive constants c and n0 such

that 0 cg(n) f(n) n n0

Asymptotic tight bound: f(n) is (g(n)) if positive constants c1, c2, and n0

such that c1 g(n) f(n) c2 g(n) n n0

f(n) = (g(n)) if and only if f(n) = O(g(n)) AND f(n) = (g(n))

Page 6: CS 332: Algorithms

David Luebke 6 04/22/23

Other Asymptotic Notations

A function f(n) is o(g(n)) if positive constants c and n0 such that

f(n) < c g(n) n n0

A function f(n) is (g(n)) if positive constants c and n0 such that

c g(n) < f(n) n n0

Intuitively, o() is like < O() is like

() is like > () is like

() is like =

Page 7: CS 332: Algorithms

David Luebke 7 04/22/23

Merge Sort

MergeSort(A, left, right) {

if (left < right) {

mid = floor((left + right) / 2);

MergeSort(A, left, mid);

MergeSort(A, mid+1, right);

Merge(A, left, mid, right);

}

}

// Merge() takes two sorted subarrays of A and

// merges them into a single sorted subarray of A

// (how long should this take?)

Page 8: CS 332: Algorithms

David Luebke 8 04/22/23

Merge Sort: Example

Show MergeSort() running on the array

A = {10, 5, 7, 6, 1, 4, 8, 3, 2, 9};

Page 9: CS 332: Algorithms

David Luebke 9 04/22/23

Analysis of Merge Sort

Statement Effort

So T(n) = (1) when n = 1, and

2T(n/2) + (n) when n > 1 So what (more succinctly) is T(n)?

MergeSort(A, left, right) { T(n) if (left < right) { (1) mid = floor((left + right) / 2); (1) MergeSort(A, left, mid); T(n/2) MergeSort(A, mid+1, right); T(n/2) Merge(A, left, mid, right); (n) }}

Page 10: CS 332: Algorithms

David Luebke 10 04/22/23

Recurrences

The expression:

is a recurrence. Recurrence: an equation that describes a function

in terms of its value on smaller functions

1

22

1

)(ncn

nT

nc

nT

Page 11: CS 332: Algorithms

David Luebke 11 04/22/23

Recurrence Examples

0

0

)1(

0)(

n

n

nscns

0)1(

00)(

nnsn

nns

1

22

1

)(nc

nT

nc

nT

1

1

)(

ncnb

naT

nc

nT

Page 12: CS 332: Algorithms

David Luebke 12 04/22/23

Solving Recurrences

Substitution method Iteration method Master method

Page 13: CS 332: Algorithms

David Luebke 13 04/22/23

Solving Recurrences

The substitution method (CLR 4.1) A.k.a. the “making a good guess method” Guess the form of the answer, then use induction

to find the constants and show that solution works Examples:

T(n) = 2T(n/2) + (n) T(n) = (n lg n) T(n) = 2T(n/2) + n ???

Page 14: CS 332: Algorithms

David Luebke 14 04/22/23

Solving Recurrences

The substitution method (CLR 4.1) A.k.a. the “making a good guess method” Guess the form of the answer, then use induction

to find the constants and show that solution works Examples:

T(n) = 2T(n/2) + (n) T(n) = (n lg n) T(n) = 2T(n/2) + n T(n) = (n lg n) T(n) = 2T(n/2 )+ 17) + n ???

Page 15: CS 332: Algorithms

David Luebke 15 04/22/23

Solving Recurrences

The substitution method (CLR 4.1) A.k.a. the “making a good guess method” Guess the form of the answer, then use induction

to find the constants and show that solution works Examples:

T(n) = 2T(n/2) + (n) T(n) = (n lg n) T(n) = 2T(n/2) + n T(n) = (n lg n) T(n) = 2T(n/2+ 17) + n (n lg n)

Page 16: CS 332: Algorithms

David Luebke 16 04/22/23

Solving Recurrences

Another option is what the book calls the “iteration method” Expand the recurrence Work some algebra to express as a summation Evaluate the summation

We will show several examples

Page 17: CS 332: Algorithms

David Luebke 17 04/22/23

s(n) =

c + s(n-1)

c + c + s(n-2)

2c + s(n-2)

2c + c + s(n-3)

3c + s(n-3)

kc + s(n-k) = ck + s(n-k)

0)1(

00)(

nnsc

nns

Page 18: CS 332: Algorithms

David Luebke 18 04/22/23

So far for n >= k we have s(n) = ck + s(n-k)

What if k = n? s(n) = cn + s(0) = cn

0)1(

00)(

nnsc

nns

Page 19: CS 332: Algorithms

David Luebke 19 04/22/23

So far for n >= k we have s(n) = ck + s(n-k)

What if k = n? s(n) = cn + s(0) = cn

So

Thus in general s(n) = cn

0)1(

00)(

nnsc

nns

0)1(

00)(

nnsc

nns

Page 20: CS 332: Algorithms

David Luebke 20 04/22/23

s(n)

= n + s(n-1)

= n + n-1 + s(n-2)

= n + n-1 + n-2 + s(n-3)

= n + n-1 + n-2 + n-3 + s(n-4)

= …

= n + n-1 + n-2 + n-3 + … + n-(k-1) + s(n-k)

0)1(

00)(

nnsn

nns

Page 21: CS 332: Algorithms

David Luebke 21 04/22/23

s(n)

= n + s(n-1)

= n + n-1 + s(n-2)

= n + n-1 + n-2 + s(n-3)

= n + n-1 + n-2 + n-3 + s(n-4)

= …

= n + n-1 + n-2 + n-3 + … + n-(k-1) + s(n-k)

=

0)1(

00)(

nnsn

nns

)(1

knsin

kni

Page 22: CS 332: Algorithms

David Luebke 22 04/22/23

So far for n >= k we have

0)1(

00)(

nnsn

nns

)(1

knsin

kni

Page 23: CS 332: Algorithms

David Luebke 23 04/22/23

So far for n >= k we have

What if k = n?

0)1(

00)(

nnsn

nns

)(1

knsin

kni

Page 24: CS 332: Algorithms

David Luebke 24 04/22/23

So far for n >= k we have

What if k = n?

0)1(

00)(

nnsn

nns

)(1

knsin

kni

2

10)0(

11

nnisi

n

i

n

i

Page 25: CS 332: Algorithms

David Luebke 25 04/22/23

So far for n >= k we have

What if k = n?

Thus in general

0)1(

00)(

nnsn

nns

)(1

knsin

kni

2

10)0(

11

nnisi

n

i

n

i

2

1)(

nnns

Page 26: CS 332: Algorithms

David Luebke 26 04/22/23

T(n) =

2T(n/2) + c

2(2T(n/2/2) + c) + c

22T(n/22) + 2c + c

22(2T(n/22/2) + c) + 3c

23T(n/23) + 4c + 3c

23T(n/23) + 7c

23(2T(n/23/2) + c) + 7c

24T(n/24) + 15c

2kT(n/2k) + (2k - 1)c

1

22

1)( nc

nT

ncnT

Page 27: CS 332: Algorithms

David Luebke 27 04/22/23

So far for n > 2k we have T(n) = 2kT(n/2k) + (2k - 1)c

What if k = lg n? T(n) = 2lg n T(n/2lg n) + (2lg n - 1)c

= n T(n/n) + (n - 1)c

= n T(1) + (n-1)c

= nc + (n-1)c = (2n - 1)c

1

22

1)( nc

nT

ncnT


Recommended