SlideShare a Scribd company logo
Advanced Algorithms – COMS31900
Approximation algorithms part one
Constant factor approximations
Benjamin Sach
NP-completeness recap
A problem A is NP-complete if
NP is the class of decision problems we can
check the answer to in polynomial time
A is in NP
Every B in NP has a polynomial time reduction to A
(this second part is the definition of NP-hard)
NP-completeness recap
A problem A is NP-complete if
NP is the class of decision problems we can
check the answer to in polynomial time
A is in NP
Every B in NP has a polynomial time reduction to A
(this second part is the definition of NP-hard)
‘yes/no’ problems
NP-completeness recap
A problem A is NP-complete if
NP is the class of decision problems we can
check the answer to in polynomial time
A is in NP
Every B in NP has a polynomial time reduction to A
(this second part is the definition of NP-hard)
NP-completeness recap
A problem A is NP-complete if
NP is the class of decision problems we can
check the answer to in polynomial time
A is in NP
Every B in NP has a polynomial time reduction to A
(this second part is the definition of NP-hard)
we can solve B
using A
NP-completeness recap
A problem A is NP-complete if
NP is the class of decision problems we can
check the answer to in polynomial time
A is in NP
Every B in NP has a polynomial time reduction to A
(this second part is the definition of NP-hard)
NP-completeness recap
A problem A is NP-complete if
NP is the class of decision problems we can
check the answer to in polynomial time
A is in NP
Every B in NP has a polynomial time reduction to A
If we could solve A quickly we could solve every problem in NP quickly
(this second part is the definition of NP-hard)
NP-completeness recap
A problem A is NP-complete if
NP is the class of decision problems we can
check the answer to in polynomial time
A is in NP
Every B in NP has a polynomial time reduction to A
If we could solve A quickly we could solve every problem in NP quickly
They are the ‘hardest’ problems in NP
(this second part is the definition of NP-hard)
NP-completeness recap
A problem A is NP-complete if
NP is the class of decision problems we can
check the answer to in polynomial time
A is in NP
Every B in NP has a polynomial time reduction to A
If we could solve A quickly we could solve every problem in NP quickly
They are the ‘hardest’ problems in NP
(this second part is the definition of NP-hard)
that you can’t solve them in polynomial time (i.e. that P = NP)
Most computer scientists (I’ve met) believe
NP-completeness recap
A problem A is NP-complete if
NP is the class of decision problems we can
check the answer to in polynomial time
A is in NP
Every B in NP has a polynomial time reduction to A
If we could solve A quickly we could solve every problem in NP quickly
They are the ‘hardest’ problems in NP
(this second part is the definition of NP-hard)
A polynomial time algorithm for an
NP-complete problem is worth
(a lot more than) a million dollars
NP-completeness recap
A problem A is NP-complete if
NP is the class of decision problems we can
check the answer to in polynomial time
A is in NP
Every B in NP has a polynomial time reduction to A
If we could solve A quickly we could solve every problem in NP quickly
They are the ‘hardest’ problems in NP
(this second part is the definition of NP-hard)
NP-completeness recap
A problem A is NP-complete if
NP is the class of decision problems we can
check the answer to in polynomial time
A is in NP
Every B in NP has a polynomial time reduction to A
If we could solve A quickly we could solve every problem in NP quickly
They are the ‘hardest’ problems in NP
So if a problem is NP-complete, we give up right?
(this second part is the definition of NP-hard)
Bin packing
1
1
4/8 4/8
7/8
3/82/82/8
Bin packing
Bins
1
1
4/8 4/8
7/8
3/82/82/8
Bin packing
Bins
1
Items
1
4/8 4/8
7/8
3/82/82/8
Bin packing
Bins
1
Items
1
4/8 4/8
7/8
3/82/82/8
0 < |Item| 1
Bin packing
Bins
1
Items
1
4/8 4/8
7/8
3/82/82/8
0 < |Item| 1
I is the sum of all item sizes
Bin packing
Bins
1
Items
1
4/8 4/8
7/8
3/82/82/8
0 < |Item| 1
|Bin| = 1 and there is an unlimited number of bins. . .
I is the sum of all item sizes
Bin packing
1
1
4/8 4/8
7/8
3/82/82/8
Problem pack all items into the fewest possible bins
Bin packing
1
1
4/8 4/8
7/8
3/82/82/8
Problem pack all items into the fewest possible bins
This is an example of an optimisation problem
Bin packing
1
1
Problem pack all items into the fewest possible bins
4/8 4/8
7/8
2/82/8
3/8
This is an example of an optimisation problem
Bin packing
1
1
4/8 4/8
7/8
3/82/82/8
Problem pack all items into the fewest possible bins
This is an example of an optimisation problem
Bin packing
1
1
Problem pack all items into the fewest possible bins
4/8
4/8
7/8
2/8
2/8
3/8
This is an example of an optimisation problem
Bin packing
1
1
Problem pack all items into the fewest possible bins
4/8
4/8
7/8
2/8
2/8
3/8
Bin packing
1
1
Problem pack all items into the fewest possible bins
4/8
4/8
7/8
2/8
2/8
3/8
The BINPACKING problem is known to be NP-hard
Bin packing
1
1
Problem pack all items into the fewest possible bins
4/8
4/8
7/8
2/8
2/8
3/8
The BINPACKING problem is known to be NP-hard
and the decision version. . . “Can you pack the items into at most k bins?”
is NP-complete
Bin packing
1
1
Problem pack all items into the fewest possible bins
4/8
4/8
7/8
2/8
2/8
3/8
The BINPACKING problem is known to be NP-hard
and the decision version. . . “Can you pack the items into at most k bins?”
is NP-complete
In the decision version,
k is part of the input
Bin packing
1
1
Problem pack all items into the fewest possible bins
4/8
4/8
7/8
2/8
2/8
3/8
The BINPACKING problem is known to be NP-hard
Bin packing
1
1
Problem pack all items into the fewest possible bins
4/8
4/8
7/8
2/8
2/8
3/8
The BINPACKING problem is known to be NP-hard
but fortunately we can approximate
Next fit
1
1
4/8 4/8
7/8
3/82/82/8
If item i fits into bin j: pack it, i++; else j++;
Next fit
1
1
4/8 4/8
7/8
3/82/82/8
If item i fits into bin j: pack it, i++; else j++;
Next fit
1
1
4/8
7/8
3/82/82/8
If item i fits into bin j: pack it, i++; else j++;
4/8
Next fit
1
1
4/8
7/8
3/82/82/8
If item i fits into bin j: pack it, i++; else j++;
4/8
Next fit
1
1
4/8
7/8
3/82/8
If item i fits into bin j: pack it, i++; else j++;
4/8
2/8
Next fit
1
1
4/8
7/8
3/82/8
If item i fits into bin j: pack it, i++; else j++;
4/8
2/8
Next fit
1
1
4/8
7/8
3/82/8
If item i fits into bin j: pack it, i++; else j++;
4/8
2/8
Next fit
1
1
7/8
3/82/8
If item i fits into bin j: pack it, i++; else j++;
4/8
2/8
4/8
Next fit
1
1
7/8
3/82/8
If item i fits into bin j: pack it, i++; else j++;
4/8
2/8
4/8
Next fit
1
1
7/8
3/82/8
If item i fits into bin j: pack it, i++; else j++;
4/8
2/8
4/8
Next fit
1
1
3/82/8
If item i fits into bin j: pack it, i++; else j++;
4/8
2/8
4/8
7/8
Next fit
1
1
3/82/8
If item i fits into bin j: pack it, i++; else j++;
4/8
2/8
4/8
7/8
Next fit
1
1
3/82/8
If item i fits into bin j: pack it, i++; else j++;
4/8
2/8
4/8
7/8
Next fit
1
1
3/8
If item i fits into bin j: pack it, i++; else j++;
4/8
2/8
4/8
7/8
2/8
Next fit
1
1
3/8
If item i fits into bin j: pack it, i++; else j++;
4/8
2/8
4/8
7/8
2/8
Next fit
1
1
If item i fits into bin j: pack it, i++; else j++;
4/8
2/8
4/8
7/8
2/8
3/8
Next fit
1
1
4/8
2/8
4/8
7/8
2/8
3/8
Next fit
1
1
4/8
2/8
4/8
7/8
2/8
3/8
Next fit runs in O(n) time but how good is it?
Next fit
1
1
4/8
2/8
4/8
7/8
2/8
3/8
Next fit runs in O(n) time but how good is it?
where n is the number of items
Next fit
1
4/8
2/8
4/8
7/8
2/8
3/8
Next fit runs in O(n) time but how good is it?
Next fit
1
4/8
2/8
4/8
7/8
2/8
3/8
Next fit runs in O(n) time but how good is it?
Let fill(i) be the sum of item sizes in bin i
and s be the number of non-empty bins (using Next fit)
Next fit
1
4/8
2/8
4/8
7/8
2/8
3/8
Next fit runs in O(n) time but how good is it?
Let fill(i) be the sum of item sizes in bin i
Observe that fill(2i − 1) + fill(2i) > 1 (for 1 2i s)
and s be the number of non-empty bins (using Next fit)
Next fit
1
4/8
2/8
4/8
7/8
2/8
3/8
Next fit runs in O(n) time but how good is it?
Let fill(i) be the sum of item sizes in bin i
Observe that fill(2i − 1) + fill(2i) > 1 (for 1 2i s)
and s be the number of non-empty bins (using Next fit)
so s/2 <
1 2i s
fill(2i − 1) + fill(2i)
Next fit
1
4/8
2/8
4/8
7/8
2/8
3/8
Next fit runs in O(n) time but how good is it?
Let fill(i) be the sum of item sizes in bin i
Observe that fill(2i − 1) + fill(2i) > 1 (for 1 2i s)
and s be the number of non-empty bins (using Next fit)
so s/2 <
1 2i s
fill(2i − 1) + fill(2i) I
Next fit
1
4/8
2/8
4/8
7/8
2/8
3/8
Next fit runs in O(n) time but how good is it?
Let fill(i) be the sum of item sizes in bin i
Observe that fill(2i − 1) + fill(2i) > 1 (for 1 2i s)
and s be the number of non-empty bins (using Next fit)
so s/2 <
1 2i s
fill(2i − 1) + fill(2i) I
the sum of the
item weights
Next fit
1
4/8
2/8
4/8
7/8
2/8
3/8
Next fit runs in O(n) time but how good is it?
Let fill(i) be the sum of item sizes in bin i
Observe that fill(2i − 1) + fill(2i) > 1 (for 1 2i s)
and s be the number of non-empty bins (using Next fit)
so s/2 <
1 2i s
fill(2i − 1) + fill(2i) I Opt
the sum of the
item weights
Next fit
1
4/8
2/8
4/8
7/8
2/8
3/8
Next fit runs in O(n) time but how good is it?
Let fill(i) be the sum of item sizes in bin i
Observe that fill(2i − 1) + fill(2i) > 1 (for 1 2i s)
and s be the number of non-empty bins (using Next fit)
so s/2 <
1 2i s
fill(2i − 1) + fill(2i) I Opt
the sum of the
item weights
the optimal number of bins
Next fit
1
4/8
2/8
4/8
7/8
2/8
3/8
Next fit runs in O(n) time but how good is it?
Let fill(i) be the sum of item sizes in bin i
Observe that fill(2i − 1) + fill(2i) > 1 (for 1 2i s)
and s be the number of non-empty bins (using Next fit)
so s/2 <
1 2i s
fill(2i − 1) + fill(2i) I Opt
Next fit
1
4/8
2/8
4/8
7/8
2/8
3/8
Next fit runs in O(n) time but how good is it?
Let fill(i) be the sum of item sizes in bin i
Observe that fill(2i − 1) + fill(2i) > 1 (for 1 2i s)
and s be the number of non-empty bins (using Next fit)
so s/2 <
1 2i s
fill(2i − 1) + fill(2i) I Opt
therefore s 2 · Opt
Next fit
1
4/8
2/8
4/8
7/8
2/8
3/8
Next fit runs in O(n) time but how good is it?
Let fill(i) be the sum of item sizes in bin i
Observe that fill(2i − 1) + fill(2i) > 1 (for 1 2i s)
and s be the number of non-empty bins (using Next fit)
so s/2 <
1 2i s
fill(2i − 1) + fill(2i) I Opt
therefore s 2 · Opt in other words the Next Fit is never worse than twice the optimal
Next fit
1
4/8
2/8
4/8
7/8
2/8
3/8
Next fit runs in O(n) time but how good is it?
Let fill(i) be the sum of item sizes in bin i
Observe that fill(2i − 1) + fill(2i) > 1 (for 1 2i s)
and s be the number of non-empty bins (using Next fit)
so s/2 <
1 2i s
fill(2i − 1) + fill(2i) I Opt
therefore s 2 · Opt
Approximation Algorithms
An algorithm A is an α-approximation algorithm for problem P if,
◦ A runs in polynomial time
◦ A always outputs a solution with value s
within an α factor of Opt
Approximation Algorithms
An algorithm A is an α-approximation algorithm for problem P if,
◦ A runs in polynomial time
◦ A always outputs a solution with value s
Here P is an optimisation problem with optimal solution of value Opt
within an α factor of Opt
Approximation Algorithms
An algorithm A is an α-approximation algorithm for problem P if,
◦ A runs in polynomial time
◦ A always outputs a solution with value s
Here P is an optimisation problem with optimal solution of value Opt
• If P is a maximisation problem,
Opt
α s Opt
within an α factor of Opt
Approximation Algorithms
An algorithm A is an α-approximation algorithm for problem P if,
◦ A runs in polynomial time
◦ A always outputs a solution with value s
Here P is an optimisation problem with optimal solution of value Opt
• If P is a maximisation problem,
Opt
α s Opt
within an α factor of Opt
• If P is a minimisation problem (like BINPACKING), Opt s α · Opt
Approximation Algorithms
An algorithm A is an α-approximation algorithm for problem P if,
◦ A runs in polynomial time
◦ A always outputs a solution with value s
Here P is an optimisation problem with optimal solution of value Opt
• If P is a maximisation problem,
Opt
α s Opt
within an α factor of Opt
• If P is a minimisation problem (like BINPACKING), Opt s α · Opt
We have seen a 2-approximation algorithm for BINPACKING
Approximation Algorithms
An algorithm A is an α-approximation algorithm for problem P if,
◦ A runs in polynomial time
◦ A always outputs a solution with value s
Here P is an optimisation problem with optimal solution of value Opt
• If P is a maximisation problem,
Opt
α s Opt
within an α factor of Opt
• If P is a minimisation problem (like BINPACKING), Opt s α · Opt
We have seen a 2-approximation algorithm for BINPACKING
the number of bins used, s is always between Opt and 2 · Opt
Approximation Algorithms
An algorithm A is an α-approximation algorithm for problem P if,
◦ A runs in polynomial time
◦ A always outputs a solution with value s
Here P is an optimisation problem with optimal solution of value Opt
• If P is a maximisation problem,
Opt
α s Opt
within an α factor of Opt
• If P is a minimisation problem (like BINPACKING), Opt s α · Opt
We have seen a 2-approximation algorithm for BINPACKING
the number of bins used, s is always between Opt and 2 · Opt
In the examples we consider, α will be a constant but it could depend on n (the input size)
1
4/8
2/8
4/8
7/8
2/8
3/8
We have seen that Next fit is a 2-approximation algorithm for Bin packing
which runs in O(n) time
can we do better?
First fit decreasing (FFD)
1
1
4/8 4/8
7/8
3/82/82/8
First fit decreasing (FFD)
1
1
Step 1: Sort the items into non-increasing order
4/8 4/8
7/8
3/82/82/8
First fit decreasing (FFD)
1
1
4/8 4/8
7/8
3/8 2/8 2/8
Step 1: Sort the items into non-increasing order
First fit decreasing (FFD)
1
1
4/8 4/8
7/8
3/8 2/8 2/8
Step 2: Put each item in the first (left-most) bin it fits in
First fit decreasing (FFD)
1
1
4/8 4/8 3/8 2/8 2/8
Step 2: Put each item in the first (left-most) bin it fits in
7/8
First fit decreasing (FFD)
1
1
4/8 3/8 2/8 2/8
Step 2: Put each item in the first (left-most) bin it fits in
7/8
4/8
First fit decreasing (FFD)
1
1
3/8 2/8 2/8
Step 2: Put each item in the first (left-most) bin it fits in
7/8
4/8
4/8
First fit decreasing (FFD)
1
1
2/8 2/8
Step 2: Put each item in the first (left-most) bin it fits in
7/8
4/8
4/8
3/8
First fit decreasing (FFD)
1
1
2/8
Step 2: Put each item in the first (left-most) bin it fits in
7/8
4/8
4/8
3/8
2/8
First fit decreasing (FFD)
1
1
Step 2: Put each item in the first (left-most) bin it fits in
7/8
4/8
4/8
3/8
2/8
2/8
First fit decreasing (FFD)
1
1
Step 2: Put each item in the first (left-most) bin it fits in
7/8
4/8
4/8
3/8
2/8
2/8
this will be important
for the proof
First fit decreasing (FFD)
1
1
Step 2: Put each item in the first (left-most) bin it fits in
7/8
4/8
4/8
3/8
2/8
2/8
First fit decreasing (FFD)
1
1
7/8
4/8
4/8
3/8
2/8
2/8
First fit decreasing (FFD)
1
1
7/8
4/8
4/8
3/8
2/8
2/8
FFD runs in O(n2) time but how good is it?
First fit decreasing (FFD)
1
7/8
4/8
4/8
3/8
2/8
2/8
FFD runs in O(n2) time but how good is it?
First fit decreasing (FFD)
Consider bin j = 2s
3 (s is the number of bins FFD uses on this input)
ss
First fit decreasing (FFD)
Consider bin j = 2s
3 (s is the number of bins FFD uses on this input)
j
ss
First fit decreasing (FFD)
Consider bin j = 2s
3 (s is the number of bins FFD uses on this input)
j
ss
First fit decreasing (FFD)
Consider bin j = 2s
3 (s is the number of bins FFD uses on this input)
Case 1: Bin j contains an item of size > 1/2
j
ss
First fit decreasing (FFD)
Consider bin j = 2s
3 (s is the number of bins FFD uses on this input)
Case 1: Bin j contains an item of size > 1/2
j
ss
First fit decreasing (FFD)
Consider bin j = 2s
3 (s is the number of bins FFD uses on this input)
Case 1: Bin j contains an item of size > 1/2
Every bin j j contains an item of size > 1/2
j
ss
First fit decreasing (FFD)
Consider bin j = 2s
3 (s is the number of bins FFD uses on this input)
Case 1: Bin j contains an item of size > 1/2
Every bin j j contains an item of size > 1/2
because we packed big things first and each thing was
packed in the lowest numbered bin
j
ss
First fit decreasing (FFD)
Consider bin j = 2s
3 (s is the number of bins FFD uses on this input)
Case 1: Bin j contains an item of size > 1/2
Every bin j j contains an item of size > 1/2
because we packed big things first and each thing was
packed in the lowest numbered bin
j
ss
First fit decreasing (FFD)
Consider bin j = 2s
3 (s is the number of bins FFD uses on this input)
Case 1: Bin j contains an item of size > 1/2
Every bin j j contains an item of size > 1/2
j
ss
First fit decreasing (FFD)
Consider bin j = 2s
3 (s is the number of bins FFD uses on this input)
Case 1: Bin j contains an item of size > 1/2
Every bin j j contains an item of size > 1/2
each of these items has to be in a different bin (even in Opt)
j
ss
First fit decreasing (FFD)
Consider bin j = 2s
3 (s is the number of bins FFD uses on this input)
Case 1: Bin j contains an item of size > 1/2
Every bin j j contains an item of size > 1/2
each of these items has to be in a different bin (even in Opt)
So Opt uses at least 2s
3 bins
j
ss
First fit decreasing (FFD)
Consider bin j = 2s
3 (s is the number of bins FFD uses on this input)
Case 1: Bin j contains an item of size > 1/2
Every bin j j contains an item of size > 1/2
each of these items has to be in a different bin (even in Opt)
So Opt uses at least 2s
3 bins
or. . . s
3Opt
2
j
ss
First fit decreasing (FFD)
Consider bin j = 2s
3 (s is the number of bins FFD uses on this input)
Case 2: Bin j contains only items of size 1/2
j
ss
First fit decreasing (FFD)
Consider bin j = 2s
3 (s is the number of bins FFD uses on this input)
Case 2: Bin j contains only items of size 1/2
when FFD packed the first item into bin j,
j
ss
First fit decreasing (FFD)
Consider bin j = 2s
3 (s is the number of bins FFD uses on this input)
Case 2: Bin j contains only items of size 1/2
when FFD packed the first item into bin j,
j
ss
First fit decreasing (FFD)
Consider bin j = 2s
3 (s is the number of bins FFD uses on this input)
Case 2: Bin j contains only items of size 1/2
when FFD packed the first item into bin j,
j
ss
First fit decreasing (FFD)
Consider bin j = 2s
3 (s is the number of bins FFD uses on this input)
Case 2: Bin j contains only items of size 1/2
when FFD packed the first item into bin j,
j
1. all bins j, (j + 1), . . . , (s − 2), (s − 1) were empty
ss
First fit decreasing (FFD)
Consider bin j = 2s
3 (s is the number of bins FFD uses on this input)
Case 2: Bin j contains only items of size 1/2
when FFD packed the first item into bin j,
j
1. all bins j, (j + 1), . . . , (s − 2), (s − 1) were empty
ss
2. and all unpacked items had size 1/2
(because we pack in non-increasing order)
First fit decreasing (FFD)
Consider bin j = 2s
3 (s is the number of bins FFD uses on this input)
Case 2: Bin j contains only items of size 1/2
so Bins j, (j + 1), . . . , (s − 2), (s − 1) each contain at least two items
when FFD packed the first item into bin j,
j
1. all bins j, (j + 1), . . . , (s − 2), (s − 1) were empty
ss
2. and all unpacked items had size 1/2
(because we pack in non-increasing order)
First fit decreasing (FFD)
Consider bin j = 2s
3 (s is the number of bins FFD uses on this input)
Case 2: Bin j contains only items of size 1/2
so Bins j, (j + 1), . . . , (s − 2), (s − 1) each contain at least two items
when FFD packed the first item into bin j,
j
1. all bins j, (j + 1), . . . , (s − 2), (s − 1) were empty
ss
2. and all unpacked items had size 1/2
(because we pack in non-increasing order)
2+ 2+ 2+ 2+
First fit decreasing (FFD)
Consider bin j = 2s
3 (s is the number of bins FFD uses on this input)
Case 2: Bin j contains only items of size 1/2
so Bins j, (j + 1), . . . , (s − 2), (s − 1) each contain at least two items
when FFD packed the first item into bin j,
j
1. all bins j, (j + 1), . . . , (s − 2), (s − 1) were empty
ss
2. and all unpacked items had size 1/2
(because we pack in non-increasing order)
2+ 2+ 2+ 2+
(we only use a new bin when the item won’t fit in any previous bin)
First fit decreasing (FFD)
Consider bin j = 2s
3 (s is the number of bins FFD uses on this input)
Case 2: Bin j contains only items of size 1/2
so Bins j, (j + 1), . . . , (s − 2), (s − 1) each contain at least two items
when FFD packed the first item into bin j,
j
1. all bins j, (j + 1), . . . , (s − 2), (s − 1) were empty
ss
2. and all unpacked items had size 1/2
(because we pack in non-increasing order)
2+ 2+ 2+ 2+
First fit decreasing (FFD)
Consider bin j = 2s
3 (s is the number of bins FFD uses on this input)
Case 2: Bin j contains only items of size 1/2
so Bins j, (j + 1), . . . , (s − 2), (s − 1) each contain at least two items
and bin s contains at least one item
when FFD packed the first item into bin j,
j
1. all bins j, (j + 1), . . . , (s − 2), (s − 1) were empty
ss
2. and all unpacked items had size 1/2
(because we pack in non-increasing order)
2+ 2+ 2+ 2+
First fit decreasing (FFD)
Consider bin j = 2s
3 (s is the number of bins FFD uses on this input)
Case 2: Bin j contains only items of size 1/2
so Bins j, (j + 1), . . . , (s − 2), (s − 1) each contain at least two items
and bin s contains at least one item
when FFD packed the first item into bin j,
j
1. all bins j, (j + 1), . . . , (s − 2), (s − 1) were empty
ss
2. and all unpacked items had size 1/2
(because we pack in non-increasing order)
2+ 2+ 2+ 2+ 1+
First fit decreasing (FFD)
Consider bin j = 2s
3 (s is the number of bins FFD uses on this input)
Case 2: Bin j contains only items of size 1/2
so Bins j, (j + 1), . . . , (s − 2), (s − 1) each contain at least two items
and bin s contains at least one item
j
ss
2+ 2+ 2+ 2+ 1+
First fit decreasing (FFD)
Consider bin j = 2s
3 (s is the number of bins FFD uses on this input)
Case 2: Bin j contains only items of size 1/2
j
ss
2+ 2+ 2+ 2+ 1+
First fit decreasing (FFD)
Consider bin j = 2s
3 (s is the number of bins FFD uses on this input)
Case 2: Bin j contains only items of size 1/2
j
ss
2+ 2+ 2+ 2+ 1+
so Bins j, (j + 1), . . . , (s − 2), (s − 1) each contain at least two items
and bin s contains at least one item
First fit decreasing (FFD)
Consider bin j = 2s
3 (s is the number of bins FFD uses on this input)
Case 2: Bin j contains only items of size 1/2
This gives a total of 2(s − j) + 1 items, none of which fits into bins 1, 2, 3, . . . , (j − 1)
j
ss
2+ 2+ 2+ 2+ 1+
so Bins j, (j + 1), . . . , (s − 2), (s − 1) each contain at least two items
and bin s contains at least one item
First fit decreasing (FFD)
Consider bin j = 2s
3 (s is the number of bins FFD uses on this input)
Case 2: Bin j contains only items of size 1/2
This gives a total of 2(s − j) + 1 items, none of which fits into bins 1, 2, 3, . . . , (j − 1)
otherwise we would have packed them there
j
ss
2+ 2+ 2+ 2+ 1+
so Bins j, (j + 1), . . . , (s − 2), (s − 1) each contain at least two items
and bin s contains at least one item
First fit decreasing (FFD)
Consider bin j = 2s
3 (s is the number of bins FFD uses on this input)
Case 2: Bin j contains only items of size 1/2
This gives a total of 2(s − j) + 1 items, none of which fits into bins 1, 2, 3, . . . , (j − 1)
j
ss
2+ 2+ 2+ 2+ 1+
so Bins j, (j + 1), . . . , (s − 2), (s − 1) each contain at least two items
and bin s contains at least one item
First fit decreasing (FFD)
Consider bin j = 2s
3 (s is the number of bins FFD uses on this input)
Case 2: Bin j contains only items of size 1/2
This gives a total of 2(s − j) + 1 items, none of which fits into bins 1, 2, 3, . . . , (j − 1)
so I > min{j − 1, 2(s − j) + 1}
j
ss
2+ 2+ 2+ 2+ 1+
so Bins j, (j + 1), . . . , (s − 2), (s − 1) each contain at least two items
and bin s contains at least one item
First fit decreasing (FFD)
Consider bin j = 2s
3 (s is the number of bins FFD uses on this input)
Case 2: Bin j contains only items of size 1/2
This gives a total of 2(s − j) + 1 items, none of which fits into bins 1, 2, 3, . . . , (j − 1)
so I > min{j − 1, 2(s − j) + 1}
recall I is the total weight of all items
j
ss
2+ 2+ 2+ 2+ 1+
so Bins j, (j + 1), . . . , (s − 2), (s − 1) each contain at least two items
and bin s contains at least one item
First fit decreasing (FFD)
Consider bin j = 2s
3 (s is the number of bins FFD uses on this input)
Case 2: Bin j contains only items of size 1/2
This gives a total of 2(s − j) + 1 items, none of which fits into bins 1, 2, 3, . . . , (j − 1)
so I > min{j − 1, 2(s − j) + 1}
recall I is the total weight of all items
pairing these with these
considerconsider
j
ss
so Bins j, (j + 1), . . . , (s − 2), (s − 1) each contain at least two items
and bin s contains at least one item
First fit decreasing (FFD)
Consider bin j = 2s
3 (s is the number of bins FFD uses on this input)
Case 2: Bin j contains only items of size 1/2
This gives a total of 2(s − j) + 1 items, none of which fits into bins 1, 2, 3, . . . , (j − 1)
so I > min{j − 1, 2(s − j) + 1}
recall I is the total weight of all items
pairing these with these
considerconsider
j
ss
so Bins j, (j + 1), . . . , (s − 2), (s − 1) each contain at least two items
and bin s contains at least one item
this + this
> 1
First fit decreasing (FFD)
Consider bin j = 2s
3 (s is the number of bins FFD uses on this input)
Case 2: Bin j contains only items of size 1/2
This gives a total of 2(s − j) + 1 items, none of which fits into bins 1, 2, 3, . . . , (j − 1)
so I > min{j − 1, 2(s − j) + 1}
recall I is the total weight of all items
j
ss
so Bins j, (j + 1), . . . , (s − 2), (s − 1) each contain at least two items
and bin s contains at least one item
First fit decreasing (FFD)
Consider bin j = 2s
3 (s is the number of bins FFD uses on this input)
Case 2: Bin j contains only items of size 1/2
This gives a total of 2(s − j) + 1 items, none of which fits into bins 1, 2, 3, . . . , (j − 1)
so I > min{j − 1, 2(s − j) + 1}
j
ss
so Bins j, (j + 1), . . . , (s − 2), (s − 1) each contain at least two items
and bin s contains at least one item
First fit decreasing (FFD)
Consider bin j = 2s
3 (s is the number of bins FFD uses on this input)
Case 2: Bin j contains only items of size 1/2
This gives a total of 2(s − j) + 1 items, none of which fits into bins 1, 2, 3, . . . , (j − 1)
so I > min{j − 1, 2(s − j) + 1} 2s/3 − 1
j
ss
so Bins j, (j + 1), . . . , (s − 2), (s − 1) each contain at least two items
and bin s contains at least one item
First fit decreasing (FFD)
Consider bin j = 2s
3 (s is the number of bins FFD uses on this input)
Case 2: Bin j contains only items of size 1/2
This gives a total of 2(s − j) + 1 items, none of which fits into bins 1, 2, 3, . . . , (j − 1)
so I > min{j − 1, 2(s − j) + 1} 2s/3 − 1
by plugging in j = 2s/3
j
ss
so Bins j, (j + 1), . . . , (s − 2), (s − 1) each contain at least two items
and bin s contains at least one item
First fit decreasing (FFD)
Consider bin j = 2s
3 (s is the number of bins FFD uses on this input)
Case 2: Bin j contains only items of size 1/2
As 2s/3 − 1 < I
j
ss
First fit decreasing (FFD)
Consider bin j = 2s
3 (s is the number of bins FFD uses on this input)
Case 2: Bin j contains only items of size 1/2
As 2s/3 − 1 < I and I Opt
j
ss
First fit decreasing (FFD)
Consider bin j = 2s
3 (s is the number of bins FFD uses on this input)
Case 2: Bin j contains only items of size 1/2
As 2s/3 − 1 < I
we have that 2s/3 − 1 < Opt
and I Opt
j
ss
First fit decreasing (FFD)
Consider bin j = 2s
3 (s is the number of bins FFD uses on this input)
Case 2: Bin j contains only items of size 1/2
As 2s/3 − 1 < I
we have that 2s/3 − 1 < Opt
and I Opt
. . . but both sides are integers. . .
so 2s/3 Opt
finally . . . 2s/3 2s/3 Opt
or s (3/2)Opt
j
ss
First fit decreasing (FFD)
Consider bin j = 2s
3 (s is the number of bins FFD uses on this input)
j
ss
First fit decreasing (FFD)
Consider bin j = 2s
3 (s is the number of bins FFD uses on this input)
Case 2: Bin j contains only items of size 1/2
Case 1: Bin j contains an item of size > 1/2
in both cases. . . s
3Opt
2
j
ss
First fit decreasing (FFD)
Consider bin j = 2s
3 (s is the number of bins FFD uses on this input)
So FFD is a 3/2-approximation algorithm for BINPACKING
Case 2: Bin j contains only items of size 1/2
Case 1: Bin j contains an item of size > 1/2
in both cases. . . s
3Opt
2
j
ss
Approximation Algorithms Summary
An algorithm A is an α-approximation algorithm for problem P if,
◦ A runs in polynomial time
◦ A always outputs a solution with value s
Here P is an optimisation problem with optimal solution of value Opt
If P is a maximisation problem,
Opt
α s Opt
within an α factor of Opt
If P is a minimisation problem (like BINPACKING), Opt s α · Opt
We have seen Next Fit which is a 2-approximation algorithm for BINPACKING
which runs in O(n) time
and First Fit Decreasing which is a 3/2-approximation algorithm for BINPACKING
which runs in O(n2) time
Bin Packing is NP-hard so solving it exactly in polynomial time would prove that P = NP

