Greedy Algorithms Interval Scheduling and Fractional Knapsack These slides are based on the Lecture Notes by David Mount for the course CMSC 451 at the.

Slides:



Advertisements
Similar presentations
COMP 482: Design and Analysis of Algorithms
Advertisements

Lecture 7 Paradigm #5 Greedy Algorithms
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Greedy Algorithms.
Chapter 5 Fundamental Algorithm Design Techniques.
Analysis of Algorithms
Vertex Cover Dominating Set These slides are based on the Lecture Notes by David Mount for the course CMSC 451 at the University of Maryland. The Copyright.
CS216: Program and Data Representation University of Virginia Computer Science Spring 2006 David Evans Lecture 7: Greedy Algorithms
Merge Sort 4/15/2017 6:09 PM The Greedy Method The Greedy Method.
Applications of DFS: Articulation Points and Biconnected Components
Lecture 7: Greedy Algorithms II Shang-Hua Teng. Greedy algorithms A greedy algorithm always makes the choice that looks best at the moment –My everyday.
Greedy Algorithms Reading Material: –Alsuwaiyel’s Book: Section 8.1 –CLR Book (2 nd Edition): Section 16.1.
Greedy Algorithms CIS 606 Spring Greedy Algorithms Similar to dynamic programming. Used for optimization problems. Idea – When we have a choice.
CSE 421 Algorithms Richard Anderson Lecture 6 Greedy Algorithms.
Week 2: Greedy Algorithms
Lecture 7: Greedy Algorithms II
1 The Greedy Method CSC401 – Analysis of Algorithms Lecture Notes 10 The Greedy Method Objectives Introduce the Greedy Method Use the greedy method to.
9/3/10 A. Smith; based on slides by E. Demaine, C. Leiserson, S. Raskhodnikova, K. Wayne Guest lecturer: Martin Furer Algorithm Design and Analysis L ECTURE.
David Luebke 1 8/23/2015 CS 332: Algorithms Greedy Algorithms.
Called as the Interval Scheduling Problem. A simpler version of a class of scheduling problems. – Can add weights. – Can add multiple resources – Can ask.
CSCI-256 Data Structures & Algorithm Analysis Lecture Note: Some slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. 8.
David Luebke 1 10/24/2015 CS 332: Algorithms Greedy Algorithms Continued.
CSC 413/513: Intro to Algorithms Greedy Algorithms.
CSCI 256 Data Structures and Algorithm Analysis Lecture 6 Some slides by Kevin Wayne copyright 2005, Pearson Addison Wesley all rights reserved, and some.
The Greedy Method. The Greedy Method Technique The greedy method is a general algorithm design paradigm, built on the following elements: configurations:
CSC 201: Design and Analysis of Algorithms Greedy Algorithms.
1 Chapter 5-1 Greedy Algorithms Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved.
Algorithm Design Methods 황승원 Fall 2011 CSE, POSTECH.
Lecture 8 CSE 331. Main Steps in Algorithm Design Problem Statement Algorithm Problem Definition “Implementation” Analysis n! Correctness+Runtime Analysis.
Greedy Algorithms BIL741: Advanced Analysis of Algorithms I (İleri Algoritma Çözümleme I)1.
CS 3343: Analysis of Algorithms Lecture 19: Introduction to Greedy Algorithms.
Greedy algorithms David Kauchak cs302 Spring 2012.
Spring 2008The Greedy Method1. Spring 2008The Greedy Method2 Outline and Reading The Greedy Method Technique (§5.1) Fractional Knapsack Problem (§5.1.1)
CS 361 – Chapter 10 “Greedy algorithms” It’s a strategy of solving some problems –Need to make a series of choices –Each choice is made to maximize current.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 17.
Greedy Algorithms. p2. Activity-selection problem: Problem : Want to schedule as many compatible activities as possible., n activities. Activity i, start.
PREPARED BY: Qurat Ul Ain SUBMITTED TO: Ma’am Samreen.
CS6045: Advanced Algorithms Greedy Algorithms. Main Concept –Divide the problem into multiple steps (sub-problems) –For each step take the best choice.
Greedy Algorithms – Chapter 5
Lecture on Design and Analysis of Computer Algorithm
Greedy Method 6/22/2018 6:57 PM Presentation for use with the textbook, Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015.
Algorithm Design Methods
Merge Sort 7/29/ :21 PM The Greedy Method The Greedy Method.
The Greedy Method and Text Compression
Presented by Po-Chuan & Chen-Chen 2016/03/08
Algorithm Design Methods
CS 3343: Analysis of Algorithms
Merge Sort 11/28/2018 2:18 AM The Greedy Method The Greedy Method.
The Greedy Method Spring 2007 The Greedy Method Merge Sort
Merge Sort 11/28/2018 2:21 AM The Greedy Method The Greedy Method.
CS6045: Advanced Algorithms
Merge Sort 11/28/2018 8:16 AM The Greedy Method The Greedy Method.
CS4335 Design and Analysis of Algorithms/WANG Lusheng
Greedy Algorithm Enyue (Annie) Lu.
Advanced Algorithms Analysis and Design
Merge Sort 1/17/2019 3:11 AM The Greedy Method The Greedy Method.
Richard Anderson Lecture 6 Greedy Algorithms
Data Structures and Algorithms (AT70. 02) Comp. Sc. and Inf. Mgmt
Richard Anderson Autumn 2016 Lecture 7
Richard Anderson Lecture 7 Greedy Algorithms
Lecture 6 Greedy Algorithms
Algorithm Design Methods
The results for Challenging Problem 1.
Richard Anderson Winter 2019 Lecture 7
Merge Sort 5/2/2019 7:53 PM The Greedy Method The Greedy Method.
Applications of DFS: Articulation Points and Biconnected Components
Week 2: Greedy Algorithms
Richard Anderson Autumn 2015 Lecture 7
Advance Algorithm Dynamic Programming
Algorithm Design Methods
Presentation transcript:

Greedy Algorithms Interval Scheduling and Fractional Knapsack These slides are based on the Lecture Notes by David Mount for the course CMSC 451 at the University of Maryland. The Copyright notice is as follows: Copyright, David M. Mount, 2008 Dept. of Computer Science, University of Maryland, College Park, MD, These lecture notes were prepared by David Mount for the course CMSC 451, Design and Analysis of Computer Algorithms, at the University of Maryland. Permission to use, copy, modify, and distribute these notes for educational purposes and without fee is hereby granted, provided that this copyright notice appear in all copies.

Greedy Algorithms A simple design technique for optimization problems – a series of selections need to be made Builds a solution by repeatedly selecting the best (locally optimal) alternative at every step Never unmakes a decision Simple and efficient however not very powerful –May not reach the optimal for every problem –Even then, finds good approximations by using fast heuristics

Problem 1: Interval Scheduling Given R={1,2,…,n} of n activity requests –To be scheduled to use some resource –i-th request should start at time s[i] and end at time f[i] Request for interval [ s[i], f[i] ) Some intervals may overlap –Not all requests may be granted two activities i and j are non-interfering if their start-finish intervals do not overlap Select a maximum size set of mutually non-interfering activities

A few approaches Earliest activity first? –Counterexample: a single very long activity with an early start time

A few approaches Shortest activity first? Counterexample:

A few approaches Lowest conflicting activity first –First schedule the activity that overlaps the samllest number of other activities –Then eliminate it and all overlapping tasks, and update the overlap counts. –Repeat until no more tasks remain. –Counterexample?

A few approaches Lowest conflicting activity first –Counterexample?

None of the previous three strategies give the optimal solution. Do not give up! Here is a greedy strategy that works: –Select the activity that finishes first and schedule it. –Then skip the ones interfering with that one, and repeat the process –Assume the activities are sorted on finish time, i.e. f[1] <= f[2] <= f[3] <=…<= f[n]

Schedule(R) { //R holds all activity requests Sort R in ascending order of finish times S = empty // S holds the schedule while (R is nonempty) { r = the request of R having the smallest finish time Append r to the end of S Delete from R all requests that overlap r until reaching a request whose start time > the finish time of r } return S }

Schedule(R) { //R holds all activity requests Sort R in ascending order of finish times S = empty // S holds the schedule while (R is nonempty) { r = the request of R having the smallest finish time Append r to the end of S Delete from R all requests that overlap r until reaching a request whose start time > the finish time of r } return S } Total: O(n log n) + O(n) = O (n log n) O(n) O(n log n)

Scheduled 1, skipped 2 and 3

Scheduled 4, skipped 5 and 6

Scheduled 7, skipped 8

Proof of Optimality General strategy for proof of optimality of greedy algorithms: –Suppose you have a nongreedy solution –Show that its cost can be reduced by being greedier at some point in the solution For the interval scheduling problem: show that any schedule that is not greedy can be made more greedy without decreasing the number of activities

Consider any optimal schedule A that is not the greedy schedule Construct a new optimal schedule B that is greedier than A

Order the activities in in increasing order of finish time. Let Let G denote the greedy schedule and let A ang G differ at activity j Note that k >= j, since otherwise G would have more activities than the optimal schedule which would be a contradiction.

does not conflict with any earlier activity and finishes before Consider the modified greedier schedule B that results by replacing with in schedule A This is a feasible schedule –Note that cannot conflict with later activities B is greedier and has same number of activities as A. –B is also optimal. By repeating this process we convert A into G. –G is also optimal.

Problem 2: Fractional Knapsack Classical 0-1 knapsack –n items –i-th item weighs w[i] pounds and worth v[i] dollars –Knapsack can hold at most W pounds –Which items to take to maximize the value of the load

$30$20 $100$90$160

Problem 2: Fractional Knapsack Classical 0-1 knapsack –NP-complete (there probably is not an efficient solution)

Problem 2: Fractional Knapsack Fractional knapsack –n items –i-th item weighs w[i] pounds and worth v[i] dollars –Knapsack can hold at most W pounds –Which items to take to maximize the value of the load? But this time taking fractions of an item is allowed.

Good to have high value and low weight Value per pound ratio: p[i] = v[i]/w[i] Sort on decreasing p[i] Add in this order –If it fits, take it all –The last item may not fit in the remaining space as a whole—take a fraction

20 35 (out of 40) $30$20 $100$90$160 5 $270

Proof of optimality Suppose greedy selection G is not optimal –There is an alternate selection A that is optimal Sort A in decreasing p values Consider the first item i on which G and A differ –G takes more than A takes from item i (greedy takes as much as it can) –Say G takes x more units of item i –All subsequent items in A have a lesser unit value than p[i] –By replacing x units of any such items with x units of item i, we would increase the total value of A—hence a contradiction.

Greedy solution to 0-1 knapsack: not optimal $30$20 $100$90$ $220

optimal solution to 0-1 knapsack $30$20 $100$90$160 $260 40