Greedy Algorithms: Introduction

Slides:



Advertisements
Similar presentations
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Advertisements

Minimum Clique Partition Problem with Constrained Weight for Interval Graphs Jianping Li Department of Mathematics Yunnan University Jointed by M.X. Chen.
Algorithm Design Techniques: Greedy Algorithms. Introduction Algorithm Design Techniques –Design of algorithms –Algorithms commonly used to solve problems.
Comp 122, Spring 2004 Greedy Algorithms. greedy - 2 Lin / Devi Comp 122, Fall 2003 Overview  Like dynamic programming, used to solve optimization problems.
Greedy Algorithms Amihood Amir Bar-Ilan University.
Greedy Algorithms Greed is good. (Some of the time)
Analysis of Algorithms
Greedy Algorithms Basic idea Connection to dynamic programming
Greedy Algorithms Basic idea Connection to dynamic programming Proof Techniques.
Cs333/cutler Greedy1 Introduction to Greedy Algorithms The greedy technique Problems explored –The coin changing problem –Activity selection.
Merge Sort 4/15/2017 6:09 PM The Greedy Method The Greedy Method.
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.
CSE115/ENGR160 Discrete Mathematics 03/03/11 Ming-Hsuan Yang UC Merced 1.
CPSC 411, Fall 2008: Set 4 1 CPSC 411 Design and Analysis of Algorithms Set 4: Greedy Algorithms Prof. Jennifer Welch Fall 2008.
Greedy Algorithms Reading Material: Chapter 8 (Except Section 8.5)
Greedy Algorithms CIS 606 Spring Greedy Algorithms Similar to dynamic programming. Used for optimization problems. Idea – When we have a choice.
Greedy Algorithms Like dynamic programming algorithms, greedy algorithms are usually designed to solve optimization problems Unlike dynamic programming.
Week 2: Greedy Algorithms
1 The Greedy Method CSC401 – Analysis of Algorithms Lecture Notes 10 The Greedy Method Objectives Introduce the Greedy Method Use the greedy method to.
2IL05 Data Structures Fall 2007 Lecture 13: Minimum Spanning Trees.
Spring 2015 Lecture 11: Minimum Spanning Trees
The Fast Optimal Voltage Partitioning Algorithm For Peak Power Density Minimization Jia Wang, Shiyan Hu Department of Electrical and Computer Engineering.
1 CPSC 320: Intermediate Algorithm Design and Analysis July 28, 2014.
The Greedy Method. The Greedy Method Technique The greedy method is a general algorithm design paradigm, built on the following elements: configurations:
Dynamic Programming.  Decomposes a problem into a series of sub- problems  Builds up correct solutions to larger and larger sub- problems  Examples.
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.
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 Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Greedy Algorithms Many of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
Algorithm Design Techniques, Greedy Method – Knapsack Problem, Job Sequencing, Divide and Conquer Method – Quick Sort, Finding Maximum and Minimum, Dynamic.
Merge Sort 1/12/2018 5:48 AM Merge Sort 7 2   7  2  2 7
CSE15 Discrete Mathematics 03/06/17
Greedy Algorithms General principle of greedy algorithm
Greedy algorithms: CSC317
Data Structures and Algorithms
Analysis of Algorithms CS 477/677
Chapter 7 Sorting Spring 14
Greedy Algorithms Basic idea Connection to dynamic programming
Analysis and design of algorithm
Bubble Sort Bubble sort is one way to sort an array of numbers. Adjacent values are swapped until the array is completely sorted. This algorithm gets its.
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 8:16 AM The Greedy Method The Greedy Method.
Algorithms (2IL15) – Lecture 2
Discrete Mathematics CMP-101 Lecture 12 Sorting, Bubble Sort, Insertion Sort, Greedy Algorithms Abdul Hameed
Advanced Algorithms Analysis and Design
Greedy Algorithms.
Merge Sort 1/17/2019 3:11 AM The Greedy Method The Greedy Method.
Richard Anderson Lecture 6 Greedy Algorithms
Lecture 19 CSE 331 Oct 8, 2014.
Richard Anderson Autumn 2016 Lecture 7
EE 312 Software Design and Implementation I
Lecture 18 CSE 331 Oct 9, 2017.
Richard Anderson Lecture 7 Greedy Algorithms
Introduction: Why Study Algorithms?
Lecture 14 Shortest Path (cont’d) Minimum Spanning Tree
Greedy Algorithms: Maximizing Loot
Greedy Algorithms Comp 122, Spring 2004.
Richard Anderson Winter 2019 Lecture 7
Merge Sort 5/2/2019 7:53 PM The Greedy Method The Greedy Method.
Introduction: Asymptotic Notation
Week 2: Greedy Algorithms
Lecture 19 CSE 331 Oct 10, 2016.
Lecture 13 Shortest Path (cont’d) Minimum Spanning Tree
Richard Anderson Autumn 2019 Lecture 7
Algorithms Tutorial 27th Sept, 2019.
Presentation transcript:

Greedy Algorithms: Introduction http://iti.wtf Algorithms and Data Structures Greedy Algorithms: Introduction Artem A. Golubnichiy artem@golubnichij.ru Department of Software of Computer Facilities and Automated Systems Katanov Khakass State University

STRUCTURE OF THE CLASS Maximize Your Salary Queue of Patients Implementation and Analysis Main Ingredients

WHAT’S COMING Solve salary maximization problem Come up with a greedy algorithm yourself Solve optimal queue arrangement problem Generalize solutions using the concepts of greedy choice, subproblem and safe choice

MAXIMIZE SALARY

MAXIMIZE SALARY

MAXIMIZE SALARY

LARGEST NUMBER Toy problem What is the largest number that consists of digits 9, 8, 9, 6, 1? Use all the digits.

LARGEST NUMBER Toy problem What is the largest number that consists of digits 9, 8, 9, 6, 1? Use all the digits. Examples 16899, 69891, 98961, . . .

LARGEST NUMBER Correct Answer 99861

GREEDY STRATEGY {9, 8, 9, 6, 1} → ? ? ? ? ?

GREEDY STRATEGY Find max {9, 8, 9, 6, 1} Find max digit

GREEDY STRATEGY Find max {9, 8, 9, 6, 1} Find max digit

GREEDY STRATEGY Find max Append {9, 8, 9, 6, 1} → Find max digit {9, 8, 9, 6, 1} → Find max digit Append it to the number

GREEDY STRATEGY Find max Append {9, 8, 9, 6, 1} → 9 Find max digit {9, 8, 9, 6, 1} → 9 Find max digit Append it to the number

GREEDY STRATEGY Find max Append {9, 8, 9, 6, 1} → 9 Remove {9, 8, 9, 6, 1} → 9 Remove Find max digit Append it to the number Remove it from the list of digits

GREEDY STRATEGY Find max Append {9, 8, 9, 6, 1} → 9 Remove {9, 8, 9, 6, 1} → 9 Remove Find max digit Append it to the number Remove it from the list of digits

GREEDY STRATEGY Find max Append {8, 9, 6, 1} → 9 Remove Find max digit {8, 9, 6, 1} → 9 Remove Find max digit Append it to the number Remove it from the list of digits Repeat while there are digits in the list

GREEDY STRATEGY Find max Append {8, 9, 6, 1} → 9 Remove Find max digit {8, 9, 6, 1} → 9 Remove Find max digit Append it to the number Remove it from the list of digits Repeat while there are digits in the list

GREEDY STRATEGY Find max Append {8, 9, 6, 1} → 99 Remove {8, 9, 6, 1} → 99 Remove Find max digit Append it to the number Remove it from the list of digits Repeat while there are digits in the list

GREEDY STRATEGY Find max Append {8, 9, 6, 1} → 99 Remove {8, 9, 6, 1} → 99 Remove Find max digit Append it to the number Remove it from the list of digits Repeat while there are digits in the list

