cosc 3101nj. elder announcements midterm exam: fri feb 27 cse c –two blocks: 16:00-17:30...

76
COSC 3101N J. Elder Announcements Midterm Exam: Fri Feb 27 CSE C Two Blocks: • 16:00-17:30 • 17:30-19:00 The exam will be 1.5 hours in length. You can attend either block. The exam will be closed book. Please remember to bring ID Course Evaluation: Wed, Mar 24, 19:00 – Volunteer?

Upload: bathsheba-burns

Post on 17-Jan-2018

214 views

Category:

Documents


0 download

DESCRIPTION

COSC 3101NJ. Elder Greed and Money Suppose we are given a system of denominations. How do we decide whether the greedy algorithm always produces an optimal representation, for all values of N (the change to be made)? It turns out that this problem can be solved efficiently (Pearson 1994).

TRANSCRIPT

Page 1: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Announcements• Midterm Exam: Fri Feb 27 CSE C

– Two Blocks:• 16:00-17:30• 17:30-19:00

– The exam will be 1.5 hours in length. – You can attend either block.– The exam will be closed book.– Please remember to bring ID

• Course Evaluation: – Wed, Mar 24, 19:00– Volunteer?

Page 2: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Making Change (Revisited)In Canada, we use a 6 coin system: 1 cent, 5 cents, 10 cents, 25 cents, 100 cents and 200 cents.

Assuming that , the change to be made, is uniformly distributed over {1,...,499}, the expected number of coins per transaction is 5.9.

N

The optimal (but non-greedy) 6-coin systems are (1,6,14,62,99,140) and (1,8,13,69,110,160), each of which give an expected 4.67 coins per transaction.

The optimal 6-coin systems are (1,3,8,26,64,{202 or 203 or 204}) and (1,3,10,25,79,{195 or 196 or 197}) with an expected cost of 5.036 coins per transaction.

greedy

Page 3: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Greed and Money

• Suppose we are given a system of denominations. How do we decide whether the greedy algorithm always produces an optimal representation, for all values of N (the change to be made)?

• It turns out that this problem can be solved efficiently (Pearson 1994).

Page 4: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Greed and Money (cntd…)

1 2

Suppose the greedy algorithm for the system of denominations 1 is not always optimal. De e e

1 2 1 1 1

Pearson showed there exist integers , with 1 such thatthe minimal representation of the minimal counterexample is of the form0 0 0 ( 1) ;

where the greedy rej j j j j D D

i j j i D

e e e a e a e a e

1 1 1 2 2presentation of -1 is .i D De a e a e a e

3This leads directly to an ( ) algorithm for determining whether a greedyalgorithm is optimal for a particular system of denominations.

O D

Page 5: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Greed and Money (cntd…)

References:•J.O. Shallit. What this country needs is an 18-cent piece, Math. Intelligencer 25 (2) (2003), 20-23. •D. Pearson. A polynomial-time algorithm for the change-making problem. Technical Report TR 94-1433, Department of Computer Science, Cornell University, June 1994.

Page 6: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Dynamic Programming

Page 7: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Example 1. Rock Climbing Problem

• A rock climber wants to get from the bottom of a rock to the top by the safest possible path.

• At every step, he reaches for handholds above him; some holds are safer than other.

• From every place, he can only reach a few nearest handholds.

Page 8: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Rock climbing (cont)

At every step our climber can reach exactly three handholds: above, above and to the right and above and to the left.

Suppose we have a wall instead of the rock.

There is a table of “danger ratings” provided. The “Danger” of a path is the sum of danger ratings of all handholds on the path.

5 3

4

2

Page 9: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Rock Climbing (cont)

•We represent the wall as a table.

•Every cell of the table contains the danger rating of the corresponding block.

2 8 9 5 84 4 6 2 35 7 5 6 13 2 5 4 8

The obvious greedy algorithm does not give an optimal solution.

2255

4422

The rating of this path is 13.The rating of an optimal path is 12.

4411

2255

However, we can solve this problem by a dynamic programming strategy in polynomial time.

Page 10: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Idea: once we know the rating of a path to every handhold on a

layer, we can easily compute the ratings of the paths to the holds

on the next layer.

For the top layer, that gives us an answer to the problem itself.

Page 11: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

For every handhold, there is only one “path” rating. Once we have reached a hold, we don’t need to know how we got there to move to the next level.

This is called an “optimal substructure” property. Once we know optimal solutions to

subproblems, we can compute an optimal solution to the problem itself.

