Presentazione standard di PowerPoint · 5 Weighted Interval Scheduling Weighted interval scheduling...

23
Programmazione dinamica: Selezione di intervalli pesati 12 ottobre 2015

Transcript of Presentazione standard di PowerPoint · 5 Weighted Interval Scheduling Weighted interval scheduling...

Page 1: Presentazione standard di PowerPoint · 5 Weighted Interval Scheduling Weighted interval scheduling problem. Job j starts at s j, finishes at f j, and has weight or value v j. Two

Programmazione dinamica:Selezione di intervalli pesati

12 ottobre 2015

Page 2: Presentazione standard di PowerPoint · 5 Weighted Interval Scheduling Weighted interval scheduling problem. Job j starts at s j, finishes at f j, and has weight or value v j. Two

Appello 29 gennaio 2015

Quesito 2 (24 punti)Dopo la Laurea in Informatica avete aperto un campo di calcetto che ha

tantissime richieste e siete diventati ricchissimi. Ciò nonostante volete

guadagnare sempre di più, per cui avete organizzato una sorta di asta: chiunque

volesse affittare il vostro campo (purtroppo è uno solo), oltre ad indicare da

che ora a che ora lo vorrebbe utilizzare, deve dire anche quanto sia disposto a

pagare. Il vostro problema è quindi scegliere le richieste compatibili per orario,

che vi diano il guadagno totale maggiore. Avete trovato una soluzione, ma è

molto lenta. Vi ricordate allora che al corso di Algoritmi vi avevano tempestato

con le varie tecniche di progettazione di algoritmi: potranno esservi utili

(almeno una volta nella vita)?

Formalizzate il problema reale in un problema computazionale e risolvetelo in

maniera che sia il più efficiente possibile. Giustificate le risposte.

Page 3: Presentazione standard di PowerPoint · 5 Weighted Interval Scheduling Weighted interval scheduling problem. Job j starts at s j, finishes at f j, and has weight or value v j. Two

6.1 Weighted Interval Scheduling

Page 4: Presentazione standard di PowerPoint · 5 Weighted Interval Scheduling Weighted interval scheduling problem. Job j starts at s j, finishes at f j, and has weight or value v j. Two

4

Interval SchedulingInterval scheduling problem.

Job j starts at sj, finishes at fj .

Two jobs compatible if they don't overlap.

Goal: find biggest subset of mutually compatible jobs.

Time0 1 2 3 4 5 6 7 8 9 10 11

f

g

h

e

a

b

c

d

Page 5: Presentazione standard di PowerPoint · 5 Weighted Interval Scheduling Weighted interval scheduling problem. Job j starts at s j, finishes at f j, and has weight or value v j. Two

5

Weighted Interval SchedulingWeighted interval scheduling problem.

Job j starts at sj, finishes at fj, and has weight or value vj .

Two jobs compatible if they don't overlap.

Goal: find maximum weight subset of mutually compatible jobs.

Time0 1 2 3 4 5 6 7 8 9 10 11

f

g

h

e

a

b

c

d

weight = 9

weight = 7

weight = 10

weight = 2

weight = 12

weight = 8

weight = 2

weight = 5

Page 6: Presentazione standard di PowerPoint · 5 Weighted Interval Scheduling Weighted interval scheduling problem. Job j starts at s j, finishes at f j, and has weight or value v j. Two

6

Weighted Interval Scheduling

Weighted interval scheduling problem.

Job j starts at sj, finishes at fj, and has weight or value vj .

Two jobs compatible if they don't overlap.

Goal: find maximum weight subset of mutually compatible jobs.

Copyright © 2005 Pearson Addison-Wesley. All rights reserved.

Insiemi compatibili: {1,3} di peso 2{2} di peso 3 è la soluzione ottimale

Page 7: Presentazione standard di PowerPoint · 5 Weighted Interval Scheduling Weighted interval scheduling problem. Job j starts at s j, finishes at f j, and has weight or value v j. Two

7

Unweighted Interval Scheduling Review

Note: Greedy algorithm works if all weights are 1.

Consider jobs in ascending order of finish time.

Add job to subset if it is compatible with previously chosen jobs.

Observation. Greedy algorithm can fail spectacularly if arbitrary weights are allowed.

Time0 1 2 3 4 5 6 7 8 9 10 11

b

a

weight = 999

weight = 1

Page 8: Presentazione standard di PowerPoint · 5 Weighted Interval Scheduling Weighted interval scheduling problem. Job j starts at s j, finishes at f j, and has weight or value v j. Two

8

Weighted Interval Scheduling

Notation. Label jobs by finishing time: f1 f2 . . . fn .

Def. p(j) = largest index i < j such that job i is compatible with j.

Ex: (independently from weights) p(8) = 5, p(7) = 3, p(2) = 0.

Time0 1 2 3 4 5 6 7 8 9 10 11

6

7

8

4

3

1

2

5

