42
14 2551 ตุลาคม Algorithm Design and Analysis This lecture note has been modified from lecture note by Prof. Francis Chin

6678908-Greedy-1

Embed Size (px)

Citation preview

Page 1: 6678908-Greedy-1

14 2551ตุลาคม

Algorithm Design and Analysis

This lecture note has been modified from lecture note by Prof. Francis Chin

Page 2: 6678908-Greedy-1

Greedy Technique 2

Greedy Method

Page 3: 6678908-Greedy-1

Greedy Technique 3

Topics Cover

The General Method Activity -Selection Problem Optimal storage on Tapes Knapsack problem Minimal Spanning Tree Single Source shortest paths

Page 4: 6678908-Greedy-1

Greedy Technique 4

Greedy Method: Definition

An algorithm which always takes the best immediate, or local, solution while finding an answer. Greedy algorithms will always find the overall, or globally, optimal solution for some optimization problems, but may find less-than-optimal solutions for some instances of other problems.

Page 5: 6678908-Greedy-1

Greedy Technique 5

Example of Greedy Method

Prim's algorithm and Kruskal's algorithm are greedy algorithms which find the globally optimal solution, a minimum spanning tree. In contrast, any known greedy algorithm to find an Euler cycle might not find the shortest path, that is, a solution to the traveling salesman problem.

Dijkstra's algorithm for finding shortest paths is another example of a greedy algorithm which finds an optimal solution.

Page 6: 6678908-Greedy-1

Greedy Technique 6

Example (cont.)

If there is no greedy algorithm which always finds the optimal solution for a problem, one may have to search (exponentially) many possible solutions to find the optimum. Greedy algorithms are usually quicker, since they don't consider possible alternatives.

Page 7: 6678908-Greedy-1

Greedy Technique 7

The Greedy Method Technique

The greedy method is a general algorithm design paradigm, built on the following elements:– configurations: different choices, collections, or

values to find– objective function: a score assigned to

configurations, which we want to either maximize or minimize

It works best when applied to problems with the greedy-choice property: – a globally-optimal solution can always be found by a

series of local improvements from a starting configuration.

Page 8: 6678908-Greedy-1

Greedy Technique 8

Making Change

Problem: A dollar amount to reach and a collection of coin amounts to use to get there.

Configuration: A dollar amount yet to return to a customer plus the coins already returned

Objective function: Minimize number of coins returned. Greedy solution: Always return the largest coin you can Example 1: Coins are valued $.32, $.08, $.01

– Has the greedy-choice property, since no amount over $.32 can be made with a minimum number of coins by omitting a $.32 coin (similarly for amounts over $.08, but under $.32).

Example 2: Coins are valued $.30, $.20, $.05, $.01– Does not have greedy-choice property, since $.40 is best made

with two $.20’s, but the greedy solution will pick three coins (which ones?)

Page 9: 6678908-Greedy-1

Greedy Technique 9

Greedy Algorithm

Start with a solution to a small subproblem Build up to a solution to the whole problem Make choices that look good in the short term

Disadvantage: Greedy algorithms don’t always work ( Short term solutions can be diastrous in the long term). Hard to prove correct

Advantage: Greedy algorithm work fast when they work. Simple algorithm, easy to implement

Page 10: 6678908-Greedy-1

Greedy Technique 10

Greedy Algorithm

Procedure GREEDY(A,n)

// A(1:n) contains the n inputs//

solution ← φ //initialize the solution to empty//

for i ← 1 to n do

x ← SELECT(A)

if FEASIBLE(solution,x)

then solution ← UNION(solution,x)

endif

repeat

return(solution)

end GREEDY

Page 11: 6678908-Greedy-1

Greedy Technique 11

Activity-Selection Problem

The problem is to select a maximum-size set of mutally compatible activities.

Example We have a set S = { 1,2,…,n} of n proposed

activities that wish to use a resource, such as a lecture hall, which can be used by only one activities at a time.

Page 12: 6678908-Greedy-1

Greedy Technique 12

Example

i si fi

1 0 62 3 53 1 44 2 135 3 86 12 147 8 118 8 129 6 1010 5 711 5 9

0 5 10 15

Page 13: 6678908-Greedy-1

Greedy Technique 13

Brute Force

Try every all possible solution Choose the largest subset which is feasible Ineffcient Θ(2n) choices

Page 14: 6678908-Greedy-1

Greedy Technique 14

Greedy Approach

150 5 10

Sort by finish time

Page 15: 6678908-Greedy-1

Greedy Technique 15

Activity-Selection Problem Pseudo code

Greedy_Activity_Selector(s,f)

1 n <- length[s]

2 A <- {1}

3 j <- 1

4 for i <- 2 to n

5 do if si > fi

6 then A <- A U {i}

7 j <- i

8 return A