Page 12: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Dynamic programming

• Step 1: Describe an array of values you want to compute.

• Step 2: Give a recurrence for computing later values from earlier (bottom-up).

• Step 3: Give a high-level program.

• Step 4: Show how to use values in the array to compute an optimal solution.

Page 13: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Rock climbing: step 1.• Step 1: Describe an array of values you want to

compute.

• For 1 i n and 1 j m, define A(i,j) to be the cumulative rating of the least dangerous path from the bottom to the hold (i,j).

• The rating of the best path to the top will be the minimal value in the last row of the array.

Page 14: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Rock climbing: step 2.• Step 2: Give a recurrence for computing later values from

earlier (bottom-up).

• Let C(i,j) be the rating of the hold (i,j). There are three cases for A(i,j):

• Left (j=1): C(i,j)+min{A(i-1,j),A(i-1,j+1)}

• Right (j=m): C(i,j)+min{A(i-1,j-1),A(i-1,j)}

• Middle: C(i,j)+min{A(i-1,j-1),A(i-1,j),A(i-1,j+1)}

• For the first row (i=1), A(i,j)=C(i,j).

Page 15: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Rock climbing: simpler step 2

• Add initialization row: A(0,j)=0. No danger to stand on the ground.

• Add two initialization columns: A(i,0)=A(i,m+1)=. It is infinitely dangerous to try to hold on to the air where the wall ends.

• Now the recurrence becomes, for every i,j:

A(i,j) = C(i,j)+min{A(i-1,j-1),A(i-1,j),A(i-1,j+1)}

Page 16: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Rock climbing: example

2 8 9 5 84 4 6 2 35 7 5 6 13 2 5 4 8

C(i,j): A(i,j):

i\j 0 1 2 3 4 5 601234

i\j 0 1 2 3 4 5 60 0 0 0 0 0 1 2 3 4

Initialization: A(i,0)=A(i,m+1)=, A(0,j)=0

Page 17: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Rock climbing: example

2 8 9 5 84 4 6 2 35 7 5 6 13 2 5 4 8

C(i,j): A(i,j):

i\j 0 1 2 3 4 5 60 0 0 0 0 0 1 3 2 5 4 8 2 3 4

The values in the first row are the same as C(i,j).

i\j 0 1 2 3 4 5 60 0 0 0 0 0 1 2 3 4

Page 18: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Rock climbing: example

2 8 9 5 84 4 6 2 35 7 5 6 13 2 5 4 8

C(i,j): A(i,j):

A(2,1)=5+min{,3,2}=7.

i\j 0 1 2 3 4 5 60 0 0 0 0 0 1 3 2 5 4 8 2 7 3 4

Page 19: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Rock climbing: example

2 8 9 5 84 4 6 2 35 7 5 6 13 2 5 4 8

C(i,j): A(i,j):

A(2,1)=5+min{,3,2}=7. A(2,2)=7+min{3,2,5}=9

i\j 0 1 2 3 4 5 60 0 0 0 0 0 1 3 2 5 4 8 2 7 9 3 4

Page 20: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Rock climbing: example

2 8 9 5 84 4 6 2 35 7 5 6 13 2 5 4 8

C(i,j): A(i,j):

A(2,1)=5+min{,3,2}=7. A(2,2)=7+min{3,2,5}=9A(2,3)=5+min{2,5,4}=7.

i\j 0 1 2 3 4 5 60 0 0 0 0 0 1 3 2 5 4 8 2 7 9 7 3 4

Page 21: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Rock climbing: example

2 8 9 5 84 4 6 2 35 7 5 6 13 2 5 4 8

C(i,j): A(i,j):

The best cumulative rating on the second row is 5.

i\j 0 1 2 3 4 5 60 0 0 0 0 0 1 3 2 5 4 8 2 7 9 7 10 5 3 4

Page 22: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Rock climbing: example

2 8 9 5 84 4 6 2 35 7 5 6 13 2 5 4 8

C(i,j): A(i,j):

The best cumulative rating on the third row is 7.

i\j 0 1 2 3 4 5 60 0 0 0 0 0 1 3 2 5 4 8 2 7 9 7 10 5 3 11 11 13 7 8 4

Page 23: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Rock climbing: example

2 8 9 5 84 4 6 2 35 7 5 6 13 2 5 4 8

C(i,j): A(i,j):

The best cumulative rating on the last row is 12.

