- Source: Next-fit bin packing
Next-fit is an online algorithm for bin packing. Its input is a list of items of different sizes. Its output is a packing - a partition of the items into bins of fixed capacity, such that the sum of sizes of items in each bin is at most the capacity. Ideally, we would like to use as few bins as possible, but minimizing the number of bins is an NP-hard problem. The next-fit algorithm uses the following heuristic:
It keeps a current bin, which is initially empty.
When an item arrives, it checks whether the item fits into the current bin.
If it fits, it is placed inside it.
Otherwise, the current bin is closed, a new bin is opened and the coming item is placed inside this new bin.
Next-Fit is a bounded space algorithm - it requires only one partially-filled bin to be open at any time. The algorithm was studied by David S. Johnson in his doctoral thesis in 1973.
Run time
The running time of NextFit can be bounded by
O
(
n
)
{\displaystyle {\mathcal {O}}(n)}
, where
n
{\displaystyle n}
is the number of items in the list.
Approximation ratio
Denote by NF(L) the number of bins used by NextFit, and by OPT(L) the optimal number of bins possible for the list L.
= Upper bound
=Then, for each list
L
{\displaystyle L}
,
N
F
(
L
)
≤
2
⋅
O
P
T
(
L
)
−
1
{\displaystyle NF(L)\leq 2\cdot \mathrm {OPT} (L)-1}
. The intuition to the proof s the following. The number of bins used by this algorithm is no more than twice the optimal number of bins. In other words, it is impossible for 2 bins to be at most half full because such a possibility implies that at some point, exactly one bin was at most half full and a new one was opened to accommodate an item of size at most
B
/
2
{\displaystyle B/2}
. But since the first one has at least a space of
B
/
2
{\displaystyle B/2}
, the algorithm will not open a new bin for any item whose size is at most
B
/
2
{\displaystyle B/2}
. Only after the bin fills with more than
B
/
2
{\displaystyle B/2}
or if an item with a size larger than
B
/
2
{\displaystyle B/2}
arrives, the algorithm may open a new bin. Thus if we have
K
{\displaystyle K}
bins, at least
K
−
1
{\displaystyle K-1}
bins are more than half full. Therefore,
∑
i
∈
I
s
(
i
)
>
K
−
1
2
B
{\displaystyle \sum _{i\in I}s(i)>{\tfrac {K-1}{2}}B}
. Because
∑
i
∈
I
s
(
i
)
B
{\displaystyle {\tfrac {\sum _{i\in I}s(i)}{B}}}
is a lower bound of the optimum value
O
P
T
{\displaystyle \mathrm {OPT} }
, we get that
K
−
1
<
2
O
P
T
{\displaystyle K-1<2\mathrm {OPT} }
and therefore
K
≤
2
O
P
T
{\displaystyle K\leq 2\mathrm {OPT} }
.: 74
= Lower bound
=For each
N
∈
N
{\displaystyle N\in \mathbb {N} }
, there exists a list
L
{\displaystyle L}
such that
O
P
T
(
L
)
=
N
{\displaystyle \mathrm {OPT} (L)=N}
and
N
F
(
L
)
=
2
⋅
O
P
T
(
L
)
−
2
{\displaystyle NF(L)=2\cdot \mathrm {OPT} (L)-2}
.
The family of lists for which it holds that
N
F
(
L
)
=
2
⋅
O
P
T
(
L
)
−
2
{\displaystyle NF(L)=2\cdot \mathrm {OPT} (L)-2}
is given by
L
:=
(
1
2
,
1
2
(
N
−
1
)
,
1
2
,
1
2
(
N
−
1
)
,
…
,
1
2
,
1
2
(
N
−
1
)
)
{\displaystyle L:=\left({\frac {1}{2}},{\frac {1}{2(N-1)}},{\frac {1}{2}},{\frac {1}{2(N-1)}},\dots ,{\frac {1}{2}},{\frac {1}{2(N-1)}}\right)}
with
|
L
|
=
4
(
N
−
1
)
{\displaystyle |L|=4(N-1)}
. The optimal solution for this list has
N
−
1
{\displaystyle N-1}
bins containing two items with size
1
/
2
{\displaystyle 1/2}
and one bin with
2
(
N
−
1
)
{\displaystyle 2(N-1)}
items with size
1
/
2
(
N
−
1
)
{\displaystyle 1/2(N-1)}
(i.e.,
N
{\displaystyle N}
bins total), while the solution generated by NF has
2
(
N
−
1
)
{\displaystyle 2(N-1)}
bins with one item of size
1
/
2
{\displaystyle 1/2}
and one item with size
1
/
(
2
(
N
−
1
)
)
{\displaystyle 1/(2(N-1))}
.
= Bounded item size
=If the maximum size of an item is
α
{\displaystyle \alpha }
, then the asymptotic approximation ratio ratio
R
N
F
∞
{\displaystyle R_{NF}^{\infty }}
satisfies:
R
N
F
∞
(
size
≤
α
)
≤
2
{\displaystyle R_{NF}^{\infty }({\text{size}}\leq \alpha )\leq 2}
for all
α
≥
1
/
2
{\displaystyle \alpha \geq 1/2}
;
R
N
F
∞
(
size
≤
α
)
≤
1
/
(
1
−
α
)
{\displaystyle R_{NF}^{\infty }({\text{size}}\leq \alpha )\leq 1/(1-\alpha )}
for all
α
≤
1
/
2
{\displaystyle \alpha \leq 1/2}
.
Other properties
Next-Fit packs a list and its inverse into the same number of bins.
Next-k-Fit (NkF)
Next-k-Fit is a variant of Next-Fit, but instead of keeping only one bin open, the algorithm keeps the last
k
{\displaystyle k}
bins open and chooses the first bin in which the item fits.
For
k
≥
2
{\displaystyle k\geq 2}
, NkF delivers results that are improved compared to the results of NF, however, increasing
k
{\displaystyle k}
to constant values larger than
2
{\displaystyle 2}
improves the algorithm no further in its worst-case behavior. If algorithm
A
{\displaystyle A}
is an AlmostAnyFit-algorithm and
m
=
⌊
1
/
α
⌋
≥
2
{\displaystyle m=\lfloor 1/\alpha \rfloor \geq 2}
then
R
A
∞
(
size
≤
α
)
≤
R
N
2
F
∞
(
size
≤
α
)
=
1
+
1
/
m
{\displaystyle R_{A}^{\infty }({\text{size}}\leq \alpha )\leq R_{N2F}^{\infty }({\text{size}}\leq \alpha )=1+1/m}
.
See also
Next-fit-decreasing (NFD) is the offline variant of Next-Fit: it accepts all input items, orders them by descending size, and calls Next-Fit. Its asymptotic approximation ratio is much better: less than 1.7, instead of 2.
References
Kata Kunci Pencarian:
- Next-fit bin packing
- Next-fit-decreasing bin packing
- Bin packing problem
- Best-fit bin packing
- First-fit bin packing
- Harmonic bin packing
- High-multiplicity bin packing
- Karmarkar–Karp bin packing algorithms
- Multifit algorithm
- Garbage truck