1 greedy methods: lecture 1 jose rolim university of geneva

21
1 Greedy methods: Lecture 1 Jose Rolim University of Geneva

Post on 20-Dec-2015

219 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: 1 Greedy methods: Lecture 1 Jose Rolim University of Geneva

1

Greedy methods: Lecture 1

Jose RolimUniversity of Geneva

Page 2: 1 Greedy methods: Lecture 1 Jose Rolim University of Geneva

2007 Algorithmique- Greedy 1

Jose Rolim 2

Reference book

Book:

Page 3: 1 Greedy methods: Lecture 1 Jose Rolim University of Geneva

2007 Algorithmique- Greedy 1

Jose Rolim 3

Also:

Brassard, Gilles et Bratley, Paul. Fundamentals of Algorithmics, Prentice-Hall. ISBN : 0-13-335068-1

E. Horowitz, S. Sahni, and S. Rajasekaran. Computer Algorithms. W. H. Freeman Press, 1997.

Page 4: 1 Greedy methods: Lecture 1 Jose Rolim University of Geneva

2007 Algorithmique- Greedy 1

Jose Rolim 4

Evaluation

Written Exam No C.C Program Challenge

Page 5: 1 Greedy methods: Lecture 1 Jose Rolim University of Geneva

2007 Algorithmique- Greedy 1

Jose Rolim 5

Program

Greedy – JR (2 weeks) Divide and Conquer – BC (2 weeks) Dynamic Programming – JR (2 weeks) Backtracking – BC (2 weeks) Advanced Graph Algorithms – JR (2

weeks) Branch and Bound – BC (2 weeks)

Page 6: 1 Greedy methods: Lecture 1 Jose Rolim University of Geneva

2007 Algorithmique- Greedy 1

Jose Rolim 6

Courses

Monday at Uni-Dufour

Wednesday at Battelle

Page 7: 1 Greedy methods: Lecture 1 Jose Rolim University of Geneva

2007 Algorithmique- Greedy 1

Jose Rolim 7

Greedy - Principles

Optimization problem Best choice at any moment Basic steps:

Define the problem and corresponding greedy strategy

Show that greedy approach leads to optimal solution

Page 8: 1 Greedy methods: Lecture 1 Jose Rolim University of Geneva

2007 Algorithmique- Greedy 1

Jose Rolim 8

Previous examples

BSF DSF

????

Page 9: 1 Greedy methods: Lecture 1 Jose Rolim University of Geneva

2007 Algorithmique- Greedy 1

Jose Rolim 9

Ex: Making changes

A dollar amount to reach and a collection of coins amount to get there

PROBLEM: Minimize the number of coins returned

Page 10: 1 Greedy methods: Lecture 1 Jose Rolim University of Geneva

2007 Algorithmique- Greedy 1

Jose Rolim 10

Example 1:

Amount to be returned: 2.89 Coins available:

100 cents 25 cents 10 cents 5 cents 1 cent

Page 11: 1 Greedy methods: Lecture 1 Jose Rolim University of Geneva

2007 Algorithmique- Greedy 1

Jose Rolim 11

Solution

10 coins: 2 dollars 3 quarters 1 dime 4 pennies

Page 12: 1 Greedy methods: Lecture 1 Jose Rolim University of Geneva

2007 Algorithmique- Greedy 1

Jose Rolim 12

Greedy strategy

At every stage add a coin of the largest possible value that does not pass the amount to be paid

Does the greed strategy always work???? YES. For this set of coins NOT NECESSARLY for another set

(EX???)

Page 13: 1 Greedy methods: Lecture 1 Jose Rolim University of Geneva

2007 Algorithmique- Greedy 1

Jose Rolim 13

Knapsack

Page 14: 1 Greedy methods: Lecture 1 Jose Rolim University of Geneva

2007 Algorithmique- Greedy 1

Jose Rolim 14

Knapsack

0-1 knapsack problem: How to fill the knapsack with best total

value w/o breaking each item Fractional knapsack problem:

How to fill the knapsack with best total value

Page 15: 1 Greedy methods: Lecture 1 Jose Rolim University of Geneva

2007 Algorithmique- Greedy 1

Jose Rolim 15

Solutions ????

NP- hard for 0-1 knapsack Greedy for fractional knapsack

Compute value per pound (VPP) for each item

Take as much as possible of the item with largest VPP

Continue till reach weight limit

Page 16: 1 Greedy methods: Lecture 1 Jose Rolim University of Geneva

2007 Algorithmique- Greedy 1

Jose Rolim 16

Example

1020

30

$60 $100 $120

VPP: 6 5 4

Knapsack: hole weight 50

10

20

20--30

Page 17: 1 Greedy methods: Lecture 1 Jose Rolim University of Geneva

2007 Algorithmique- Greedy 1

Jose Rolim 17

Greedy Fails for 0-1 Knapsack

1020

30

$60 $100 $120

VPP: 6 5 410

20

$160

10

30

$180

20

30

$220

Page 18: 1 Greedy methods: Lecture 1 Jose Rolim University of Geneva

2007 Algorithmique- Greedy 1

Jose Rolim 18

Task scheduling problem

We have a set T of N tasks, each with a start time si and a finishing time fi (si<fi).

Each task must start at time si and will finish at time fi.

Each task is executed on a machine Mj. A machine can execute only one task at a time, but

can start a task at the same time as the current task ends (red lines on the figure to the right).

Tasks are non-conflicting if they do not conflict, i.e., if fi≤sj or fj≤si. For example, think of tasks as classes.

The problem is to schedule all the tasks in T using the minimal number of machines.

Page 19: 1 Greedy methods: Lecture 1 Jose Rolim University of Geneva

2007 Algorithmique- Greedy 1

Jose Rolim 19

Example

There are 6 tasks, with start times and finishing times (1,3), (2,5), (2,6), (4,5), (5,8), (5,7).

Page 20: 1 Greedy methods: Lecture 1 Jose Rolim University of Geneva

2007 Algorithmique- Greedy 1

Jose Rolim 20

Greedy strategy

In our greedy strategy we only go to a new machine when we find a task that cannot be scheduled on the current machines

Page 21: 1 Greedy methods: Lecture 1 Jose Rolim University of Geneva

2007 Algorithmique- Greedy 1

Jose Rolim 21

In the example

They are scheduled on three machines M1, M2, M3.

Clearly 3 machines are needed as can be seen by looking at time 4.