i\j 0 1 2 3 4 5 60 0 0 0 0 0 1 3 2 5 4 8 2 7 9 7 10 5 3 11 11 13 7 8 4 13 19 16 12 15

Page 24: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Rock climbing: example

2 8 9 5 84 4 6 2 35 7 5 6 13 2 5 4 8

C(i,j): A(i,j):

The best cumulative rating on the last row is 12.

i\j 0 1 2 3 4 5 60 0 0 0 0 0 1 3 2 5 4 8 2 7 9 7 10 5 3 11 11 13 7 8 4 13 19 16 12 15

So the rating of the best path to the top is 12.

Page 25: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Rock climbing example: step 4

2 8 9 5 84 4 6 2 35 7 5 6 13 2 5 4 8

C(i,j): A(i,j):

i\j 0 1 2 3 4 5 60 0 0 0 0 0 1 3 2 5 4 8 2 7 9 7 10 5 3 11 11 13 7 8 4 13 19 16 12 15

To find the actual path we need to retrace backwardsthe decisions made during the calculation of A(i,j).

Page 26: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Rock climbing example: step 4

2 8 9 5 84 4 6 2 35 7 5 6 13 2 5 4 8

C(i,j): A(i,j):

i\j 0 1 2 3 4 5 60 0 0 0 0 0 1 3 2 5 4 8 2 7 9 7 10 5 3 11 11 13 7 8 4 13 19 16 12 15 The last hold was (4,4).

To find the actual path we need to retrace backwardsthe decisions made during the calculation of A(i,j).

Page 27: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Rock climbing example: step 4

2 8 9 5 84 4 6 2 35 7 5 6 13 2 5 4 8

C(i,j): A(i,j):

i\j 0 1 2 3 4 5 60 0 0 0 0 0 1 3 2 5 4 8 2 7 9 7 10 5 3 11 11 13 7 8 4 13 19 16 12 15 The hold before the last

was (3,4), since min{13,7,8} was 7.

To find the actual path we need to retrace backwardsthe decisions made during the calculation of A(i,j).

Page 28: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Rock climbing example: step 4

2 8 9 5 84 4 6 2 35 7 5 6 13 2 5 4 8

C(i,j): A(i,j):

To find the actual path we need to retrace backwardsthe decisions made during the calculation of A(i,j).

i\j 0 1 2 3 4 5 60 0 0 0 0 0 1 3 2 5 4 8 2 7 9 7 10 5 3 11 11 13 7 8 4 13 19 16 12 15 The hold before that

was (2,5), since min{7,10,5} was 5.

Page 29: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Rock climbing example: step 4

2 8 9 5 84 4 6 2 35 7 5 6 13 2 5 4 8

C(i,j): A(i,j):

To find the actual path we need to retrace backwardsthe decisions made during the calculation of A(i,j).

i\j 0 1 2 3 4 5 60 0 0 0 0 0 1 3 2 5 4 8 2 7 9 7 10 5 3 11 11 13 7 8 4 13 19 16 12 15 Finally, the first hold

was (1,4), since min{5,4,8} was 4.

Page 30: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Rock climbing example: step 4

2 8 9 5 84 4 6 2 35 7 5 6 13 2 5 4 8

C(i,j): A(i,j):

We are done!

i\j 0 1 2 3 4 5 60 0 0 0 0 0 1 3 2 5 4 8 2 7 9 7 10 5 3 11 11 13 7 8 4 13 19 16 12 15

Page 31: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Ingredients:•Instances: Events with starting and finishing times <<s1,f1>,<s2,f2>,… ,<sn,fn>>.•Solutions: A set of events that do not overlap. •Value of Solution: The number of events scheduled.

•Goal: Given a set of events, schedule as many as possible.

Example 2: The Activity Selection Problem

Page 32: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

From Lecture 6: Problem can be solved by greedy algorithm

Earliest Finishing TimeSchedule the event that will free up your room for someone else as soon as possible.

Motivation:

Works!

Greedy Criteria:

Page 33: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

But what if activities have different values?

information ( , , ) about activities, where start time of activity finishing time of activity value (profit) of activit

Input

y

:

i i i

i

i

i

s f g ns if ig i

A is a set {1,2,..., } such that, , activities fea

sible sch

and do not confliced

tul

.e S n

i j S i j

A feasible schedule with maximum profit(

Output:) i

i S

SP S g

Activity Selection with Profits:

Page 34: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Will a greedy algorithm based on finishing time still work?

No!

1 1g 2 10g

3 1g