More Related Content

More from Benjamin Sach

Orthogonal Range Searching
Orthogonal Range SearchingOrthogonal Range Searching
Orthogonal Range Searching
Benjamin Sach
 
Pattern Matching Part Two: k-mismatches
Pattern Matching Part Two: k-mismatchesPattern Matching Part Two: k-mismatches
Pattern Matching Part Two: k-mismatches
Benjamin Sach
 
Pattern Matching Part Three: Hamming Distance
Pattern Matching Part Three: Hamming DistancePattern Matching Part Three: Hamming Distance
Pattern Matching Part Three: Hamming Distance
Benjamin Sach
 
Lowest Common Ancestor
Lowest Common AncestorLowest Common Ancestor
Lowest Common Ancestor
Benjamin Sach
 
Range Minimum Queries
Range Minimum QueriesRange Minimum Queries
Range Minimum Queries
Benjamin Sach
 
Pattern Matching Part Two: Suffix Arrays
Pattern Matching Part Two: Suffix ArraysPattern Matching Part Two: Suffix Arrays
Pattern Matching Part Two: Suffix Arrays
Benjamin Sach
 
Pattern Matching Part One: Suffix Trees
Pattern Matching Part One: Suffix TreesPattern Matching Part One: Suffix Trees
Pattern Matching Part One: Suffix Trees
Benjamin Sach
 
