Greedy algorithms: introduction

Содержание

Слайд 2

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

STRUCTURE OF THE CLASS

Maximize Your Salary
Queue of Patients
Implementation and

Analysis
Main Ingredients
Слайд 3

WHAT’S COMING Solve salary maximization problem Come up with a greedy

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
Слайд 4

MAXIMIZE SALARY

MAXIMIZE SALARY

Слайд 5

MAXIMIZE SALARY

MAXIMIZE SALARY

Слайд 6

MAXIMIZE SALARY

MAXIMIZE SALARY

Слайд 7

LARGEST NUMBER

LARGEST NUMBER

Слайд 8

LARGEST NUMBER

LARGEST NUMBER

Слайд 9

LARGEST NUMBER

LARGEST NUMBER

Слайд 10

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

GREEDY STRATEGY

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

? ?
Слайд 11

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

GREEDY STRATEGY

{9, 8, 9, 6, 1}

Find max

Find max

digit
Слайд 12

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

GREEDY STRATEGY

{9, 8, 9, 6, 1}

Find max

Find max

digit
Слайд 13

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

GREEDY STRATEGY

{9, 8, 9, 6, 1} →

Find max

Find

max digit
Append it to the number

Append

Слайд 14

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

GREEDY STRATEGY

{9, 8, 9, 6, 1} → 9

Find max

Find

max digit
Append it to the number

Append

Слайд 15

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

GREEDY STRATEGY

{9, 8, 9, 6, 1} → 9

Find max

Find

max digit
Append it to the number
Remove it from the list of digits

Append

Remove

Слайд 16

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

GREEDY STRATEGY

{9, 8, 9, 6, 1} → 9

Find max

Find

max digit
Append it to the number
Remove it from the list of digits

Append

Remove

Слайд 17

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

GREEDY STRATEGY

{8, 9, 6, 1} → 9

Find max

Find max

digit
Append it to the number
Remove it from the list of digits
Repeat while there are digits in the list

Append

Remove

Слайд 18

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

GREEDY STRATEGY

{8, 9, 6, 1} → 9

Find max

Find max

digit
Append it to the number
Remove it from the list of digits
Repeat while there are digits in the list

Append

Remove

Слайд 19

GREEDY STRATEGY {8, 9, 6, 1} → 99 Find max Find

GREEDY STRATEGY

{8, 9, 6, 1} → 99

Find max

Find max

digit
Append it to the number
Remove it from the list of digits
Repeat while there are digits in the list

Append

Remove

Слайд 20

GREEDY STRATEGY {8, 9, 6, 1} → 99 Find max Find

GREEDY STRATEGY

{8, 9, 6, 1} → 99

Find max

Find max

digit
Append it to the number
Remove it from the list of digits
Repeat while there are digits in the list

Append

Remove

Слайд 21

GREEDY STRATEGY {8, 6, 1} → 99 Find max Find max

GREEDY STRATEGY

{8, 6, 1} → 99

Find max

Find max digit


Append it to the number
Remove it from the list of digits
Repeat while there are digits in the list

Append

Remove

Слайд 22

GREEDY STRATEGY {8, 6, 1} → 99 Find max Find max

GREEDY STRATEGY

{8, 6, 1} → 99

Find max

Find max digit


Append it to the number
Remove it from the list of digits
Repeat while there are digits in the list

Append

Remove

Слайд 23

GREEDY STRATEGY {8, 6, 1} → 998 Find max Find max

GREEDY STRATEGY

{8, 6, 1} → 998

Find max

Find max digit


Append it to the number
Remove it from the list of digits
Repeat while there are digits in the list

Append

Remove

Слайд 24

GREEDY STRATEGY {8, 6, 1} → 998 Find max Find max

GREEDY STRATEGY

{8, 6, 1} → 998

Find max

Find max digit


Append it to the number
Remove it from the list of digits
Repeat while there are digits in the list

Append

Remove

Слайд 25

GREEDY STRATEGY {6, 1} → 998 Find max Find max digit

GREEDY STRATEGY

{6, 1} → 998

Find max

Find max digit
Append

it to the number
Remove it from the list of digits
Repeat while there are digits in the list

Append

Remove

Слайд 26

GREEDY STRATEGY {6, 1} → 998 Find max Find max digit

GREEDY STRATEGY

{6, 1} → 998

Find max

Find max digit
Append

it to the number
Remove it from the list of digits
Repeat while there are digits in the list

Append

Remove

Слайд 27

GREEDY STRATEGY {6, 1} → 9986 Find max Find max digit

GREEDY STRATEGY

{6, 1} → 9986

Find max

Find max digit
Append

it to the number
Remove it from the list of digits
Repeat while there are digits in the list

Append

Remove

Слайд 28

GREEDY STRATEGY {6, 1} → 9986 Find max Find max digit

GREEDY STRATEGY

{6, 1} → 9986

Find max

Find max digit
Append

it to the number
Remove it from the list of digits
Repeat while there are digits in the list

Append

Remove

Слайд 29

GREEDY STRATEGY {1} → 9986 Find max Find max digit Append

GREEDY STRATEGY

{1} → 9986

Find max

Find max digit
Append it

to the number
Remove it from the list of digits
Repeat while there are digits in the list

Append

Remove

Слайд 30

GREEDY STRATEGY {1} → 9986 Find max Find max digit Append

GREEDY STRATEGY

{1} → 9986

Find max

Find max digit
Append it

to the number
Remove it from the list of digits
Repeat while there are digits in the list

Append

Remove

Слайд 31

GREEDY STRATEGY {1} → 99861 Find max Find max digit Append

GREEDY STRATEGY

{1} → 99861

Find max

Find max digit
Append it