e.g.

Page 35: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Dynamic Programming Solution

Precomputation:

1 21. Sort activities according to fini ( (shing lo ti g ))me: nf f O nf n

2. {1,..., }, compute ( ) max{ {1,2,..., 1 ( ( log )} } )| l ii n H Oi l i f s n n

. . ( ) is the last event that ends before event starts.i e H i i

Page 36: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Step 1. Define an array of values to compute

{0,..., }, largest profit attainable from the(feasible) scheduling of a subset of activities from {1,

(2

),..., }

i ni

A i

Ultimately, we are interested in ( )A n

Page 37: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Step 2. Provide a Recurrent Solution

(0) 0( ) max{ ( 1), ( ( ))}, {1,..., }i

AA i A i g A H i i n

Decide not to schedule activity i

Profit from scheduling activity i

Profit from scheduling activities that end before activity i begins

Page 38: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Step 3. Provide an Algorithm

function A=actselwithp(g, H)

% assumes inputs sorted by finishing time

A(0)=0;

for i=1:length(g)

A(i)=max(A(i-1), g(i)+A(H(i)));

endRunning time? O(n)

Page 39: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Step 4. Compute Optimal Solution

function actstring=printasp(A,H,i,actstring)

if i==0 returnend

if A(i)>A(i-1) actstring = printasp(A, H, H(i), actstring); actstring = [actstring, sprintf('%d ', i)];else actstring = printasp(A, H, i-1, actstring);end

Running time? O(n)

Page 40: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Example

Activity i 1 2 3 4

Start si 0 2 3 2

Finish fi 3 6 6 10

Profit gi 20 30 20 30

H(i) ? ? ? ?

Page 41: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Example

Activity i 1 2 3 4

Start si 0 2 3 2

Finish fi 3 6 6 10

Profit gi 20 30 20 30

H(i) 0 ? ? ?

Page 42: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Example

Activity i 1 2 3 4

Start si 0 2 3 2

Finish fi 3 6 6 10

Profit gi 20 30 20 30

H(i) 0 0 ? ?

Page 43: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Example

Activity i 1 2 3 4

Start si 0 2 3 2

Finish fi 3 6 6 10

Profit gi 20 30 20 30

H(i) 0 0 1 ?

Page 44: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Example

Activity i 1 2 3 4

Start si 0 2 3 2

Finish fi 3 6 6 10

Profit gi 20 30 20 30

H(i) 0 0 1 0

(0) 0A

(1) max{0,20 ( (1))} 20A A H

(2) max{20,30 ( (2))} 30A A H

(3) max{30, 20 ( (3))} 40A A H

(4) max{40,30 ( (4))} 40A A H

Page 45: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Example 3: Scheduling Jobs with Deadlines, Profits and Durations

information ( , , ) about activities, whereinteger deadline for job integer duration of job real-valued pr

Input

ofit of job

:

i i i

i

i

i

d t g nd it ig i

A C is a sequence { (1), (2),..., ( )} such that: ( ) scheduled start time of job ( ) 1 if job is not scheduled

schedule C C C C nC i iC i i

A feasible schedule C with maximum profit: (Output ): ii C

P C g

A schedule C is if each scheduled job finishes by its deadlineand no two scheduled jobs over

feasilap

bleped.

Page 46: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Dynamic Programming Solution

Precomputation:

1 2Sort activities according to de ( ( log ))adline: nd Od n nd

Page 47: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Step 1. Define an array of values to compute

{0,..., }, {0,..., }, largest profit attainable from the (feasible) scheduling of a subset of jobs from {1,2,..., }, all of which finish by t

)

i

,

m