Hashing Part Two: Cuckoo Hashing
Hashing Part Two: Cuckoo HashingHashing Part Two: Cuckoo Hashing
Hashing Part Two: Cuckoo Hashing
Benjamin Sach
 
Bloom Filters
Bloom FiltersBloom Filters
Bloom Filters
Benjamin Sach
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
Benjamin Sach
 
Minimum Spanning Trees (via Disjoint Sets)
Minimum Spanning Trees (via Disjoint Sets)Minimum Spanning Trees (via Disjoint Sets)
Minimum Spanning Trees (via Disjoint Sets)
Benjamin Sach
 
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's AlgorithmShortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Benjamin Sach
 
Depth First Search and Breadth First Search
Depth First Search and Breadth First SearchDepth First Search and Breadth First Search
Depth First Search and Breadth First Search
Benjamin Sach
 
Shortest Paths Part 2: Negative Weights and All-pairs
Shortest Paths Part 2: Negative Weights and All-pairsShortest Paths Part 2: Negative Weights and All-pairs
Shortest Paths Part 2: Negative Weights and All-pairs
Benjamin Sach
 
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip ListsSelf-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
Benjamin Sach
 

More from Benjamin Sach (15)

Orthogonal Range Searching
Orthogonal Range SearchingOrthogonal Range Searching
Orthogonal Range Searching
 
Pattern Matching Part Two: k-mismatches
Pattern Matching Part Two: k-mismatchesPattern Matching Part Two: k-mismatches
Pattern Matching Part Two: k-mismatches
 
Pattern Matching Part Three: Hamming Distance
Pattern Matching Part Three: Hamming DistancePattern Matching Part Three: Hamming Distance
Pattern Matching Part Three: Hamming Distance
 
Lowest Common Ancestor
Lowest Common AncestorLowest Common Ancestor
Lowest Common Ancestor
 
Range Minimum Queries
Range Minimum QueriesRange Minimum Queries
Range Minimum Queries
 
Pattern Matching Part Two: Suffix Arrays
Pattern Matching Part Two: Suffix ArraysPattern Matching Part Two: Suffix Arrays
Pattern Matching Part Two: Suffix Arrays
 
Pattern Matching Part One: Suffix Trees
Pattern Matching Part One: Suffix TreesPattern Matching Part One: Suffix Trees
Pattern Matching Part One: Suffix Trees
 
Hashing Part Two: Cuckoo Hashing
Hashing Part Two: Cuckoo HashingHashing Part Two: Cuckoo Hashing
Hashing Part Two: Cuckoo Hashing
 
Bloom Filters
Bloom FiltersBloom Filters
Bloom Filters
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
 
Minimum Spanning Trees (via Disjoint Sets)
Minimum Spanning Trees (via Disjoint Sets)Minimum Spanning Trees (via Disjoint Sets)
Minimum Spanning Trees (via Disjoint Sets)
 
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's AlgorithmShortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
 
Depth First Search and Breadth First Search
Depth First Search and Breadth First SearchDepth First Search and Breadth First Search
Depth First Search and Breadth First Search
 
Shortest Paths Part 2: Negative Weights and All-pairs
Shortest Paths Part 2: Negative Weights and All-pairsShortest Paths Part 2: Negative Weights and All-pairs
Shortest Paths Part 2: Negative Weights and All-pairs
 
Self-balancing Trees and Skip Lists
Self-balancing Trees and Skip ListsSelf-balancing Trees and Skip Lists
Self-balancing Trees and Skip Lists
 

Recently uploaded

Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
timhan337
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
Sandy Millin
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
Celine George
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
Jean Carlos Nunes Paixão
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
JosvitaDsouza2
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
Peter Windle
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
siemaillard
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
TechSoup
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
Jheel Barad
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
DhatriParmar
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
Peter Windle
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
CarlosHernanMontoyab2
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
beazzy04
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
Levi Shapiro
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
heathfieldcps1
 

Recently uploaded (20)

Honest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptxHonest Reviews of Tim Han LMA Course Program.pptx
Honest Reviews of Tim Han LMA Course Program.pptx
 
The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...2024.06.01 Introducing a competency framework for languag learning materials ...
2024.06.01 Introducing a competency framework for languag learning materials ...
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17How to Make a Field invisible in Odoo 17
How to Make a Field invisible in Odoo 17
 
Lapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdfLapbook sobre os Regimes Totalitários.pdf
Lapbook sobre os Regimes Totalitários.pdf
 
1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx1.4 modern child centered education - mahatma gandhi-2.pptx
1.4 modern child centered education - mahatma gandhi-2.pptx
 
Embracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic ImperativeEmbracing GenAI - A Strategic Imperative
Embracing GenAI - A Strategic Imperative
 
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup   New Member Orientation and Q&A (May 2024).pdfWelcome to TechSoup   New Member Orientation and Q&A (May 2024).pdf
Welcome to TechSoup New Member Orientation and Q&A (May 2024).pdf
 
Instructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptxInstructions for Submissions thorugh G- Classroom.pptx
Instructions for Submissions thorugh G- Classroom.pptx
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
The Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptxThe Accursed House by Émile Gaboriau.pptx
The Accursed House by Émile Gaboriau.pptx
 
A Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in EducationA Strategic Approach: GenAI in Education
A Strategic Approach: GenAI in Education
 
678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf678020731-Sumas-y-Restas-Para-Colorear.pdf
678020731-Sumas-y-Restas-Para-Colorear.pdf
 
Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345Sha'Carri Richardson Presentation 202345
Sha'Carri Richardson Presentation 202345
 
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
June 3, 2024 Anti-Semitism Letter Sent to MIT President Kornbluth and MIT Cor...
 
The basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptxThe basics of sentences session 5pptx.pptx
The basics of sentences session 5pptx.pptx
 