It can schdule a set S of n activities in Θ(n) time, assuming that the activities were already sorted

Page 16: 6678908-Greedy-1

Greedy Technique 16

Proving the greedy algorithm correct

We assume that the input activities are in order by increasing finishing time

f1 < f2 < … < fn Activities #1 has the earliest finish time then it must

be in an optimal solution.0 5 10 15

k

1

1 possible solution

Activitiy 1

Page 17: 6678908-Greedy-1

Greedy Technique 17

Proving (cont.)

0 5 10 15

k

1

Eliminate the activities which has a start time early than the finish time of activity 1

Page 18: 6678908-Greedy-1

Greedy Technique 18

Proving (cont.)

0 5 10 15

1

Greedy algorithm produces an optimal solution

Page 19: 6678908-Greedy-1

Greedy Technique 19

Element of the Greedy Strategy

Question? How can one tell if a greedy algorithm will

solve a particular optimization problem? No general way to tell!!! There are 2 ingredients that exhibited by most

problems that lend themselves to a greedy strategy– The Greedy Choice Property– Optimal Substructure

Page 20: 6678908-Greedy-1

Greedy Technique 20

The Greedy Choice Property

A globally optimal solution can be arrived at by making a locally optimal (greedy) choice.

Make whatever choice seems best at the moment. May depend on choice so far, but not depend on any

future choices or on the solutions to subproblems

Page 21: 6678908-Greedy-1

Greedy Technique 21

Optimal Substructure

An optimal solution to the problem contains within it optimal solutions to subproblems

Page 22: 6678908-Greedy-1

Greedy Technique 22

Optimal Storage on Tapes

There are n programs that are to be stored on a computer tape of length L.

Each program i has a length li , 1≤ i ≤ n

All programs are retrieved equally often, the expected or mean retrieval time (MRT) is

∑≤≤ nj

jtn1

)/1(

Page 23: 6678908-Greedy-1

Greedy Technique 23

Optimal Storage on Tapes (cont.)

We are required to find a permutation for the n programs so that when they are stored on tape in the order the MRT is minimized.

Minimizing the MRT is equivalent to minimizing

∑∑≤≤≤≤

=jknjliID

k11

)(

Page 24: 6678908-Greedy-1

Greedy Technique 24

Example

Let n = 3 and (l1,l2,l3) = (5,10,3)

Ordering I D(I)

1,2,3 5 + 5 + 10 + 5 + 10 + 3 = 38

1,3,2 5 + 5 + 3 + 5 + 3 + 10 = 31

2,1,3 10 + 10 + 5 + 10 + 5 + 3 = 43

2,3,1 10 + 10 + 3 + 10 + 3 + 5 = 41

3,1,2 3 + 3 + 5 + 3 + 5 + 10 = 29

3,2,1 3 + 3 + 10 + 3 + 10 + 5 = 34

Page 25: 6678908-Greedy-1

Greedy Technique 25

The Greedy Solution

Make tape empty

for i := 1 to n do

grab the next shortest file

put it next on tape

The algorithm takes the best short term choice without checking to see weather it is the best long term decision.

Page 26: 6678908-Greedy-1

Greedy Technique 26

Optimal Storage on Tapes (cont.)

Theorem 4.1 If l1 ≤ l2 ≤ … ≤ ln then the ordering ij = j, 1 ≤ j ≤ n

minimizes

Over all possible permutation of the ij

∑∑= =

n

k

k

jli j1 1

Page 27: 6678908-Greedy-1

Greedy Technique 27

Knapsack Problem

We are given n objects and a knapsack. Object i has a weight wi and the knapsack has a capacity M.

If a fraction xi, 0 ≤ xi ≤ 1, of object I is placed into the knapsack the a profit of pixi is earned.

The objective is to obtain a filling of the knapsack that maximizes the total weight of all chosen objects to be at most M

maximize

subject to

and 0 ≤ xi ≤ 1, 1 ≤ I ≤ n

∑≤≤ ni

iixp1

Mxwni

ii ≤∑≤≤1

Page 28: 6678908-Greedy-1

Greedy Technique 28

Example

1020

30

50

$60 $100 $120

Item 1

Item 2

Item 3

knapsack

Page 29: 6678908-Greedy-1

Greedy Technique 29

Knapsack 0/1

30

20

$120

$100

Total =$220

20

10

$100

$60

=$160

30

10

$120

$60

=$180

Page 30: 6678908-Greedy-1

Greedy Technique 30

Fractional Knapsack

Taking the items in order of greatest value per pound yields an optimal solution

20

10

$100

$60

=$240Total

2030

$80

Page 31: 6678908-Greedy-1

Greedy Technique 31

Optimal Substructure

Both fractional knapsack and 0/1 knapsack have an optimal substructure.

Page 32: 6678908-Greedy-1

Greedy Technique 32

Example Fractional Knapsack (cont.)

There are 5 objects that have a price and weight list below, the knapsack can contain at most 100 Lbs.

Method 1 choose the least weight first– Total Weight = 10 + 20 + 30 + 40 = 100

– Total Price = 20 + 30 + 65 + 40 = 155

Price ($US) 20 30 65 40 60weight (Lbs.) 10 20 30 40 50

Page 33: 6678908-Greedy-1

Greedy Technique 33

Example Fractional Knapsack (cont.)

Method 2 choose the most expensive first– Total Weight = 30 + 50 + 20 = 100

– Total Price = 65 + 60 + 20 = 145

Price ($US) 20 30 65 40 60weight (Lbs.) 10 20 30 40 50

Page 34: 6678908-Greedy-1

Greedy Technique 34

Example Fractional Knapsack (cont.)

Method 3 choose the most price/ weight first– Total weight = 30 + 10 + 20 + 40 = 100

– Total Price = 65 + 20 + 30 + 48 = 163

Price ($US) 20 30 65 40 60weight (Lbs.) 10 20 30 40 50price/weight 2 1.5 2.1 1 1.2

Page 35: 6678908-Greedy-1

Greedy Technique 35

More Example on fractional knapsac

Consider the following instance of the knapsack problem: n = 3, M = 20, (p1,p2,p3) = 25,24,15 and (w1,w2,w3) = (18,15,10)

(x1,x2,x3)

1) (1/2,1/3,1/4) 16.5 24.25