(

e

i n tt di

A

t

i

Ultimately, we are interested (n ,i )A n d

Page 48: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Step 2. Provide a Recurrent Solution(0, ) 0 {0,..., }

For {1,..., }, {0,..., }, define t min{ , }( is the latest time that we can schedule job so that itends both by its deadline and by time .)Then:

0 ( , ) ( 1, )

i i

A t t d

i n t d t d tt i

t

t A i t A i tt

0 ( , ) max{ ( 1, ), ( 1, )}iA i t A i t g A i t

Decide not to schedule job i

Profit from job i Profit from scheduling activities that end before job i begins

Page 49: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Step 2. (cntd…) Proving the Recurrent Solution

We effectively schedule job i at the latest possible time.

This leaves the largest and earliest contiguous block of time for scheduling jobs with earlier deadlines.

For {1,..., }, {0,..., }, define t min{ , }( is the latest time that we can schedule job so that itends both by its deadline and by time .)

i ii n t d t d tt i

t

Then:0 ( , ) ( 1, )t A i t A i t

0 ( , ) max{ ( 1, ), ( 1, )}it A i t A i t g A i t

Page 50: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Step 3. Provide an Algorithm

Running time? O(nd)

Page 51: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Step 4. Compute Optimal Solution

Running time? O(n)

Page 52: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Example 4: The (General) Knapsack Problem

information ( , ) about objects, where integer weight of object real value of object integer weight

Input:

capacity of knapsack

i i

i

i

w g nw ig iC

Let S {1,..., } = set of objects sele

feasibl

cted.

is if ( )e ii S

n

S K S w C

A feasible set with maximum profit: (Output ): ii S

S P S g

Page 53: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Job deadlines knapsack capacity: Identify time (job scheduling) with

{1,...,}Job duratio

w

ns

eight (k

object weights:

napsack):

i

ii

dCintw

The general knapsack problem can be treated and solved as a special case of the job scheduling problem

Running time? O(nC)

time

job k

job j

job i

d

weight

object k

object j

object i

C

The Knapsack Problem: A Special Case of Job Scheduling

Page 54: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Example 5: Longest Common Subsequence

• Input: 2 sequences, X = x1, . . . , xm and Y = y1, . . . , yn.

• Output: a subsequence common to both whose length is longest.

• Note: A subsequence doesn’t have to be consecutive, but it has to be in order.

Page 55: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Examples

Page 56: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Brute-force Algorithm

For every subsequence of , check whether it's a subsequence of .X Y

Time: ( 2 ).mn

2 subsequences of to check.m X

Each subsequence takes ( ) time to check: scan for first letter, from there scan for second, and so on.

nY

Page 57: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Optimal Substructure

Page 58: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Step 1. Define an array of values to compute

c( ,{0,..., }, {0,..., }, length of LCS o) f and i ji ji m j n X Y

Ultimately, we are interested i ) .(n ,c m n

Page 59: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Step 2. Provide a Recurrent Solution

Input sequences are empty

Last elements match: must be part of LCS

Last elements don’t match: at most one of them is part of LCS

Page 60: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Step 3. Provide an Algorithm

Running time? O(mn)

Page 61: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Step 4. Compute Optimal Solution

Running time? O(m+n)

Page 62: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Example

Page 63: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Example 6: Optimal Binary Search Trees

1 1 2

Sequence { ,..., } of distinct keys,

probability

Inp

that a search is for key

ut:

n n

i i

K k k n k k kp k

BST with minimum expected searcOutp

h ut:

cost

Page 64: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Expected Search Cost

Which BST is more efficient?

Cost = # of items examined.

For key , cost = depth ( ) 1i T ik k

1

[search cost in ]

(depth ( ) 1)n

T i ii

E T

k p

1

1 depth ( )n

T i ii

k p

Page 65: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Observations

Page 66: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Optimal Substructure

Page 67: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Recursive Solution

Page 68: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Recursive Solution (cntd…)

Page 69: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Step 2. Provide a Recurrent Solution

Expected cost of search for left subtree

Expected cost of forming tree from root and subtrees

Expected cost of search for right subtree

Page 70: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Step 3. Provide an Algorithm

Running time? O(n3)

work on subtrees of increasing size l

Page 71: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Example

Page 72: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Example (cntd…)

Page 73: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Step 4. Compute Optimal Solution

Running time? O(n)

Page 74: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Elements of Dynamic Programming

• Optimal substructure: – an optimal solution to the problem contains

within it optimal solutions to subproblems.

Page 75: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

Elements of Dynamic Programming

• Cut and paste: prove optimal substructure by contradiction:– assume an optimal solution to a problem with suboptimal solution to

subproblem

– cut out the suboptimal solution to the subproblem.

– paste in the optimal solution to the subproblem.

– show that this results in a better solution to the original problem.

– This contradicts our assertion that our original solution is optimal.

Page 76: COSC 3101NJ. Elder Announcements Midterm Exam: Fri Feb 27 CSE C –Two Blocks: 16:00-17:30 17:30-19:00 –The exam will be 1.5 hours in length. –You can attend

COSC 3101N J. Elder

• Dynamic programming uses optimal substructure from the bottom up:– First find optimal solutions to subproblems

– Then choose which to use in optimal solution to problem.

Elements of Dynamic Programming