Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Greedy Algorithms - Algorithm and Complexity Analysis - Lecture Slides, Slides of Computer Science

These are the Lecture Slides of Algorithm and Complexity Analysis which includes Approximation Algorithms, Coping with Np-Hardness, Fully Polynomial-Time, Brute-Force Algorithms, Approximation Scheme, Knapsack Problem, Profit Subset of Items, Nonnegative Values etc. Key important points are:Greedy Algorithms, Selecting Breakpoints, Minimizing Breakpoints, Predetermined Route, Dijkstra’s Shortest Path Algorithm, Huffman Codes, Activity Selection Problem, Set of Breakpoints

Typology: Slides

2012/2013

Uploaded on 03/21/2013

dharmaveer
dharmaveer 🇮🇳

4.5

(2)

54 documents

1 / 6

Toggle sidebar

Related documents


Partial preview of the text

Download Greedy Algorithms - Algorithm and Complexity Analysis - Lecture Slides and more Slides Computer Science in PDF only on Docsity! Greed "Greed is good. Greed is right. Greed works. Greed cuts through, clarifies, and captures the essence of the evolutionary spirit." Gordon Gecko (Michael Douglas) 2 Greedy Algorithms Some possibly familiar examples: ■ Gale-Shapley stable matching algorithm. ■ Dijkstra’s shortest path algorithm. ■ Prim and Kruskal MST algorithms. ■ Huffman codes. ■ . . . 3 Selecting Breakpoints Minimizing breakpoints. ■ Truck driver going from Princeton to Palo Alto along predetermined route. ■ Refueling stations at certain points along the way. ■ Truck fuel capacity = C. Greedy algorithm. ■ Go as far as you can before refueling. Princeton Palo Alto 1 C C 2 C 3 C 4 C 5 C 6 C 7 4 Sort breakpoints by increasing value: 0 = b0 < b1 < b2 < ... < bn. S ← {0} x = 0 while (x ≠ bn) let p be largest integer such that bp ≤ x + C if (bp = x) return "no solution" x ← bp S ← S ∪ {p} return S Greedy Breakpoint Selection Algorithm Selecting Breakpoints: Greedy Algorithm S = breakpoints selected. Docsity.com 5 Selecting Breakpoints Theorem: greedy algorithm is optimal. Proof (by contradiction): ■ Let 0 = g0 < g1 < . . . < gp = L denote set of breakpoints chosen by greedy and assume it is not optimal. ■ Let 0 = f0 < f1 < . . . < fq = L denote set of breakpoints in optimal solution with f0 = g0, f1= g1 , . . . , fr = gr for largest possible value of r. ■ Note: q < p. 1 2 3 4 5 6 7 1 2 3 4 5 6 7 98 r = 4 Greedy: OPT: g0 g1 g2 f0 f1 f2 gp fq gr fr 6 Selecting Breakpoints Theorem: greedy algorithm is optimal. Proof (by contradiction): ■ Let 0 = g0 < g1 < . . . < gp = L denote set of breakpoints chosen by greedy and assume it is not optimal. ■ Let 0 = f0 < f1 < . . . < fq = L denote set of breakpoints in optimal solution with f0 = g0, f1= g1 , . . . , fr = gr for largest possible value of r. ■ Note: q < p. 1 2 3 4 5 6 7 1 2 3 4 5 6 7 98Greedy: OPT: 5 r = 5 g0 g1 g2 f0 f1 f2 gp fq r = 4 7 Selecting Breakpoints Theorem: greedy algorithm is optimal. Proof (by contradiction): ■ Let 0 = g0 < g1 < . . . < gp = L denote set of breakpoints chosen by greedy and assume it is not optimal. ■ Let 0 = f0 < f1 < . . . < fq = L denote set of breakpoints in optimal solution with f0 = g0, f1= g1 , . . . , fr = gr for largest possible value of r. ■ Note: q < p. ■ Thus, f0 = g0, f1= g1 , . . . , fq = gq 1 2 3 4 1 2 3 4 6 7Greedy: OPT: 5 5 r = q = 5 g0 g1 g2 f0 f1 f2 gq fq gp 8 Activity Selection Activity selection problem (CLR 17.1). ■ Job requests 1, 2, … , n. ■ Job j starts at sj and finishes at fj. ■ Two jobs compatible if they don't overlap. ■ Goal: find maximum subset of mutually compatible jobs. Time 0 A C F B D G E 1 2 3 4 5 6 7 8 9 10 11 H Docsity.com 17 Does Greedy Always Work? US postal denominations: 1, 10, 21, 34, 70, 100, 350, 1225, 1500. ■ Ex. 140¢. ■ Greedy: 100, 34, 1, 1, 1, 1, 1, 1. ■ Optimal: 70, 70. 18 Characteristics of Greedy Algorithms Greedy choice property. ■ Globally optimal solution can be arrived at by making locally optimal (greedy) choice. ■ At each step, choose most "promising" candidate, without worrying whether it will prove to be a sound decision in long run. Optimal substructure property. ■ Optimal solution to the problem contains optimal solutions to sub- problems. – if best way to change 34¢ is {25, 5, 1, 1, 1, 1} then best way to change 29¢ is {25, 1, 1, 1, 1}. Objective function does not explicitly appear in greedy algorithm! Hard, if not impossible, to precisely define "greedy algorithm." ■ See matroids (CLR 17.4), greedoids for very general frameworks. 19 Minimizing Lateness Minimizing lateness problem. ■ Single resource can process one job at a time. ■ n jobs to be processed. – job j requires pj units of processing time. – job j has due date dj. ■ If we assign job j to start at time sj, it finishes at time fj = sj + pj. ■ Lateness: lj = max { 0, fj - dj }. ■ Goal: schedule all jobs to minimize maximum lateness L = max lj. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 d = 11 d = 8 d = 15 d = 6 d = 9 d = 9 d = 11d = 8 d = 2 d = 6 d = 9d = 9 Lateness = 3 20 Minimizing Lateness: Greedy Algorithm Sort jobs by increasing deadline so that d1 ≤ d2 ≤ … ≤ dn. t = 0 for j = 1 to n Assign job j to interval [t, t + pj] sj ← t, fj ← t + pj t ← t + pj output intervals [sj, fj] Greedy Activity Selection Algorithm max lateness = 2 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 d5 = 11d2 = 8 d6 = 15d1 = 6 d4 = 9d3 = 9 Docsity.com 21 Minimizing Lateness: No Idle Time Fact 1: there exists an optimal schedule with no idle time. Fact 2: the greedy schedule has no idle time. 0 1 2 3 4 5 6 d = 4 d = 6 7 8 9 10 11 d = 12 0 1 2 3 4 5 6 d = 4 d = 6 7 8 9 10 11 d = 12 22 Minimizing Lateness: Inversions An inversion in schedule S is a pair of jobs i and j such that: ■ i < j ■ j scheduled before i Fact 3: greedy schedule ⇔ no inversions. Fact 4: if a schedule (with no idle time) has an inversion, it has one whose with a pair of inverted jobs scheduled consecutively. d2 = 4d5 = 8 inversion 23 Minimizing Lateness: Inversions An inversion in schedule S is a pair of jobs i and j such that: ■ i < j ■ j scheduled before i Fact 3: greedy schedule ⇔ no inversions. Fact 4: if a schedule (with no idle time) has an inversion, it has one whose with a pair of inverted jobs scheduled consecutively. Fact 5: swapping two adjacent, inverted jobs: ■ Reduces the number of inversions by one. ■ Does not increase the maximum lateness. Theorem: greedy schedule is optimal. d2 = 4 d5 = 8 d2 = 4d5 = 8 inversion 24 Minimizing Lateness: Proof of Fact 5 An inversion in schedule S is a pair of jobs i and j such that: ■ i < j ■ j scheduled before i Swapping two adjacent, inverted jobs does not increase max lateness. ■ l’k = lk for all k ≠ i, j ■ l’i ≤ li ■ If job j is late: ij i j fi f’j n)(definitio )( ) timeat finishes ( n)(definitio i ii iji jjj jidf fjdf df l l ≤ <−≤ −= −′=′ Docsity.com
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved