+ All Categories
Home > Documents > Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of...

Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of...

Date post: 25-Apr-2018
Category:
Upload: phamdat
View: 254 times
Download: 4 times
Share this document with a friend
58
Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 1 / 14
Transcript
Page 1: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

Greedy Algorithms: The Fractional Knapsack

Version of November 5, 2014

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 1 / 14

Page 2: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

Outline

Outline

Introduction

The Knapsack problem.

A greedy algorithm for the fractional knapsack problem

Correctness

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 2 / 14

Page 3: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

Introduction to Greedy Algorithm

A greedy algorithm for an optimization problem always makesthe choice that looks best at the moment and adds it to thecurrent subsolution.

Final output is an optimal solution.

Greedy algorithms don’t always yield optimal solutions but,when they do, they’re usually the simplest and most efficientalgorithms available.

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 3 / 14

Page 4: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

Introduction to Greedy Algorithm

A greedy algorithm for an optimization problem always makesthe choice that looks best at the moment and adds it to thecurrent subsolution.

Final output is an optimal solution.

Greedy algorithms don’t always yield optimal solutions but,when they do, they’re usually the simplest and most efficientalgorithms available.

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 3 / 14

Page 5: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

Introduction to Greedy Algorithm

A greedy algorithm for an optimization problem always makesthe choice that looks best at the moment and adds it to thecurrent subsolution.

Final output is an optimal solution.

Greedy algorithms don’t always yield optimal solutions

but,when they do, they’re usually the simplest and most efficientalgorithms available.

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 3 / 14

Page 6: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

Introduction to Greedy Algorithm

A greedy algorithm for an optimization problem always makesthe choice that looks best at the moment and adds it to thecurrent subsolution.

Final output is an optimal solution.

Greedy algorithms don’t always yield optimal solutions but,when they do, they’re usually the simplest and most efficientalgorithms available.

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 3 / 14

Page 7: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

Outline

Introduction

The Knapsack problem.

A greedy algorithm for the fractional knapsack problem

Correctness

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 4 / 14

Page 8: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

The Knapsack Problem...

Gifts

2pd 3 pd2 pd

C

BA

$120$10$100

Capacity of knapsack: K = 4

Fractional Knapsack Problem:Can take a fraction of an item.

$100

2 pd A

2 pd C$80

Solution:

0-1 Knapsack Problem:Can only take or leave item. Youcan’t take a fraction.

Solution:

$120

C3 pd

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 5 / 14

Page 9: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

The Knapsack Problem...

Gifts

2pd 3 pd2 pd

C

BA

$120$10$100

Capacity of knapsack: K = 4

Fractional Knapsack Problem:

Can take a fraction of an item.

$100

2 pd A

2 pd C$80

Solution:

0-1 Knapsack Problem:Can only take or leave item. Youcan’t take a fraction.

Solution:

$120

C3 pd

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 5 / 14

Page 10: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

The Knapsack Problem...

Gifts

2pd 3 pd2 pd

C

BA

$120$10$100

Capacity of knapsack: K = 4

Fractional Knapsack Problem:Can take a fraction of an item.

$100

2 pd A

2 pd C$80

Solution:

0-1 Knapsack Problem:Can only take or leave item. Youcan’t take a fraction.

Solution:

$120

C3 pd

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 5 / 14

Page 11: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

The Knapsack Problem...

Gifts

2pd 3 pd2 pd

C

BA

$120$10$100

Capacity of knapsack: K = 4

Fractional Knapsack Problem:Can take a fraction of an item.

$100

2 pd A

2 pd C$80

Solution:

0-1 Knapsack Problem:Can only take or leave item. Youcan’t take a fraction.

Solution:

$120

C3 pd

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 5 / 14

Page 12: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

The Knapsack Problem...

Gifts

2pd 3 pd2 pd

C

BA

$120$10$100

Capacity of knapsack: K = 4

Fractional Knapsack Problem:Can take a fraction of an item.

$100

2 pd A

2 pd C$80

Solution:

0-1 Knapsack Problem:Can only take or leave item. Youcan’t take a fraction.

Solution:

$120

C3 pd

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 5 / 14

Page 13: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

The Knapsack Problem...

Gifts

2pd 3 pd2 pd

C

BA

$120$10$100

Capacity of knapsack: K = 4

Fractional Knapsack Problem:Can take a fraction of an item.

$100

2 pd A

2 pd C$80

Solution:

0-1 Knapsack Problem:Can only take or leave item. Youcan’t take a fraction.

Solution:

$120

C3 pd

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 5 / 14

Page 14: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

The Fractional Knapsack Problem: Formal Definition

Given K and a set of n items:

weight w1 w2 . . . wn

value v1 v2 . . . vn

Find: 0 ≤ xi ≤ 1, i = 1, 2, . . . , n such that

n∑i=1

xiwi ≤ K

and the following is maximized:

n∑i=1

xivi

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 6 / 14

Page 15: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

Outline

Introduction

The Knapsack problem.

A greedy algorithm for the fractional knapsack problem

Correctness

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 7 / 14

Page 16: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

Greedy Solution for Fractional Knapsack

Sort items by decreasing value-per-pound

$200

$240

$140

$150

5 pd2pd3 pd1 pd

value−per−pound: 200 80 70 30

A

B

D

C

If knapsack holds K = 5 pd, solution is:

1 pd A

3 pd B

1 pd C

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 8 / 14

Page 17: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

Greedy Solution for Fractional Knapsack

Sort items by decreasing value-per-pound

$200

$240

$140

$150

5 pd2pd3 pd1 pd

value−per−pound: 200 80 70 30

A

B

D

C

If knapsack holds K = 5 pd, solution is:

1 pd A

3 pd B

1 pd C

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 8 / 14

Page 18: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

Greedy Solution for Fractional Knapsack

Sort items by decreasing value-per-pound

$200

$240

$140

$150

5 pd2pd3 pd1 pd

value−per−pound: 200 80 70 30

A

B

D

C

If knapsack holds K = 5 pd, solution is:

1 pd A

3 pd B

1 pd C

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 8 / 14

Page 19: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

Greedy Solution for Fractional Knapsack

Calculate the value-per-pound ρi = viwi

for i = 1, 2, . . . , n.

Sort the items by decreasing ρi .Let the sorted item sequence be 1, 2, . . . , i , . . . n, and thecorresponding value-per-pound and weight be ρi and wi

respectively.

Let k be the current weight limit (Initially, k = K ).In each iteration, we choose item i from the head of theunselected list.

If k ≥ wi , set xi = 1 (we take item i), and reduce k = k − wi ,then consider the next unselected item.If k < wi , set xi = k/wi ( we take a fraction k/wi of item i),Then the algorithm terminates.

Running time: O(n log n).

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 9 / 14

Page 20: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

Greedy Solution for Fractional Knapsack

Calculate the value-per-pound ρi = viwi

for i = 1, 2, . . . , n.

Sort the items by decreasing ρi .

Let the sorted item sequence be 1, 2, . . . , i , . . . n, and thecorresponding value-per-pound and weight be ρi and wi

respectively.

Let k be the current weight limit (Initially, k = K ).In each iteration, we choose item i from the head of theunselected list.

If k ≥ wi , set xi = 1 (we take item i), and reduce k = k − wi ,then consider the next unselected item.If k < wi , set xi = k/wi ( we take a fraction k/wi of item i),Then the algorithm terminates.

Running time: O(n log n).

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 9 / 14

Page 21: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

Greedy Solution for Fractional Knapsack

Calculate the value-per-pound ρi = viwi

for i = 1, 2, . . . , n.

Sort the items by decreasing ρi .Let the sorted item sequence be 1, 2, . . . , i , . . . n,

and thecorresponding value-per-pound and weight be ρi and wi

respectively.

Let k be the current weight limit (Initially, k = K ).In each iteration, we choose item i from the head of theunselected list.

If k ≥ wi , set xi = 1 (we take item i), and reduce k = k − wi ,then consider the next unselected item.If k < wi , set xi = k/wi ( we take a fraction k/wi of item i),Then the algorithm terminates.

Running time: O(n log n).

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 9 / 14

Page 22: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

Greedy Solution for Fractional Knapsack

Calculate the value-per-pound ρi = viwi

for i = 1, 2, . . . , n.

Sort the items by decreasing ρi .Let the sorted item sequence be 1, 2, . . . , i , . . . n, and thecorresponding value-per-pound and weight be ρi and wi

respectively.

Let k be the current weight limit (Initially, k = K ).In each iteration, we choose item i from the head of theunselected list.

If k ≥ wi , set xi = 1 (we take item i), and reduce k = k − wi ,then consider the next unselected item.If k < wi , set xi = k/wi ( we take a fraction k/wi of item i),Then the algorithm terminates.

Running time: O(n log n).

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 9 / 14

Page 23: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

Greedy Solution for Fractional Knapsack

Calculate the value-per-pound ρi = viwi

for i = 1, 2, . . . , n.

Sort the items by decreasing ρi .Let the sorted item sequence be 1, 2, . . . , i , . . . n, and thecorresponding value-per-pound and weight be ρi and wi

respectively.

Let k be the current weight limit (Initially, k = K ).

In each iteration, we choose item i from the head of theunselected list.

If k ≥ wi , set xi = 1 (we take item i), and reduce k = k − wi ,then consider the next unselected item.If k < wi , set xi = k/wi ( we take a fraction k/wi of item i),Then the algorithm terminates.

Running time: O(n log n).

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 9 / 14

Page 24: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

Greedy Solution for Fractional Knapsack

Calculate the value-per-pound ρi = viwi

for i = 1, 2, . . . , n.

Sort the items by decreasing ρi .Let the sorted item sequence be 1, 2, . . . , i , . . . n, and thecorresponding value-per-pound and weight be ρi and wi

respectively.

Let k be the current weight limit (Initially, k = K ).In each iteration, we choose item i from the head of theunselected list.

If k ≥ wi , set xi = 1 (we take item i), and reduce k = k − wi ,then consider the next unselected item.If k < wi , set xi = k/wi ( we take a fraction k/wi of item i),Then the algorithm terminates.

Running time: O(n log n).

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 9 / 14

Page 25: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

Greedy Solution for Fractional Knapsack

Calculate the value-per-pound ρi = viwi

for i = 1, 2, . . . , n.

Sort the items by decreasing ρi .Let the sorted item sequence be 1, 2, . . . , i , . . . n, and thecorresponding value-per-pound and weight be ρi and wi

respectively.

Let k be the current weight limit (Initially, k = K ).In each iteration, we choose item i from the head of theunselected list.

If k ≥ wi ,

set xi = 1 (we take item i), and reduce k = k − wi ,then consider the next unselected item.If k < wi , set xi = k/wi ( we take a fraction k/wi of item i),Then the algorithm terminates.

Running time: O(n log n).

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 9 / 14

Page 26: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

Greedy Solution for Fractional Knapsack

Calculate the value-per-pound ρi = viwi

for i = 1, 2, . . . , n.

Sort the items by decreasing ρi .Let the sorted item sequence be 1, 2, . . . , i , . . . n, and thecorresponding value-per-pound and weight be ρi and wi

respectively.

Let k be the current weight limit (Initially, k = K ).In each iteration, we choose item i from the head of theunselected list.

If k ≥ wi , set xi = 1 (we take item i), and reduce k = k − wi ,then consider the next unselected item.

If k < wi , set xi = k/wi ( we take a fraction k/wi of item i),Then the algorithm terminates.

Running time: O(n log n).

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 9 / 14

Page 27: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

Greedy Solution for Fractional Knapsack

Calculate the value-per-pound ρi = viwi

for i = 1, 2, . . . , n.

Sort the items by decreasing ρi .Let the sorted item sequence be 1, 2, . . . , i , . . . n, and thecorresponding value-per-pound and weight be ρi and wi

respectively.

Let k be the current weight limit (Initially, k = K ).In each iteration, we choose item i from the head of theunselected list.

If k ≥ wi , set xi = 1 (we take item i), and reduce k = k − wi ,then consider the next unselected item.If k < wi ,

set xi = k/wi ( we take a fraction k/wi of item i),Then the algorithm terminates.

Running time: O(n log n).

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 9 / 14

Page 28: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

Greedy Solution for Fractional Knapsack

Calculate the value-per-pound ρi = viwi

for i = 1, 2, . . . , n.

Sort the items by decreasing ρi .Let the sorted item sequence be 1, 2, . . . , i , . . . n, and thecorresponding value-per-pound and weight be ρi and wi

respectively.

Let k be the current weight limit (Initially, k = K ).In each iteration, we choose item i from the head of theunselected list.

If k ≥ wi , set xi = 1 (we take item i), and reduce k = k − wi ,then consider the next unselected item.If k < wi , set xi = k/wi ( we take a fraction k/wi of item i),

Then the algorithm terminates.

Running time: O(n log n).

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 9 / 14

Page 29: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

Greedy Solution for Fractional Knapsack

Calculate the value-per-pound ρi = viwi

for i = 1, 2, . . . , n.

Sort the items by decreasing ρi .Let the sorted item sequence be 1, 2, . . . , i , . . . n, and thecorresponding value-per-pound and weight be ρi and wi

respectively.

Let k be the current weight limit (Initially, k = K ).In each iteration, we choose item i from the head of theunselected list.

If k ≥ wi , set xi = 1 (we take item i), and reduce k = k − wi ,then consider the next unselected item.If k < wi , set xi = k/wi ( we take a fraction k/wi of item i),Then the algorithm terminates.

Running time: O(n log n).

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 9 / 14

Page 30: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

Greedy Solution for Fractional Knapsack

Calculate the value-per-pound ρi = viwi

for i = 1, 2, . . . , n.

Sort the items by decreasing ρi .Let the sorted item sequence be 1, 2, . . . , i , . . . n, and thecorresponding value-per-pound and weight be ρi and wi

respectively.

Let k be the current weight limit (Initially, k = K ).In each iteration, we choose item i from the head of theunselected list.

If k ≥ wi , set xi = 1 (we take item i), and reduce k = k − wi ,then consider the next unselected item.If k < wi , set xi = k/wi ( we take a fraction k/wi of item i),Then the algorithm terminates.

Running time: O(n log n).

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 9 / 14

Page 31: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

Greedy Solution for Fractional Knapsack

Observe that the algorithm may take a fraction of an item.

This can only be the last selected item.

We claim that the total value for this set of items is theoptimal value.

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 10 / 14

Page 32: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

Greedy Solution for Fractional Knapsack

Observe that the algorithm may take a fraction of an item.This can only be the last selected item.

We claim that the total value for this set of items is theoptimal value.

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 10 / 14

Page 33: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

Greedy Solution for Fractional Knapsack

Observe that the algorithm may take a fraction of an item.This can only be the last selected item.

We claim that the total value for this set of items is theoptimal value.

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 10 / 14

Page 34: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

Outline

Introduction

The Knapsack problem.

A greedy algorithm for the fractional knapsack problem

Correctness

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 11 / 14

Page 35: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

Correctness

Given a set of n items {1, 2, ..., n}.

Assume items sorted by per-pound values: ρ1 ≥ ρ2 ≥ ... ≥ ρn.

Let the greedy solution be G = 〈x1, x2, ..., xk〉xi indicates fraction of item i taken (all xi = 1, except possibly for i = k).

Consider any optimal solution O = 〈y1, y2, ..., yn〉yi indicates fraction of item i taken in O (for all i, 0 ≤ yi≤1).

Knapsack must be full in both G and O:∑ni=1 xiwi =

∑ni=1 yiwi = K .

Consider the first item i where the two selections differ.

By definition, solution G takes a greater amount of item ithan solution O (because the greedy solution always takes asmuch as it can). Let x = xi − yi .

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 12 / 14

Page 36: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

Correctness

Given a set of n items {1, 2, ..., n}.Assume items sorted by per-pound values: ρ1 ≥ ρ2 ≥ ... ≥ ρn.

Let the greedy solution be G = 〈x1, x2, ..., xk〉xi indicates fraction of item i taken (all xi = 1, except possibly for i = k).

Consider any optimal solution O = 〈y1, y2, ..., yn〉yi indicates fraction of item i taken in O (for all i, 0 ≤ yi≤1).

Knapsack must be full in both G and O:∑ni=1 xiwi =

∑ni=1 yiwi = K .

Consider the first item i where the two selections differ.

By definition, solution G takes a greater amount of item ithan solution O (because the greedy solution always takes asmuch as it can). Let x = xi − yi .

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 12 / 14

Page 37: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

Correctness

Given a set of n items {1, 2, ..., n}.Assume items sorted by per-pound values: ρ1 ≥ ρ2 ≥ ... ≥ ρn.

Let the greedy solution be G = 〈x1, x2, ..., xk〉

xi indicates fraction of item i taken (all xi = 1, except possibly for i = k).

Consider any optimal solution O = 〈y1, y2, ..., yn〉yi indicates fraction of item i taken in O (for all i, 0 ≤ yi≤1).

Knapsack must be full in both G and O:∑ni=1 xiwi =

∑ni=1 yiwi = K .

Consider the first item i where the two selections differ.

By definition, solution G takes a greater amount of item ithan solution O (because the greedy solution always takes asmuch as it can). Let x = xi − yi .

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 12 / 14

Page 38: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

Correctness

Given a set of n items {1, 2, ..., n}.Assume items sorted by per-pound values: ρ1 ≥ ρ2 ≥ ... ≥ ρn.

Let the greedy solution be G = 〈x1, x2, ..., xk〉xi indicates fraction of item i taken

(all xi = 1, except possibly for i = k).

Consider any optimal solution O = 〈y1, y2, ..., yn〉yi indicates fraction of item i taken in O (for all i, 0 ≤ yi≤1).

Knapsack must be full in both G and O:∑ni=1 xiwi =

∑ni=1 yiwi = K .

Consider the first item i where the two selections differ.

By definition, solution G takes a greater amount of item ithan solution O (because the greedy solution always takes asmuch as it can). Let x = xi − yi .

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 12 / 14

Page 39: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

Correctness

Given a set of n items {1, 2, ..., n}.Assume items sorted by per-pound values: ρ1 ≥ ρ2 ≥ ... ≥ ρn.

Let the greedy solution be G = 〈x1, x2, ..., xk〉xi indicates fraction of item i taken (all xi = 1, except possibly for i = k).

Consider any optimal solution O = 〈y1, y2, ..., yn〉yi indicates fraction of item i taken in O (for all i, 0 ≤ yi≤1).

Knapsack must be full in both G and O:∑ni=1 xiwi =

∑ni=1 yiwi = K .

Consider the first item i where the two selections differ.

By definition, solution G takes a greater amount of item ithan solution O (because the greedy solution always takes asmuch as it can). Let x = xi − yi .

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 12 / 14

Page 40: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

Correctness

Given a set of n items {1, 2, ..., n}.Assume items sorted by per-pound values: ρ1 ≥ ρ2 ≥ ... ≥ ρn.

Let the greedy solution be G = 〈x1, x2, ..., xk〉xi indicates fraction of item i taken (all xi = 1, except possibly for i = k).

Consider any optimal solution O = 〈y1, y2, ..., yn〉

yi indicates fraction of item i taken in O (for all i, 0 ≤ yi≤1).

Knapsack must be full in both G and O:∑ni=1 xiwi =

∑ni=1 yiwi = K .

Consider the first item i where the two selections differ.

By definition, solution G takes a greater amount of item ithan solution O (because the greedy solution always takes asmuch as it can). Let x = xi − yi .

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 12 / 14

Page 41: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

Correctness

Given a set of n items {1, 2, ..., n}.Assume items sorted by per-pound values: ρ1 ≥ ρ2 ≥ ... ≥ ρn.

Let the greedy solution be G = 〈x1, x2, ..., xk〉xi indicates fraction of item i taken (all xi = 1, except possibly for i = k).

Consider any optimal solution O = 〈y1, y2, ..., yn〉yi indicates fraction of item i taken in O

(for all i, 0 ≤ yi≤1).

Knapsack must be full in both G and O:∑ni=1 xiwi =

∑ni=1 yiwi = K .

Consider the first item i where the two selections differ.

By definition, solution G takes a greater amount of item ithan solution O (because the greedy solution always takes asmuch as it can). Let x = xi − yi .

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 12 / 14

Page 42: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

Correctness

Given a set of n items {1, 2, ..., n}.Assume items sorted by per-pound values: ρ1 ≥ ρ2 ≥ ... ≥ ρn.

Let the greedy solution be G = 〈x1, x2, ..., xk〉xi indicates fraction of item i taken (all xi = 1, except possibly for i = k).

Consider any optimal solution O = 〈y1, y2, ..., yn〉yi indicates fraction of item i taken in O (for all i, 0 ≤ yi≤1).

Knapsack must be full in both G and O:∑ni=1 xiwi =

∑ni=1 yiwi = K .

Consider the first item i where the two selections differ.

By definition, solution G takes a greater amount of item ithan solution O (because the greedy solution always takes asmuch as it can). Let x = xi − yi .

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 12 / 14

Page 43: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

Correctness

Given a set of n items {1, 2, ..., n}.Assume items sorted by per-pound values: ρ1 ≥ ρ2 ≥ ... ≥ ρn.

Let the greedy solution be G = 〈x1, x2, ..., xk〉xi indicates fraction of item i taken (all xi = 1, except possibly for i = k).

Consider any optimal solution O = 〈y1, y2, ..., yn〉yi indicates fraction of item i taken in O (for all i, 0 ≤ yi≤1).

Knapsack must be full in both G and O:∑ni=1 xiwi =

∑ni=1 yiwi = K .

Consider the first item i where the two selections differ.

By definition, solution G takes a greater amount of item ithan solution O (because the greedy solution always takes asmuch as it can). Let x = xi − yi .

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 12 / 14

Page 44: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

Correctness

Given a set of n items {1, 2, ..., n}.Assume items sorted by per-pound values: ρ1 ≥ ρ2 ≥ ... ≥ ρn.

Let the greedy solution be G = 〈x1, x2, ..., xk〉xi indicates fraction of item i taken (all xi = 1, except possibly for i = k).

Consider any optimal solution O = 〈y1, y2, ..., yn〉yi indicates fraction of item i taken in O (for all i, 0 ≤ yi≤1).

Knapsack must be full in both G and O:∑ni=1 xiwi =

∑ni=1 yiwi = K .

Consider the first item i where the two selections differ.

By definition, solution G takes a greater amount of item ithan solution O (because the greedy solution always takes asmuch as it can). Let x = xi − yi .

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 12 / 14

Page 45: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

Correctness

Given a set of n items {1, 2, ..., n}.Assume items sorted by per-pound values: ρ1 ≥ ρ2 ≥ ... ≥ ρn.

Let the greedy solution be G = 〈x1, x2, ..., xk〉xi indicates fraction of item i taken (all xi = 1, except possibly for i = k).

Consider any optimal solution O = 〈y1, y2, ..., yn〉yi indicates fraction of item i taken in O (for all i, 0 ≤ yi≤1).

Knapsack must be full in both G and O:∑ni=1 xiwi =

∑ni=1 yiwi = K .

Consider the first item i where the two selections differ.

By definition, solution G takes a greater amount of item ithan solution O

(because the greedy solution always takes asmuch as it can). Let x = xi − yi .

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 12 / 14

Page 46: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

Correctness

Given a set of n items {1, 2, ..., n}.Assume items sorted by per-pound values: ρ1 ≥ ρ2 ≥ ... ≥ ρn.

Let the greedy solution be G = 〈x1, x2, ..., xk〉xi indicates fraction of item i taken (all xi = 1, except possibly for i = k).

Consider any optimal solution O = 〈y1, y2, ..., yn〉yi indicates fraction of item i taken in O (for all i, 0 ≤ yi≤1).

Knapsack must be full in both G and O:∑ni=1 xiwi =

∑ni=1 yiwi = K .

Consider the first item i where the two selections differ.

By definition, solution G takes a greater amount of item ithan solution O (because the greedy solution always takes asmuch as it can).

Let x = xi − yi .

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 12 / 14

Page 47: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

Correctness

Given a set of n items {1, 2, ..., n}.Assume items sorted by per-pound values: ρ1 ≥ ρ2 ≥ ... ≥ ρn.

Let the greedy solution be G = 〈x1, x2, ..., xk〉xi indicates fraction of item i taken (all xi = 1, except possibly for i = k).

Consider any optimal solution O = 〈y1, y2, ..., yn〉yi indicates fraction of item i taken in O (for all i, 0 ≤ yi≤1).

Knapsack must be full in both G and O:∑ni=1 xiwi =

∑ni=1 yiwi = K .

Consider the first item i where the two selections differ.

By definition, solution G takes a greater amount of item ithan solution O (because the greedy solution always takes asmuch as it can). Let x = xi − yi .

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 12 / 14

Page 48: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

Correctness...

Consider the following new solution O ′ constructed from O:

For j < i , keep y ′j = yj .

Set y ′i = xi .

In O, remove items of total weight xwi from items i + 1 to n,resetting the y ′j appropriately.

This is always doable because∑n

j=i xj =∑n

j=i yj

The total value of solution O ′ is greater than or equal to thetotal value of solution O (why?)

Since O is largest possible solution and value of O ′ cannot besmaller than that of O, O and O ′ must be equal.

Thus solution O ′ is also optimal.

By repeating this process, we will eventually convert O into G ,without changing the total value of the selection.Therefore G is also optimal!

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 13 / 14

Page 49: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

Correctness...

Consider the following new solution O ′ constructed from O:

For j < i , keep y ′j = yj .

Set y ′i = xi .

In O, remove items of total weight xwi from items i + 1 to n,resetting the y ′j appropriately.

This is always doable because∑n

j=i xj =∑n

j=i yj

The total value of solution O ′ is greater than or equal to thetotal value of solution O (why?)

Since O is largest possible solution and value of O ′ cannot besmaller than that of O, O and O ′ must be equal.

Thus solution O ′ is also optimal.

By repeating this process, we will eventually convert O into G ,without changing the total value of the selection.Therefore G is also optimal!

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 13 / 14

Page 50: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

Correctness...

Consider the following new solution O ′ constructed from O:

For j < i , keep y ′j = yj .

Set y ′i = xi .

In O, remove items of total weight xwi from items i + 1 to n,resetting the y ′j appropriately.

This is always doable because∑n

j=i xj =∑n

j=i yj

The total value of solution O ′ is greater than or equal to thetotal value of solution O (why?)

Since O is largest possible solution and value of O ′ cannot besmaller than that of O, O and O ′ must be equal.

Thus solution O ′ is also optimal.

By repeating this process, we will eventually convert O into G ,without changing the total value of the selection.Therefore G is also optimal!

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 13 / 14

Page 51: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

Correctness...

Consider the following new solution O ′ constructed from O:

For j < i , keep y ′j = yj .

Set y ′i = xi .

In O, remove items of total weight xwi from items i + 1 to n,resetting the y ′j appropriately.

This is always doable because∑n

j=i xj =∑n

j=i yj

The total value of solution O ′ is greater than or equal to thetotal value of solution O (why?)

Since O is largest possible solution and value of O ′ cannot besmaller than that of O, O and O ′ must be equal.

Thus solution O ′ is also optimal.

By repeating this process, we will eventually convert O into G ,without changing the total value of the selection.Therefore G is also optimal!

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 13 / 14

Page 52: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

Correctness...

Consider the following new solution O ′ constructed from O:

For j < i , keep y ′j = yj .

Set y ′i = xi .

In O, remove items of total weight xwi from items i + 1 to n,resetting the y ′j appropriately.