2) (1,2/15,0) 20 28.2

3) ( 0,2/3,1) 20 31

4) ( 0,1,1/2) 20 31.5

∑ iixw ∑ iixp

Page 36: 6678908-Greedy-1

Greedy Technique 36

The Greedy Solution

Define the density of object Ai to be wi/si. Use as much of low density objects as possible. That is, process each in increasing order of density. If the whole thing ts, use all of it. If not, fill the remaining space with a fraction of the current object,and discard the rest.

First, sort the objects in nondecreasing density, so that wi/si ≤ w i+1/s i+1 for 1 ≤ i < n.

Then do the following

Page 37: 6678908-Greedy-1

Greedy Technique 37

PseudoCode

Procedure GREEDY_KNAPSACK(P,W,M,X,n)

//P(1:n) and W(1:n) contain the profits and weights respectively of the n objects ordered so that P(I)/W(I) > P(I+1)/W(I+1). M is the knapsack size and X(1:n) is the solution vector//

real P(1:n), W(1:n), X(1:n), M, cu;

integer I,n;

x <− 0; //initialize solution to zero //

cu <− M; // cu = remaining knapsack capacity //

for i <− 1 to n do

if W(i) > cu then exit endif

X(I) <− 1;

cu c - W(i);

repeat

if I < n then X(I) <− cu/W(I) endif

End GREEDY_KNAPSACK

Page 38: 6678908-Greedy-1

Greedy Technique 38

Proving Optimality

Let p1/w1 > p2/w2 > … > pn/wn

Let X = (x1,x2,…,xn) be the solution generated by

GREEDY_KNAPSACK

Let Y = (y1,y2,…,yn) be any feasible solution

We want to show that 0p)yx(

n

1iiii ≥−∑

=

Page 39: 6678908-Greedy-1

Greedy Technique 39

Proving Optimality (cont.)

If all the xi are 1, then the solution is clearly optimal

(It is the only solution) otherwise, let k be the smallest number such that xk < 1.

1 1 1 1 1 1 1 1 1 1 1 1

1 2 n

1 1 .. .. 1 xk0 0 .. .. 0 0

1 2 nk

Page 40: 6678908-Greedy-1

Greedy Technique 40

Proving Optimality (cont.)

∑=

=−n

1iiii p)yx(

1 1 .. .. 1 xk0 0 .. .. 0 0

1 2 nk

∑−

=

−1k

1i i

iiii w

pw)yx(

k

kkkk w

pw)yx( −+

∑+=

−+n

1ki i

iiii w

pw)yx(

1 2

3

Page 41: 6678908-Greedy-1

Greedy Technique 41

Proving Optimality (cont.)

Consider each of these block

∑−

=

−1k

1i i

iiii w

pw)yx( ∑

=

−1k

1i k

kiii w

pw)yx(

k

kkkk w

pw)yx( −

k

kkkk w

pw)yx( −

∑+=

−n

1ki i

iiii w

pw)yx( ∑

+=

−n

1ki k

kiii w

pw)yx(

=

Page 42: 6678908-Greedy-1

Greedy Technique 42

Proving Optimality (cont.)

∑=

≥−n

1iiii p)yx( ∑

=

−n

1i k

kiii w

pw)yx(

∑=

−n

1iiii

k

k w)yx(w

p

= W

0p)yx(n

1iiii ≥−∑

=

Since W always > 0, therefore