Algoritmi e strutture di dati 2 Paola Vocca Lezione 2: Tecniche golose (greedy) Lezione1- Divide et impera 1 Progettazione di algoritmi greedy Tecniche di dimostrazione (progettazione) o Greedy algorithms stays ahead: Ad ogni passo la scelta effettuata dall algoritmo risulta migliore rispetto alla soluzione ottima o Exachage algorithm: trasformazione di una qualsiasi soluzione ottima nella soluzione trovata dall algoritmo senza alterare la misura della soluzione Algoritmi e strutture di dati 2 Lezione1- Divide et impera 2 1
Algoritmi golosi Un algoritmo goloso basa le sue scelte sulla configurazione attuale senza valutare le conseguenze sulle configurazioni successive Il paradigma dell algoritmo goloso non è facilmente formalizzabile Il successo di tale tecnica dipende da proprietà strutturali del problema o Problemi già affrontati o Cammino minimo o Minimum spannning tree Algoritmi e strutture di dati 2 Lezione1- Divide et impera 3 Interval scheduling Job j starts at and finishes at. Two jobs compatible if they don't overlap. Goal: find maximum subset of mutually compatible jobs. a b c d e f g h Algoritmi e strutture di dati 2 Lezione1- Divide et impera 4 2
Interval Scheduling: Greedy Algorithms Greedy template. Consider jobs in some natural order. Take each job provided it's compatible with the ones already taken. [Earliest start time] Consider jobs in ascending order of. [Earliest finish time] Consider jobs in ascending order of. [Shortest interval] Consider jobs in ascending order of. [Fewest conflicts] For each job, count the number of conflicting jobs. Schedule in ascending order of. Algoritmi e strutture di dati 2 Lezione1- Divide et impera 5 Interval Scheduling: Greedy Algorithms Greedy template. Consider jobs in some natural order. Take each job provided it's compatible with the ones already taken. [Earliest start time] Consider jobs in ascending order of. o Does it work? o 1st Step of Problem Solvers: Find bad situations for the Algorithm. counterexample for earliest start time Algoritmi e strutture di dati 2 Lezione1- Divide et impera 6 3
Interval Scheduling: Greedy Algorithms Greedy template. Consider jobs in some natural order. Take each job provided it's compatible with the ones already taken. [Shortest interval] Consider jobs in ascending order of. Bad Situations??? counterexample shortest interval Algoritmi e strutture di dati 2 Lezione1- Divide et impera 7 Interval Scheduling: Greedy Algorithms Greedy template. Consider jobs in some natural order. Take each job provided it's compatible with the ones already taken. [Fewest conflicts] For each job, count the number of conflicting jobs. Schedule in ascending order of. o IDEA: Use GRAPH MODELLING; o Which is the problem in terms of GRAPHS? counterexample shortest interval Algoritmi e strutture di dati 2 Lezione1- Divide et impera 8 4
Interval Scheduling: Greedy Algorithms Greedy template Consider jobs in increasing order of finish time. Take each job provided it's compatible with the ones already taken Implementation.. o Remember job that was added last to. o Job is compatible with if A is a compatible set of request Algoritmi e strutture di dati 2 Lezione1- Divide et impera 9 Example Algoritmi e strutture di dati 2 Lezione1- Divide et impera 10 5
Interval Scheduling: Analysis (2) Theorem. Greedy algorithm is optimal. Proof o o o o o Sia l insieme ottimo degli intervalli e l insieme riportato dall algoritmo Dimostriamo = (non possiamo dimostrare che = perché potrebbero esserci più soluzioni ottime) Let,, denote set of jobs selected by greedy. (ordine in cui vengono selezionate) Let,, denote set of jobs in the optimal solution (ordinate per inzio crescente stesso ordine della fine poichè gli intervalli sono compatibili). La dimostrazione procede in due passi 1. For all indices we have (per induzione) 2. The greedy algorithm returns an optimal set A. (by contradiction) Algoritmi e strutture di dati 2 Lezione1- Divide et impera 11 Interval Scheduling: Analysis Theorem. For all indices we have Proof (by induction) 1. Caso base: vero per costruzione 2. Passo r-th: Per induzione 1 (l algoritmo greedy sceglie sempre l intervallo che termina prima. The greedy algorithm stays ahead! "#! "? Can the greedy algorithm s r th interval really finish later? "# " Algoritmi e strutture di dati 2 Lezione1- Divide et impera 12 6
Interval Scheduling: Analysis (2) Theorem. The greedy algorithm returns an optimal set A. Proof (by contradiction) o Supponiamo che > % >& o esiste ' in compatibile con le richieste precedenti. o Assurdo altrimenti sarebbe stato scelto dall algoritmo greedy. Algoritmi e strutture di dati 2 Lezione1- Divide et impera 13 Interval Partitioning Interval partitioning. Many identical resources Many requests Goal: find minimum number of resources to schedule all requests without ovelapping Algoritmi e strutture di dati 2 Lezione1- Divide et impera 14 7
Interval Partioning Example Example o Lecture starts at and finishes at. o Goal: find minimum number of classrooms to schedule all lectures so that no two occur at the same time in the same room. o This schedule uses 4 classrooms to schedule 10 lectures. c d g b a f e h j i This schedule uses only 3 classrooms c d f j b g i a e h Algoritmi e strutture di dati 2 Lezione1- Divide et impera 15 Interval Partitioning: Lower Bound on Optimal Solution Def.: The depth of a set intervals to be the maximum number that pass over any single point on the time-line. Claim: In any instance of the Interval Partitioning, the number of resources needed depth. c d f j b g i a e h Algoritmi e strutture di dati 2 Lezione1- Divide et impera 16 8
Strategia E possibile determinare un algoritmo che utilizzi sempre il minimo numero di risorse? Esiste sempre uno scheduling che utilizza un numero di risorse pari alla profondità? Greedy algorithm. Consider intervals in increasing order of start time: assign an interval to any compatible resource Sort the intervals by their start times, breaking ties arbitrarily Let ( #,( ),.,( denote the intervals in this order For = #,),*.., For each interval (! that precedes ( in sorted order and overlaps it Exclude the label of Endfor If there is any label from {#,),..,,} that has not been excluded then Assign a nonexcluded label to ( Else Leave ( unlabeled Endif Endfor Algoritmi e strutture di dati 2 Lezione1- Divide et impera 17 Interval Partitioning: Greedy Algorithm Implementation. log. o For each resource, maintain the finish time of the last job added. o Keep the resource in a priority queue. Algoritmi e strutture di dati 2 Lezione1- Divide et impera 18 9
Interval Partitioning: Greedy Analysis Theorem. Greedy algorithm is optimal. Pf(Idea). 1. Every interval will be assigned a label; 2. No two incompatible intervals receive the same label Let ( and suppose there are 1 intervals overlapping There are 2+1 4 overlapping intervals 2< 4. No two incompatible intervals receive the same label: Since we sorted by start time, all these incompatibilities are caused by jobs that start no later than. all labels have been excluded, is a lower bound the algorithm is optimal. Algoritmi e strutture di dati 2 Lezione1- Divide et impera 19 Scheduling to Minimizing Lateness Algoritmi e strutture di dati 2 Lezione1- Divide et impera 20 10
Minimizing Lateness: Greedy Algorithms Algoritmi e strutture di dati 2 Lezione1- Divide et impera 21 Minimizing Lateness: Greedy Algorithms Algoritmi e strutture di dati 2 Lezione1- Divide et impera 22 11
Minimizing Lateness: Greedy Algorithms Algoritmi e strutture di dati 2 Lezione1- Divide et impera 23 Minimizing Lateness: Greedy Algorithm Algoritmi e strutture di dati 2 Lezione1- Divide et impera 24 12
Minimizing Lateness: No Idle Time Algoritmi e strutture di dati 2 Lezione1- Divide et impera 25 Minimizing Lateness: Inversions By construction Algoritmi e strutture di dati 2 Lezione1- Divide et impera 26 13
Minimizing Lateness: Inversions Algoritmi e strutture di dati 2 Lezione1- Divide et impera 27 Minimizing Lateness: Analysis of Greedy Algorithm Algoritmi e strutture di dati 2 Lezione1- Divide et impera 28 14
Greedy Analysis Strategies Algoritmi e strutture di dati 2 Lezione1- Divide et impera 29 Optimal Offline Caching Algoritmi e strutture di dati 2 Lezione1- Divide et impera 30 15
Optimal Offline Caching: Farthest-In- Future Algoritmi e strutture di dati 2 Lezione1- Divide et impera 31 Reduced Eviction Schedules In a reduced schedule the number of insertions is equal to the number of misses Algoritmi e strutture di dati 2 Lezione1- Divide et impera 32 16
Reduced Eviction Schedules Algoritmi e strutture di dati 2 Lezione1- Divide et impera 33 Farthest-In-Future: Analysis Algoritmi e strutture di dati 2 Lezione1- Divide et impera 34 17
Farthest-In-Future: Analysis same e f same e f same e d same d f Algoritmi e strutture di dati 2 Lezione1- Divide et impera 35 Farthest-In-Future: Analysis same e same f Algoritmi e strutture di dati 2 Lezione1- Divide et impera 36 18
Farthest-In-Future: Analysis same e same f same g same g Algoritmi e strutture di dati 2 Lezione1- Divide et impera 37 Caching Perspective Algoritmi e strutture di dati 2 Lezione1- Divide et impera 38 19