GREEDY STRATEGY Find max Append {8, 6, 1} → 99 Remove Find max digit {8, 6, 1} → 99 Remove Find max digit Append it to the number Remove it from the list of digits Repeat while there are digits in the list

GREEDY STRATEGY Find max Append {8, 6, 1} → 99 Remove Find max digit {8, 6, 1} → 99 Remove Find max digit Append it to the number Remove it from the list of digits Repeat while there are digits in the list

GREEDY STRATEGY Find max Append {8, 6, 1} → 998 Remove Find max digit {8, 6, 1} → 998 Remove Find max digit Append it to the number Remove it from the list of digits Repeat while there are digits in the list

GREEDY STRATEGY Find max Append {8, 6, 1} → 998 Remove Find max digit {8, 6, 1} → 998 Remove Find max digit Append it to the number Remove it from the list of digits Repeat while there are digits in the list

GREEDY STRATEGY Find max Append {6, 1} → 998 Remove Find max digit {6, 1} → 998 Remove Find max digit Append it to the number Remove it from the list of digits Repeat while there are digits in the list

GREEDY STRATEGY Find max Append {6, 1} → 998 Remove Find max digit {6, 1} → 998 Remove Find max digit Append it to the number Remove it from the list of digits Repeat while there are digits in the list

GREEDY STRATEGY Find max Append {6, 1} → 9986 Remove Find max digit {6, 1} → 9986 Remove Find max digit Append it to the number Remove it from the list of digits Repeat while there are digits in the list

GREEDY STRATEGY Find max Append {6, 1} → 9986 Remove Find max digit {6, 1} → 9986 Remove Find max digit Append it to the number Remove it from the list of digits Repeat while there are digits in the list

GREEDY STRATEGY Find max Append {1} → 9986 Remove Find max digit {1} → 9986 Remove Find max digit Append it to the number Remove it from the list of digits Repeat while there are digits in the list

GREEDY STRATEGY Find max Append {1} → 9986 Remove Find max digit {1} → 9986 Remove Find max digit Append it to the number Remove it from the list of digits Repeat while there are digits in the list

GREEDY STRATEGY Find max Append {1} → 99861 Remove Find max digit {1} → 99861 Remove Find max digit Append it to the number Remove it from the list of digits Repeat while there are digits in the list

GREEDY STRATEGY Find max Append {1} → 99861 Remove Find max digit {1} → 99861 Remove Find max digit Append it to the number Remove it from the list of digits Repeat while there are digits in the list

GREEDY STRATEGY Find max Append {} → 99861 Remove Find max digit {} → 99861 Remove Find max digit Append it to the number Remove it from the list of digits Repeat while there are digits in the list

GREEDY STRATEGY Find max Append {9, 8, 9, 6, 1} → 99861 Remove {9, 8, 9, 6, 1} → 99861 Remove Success! Find max digit Append it to the number Remove it from the list of digits Repeat while there are digits in the list

QUEUE OF PATIENTS

QUEUE OF PATIENTS Queue Arrangement Input: n patients have come to the doctor’s office at 9:00AM. They can be treated in any order. For i-th patient, the time needed for treatment is ti. You need to arrange the patients in such a queue that the total waiting time is minimized. Output: The minimum total waiting time.

QUEUE OF PATIENTS Optimal Queue Arrangement t1 =15, t2 =20 and t3 =10. Arrangement (1, 2, 3): First patient doesn’t wait

QUEUE OF PATIENTS Optimal Queue Arrangement t1 =15, t2 =20 and t3 =10. Arrangement (1, 2, 3): First patient doesn’t wait Second patient waits for 15 minutes

QUEUE OF PATIENTS Optimal Queue Arrangement t1 =15, t2 =20 and t3 =10. Arrangement (1, 2, 3): First patient doesn’t wait Second patient waits for 15 minutes Third patient waits for 15 + 20 = 35 minutes

QUEUE OF PATIENTS Optimal Queue Arrangement t1 =15, t2 =20 and t3 =10. Arrangement (1, 2, 3): First patient doesn’t wait Second patient waits for 15 minutes Third patient waits for 15 + 20 = 35 minutes Total waiting time 15 + 35 = 50 minutes

QUEUE OF PATIENTS Optimal Queue Arrangement t1 =15, t2 =20 and t3 =10. Arrangement (3, 1, 2): First patient doesn’t wait

QUEUE OF PATIENTS Optimal Queue Arrangement t1 =15, t2 =20 and t3 =10. Arrangement (3, 1, 2): First patient doesn’t wait Second patient waits for 10 minutes

QUEUE OF PATIENTS Optimal Queue Arrangement t1 =15, t2 =20 and t3 =10. Arrangement (3, 1, 2): First patient doesn’t wait Second patient waits for 10 minutes Third patient waits for 10 + 15 = 25 minutes

QUEUE OF PATIENTS Optimal Queue Arrangement t1 =15, t2 =20 and t3 =10. Arrangement (3, 1, 2): First patient doesn’t wait Second patient waits for 10 minutes Third patient waits for 10 + 15 = 25 minutes Total waiting time 10 + 25 = 35 minutes

GREEDY STRATEGY Make some greedy choice Reduce to a smaller problem Iterate

GREEDY CHOICE First treat the patient with the maximum treatment time First treat the patient with the minimum treatment time First treat the patient with average treatment time

GREEDY ALGORITHM First treat the patient with the minimum treatment time

GREEDY ALGORITHM First treat the patient with the minimum treatment time Remove this patient from the queue

GREEDY ALGORITHM First treat the patient with the minimum treatment time Remove this patient from the queue Treat all the remaining patients in such order as to minimize their total waiting time

SUBPROBLEM Definition Subproblem is a similar problem of smaller size.

SUBPROBLEM Examples MaximumSalary(1, 9, 8, 9, 6) =

SUBPROBLEM Examples MaximumSalary(1, 9, 8, 9, 6) =

SUBPROBLEM Examples MaximumSalary(1, 9, 8, 9, 6) = Minimum total waiting time for n patients = (n − 1) · tmin+

SUBPROBLEM Examples MaximumSalary(1, 9, 8, 9, 6) = Minimum total waiting time for n patients = (n − 1) · tmin+ minimum total waiting time for n − 1 patients without tmin

SAFE CHOICE Definition A greedy choice is called safe choice if there is an optimal solution consistent with this first choice.

SAFE CHOICE Lemma To treat the patient with minimum treatment time tmin first is a safe choice.

PROOF IDEA Is it possible for an optimal arrangement to have two consecutive patients in order with treatment times t1 and t2 such that t1 > t2?

PROOF IDEA Is it possible for an optimal arrangement to have two consecutive patients in order with treatment times t1 and t2 such that t1 > t2? It is impossible. Assume there is such an optimal arrangement and consider what happens if we swap these two patients.

PROOF IDEA Is it possible for an optimal arrangement to have two consecutive patients in order with treatment times t1 and t2 such that t1 > t2? It is impossible. Assume there is such an optimal arrangement and consider what happens if we swap these two patients. If we swap two consecutive patients with treatment times t1 > t2: Waiting time for all the patients before and after these two doesn’t change

PROOF IDEA Is it possible for an optimal arrangement to have two consecutive patients in order with treatment times t1 and t2 such that t1 > t2? It is impossible. Assume there is such an optimal arrangement and consider what happens if we swap these two patients. If we swap two consecutive patients with treatment times t1 > t2: Waiting time for all the patients before and after these two doesn’t change Waiting time for the patient which was first increases by t2, and for the second one it decreases by t1

PROOF IDEA Is it possible for an optimal arrangement to have two consecutive patients in order with treatment times t1 and t2 such that t1 > t2? It is impossible. Assume there is such an optimal arrangement and consider what happens if we swap these two patients. If we swap two consecutive patients with treatment times t1 > t2: Waiting time for all the patients before and after these two doesn’t change Waiting time for the patient which was first increases by t2, and for the second one it decreases by t1 Total waiting time increases by t2 − t1 < 0, so it actually decreases

PROOF IDEA We have just proved: Lemma In any optimal arrangement of the patients, first of any two consecutive patients has smaller treatment time.

SAFE CHOICE PROOF Assume the patient with treatment time tmin is not the first

SAFE CHOICE PROOF Assume the patient with treatment time tmin is not the first Let i > 1 be the position of the first patient with treatment time tmin in the optimal arrangement

SAFE CHOICE PROOF Assume the patient with treatment time tmin is not the first Let i > 1 be the position of the first patient with treatment time tmin in the optimal arrangement Then the patient at position i − 1 has bigger treatment time — a contradiction

Conclusion Now we know that the following greedy algorithm works correctly: First treat the patient with the minimum treatment time

Conclusion Now we know that the following greedy algorithm works correctly: First treat the patient with the minimum treatment time Remove this patient from the queue

Conclusion Now we know that the following greedy algorithm works correctly: First treat the patient with the minimum treatment time Remove this patient from the queue Treat all the remaining patients in such order as to minimize their total waiting time

MinTotalWaitingTime(t, n) waitingTime ← 0 treated ← array of n zeros for i from 1 to n: tmin ← +∞ minIndex ← 0 for j from 1 to n: if treated[j] == 0 and t[j] < tmin: tmin ← t[j] minIndex ← j waitingTime ← waitingTime + (n − i) · tmin treated[minIndex] = 1 return waitingTime

MinTotalWaitingTime(t, n) waitingTime ← 0 treated ← array of n zeros for i from 1 to n: tmin ← +∞ minIndex ← 0 for j from 1 to n: if treated[j] == 0 and t[j] < tmin: tmin ← t[j] minIndex ← j waitingTime ← waitingTime + (n − i) · tmin treated[minIndex] = 1 return waitingTime

MinTotalWaitingTime(t, n) waitingTime ← 0 treated ← array of n zeros for i from 1 to n: tmin ← +∞ minIndex ← 0 for j from 1 to n: if treated[j] == 0 and t[j] < tmin: tmin ← t[j] minIndex ← j waitingTime ← waitingTime + (n − i) · tmin treated[minIndex] = 1 return waitingTime

MinTotalWaitingTime(t, n) waitingTime ← 0 treated ← array of n zeros for i from 1 to n: tmin ← +∞ minIndex ← 0 for j from 1 to n: if treated[j] == 0 and t[j] < tmin: tmin ← t[j] minIndex ← j waitingTime ← waitingTime + (n − i) · tmin treated[minIndex] = 1 return waitingTime

MinTotalWaitingTime(t, n) waitingTime ← 0 treated ← array of n zeros for i from 1 to n: tmin ← +∞ minIndex ← 0 for j from 1 to n: if treated[j] == 0 and t[j] < tmin: tmin ← t[j] minIndex ← j waitingTime ← waitingTime + (n − i) · tmin treated[minIndex] = 1 return waitingTime

MinTotalWaitingTime(t, n) waitingTime ← 0 treated ← array of n zeros for i from 1 to n: tmin ← +∞ minIndex ← 0 for j from 1 to n: if treated[j] == 0 and t[j] < tmin: tmin ← t[j] minIndex ← j waitingTime ← waitingTime + (n − i) · tmin treated[minIndex] = 1 return waitingTime

MinTotalWaitingTime(t, n) waitingTime ← 0 treated ← array of n zeros for i from 1 to n: tmin ← +∞ minIndex ← 0 for j from 1 to n: if treated[j] == 0 and t[j] < tmin: tmin ← t[j] minIndex ← j waitingTime ← waitingTime + (n − i) · tmin treated[minIndex] = 1 return waitingTime