This is always doable because∑n

j=i xj =∑n

j=i yj

The total value of solution O ′ is greater than or equal to thetotal value of solution O (why?)

Since O is largest possible solution and value of O ′ cannot besmaller than that of O, O and O ′ must be equal.

Thus solution O ′ is also optimal.

By repeating this process, we will eventually convert O into G ,without changing the total value of the selection.Therefore G is also optimal!

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 13 / 14

Page 53: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

Correctness...

Consider the following new solution O ′ constructed from O:

For j < i , keep y ′j = yj .

Set y ′i = xi .

In O, remove items of total weight xwi from items i + 1 to n,resetting the y ′j appropriately.

This is always doable because∑n

j=i xj =∑n

j=i yj

The total value of solution O ′ is greater than or equal to thetotal value of solution O (why?)

Since O is largest possible solution and value of O ′ cannot besmaller than that of O, O and O ′ must be equal.

Thus solution O ′ is also optimal.

By repeating this process, we will eventually convert O into G ,without changing the total value of the selection.

Therefore G is also optimal!

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 13 / 14

Page 54: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

Correctness...

Consider the following new solution O ′ constructed from O:

For j < i , keep y ′j = yj .

Set y ′i = xi .

In O, remove items of total weight xwi from items i + 1 to n,resetting the y ′j appropriately.

This is always doable because∑n

j=i xj =∑n

j=i yj

The total value of solution O ′ is greater than or equal to thetotal value of solution O (why?)

Since O is largest possible solution and value of O ′ cannot besmaller than that of O, O and O ′ must be equal.

Thus solution O ′ is also optimal.

By repeating this process, we will eventually convert O into G ,without changing the total value of the selection.Therefore G is also optimal!

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 13 / 14

Page 55: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

Greedy solution for 0-1 Knapsack Problem?

The 0-1 Knapsack Problem does not have a greedy solution!

Example

3 pd

$180$190

$300

CB

A

2 pd

9095100per−pound:value−

2pd

K = 4. Solution is

item B + item C

Question

Suppose we tried to prove the greedy algorithm for 0-1 knapsackproblem does construct an optimal solution. If we follow exactlythe same argument as in the fractional knapsack problem wheredoes the proof fail?

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 14 / 14

Page 56: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

Greedy solution for 0-1 Knapsack Problem?

The 0-1 Knapsack Problem does not have a greedy solution!

Example

3 pd

$180$190

$300

CB

A

2 pd

9095100per−pound:value−

2pd

K = 4. Solution is item B + item C

Question

Suppose we tried to prove the greedy algorithm for 0-1 knapsackproblem does construct an optimal solution. If we follow exactlythe same argument as in the fractional knapsack problem wheredoes the proof fail?

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 14 / 14

Page 57: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

Greedy solution for 0-1 Knapsack Problem?

The 0-1 Knapsack Problem does not have a greedy solution!

Example

3 pd

$180$190

$300

CB

A

2 pd

9095100per−pound:value−

2pd

K = 4. Solution is item B + item C

Question

Suppose we tried to prove the greedy algorithm for 0-1 knapsackproblem does construct an optimal solution.

If we follow exactlythe same argument as in the fractional knapsack problem wheredoes the proof fail?

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 14 / 14

Page 58: Greedy Algorithms: The Fractional Knapsack · Greedy Algorithms: The Fractional Knapsack Version of November 5, 2014 Version of November 5, 2014 Greedy Algorithms: The Fractional

Greedy solution for 0-1 Knapsack Problem?

The 0-1 Knapsack Problem does not have a greedy solution!

Example

3 pd

$180$190

$300

CB

A

2 pd

9095100per−pound:value−

2pd

K = 4. Solution is item B + item C

Question

Suppose we tried to prove the greedy algorithm for 0-1 knapsackproblem does construct an optimal solution. If we follow exactlythe same argument as in the fractional knapsack problem wheredoes the proof fail?

Version of November 5, 2014 Greedy Algorithms: The Fractional Knapsack 14 / 14


Recommended