to the number
Remove it from the list of digits
Repeat while there are digits in the list

Append

Remove

Слайд 32

GREEDY STRATEGY {1} → 99861 Find max Find max digit Append

GREEDY STRATEGY

{1} → 99861

Find max

Find max digit
Append it

to the number
Remove it from the list of digits
Repeat while there are digits in the list

Append

Remove

Слайд 33

GREEDY STRATEGY {} → 99861 Find max Find max digit Append

GREEDY STRATEGY

{} → 99861

Find max

Find max digit
Append it

to the number
Remove it from the list of digits
Repeat while there are digits in the list

Append

Remove

Слайд 34

GREEDY STRATEGY {9, 8, 9, 6, 1} → 99861 Find max

GREEDY STRATEGY

{9, 8, 9, 6, 1} → 99861

Find max

Find

max digit
Append it to the number
Remove it from the list of digits
Repeat while there are digits in the list

Append

Remove

Success!

Слайд 35

QUEUE OF PATIENTS

QUEUE OF PATIENTS

Слайд 36

QUEUE OF PATIENTS

QUEUE OF PATIENTS

Слайд 37

QUEUE OF PATIENTS

QUEUE OF PATIENTS

Слайд 38

QUEUE OF PATIENTS

QUEUE OF PATIENTS

Слайд 39

QUEUE OF PATIENTS

QUEUE OF PATIENTS

Слайд 40

QUEUE OF PATIENTS

QUEUE OF PATIENTS

Слайд 41

QUEUE OF PATIENTS

QUEUE OF PATIENTS

Слайд 42

QUEUE OF PATIENTS

QUEUE OF PATIENTS

Слайд 43

QUEUE OF PATIENTS

QUEUE OF PATIENTS

Слайд 44

QUEUE OF PATIENTS

QUEUE OF PATIENTS

Слайд 45

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

GREEDY STRATEGY

Make some greedy choice
Reduce to a smaller problem


Iterate
Слайд 46

GREEDY CHOICE First treat the patient with the maximum treatment time

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
Слайд 47

GREEDY ALGORITHM First treat the patient with the minimum treatment time

GREEDY ALGORITHM

First treat the patient with the minimum treatment time


Слайд 48

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
Слайд 49

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
Treat all the remaining patients in such order as to minimize their total waiting time
Слайд 50

SUBPROBLEM

SUBPROBLEM

Слайд 51

SUBPROBLEM

SUBPROBLEM

Слайд 52

SUBPROBLEM

SUBPROBLEM

Слайд 53

SUBPROBLEM

SUBPROBLEM

Слайд 54

SUBPROBLEM

SUBPROBLEM

Слайд 55

SAFE CHOICE

SAFE CHOICE

Слайд 56

SAFE CHOICE

SAFE CHOICE

Слайд 57

PROOF IDEA Is it possible for an optimal arrangement to have

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?
Слайд 58

PROOF IDEA Is it possible for an optimal arrangement to have

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.
Слайд 59

PROOF IDEA Is it possible for an optimal arrangement to have

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
Слайд 60

PROOF IDEA Is it possible for an optimal arrangement to have

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
Слайд 61

PROOF IDEA Is it possible for an optimal arrangement to have

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
Слайд 62

PROOF IDEA We have just proved:

PROOF IDEA

We have just proved:

Слайд 63

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
Слайд 64

SAFE CHOICE PROOF Assume the patient with treatment time tmin is

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
Слайд 65

SAFE CHOICE PROOF Assume the patient with treatment time tmin is

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
Слайд 66

Conclusion Now we know that the following greedy algorithm works correctly:

Conclusion

Now we know that the following greedy algorithm works correctly:


First treat the patient with the minimum treatment time
Слайд 67

Conclusion Now we know that the following greedy algorithm works correctly:

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
Слайд 68

Conclusion Now we know that the following greedy algorithm works correctly:

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
Слайд 69

Слайд 70

Слайд 71

Слайд 72

Слайд 73

Слайд 74

Слайд 75

Слайд 76

Слайд 77

Слайд 78

Слайд 79

Слайд 80

Слайд 81

Слайд 82

Слайд 83

Слайд 84

Слайд 85

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

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


Слайд 86

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
Слайд 87

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
This sorted arrangement is optimal
Слайд 88

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
This sorted arrangement is optimal
It is possible to sort n patients in time O(n log n)
Слайд 89

Make some first choice Then solve a problem of the same

Make some first choice
Then solve a problem of the same kind


Smaller: fewer digits, fewer patients
This is called a “subproblem”

REDUCTION TO SUBPROBLEM

Слайд 90

A choice is called safe if there is an optimal solution

A choice is called safe if there is an optimal solution

consistent with this first choice

SAFE CHOICE

Слайд 91

A choice is called safe if there is an optimal solution

A choice is called safe if there is an optimal solution

consistent with this first choice
Not all first choices are safe

SAFE CHOICE

Слайд 92

A choice is called safe if there is an optimal solution

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

SAFE CHOICE

Слайд 93

GENERAL STRATEGY Problem

GENERAL STRATEGY

Problem

Слайд 94

GENERAL STRATEGY Problem greedy choice Make a greedy choice

GENERAL STRATEGY

Problem

greedy choice

Make a greedy choice

Слайд 95

GENERAL STRATEGY Problem Safe choice greedy choice Make a greedy choice

GENERAL STRATEGY

Problem

Safe choice

greedy choice

Make a greedy

choice
Prove that it is a safe choice
Слайд 96

GENERAL STRATEGY Problem Safe choice greedy choice Subproblem Make a greedy

GENERAL STRATEGY

Problem

Safe choice

greedy choice

Subproblem

Make a

greedy choice
Prove that it is a safe choice
Reduce to a subproblem