MinTotalWaitingTime(t, n) waitingTime ← 0 treated ← array of n zeros for i from 1 to n: tmin ← +∞ minIndex ← 0 for j from 1 to n: if treated[j] == 0 and t[j] < tmin: tmin ← t[j] minIndex ← j waitingTime ← waitingTime + (n − i) · tmin treated[minIndex] = 1 return waitingTime

MinTotalWaitingTime(t, n) waitingTime ← 0 treated ← array of n zeros for i from 1 to n: tmin ← +∞ minIndex ← 0 for j from 1 to n: if treated[j] == 0 and t[j] < tmin: tmin ← t[j] minIndex ← j waitingTime ← waitingTime + (n − i) · tmin treated[minIndex] = 1 return waitingTime

MinTotalWaitingTime(t, n) waitingTime ← 0 treated ← array of n zeros for i from 1 to n: tmin ← +∞ minIndex ← 0 for j from 1 to n: if treated[j] == 0 and t[j] < tmin: tmin ← t[j] minIndex ← j waitingTime ← waitingTime + (n − i) · tmin treated[minIndex] = 1 return waitingTime

MinTotalWaitingTime(t, n) waitingTime ← 0 treated ← array of n zeros for i from 1 to n: tmin ← +∞ minIndex ← 0 for j from 1 to n: if treated[j] == 0 and t[j] < tmin: tmin ← t[j] minIndex ← j waitingTime ← waitingTime + (n − i) · tmin treated[minIndex] = 1 return waitingTime

MinTotalWaitingTime(t, n) waitingTime ← 0 treated ← array of n zeros for i from 1 to n: tmin ← +∞ minIndex ← 0 for j from 1 to n: if treated[j] == 0 and t[j] < tmin: tmin ← t[j] minIndex ← j waitingTime ← waitingTime + (n − i) · tmin treated[minIndex] = 1 return waitingTime

Lemma The running time of MinTotalWaitingTime(t, n) is O(n2).

Lemma The running time of MinTotalWaitingTime(t, n) is O(n2). Proof i changes from 1 to n

Lemma The running time of MinTotalWaitingTime(t, n) is O(n2). Proof i changes from 1 to n For each value of i, j changes from 1 to n

Lemma The running time of MinTotalWaitingTime(t, n) is O(n2). Proof i changes from 1 to n For each value of i, j changes from 1 to n This results in O(n2)

Actually, this problem can be solved in time O(n log n)

Actually, this problem can be solved in time O(n log n) Instead of choosing the patient with minimum treatment time out of remaining ones n times, sort patients by increasing treatment time

Actually, this problem can be solved in time O(n log n) Instead of choosing the patient with minimum treatment time out of remaining ones n times, sort patients by increasing treatment time This sorted arrangement is optimal

Actually, this problem can be solved in time O(n log n) Instead of choosing the patient with minimum treatment time out of remaining ones n times, sort patients by increasing treatment time This sorted arrangement is optimal It is possible to sort n patients in time O(n log n)

REDUCTION TO SUBPROBLEM Make some first choice Then solve a problem of the same kind Smaller: fewer digits, fewer patients This is called a “subproblem”

SAFE CHOICE A choice is called safe if there is an optimal solution consistent with this first choice

SAFE CHOICE A choice is called safe if there is an optimal solution consistent with this first choice Not all first choices are safe

SAFE CHOICE A choice is called safe if there is an optimal solution consistent with this first choice Not all first choices are safe Greedy choices are often unsafe

GENERAL STRATEGY Problem

GENERAL STRATEGY greedy choice Problem Make a greedy choice

GENERAL STRATEGY greedy choice Problem Safe choice Make a greedy choice Prove that it is a safe choice

GENERAL STRATEGY greedy choice Problem Safe choice Subproblem Make a greedy choice Prove that it is a safe choice Reduce to a subproblem

GENERAL STRATEGY greedy choice Problem Safe choice Subproblem Make a greedy choice Prove that it is a safe choice Reduce to a subproblem Solve the subproblem