Page 9: Presentazione standard di PowerPoint · 5 Weighted Interval Scheduling Weighted interval scheduling problem. Job j starts at s j, finishes at f j, and has weight or value v j. Two

Copyright © 2005 Pearson Addison-Wesley. All rights reserved.

6-9

Soluzione = {5, 3, 1}

Valore = 2+4+2 = 8

Page 10: Presentazione standard di PowerPoint · 5 Weighted Interval Scheduling Weighted interval scheduling problem. Job j starts at s j, finishes at f j, and has weight or value v j. Two

10

Dynamic Programming: Binary Choice

Notation. OPT(j) = value of optimal solution to the problem consisting of job requests 1, 2, ..., j.

Case 1: OPT selects job j.can't use incompatible jobs { p(j) + 1, p(j) + 2, ..., j - 1 }

must include optimal solution to problem consisting of remaining compatible jobs 1, 2, ..., p(j)

Case 2: OPT does not select job j.must include optimal solution to problem consisting of remaining compatible jobs 1, 2, ..., j-1

otherwise)1()),((max

0j if0)(

jOPTjpOPTvjOPT

j

optimal substructure

Page 11: Presentazione standard di PowerPoint · 5 Weighted Interval Scheduling Weighted interval scheduling problem. Job j starts at s j, finishes at f j, and has weight or value v j. Two

11

Input: n, s1,…,sn , f1,…,fn , v1,…,vn

Sort jobs by finish times so that f1 f2 ... fn.

Compute p(1), p(2), …, p(n)

Compute-Opt(j) {

if (j = 0)

return 0

else

return max(vj + Compute-Opt(p(j)), Compute-Opt(j-1))

}

Weighted Interval Scheduling: Brute Force

Brute force recursive algorithm.

Page 12: Presentazione standard di PowerPoint · 5 Weighted Interval Scheduling Weighted interval scheduling problem. Job j starts at s j, finishes at f j, and has weight or value v j. Two

Copyright © 2005 Pearson Addison-Wesley. All rights reserved.

6-12

otherwise)1()),((max

0j if0)(

jOPTjpOPTvjOPT

j

Page 13: Presentazione standard di PowerPoint · 5 Weighted Interval Scheduling Weighted interval scheduling problem. Job j starts at s j, finishes at f j, and has weight or value v j. Two

13

Weighted Interval Scheduling: Brute Force

Observation. Recursive algorithm fails spectacularly because of redundant sub-problems exponential algorithms.

Ex. Number of recursive calls for family of "layered" instances grows like Fibonacci sequence.

3

4

5

1

2

p(1) = 0, p(j) = j-2

5

4 3

3 2 2 1

2 1

1 0

1 0 1 0

T(n)= O(2n): too much!

Page 14: Presentazione standard di PowerPoint · 5 Weighted Interval Scheduling Weighted interval scheduling problem. Job j starts at s j, finishes at f j, and has weight or value v j. Two

14

Input: n, s1,…,sn , f1,…,fn , v1,…,vn

Sort jobs by finish times so that f1 f2 ... fn.

Compute p(1), p(2), …, p(n)

for j = 1 to n

M[j] = empty

M[0] = 0

M-Compute-Opt(j) {

if (M[j] is empty)

M[j] = max(wj + M-Compute-Opt(p(j)), M-Compute-Opt(j-1))

return M[j]

}

Weighted Interval Scheduling: Memoization

Memoization. Store results of each sub-problem in a cache; lookup as needed.

max(vj +

Page 15: Presentazione standard di PowerPoint · 5 Weighted Interval Scheduling Weighted interval scheduling problem. Job j starts at s j, finishes at f j, and has weight or value v j. Two

15

Weighted Interval Scheduling: Running Time

Claim. Memoized version of algorithm takes O(n log n) time.Sort by finish time: O(n log n).

Computing p() : O(n) after sorting by start time (exercise).

M-Compute-Opt(j): each invocation takes O(1) time and either

(i) returns an existing value M[j]

(ii) fills in one new entry M[j] and makes two recursive calls

Progress measure = # nonempty entries of M[].initially = 0, throughout n.

(ii) increases by 1 at most 2n recursive calls.

Overall running time of M-Compute-Opt(n) is O(n).

Remark. O(n) if jobs are pre-sorted by start and finish times.

Page 16: Presentazione standard di PowerPoint · 5 Weighted Interval Scheduling Weighted interval scheduling problem. Job j starts at s j, finishes at f j, and has weight or value v j. Two

16

Weighted Interval Scheduling: Bottom-Up

Bottom-up dynamic programming. Unwind recursion.

Input: n, s1,…,sn , f1,…,fn , v1,…,vn

Sort jobs by finish times so that f1 f2 ... fn.

Compute p(1), p(2), …, p(n)

Iterative-Compute-Opt {

M[0] = 0

for j = 1 to n

M[j] = max(vj + M[p(j)], M[j-1])

}

Page 17: Presentazione standard di PowerPoint · 5 Weighted Interval Scheduling Weighted interval scheduling problem. Job j starts at s j, finishes at f j, and has weight or value v j. Two

Copyright © 2005 Pearson Addison-Wesley. All rights reserved.

6-17

Iterative-Compute-Opt {

M[0] = 0

for j = 1 to n

M[j] = max(wj + M[p(j)], M[j-1])

}

M[1] = max ( 2+M[0], M[0]) = max (2+0, 0) = 2M[2] = max ( 4+M[0], M[1]) = max (4+0, 2) = 4M[3] = max ( 4+M[1], M[2]) = max (4+2, 4) = 6M[4] = max ( 7+M[0], M[3]) = max (7+0, 6) = 7M[5] = max ( 2+M[3], M[4]) = max (2+6, 7) = 8M[6] = max ( 1+M[3], M[5]) = max (1+6, 8) = 8

Page 18: Presentazione standard di PowerPoint · 5 Weighted Interval Scheduling Weighted interval scheduling problem. Job j starts at s j, finishes at f j, and has weight or value v j. Two

18

Weighted Interval Scheduling: Finding a Solution

Q. Dynamic programming algorithms computes optimal value.

What if we want the solution itself (the set of intervals)?

A. Do some post-processing.

# of recursive calls n O(n).

Run M-Compute-Opt(n)

Run Find-Solution(n)

Find-Solution(j) {

if (j = 0)

output nothing

else if (vj + M[p(j)] > M[j-1])

print j

Find-Solution(p(j))

else

Find-Solution(j-1)

}

Page 19: Presentazione standard di PowerPoint · 5 Weighted Interval Scheduling Weighted interval scheduling problem. Job j starts at s j, finishes at f j, and has weight or value v j. Two

Esempio del calcolo di una soluzione

otherwise)1()),((max

0j if0)(

jOPTjpOPTvjOPT

j

Soluzione = {5, 3, 1}

Valore = 2+4+2 = 8

M[1] = max ( 2+M[0], M[0]) = max (2+0, 0) = 2M[2] = max ( 4+M[0], M[1]) = max (4+0, 2) = 4M[3] = max ( 4+M[1], M[2]) = max (4+2, 4) = 6M[4] = max ( 7+M[0], M[3]) = max (7+0, 6) = 7M[5] = max ( 2+M[3], M[4]) = max (2+6, 7) = 8M[6] = max ( 1+M[3], M[5]) = max (1+6, 8) = 8

M[6]=M[5]: 6 non appartiene a OPTM[5]=v5+M[3]: OPT contiene 5 e una soluzione ottimale al problema per {1,2,3}M[3]=v3+M[1]: OPT contiene 5, 3 e una soluzione ottimale al problema per {1}M[1]=v1+M[0]: OPT contiene 5, 3 e 1 (e una soluzione ottimale al problema vuoto)

Page 20: Presentazione standard di PowerPoint · 5 Weighted Interval Scheduling Weighted interval scheduling problem. Job j starts at s j, finishes at f j, and has weight or value v j. Two

20

Dynamic Programming SummaryRecipe.

Characterize structure of problem.Recursively define value of optimal solution.Compute value of optimal solution.Construct optimal solution from computed information.

Dynamic programming techniques.Binary choice: weighted interval scheduling.Multi-way choice: segmented least squares.Adding a new variable: knapsack.Dynamic programming over intervals: RNA secondary structure.

Top-down vs. bottom-up: different people have different intuitions.

Page 21: Presentazione standard di PowerPoint · 5 Weighted Interval Scheduling Weighted interval scheduling problem. Job j starts at s j, finishes at f j, and has weight or value v j. Two

Tale proprietà è detta: Proprietà della Sottostruttura Ottima.

Page 22: Presentazione standard di PowerPoint · 5 Weighted Interval Scheduling Weighted interval scheduling problem. Job j starts at s j, finishes at f j, and has weight or value v j. Two

Proprietà della sottostruttura ottima: Scheduling di attività

Il punto cruciale qui è che O – {n} :

• è una soluzione, cioè è un insieme di attività compatibili di {1, …, p(n)} (perché le attività di O sono compatibili)

• è ottima, cioè ha cardinalità massima fra le soluzioni al sottoproblema relativo alle attività {1, …, p(n)} (infatti, se così non fosse, esisterebbe una soluzione O’ per {1, …, p(n)} di cardinalità strettamente superiore a quella di O – {n} e allora O’∪{n} sarebbe una soluzione strettamente migliore di O.

Page 23: Presentazione standard di PowerPoint · 5 Weighted Interval Scheduling Weighted interval scheduling problem. Job j starts at s j, finishes at f j, and has weight or value v j. Two

Riepilogo e riferimenti

• Primi esempi della programmazione dinamica: calcolo dei numeri di Fibonacci (la volta scorsa), selezione di intervalli pesati (oggi: [KT] par. 6.1)