Approximation Algorithms Part One: Constant factor approximations

  • 1. Advanced Algorithms – COMS31900 Approximation algorithms part one Constant factor approximations Benjamin Sach
  • 2. NP-completeness recap A problem A is NP-complete if NP is the class of decision problems we can check the answer to in polynomial time A is in NP Every B in NP has a polynomial time reduction to A (this second part is the definition of NP-hard)
  • 3. NP-completeness recap A problem A is NP-complete if NP is the class of decision problems we can check the answer to in polynomial time A is in NP Every B in NP has a polynomial time reduction to A (this second part is the definition of NP-hard) ‘yes/no’ problems
  • 4. NP-completeness recap A problem A is NP-complete if NP is the class of decision problems we can check the answer to in polynomial time A is in NP Every B in NP has a polynomial time reduction to A (this second part is the definition of NP-hard)
  • 5. NP-completeness recap A problem A is NP-complete if NP is the class of decision problems we can check the answer to in polynomial time A is in NP Every B in NP has a polynomial time reduction to A (this second part is the definition of NP-hard) we can solve B using A
  • 6. NP-completeness recap A problem A is NP-complete if NP is the class of decision problems we can check the answer to in polynomial time A is in NP Every B in NP has a polynomial time reduction to A (this second part is the definition of NP-hard)
  • 7. NP-completeness recap A problem A is NP-complete if NP is the class of decision problems we can check the answer to in polynomial time A is in NP Every B in NP has a polynomial time reduction to A If we could solve A quickly we could solve every problem in NP quickly (this second part is the definition of NP-hard)
  • 8. NP-completeness recap A problem A is NP-complete if NP is the class of decision problems we can check the answer to in polynomial time A is in NP Every B in NP has a polynomial time reduction to A If we could solve A quickly we could solve every problem in NP quickly They are the ‘hardest’ problems in NP (this second part is the definition of NP-hard)
  • 9. NP-completeness recap A problem A is NP-complete if NP is the class of decision problems we can check the answer to in polynomial time A is in NP Every B in NP has a polynomial time reduction to A If we could solve A quickly we could solve every problem in NP quickly They are the ‘hardest’ problems in NP (this second part is the definition of NP-hard) that you can’t solve them in polynomial time (i.e. that P = NP) Most computer scientists (I’ve met) believe
  • 10. NP-completeness recap A problem A is NP-complete if NP is the class of decision problems we can check the answer to in polynomial time A is in NP Every B in NP has a polynomial time reduction to A If we could solve A quickly we could solve every problem in NP quickly They are the ‘hardest’ problems in NP (this second part is the definition of NP-hard) A polynomial time algorithm for an NP-complete problem is worth (a lot more than) a million dollars
  • 11. NP-completeness recap A problem A is NP-complete if NP is the class of decision problems we can check the answer to in polynomial time A is in NP Every B in NP has a polynomial time reduction to A If we could solve A quickly we could solve every problem in NP quickly They are the ‘hardest’ problems in NP (this second part is the definition of NP-hard)
  • 12. NP-completeness recap A problem A is NP-complete if NP is the class of decision problems we can check the answer to in polynomial time A is in NP Every B in NP has a polynomial time reduction to A If we could solve A quickly we could solve every problem in NP quickly They are the ‘hardest’ problems in NP So if a problem is NP-complete, we give up right? (this second part is the definition of NP-hard)
  • 17. Bin packing Bins 1 Items 1 4/8 4/8 7/8 3/82/82/8 0 < |Item| 1 I is the sum of all item sizes
  • 18. Bin packing Bins 1 Items 1 4/8 4/8 7/8 3/82/82/8 0 < |Item| 1 |Bin| = 1 and there is an unlimited number of bins. . . I is the sum of all item sizes
  • 19. Bin packing 1 1 4/8 4/8 7/8 3/82/82/8 Problem pack all items into the fewest possible bins
  • 20. Bin packing 1 1 4/8 4/8 7/8 3/82/82/8 Problem pack all items into the fewest possible bins This is an example of an optimisation problem
  • 21. Bin packing 1 1 Problem pack all items into the fewest possible bins 4/8 4/8 7/8 2/82/8 3/8 This is an example of an optimisation problem
  • 22. Bin packing 1 1 4/8 4/8 7/8 3/82/82/8 Problem pack all items into the fewest possible bins This is an example of an optimisation problem
  • 23. Bin packing 1 1 Problem pack all items into the fewest possible bins 4/8 4/8 7/8 2/8 2/8 3/8 This is an example of an optimisation problem
  • 24. Bin packing 1 1 Problem pack all items into the fewest possible bins 4/8 4/8 7/8 2/8 2/8 3/8
  • 25. Bin packing 1 1 Problem pack all items into the fewest possible bins 4/8 4/8 7/8 2/8 2/8 3/8 The BINPACKING problem is known to be NP-hard
  • 26. Bin packing 1 1 Problem pack all items into the fewest possible bins 4/8 4/8 7/8 2/8 2/8 3/8 The BINPACKING problem is known to be NP-hard and the decision version. . . “Can you pack the items into at most k bins?” is NP-complete
  • 27. Bin packing 1 1 Problem pack all items into the fewest possible bins 4/8 4/8 7/8 2/8 2/8 3/8 The BINPACKING problem is known to be NP-hard and the decision version. . . “Can you pack the items into at most k bins?” is NP-complete In the decision version, k is part of the input
  • 28. Bin packing 1 1 Problem pack all items into the fewest possible bins 4/8 4/8 7/8 2/8 2/8 3/8 The BINPACKING problem is known to be NP-hard
  • 29. Bin packing 1 1 Problem pack all items into the fewest possible bins 4/8 4/8 7/8 2/8 2/8 3/8 The BINPACKING problem is known to be NP-hard but fortunately we can approximate
  • 30. Next fit 1 1 4/8 4/8 7/8 3/82/82/8 If item i fits into bin j: pack it, i++; else j++;
  • 31. Next fit 1 1 4/8 4/8 7/8 3/82/82/8 If item i fits into bin j: pack it, i++; else j++;
  • 32. Next fit 1 1 4/8 7/8 3/82/82/8 If item i fits into bin j: pack it, i++; else j++; 4/8
  • 33. Next fit 1 1 4/8 7/8 3/82/82/8 If item i fits into bin j: pack it, i++; else j++; 4/8
  • 34. Next fit 1 1 4/8 7/8 3/82/8 If item i fits into bin j: pack it, i++; else j++; 4/8 2/8
  • 35. Next fit 1 1 4/8 7/8 3/82/8 If item i fits into bin j: pack it, i++; else j++; 4/8 2/8
  • 36. Next fit 1 1 4/8 7/8 3/82/8 If item i fits into bin j: pack it, i++; else j++; 4/8 2/8
  • 37. Next fit 1 1 7/8 3/82/8 If item i fits into bin j: pack it, i++; else j++; 4/8 2/8 4/8
  • 38. Next fit 1 1 7/8 3/82/8 If item i fits into bin j: pack it, i++; else j++; 4/8 2/8 4/8
  • 39. Next fit 1 1 7/8 3/82/8 If item i fits into bin j: pack it, i++; else j++; 4/8 2/8 4/8
  • 40. Next fit 1 1 3/82/8 If item i fits into bin j: pack it, i++; else j++; 4/8 2/8 4/8 7/8
  • 41. Next fit 1 1 3/82/8 If item i fits into bin j: pack it, i++; else j++; 4/8 2/8 4/8 7/8
  • 42. Next fit 1 1 3/82/8 If item i fits into bin j: pack it, i++; else j++; 4/8 2/8 4/8 7/8
  • 43. Next fit 1 1 3/8 If item i fits into bin j: pack it, i++; else j++; 4/8 2/8 4/8 7/8 2/8
  • 44. Next fit 1 1 3/8 If item i fits into bin j: pack it, i++; else j++; 4/8 2/8 4/8 7/8 2/8
  • 45. Next fit 1 1 If item i fits into bin j: pack it, i++; else j++; 4/8 2/8 4/8 7/8 2/8 3/8
  • 47. Next fit 1 1 4/8 2/8 4/8 7/8 2/8 3/8 Next fit runs in O(n) time but how good is it?
  • 48. Next fit 1 1 4/8 2/8 4/8 7/8 2/8 3/8 Next fit runs in O(n) time but how good is it? where n is the number of items
  • 49. Next fit 1 4/8 2/8 4/8 7/8 2/8 3/8 Next fit runs in O(n) time but how good is it?
  • 50. Next fit 1 4/8 2/8 4/8 7/8 2/8 3/8 Next fit runs in O(n) time but how good is it? Let fill(i) be the sum of item sizes in bin i and s be the number of non-empty bins (using Next fit)
  • 51. Next fit 1 4/8 2/8 4/8 7/8 2/8 3/8 Next fit runs in O(n) time but how good is it? Let fill(i) be the sum of item sizes in bin i Observe that fill(2i − 1) + fill(2i) > 1 (for 1 2i s) and s be the number of non-empty bins (using Next fit)
  • 52. Next fit 1 4/8 2/8 4/8 7/8 2/8 3/8 Next fit runs in O(n) time but how good is it? Let fill(i) be the sum of item sizes in bin i Observe that fill(2i − 1) + fill(2i) > 1 (for 1 2i s) and s be the number of non-empty bins (using Next fit) so s/2 < 1 2i s fill(2i − 1) + fill(2i)
  • 53. Next fit 1 4/8 2/8 4/8 7/8 2/8 3/8 Next fit runs in O(n) time but how good is it? Let fill(i) be the sum of item sizes in bin i Observe that fill(2i − 1) + fill(2i) > 1 (for 1 2i s) and s be the number of non-empty bins (using Next fit) so s/2 < 1 2i s fill(2i − 1) + fill(2i) I
  • 54. Next fit 1 4/8 2/8 4/8 7/8 2/8 3/8 Next fit runs in O(n) time but how good is it? Let fill(i) be the sum of item sizes in bin i Observe that fill(2i − 1) + fill(2i) > 1 (for 1 2i s) and s be the number of non-empty bins (using Next fit) so s/2 < 1 2i s fill(2i − 1) + fill(2i) I the sum of the item weights
  • 55. Next fit 1 4/8 2/8 4/8 7/8 2/8 3/8 Next fit runs in O(n) time but how good is it? Let fill(i) be the sum of item sizes in bin i Observe that fill(2i − 1) + fill(2i) > 1 (for 1 2i s) and s be the number of non-empty bins (using Next fit) so s/2 < 1 2i s fill(2i − 1) + fill(2i) I Opt the sum of the item weights
  • 56. Next fit 1 4/8 2/8 4/8 7/8 2/8 3/8 Next fit runs in O(n) time but how good is it? Let fill(i) be the sum of item sizes in bin i Observe that fill(2i − 1) + fill(2i) > 1 (for 1 2i s) and s be the number of non-empty bins (using Next fit) so s/2 < 1 2i s fill(2i − 1) + fill(2i) I Opt the sum of the item weights the optimal number of bins
  • 57. Next fit 1 4/8 2/8 4/8 7/8 2/8 3/8 Next fit runs in O(n) time but how good is it? Let fill(i) be the sum of item sizes in bin i Observe that fill(2i − 1) + fill(2i) > 1 (for 1 2i s) and s be the number of non-empty bins (using Next fit) so s/2 < 1 2i s fill(2i − 1) + fill(2i) I Opt
  • 58. Next fit 1 4/8 2/8 4/8 7/8 2/8 3/8 Next fit runs in O(n) time but how good is it? Let fill(i) be the sum of item sizes in bin i Observe that fill(2i − 1) + fill(2i) > 1 (for 1 2i s) and s be the number of non-empty bins (using Next fit) so s/2 < 1 2i s fill(2i − 1) + fill(2i) I Opt therefore s 2 · Opt
  • 59. Next fit 1 4/8 2/8 4/8 7/8 2/8 3/8 Next fit runs in O(n) time but how good is it? Let fill(i) be the sum of item sizes in bin i Observe that fill(2i − 1) + fill(2i) > 1 (for 1 2i s) and s be the number of non-empty bins (using Next fit) so s/2 < 1 2i s fill(2i − 1) + fill(2i) I Opt therefore s 2 · Opt in other words the Next Fit is never worse than twice the optimal
  • 60. Next fit 1 4/8 2/8 4/8 7/8 2/8 3/8 Next fit runs in O(n) time but how good is it? Let fill(i) be the sum of item sizes in bin i Observe that fill(2i − 1) + fill(2i) > 1 (for 1 2i s) and s be the number of non-empty bins (using Next fit) so s/2 < 1 2i s fill(2i − 1) + fill(2i) I Opt therefore s 2 · Opt
  • 61. Approximation Algorithms An algorithm A is an α-approximation algorithm for problem P if, ◦ A runs in polynomial time ◦ A always outputs a solution with value s within an α factor of Opt
  • 62. Approximation Algorithms An algorithm A is an α-approximation algorithm for problem P if, ◦ A runs in polynomial time ◦ A always outputs a solution with value s Here P is an optimisation problem with optimal solution of value Opt within an α factor of Opt
  • 63. Approximation Algorithms An algorithm A is an α-approximation algorithm for problem P if, ◦ A runs in polynomial time ◦ A always outputs a solution with value s Here P is an optimisation problem with optimal solution of value Opt • If P is a maximisation problem, Opt α s Opt within an α factor of Opt
  • 64. Approximation Algorithms An algorithm A is an α-approximation algorithm for problem P if, ◦ A runs in polynomial time ◦ A always outputs a solution with value s Here P is an optimisation problem with optimal solution of value Opt • If P is a maximisation problem, Opt α s Opt within an α factor of Opt • If P is a minimisation problem (like BINPACKING), Opt s α · Opt
  • 65. Approximation Algorithms An algorithm A is an α-approximation algorithm for problem P if, ◦ A runs in polynomial time ◦ A always outputs a solution with value s Here P is an optimisation problem with optimal solution of value Opt • If P is a maximisation problem, Opt α s Opt within an α factor of Opt • If P is a minimisation problem (like BINPACKING), Opt s α · Opt We have seen a 2-approximation algorithm for BINPACKING
  • 66. Approximation Algorithms An algorithm A is an α-approximation algorithm for problem P if, ◦ A runs in polynomial time ◦ A always outputs a solution with value s Here P is an optimisation problem with optimal solution of value Opt • If P is a maximisation problem, Opt α s Opt within an α factor of Opt • If P is a minimisation problem (like BINPACKING), Opt s α · Opt We have seen a 2-approximation algorithm for BINPACKING the number of bins used, s is always between Opt and 2 · Opt
  • 67. Approximation Algorithms An algorithm A is an α-approximation algorithm for problem P if, ◦ A runs in polynomial time ◦ A always outputs a solution with value s Here P is an optimisation problem with optimal solution of value Opt • If P is a maximisation problem, Opt α s Opt within an α factor of Opt • If P is a minimisation problem (like BINPACKING), Opt s α · Opt We have seen a 2-approximation algorithm for BINPACKING the number of bins used, s is always between Opt and 2 · Opt In the examples we consider, α will be a constant but it could depend on n (the input size)
  • 68. 1 4/8 2/8 4/8 7/8 2/8 3/8 We have seen that Next fit is a 2-approximation algorithm for Bin packing which runs in O(n) time can we do better?
  • 69. First fit decreasing (FFD) 1 1 4/8 4/8 7/8 3/82/82/8
  • 70. First fit decreasing (FFD) 1 1 Step 1: Sort the items into non-increasing order 4/8 4/8 7/8 3/82/82/8
  • 71. First fit decreasing (FFD) 1 1 4/8 4/8 7/8 3/8 2/8 2/8 Step 1: Sort the items into non-increasing order
  • 72. First fit decreasing (FFD) 1 1 4/8 4/8 7/8 3/8 2/8 2/8 Step 2: Put each item in the first (left-most) bin it fits in
  • 73. First fit decreasing (FFD) 1 1 4/8 4/8 3/8 2/8 2/8 Step 2: Put each item in the first (left-most) bin it fits in 7/8
  • 74. First fit decreasing (FFD) 1 1 4/8 3/8 2/8 2/8 Step 2: Put each item in the first (left-most) bin it fits in 7/8 4/8
  • 75. First fit decreasing (FFD) 1 1 3/8 2/8 2/8 Step 2: Put each item in the first (left-most) bin it fits in 7/8 4/8 4/8
  • 76. First fit decreasing (FFD) 1 1 2/8 2/8 Step 2: Put each item in the first (left-most) bin it fits in 7/8 4/8 4/8 3/8
  • 77. First fit decreasing (FFD) 1 1 2/8 Step 2: Put each item in the first (left-most) bin it fits in 7/8 4/8 4/8 3/8 2/8
  • 78. First fit decreasing (FFD) 1 1 Step 2: Put each item in the first (left-most) bin it fits in 7/8 4/8 4/8 3/8 2/8 2/8
  • 79. First fit decreasing (FFD) 1 1 Step 2: Put each item in the first (left-most) bin it fits in 7/8 4/8 4/8 3/8 2/8 2/8 this will be important for the proof
  • 80. First fit decreasing (FFD) 1 1 Step 2: Put each item in the first (left-most) bin it fits in 7/8 4/8 4/8 3/8 2/8 2/8
  • 81. First fit decreasing (FFD) 1 1 7/8 4/8 4/8 3/8 2/8 2/8
  • 82. First fit decreasing (FFD) 1 1 7/8 4/8 4/8 3/8 2/8 2/8 FFD runs in O(n2) time but how good is it?
  • 83. First fit decreasing (FFD) 1 7/8 4/8 4/8 3/8 2/8 2/8 FFD runs in O(n2) time but how good is it?
  • 84. First fit decreasing (FFD) Consider bin j = 2s 3 (s is the number of bins FFD uses on this input) ss
  • 85. First fit decreasing (FFD) Consider bin j = 2s 3 (s is the number of bins FFD uses on this input) j ss
  • 86. First fit decreasing (FFD) Consider bin j = 2s 3 (s is the number of bins FFD uses on this input) j ss
  • 87. First fit decreasing (FFD) Consider bin j = 2s 3 (s is the number of bins FFD uses on this input) Case 1: Bin j contains an item of size > 1/2 j ss
  • 88. First fit decreasing (FFD) Consider bin j = 2s 3 (s is the number of bins FFD uses on this input) Case 1: Bin j contains an item of size > 1/2 j ss
  • 89. First fit decreasing (FFD) Consider bin j = 2s 3 (s is the number of bins FFD uses on this input) Case 1: Bin j contains an item of size > 1/2 Every bin j j contains an item of size > 1/2 j ss
  • 90. First fit decreasing (FFD) Consider bin j = 2s 3 (s is the number of bins FFD uses on this input) Case 1: Bin j contains an item of size > 1/2 Every bin j j contains an item of size > 1/2 because we packed big things first and each thing was packed in the lowest numbered bin j ss
  • 91. First fit decreasing (FFD) Consider bin j = 2s 3 (s is the number of bins FFD uses on this input) Case 1: Bin j contains an item of size > 1/2 Every bin j j contains an item of size > 1/2 because we packed big things first and each thing was packed in the lowest numbered bin j ss
  • 92. First fit decreasing (FFD) Consider bin j = 2s 3 (s is the number of bins FFD uses on this input) Case 1: Bin j contains an item of size > 1/2 Every bin j j contains an item of size > 1/2 j ss
  • 93. First fit decreasing (FFD) Consider bin j = 2s 3 (s is the number of bins FFD uses on this input) Case 1: Bin j contains an item of size > 1/2 Every bin j j contains an item of size > 1/2 each of these items has to be in a different bin (even in Opt) j ss
  • 94. First fit decreasing (FFD) Consider bin j = 2s 3 (s is the number of bins FFD uses on this input) Case 1: Bin j contains an item of size > 1/2 Every bin j j contains an item of size > 1/2 each of these items has to be in a different bin (even in Opt) So Opt uses at least 2s 3 bins j ss
  • 95. First fit decreasing (FFD) Consider bin j = 2s 3 (s is the number of bins FFD uses on this input) Case 1: Bin j contains an item of size > 1/2 Every bin j j contains an item of size > 1/2 each of these items has to be in a different bin (even in Opt) So Opt uses at least 2s 3 bins or. . . s 3Opt 2 j ss
  • 96. First fit decreasing (FFD) Consider bin j = 2s 3 (s is the number of bins FFD uses on this input) Case 2: Bin j contains only items of size 1/2 j ss
  • 97. First fit decreasing (FFD) Consider bin j = 2s 3 (s is the number of bins FFD uses on this input) Case 2: Bin j contains only items of size 1/2 when FFD packed the first item into bin j, j ss
  • 98. First fit decreasing (FFD) Consider bin j = 2s 3 (s is the number of bins FFD uses on this input) Case 2: Bin j contains only items of size 1/2 when FFD packed the first item into bin j, j ss
  • 99. First fit decreasing (FFD) Consider bin j = 2s 3 (s is the number of bins FFD uses on this input) Case 2: Bin j contains only items of size 1/2 when FFD packed the first item into bin j, j ss
  • 100. First fit decreasing (FFD) Consider bin j = 2s 3 (s is the number of bins FFD uses on this input) Case 2: Bin j contains only items of size 1/2 when FFD packed the first item into bin j, j 1. all bins j, (j + 1), . . . , (s − 2), (s − 1) were empty ss
  • 101. First fit decreasing (FFD) Consider bin j = 2s 3 (s is the number of bins FFD uses on this input) Case 2: Bin j contains only items of size 1/2 when FFD packed the first item into bin j, j 1. all bins j, (j + 1), . . . , (s − 2), (s − 1) were empty ss 2. and all unpacked items had size 1/2 (because we pack in non-increasing order)
  • 102. First fit decreasing (FFD) Consider bin j = 2s 3 (s is the number of bins FFD uses on this input) Case 2: Bin j contains only items of size 1/2 so Bins j, (j + 1), . . . , (s − 2), (s − 1) each contain at least two items when FFD packed the first item into bin j, j 1. all bins j, (j + 1), . . . , (s − 2), (s − 1) were empty ss 2. and all unpacked items had size 1/2 (because we pack in non-increasing order)
  • 103. First fit decreasing (FFD) Consider bin j = 2s 3 (s is the number of bins FFD uses on this input) Case 2: Bin j contains only items of size 1/2 so Bins j, (j + 1), . . . , (s − 2), (s − 1) each contain at least two items when FFD packed the first item into bin j, j 1. all bins j, (j + 1), . . . , (s − 2), (s − 1) were empty ss 2. and all unpacked items had size 1/2 (because we pack in non-increasing order) 2+ 2+ 2+ 2+
  • 104. First fit decreasing (FFD) Consider bin j = 2s 3 (s is the number of bins FFD uses on this input) Case 2: Bin j contains only items of size 1/2 so Bins j, (j + 1), . . . , (s − 2), (s − 1) each contain at least two items when FFD packed the first item into bin j, j 1. all bins j, (j + 1), . . . , (s − 2), (s − 1) were empty ss 2. and all unpacked items had size 1/2 (because we pack in non-increasing order) 2+ 2+ 2+ 2+ (we only use a new bin when the item won’t fit in any previous bin)
  • 105. First fit decreasing (FFD) Consider bin j = 2s 3 (s is the number of bins FFD uses on this input) Case 2: Bin j contains only items of size 1/2 so Bins j, (j + 1), . . . , (s − 2), (s − 1) each contain at least two items when FFD packed the first item into bin j, j 1. all bins j, (j + 1), . . . , (s − 2), (s − 1) were empty ss 2. and all unpacked items had size 1/2 (because we pack in non-increasing order) 2+ 2+ 2+ 2+
  • 106. First fit decreasing (FFD) Consider bin j = 2s 3 (s is the number of bins FFD uses on this input) Case 2: Bin j contains only items of size 1/2 so Bins j, (j + 1), . . . , (s − 2), (s − 1) each contain at least two items and bin s contains at least one item when FFD packed the first item into bin j, j 1. all bins j, (j + 1), . . . , (s − 2), (s − 1) were empty ss 2. and all unpacked items had size 1/2 (because we pack in non-increasing order) 2+ 2+ 2+ 2+
  • 107. First fit decreasing (FFD) Consider bin j = 2s 3 (s is the number of bins FFD uses on this input) Case 2: Bin j contains only items of size 1/2 so Bins j, (j + 1), . . . , (s − 2), (s − 1) each contain at least two items and bin s contains at least one item when FFD packed the first item into bin j, j 1. all bins j, (j + 1), . . . , (s − 2), (s − 1) were empty ss 2. and all unpacked items had size 1/2 (because we pack in non-increasing order) 2+ 2+ 2+ 2+ 1+
  • 108. First fit decreasing (FFD) Consider bin j = 2s 3 (s is the number of bins FFD uses on this input) Case 2: Bin j contains only items of size 1/2 so Bins j, (j + 1), . . . , (s − 2), (s − 1) each contain at least two items and bin s contains at least one item j ss 2+ 2+ 2+ 2+ 1+
  • 109. First fit decreasing (FFD) Consider bin j = 2s 3 (s is the number of bins FFD uses on this input) Case 2: Bin j contains only items of size 1/2 j ss 2+ 2+ 2+ 2+ 1+
  • 110. First fit decreasing (FFD) Consider bin j = 2s 3 (s is the number of bins FFD uses on this input) Case 2: Bin j contains only items of size 1/2 j ss 2+ 2+ 2+ 2+ 1+ so Bins j, (j + 1), . . . , (s − 2), (s − 1) each contain at least two items and bin s contains at least one item
  • 111. First fit decreasing (FFD) Consider bin j = 2s 3 (s is the number of bins FFD uses on this input) Case 2: Bin j contains only items of size 1/2 This gives a total of 2(s − j) + 1 items, none of which fits into bins 1, 2, 3, . . . , (j − 1) j ss 2+ 2+ 2+ 2+ 1+ so Bins j, (j + 1), . . . , (s − 2), (s − 1) each contain at least two items and bin s contains at least one item
  • 112. First fit decreasing (FFD) Consider bin j = 2s 3 (s is the number of bins FFD uses on this input) Case 2: Bin j contains only items of size 1/2 This gives a total of 2(s − j) + 1 items, none of which fits into bins 1, 2, 3, . . . , (j − 1) otherwise we would have packed them there j ss 2+ 2+ 2+ 2+ 1+ so Bins j, (j + 1), . . . , (s − 2), (s − 1) each contain at least two items and bin s contains at least one item
  • 113. First fit decreasing (FFD) Consider bin j = 2s 3 (s is the number of bins FFD uses on this input) Case 2: Bin j contains only items of size 1/2 This gives a total of 2(s − j) + 1 items, none of which fits into bins 1, 2, 3, . . . , (j − 1) j ss 2+ 2+ 2+ 2+ 1+ so Bins j, (j + 1), . . . , (s − 2), (s − 1) each contain at least two items and bin s contains at least one item
  • 114. First fit decreasing (FFD) Consider bin j = 2s 3 (s is the number of bins FFD uses on this input) Case 2: Bin j contains only items of size 1/2 This gives a total of 2(s − j) + 1 items, none of which fits into bins 1, 2, 3, . . . , (j − 1) so I > min{j − 1, 2(s − j) + 1} j ss 2+ 2+ 2+ 2+ 1+ so Bins j, (j + 1), . . . , (s − 2), (s − 1) each contain at least two items and bin s contains at least one item
  • 115. First fit decreasing (FFD) Consider bin j = 2s 3 (s is the number of bins FFD uses on this input) Case 2: Bin j contains only items of size 1/2 This gives a total of 2(s − j) + 1 items, none of which fits into bins 1, 2, 3, . . . , (j − 1) so I > min{j − 1, 2(s − j) + 1} recall I is the total weight of all items j ss 2+ 2+ 2+ 2+ 1+ so Bins j, (j + 1), . . . , (s − 2), (s − 1) each contain at least two items and bin s contains at least one item
  • 116. First fit decreasing (FFD) Consider bin j = 2s 3 (s is the number of bins FFD uses on this input) Case 2: Bin j contains only items of size 1/2 This gives a total of 2(s − j) + 1 items, none of which fits into bins 1, 2, 3, . . . , (j − 1) so I > min{j − 1, 2(s − j) + 1} recall I is the total weight of all items pairing these with these considerconsider j ss so Bins j, (j + 1), . . . , (s − 2), (s − 1) each contain at least two items and bin s contains at least one item
  • 117. First fit decreasing (FFD) Consider bin j = 2s 3 (s is the number of bins FFD uses on this input) Case 2: Bin j contains only items of size 1/2 This gives a total of 2(s − j) + 1 items, none of which fits into bins 1, 2, 3, . . . , (j − 1) so I > min{j − 1, 2(s − j) + 1} recall I is the total weight of all items pairing these with these considerconsider j ss so Bins j, (j + 1), . . . , (s − 2), (s − 1) each contain at least two items and bin s contains at least one item this + this > 1
  • 118. First fit decreasing (FFD) Consider bin j = 2s 3 (s is the number of bins FFD uses on this input) Case 2: Bin j contains only items of size 1/2 This gives a total of 2(s − j) + 1 items, none of which fits into bins 1, 2, 3, . . . , (j − 1) so I > min{j − 1, 2(s − j) + 1} recall I is the total weight of all items j ss so Bins j, (j + 1), . . . , (s − 2), (s − 1) each contain at least two items and bin s contains at least one item
  • 119. First fit decreasing (FFD) Consider bin j = 2s 3 (s is the number of bins FFD uses on this input) Case 2: Bin j contains only items of size 1/2 This gives a total of 2(s − j) + 1 items, none of which fits into bins 1, 2, 3, . . . , (j − 1) so I > min{j − 1, 2(s − j) + 1} j ss so Bins j, (j + 1), . . . , (s − 2), (s − 1) each contain at least two items and bin s contains at least one item
  • 120. First fit decreasing (FFD) Consider bin j = 2s 3 (s is the number of bins FFD uses on this input) Case 2: Bin j contains only items of size 1/2 This gives a total of 2(s − j) + 1 items, none of which fits into bins 1, 2, 3, . . . , (j − 1) so I > min{j − 1, 2(s − j) + 1} 2s/3 − 1 j ss so Bins j, (j + 1), . . . , (s − 2), (s − 1) each contain at least two items and bin s contains at least one item
  • 121. First fit decreasing (FFD) Consider bin j = 2s 3 (s is the number of bins FFD uses on this input) Case 2: Bin j contains only items of size 1/2 This gives a total of 2(s − j) + 1 items, none of which fits into bins 1, 2, 3, . . . , (j − 1) so I > min{j − 1, 2(s − j) + 1} 2s/3 − 1 by plugging in j = 2s/3 j ss so Bins j, (j + 1), . . . , (s − 2), (s − 1) each contain at least two items and bin s contains at least one item
  • 122. First fit decreasing (FFD) Consider bin j = 2s 3 (s is the number of bins FFD uses on this input) Case 2: Bin j contains only items of size 1/2 As 2s/3 − 1 < I j ss
  • 123. First fit decreasing (FFD) Consider bin j = 2s 3 (s is the number of bins FFD uses on this input) Case 2: Bin j contains only items of size 1/2 As 2s/3 − 1 < I and I Opt j ss
  • 124. First fit decreasing (FFD) Consider bin j = 2s 3 (s is the number of bins FFD uses on this input) Case 2: Bin j contains only items of size 1/2 As 2s/3 − 1 < I we have that 2s/3 − 1 < Opt and I Opt j ss
  • 125. First fit decreasing (FFD) Consider bin j = 2s 3 (s is the number of bins FFD uses on this input) Case 2: Bin j contains only items of size 1/2 As 2s/3 − 1 < I we have that 2s/3 − 1 < Opt and I Opt . . . but both sides are integers. . . so 2s/3 Opt finally . . . 2s/3 2s/3 Opt or s (3/2)Opt j ss
  • 126. First fit decreasing (FFD) Consider bin j = 2s 3 (s is the number of bins FFD uses on this input) j ss
  • 127. First fit decreasing (FFD) Consider bin j = 2s 3 (s is the number of bins FFD uses on this input) Case 2: Bin j contains only items of size 1/2 Case 1: Bin j contains an item of size > 1/2 in both cases. . . s 3Opt 2 j ss
  • 128. First fit decreasing (FFD) Consider bin j = 2s 3 (s is the number of bins FFD uses on this input) So FFD is a 3/2-approximation algorithm for BINPACKING Case 2: Bin j contains only items of size 1/2 Case 1: Bin j contains an item of size > 1/2 in both cases. . . s 3Opt 2 j ss
  • 129. Approximation Algorithms Summary An algorithm A is an α-approximation algorithm for problem P if, ◦ A runs in polynomial time ◦ A always outputs a solution with value s Here P is an optimisation problem with optimal solution of value Opt If P is a maximisation problem, Opt α s Opt within an α factor of Opt If P is a minimisation problem (like BINPACKING), Opt s α · Opt We have seen Next Fit which is a 2-approximation algorithm for BINPACKING which runs in O(n) time and First Fit Decreasing which is a 3/2-approximation algorithm for BINPACKING which runs in O(n2) time Bin Packing is NP-hard so solving it exactly in polynomial time would prove that P = NP