给定 n 个不同权重的物品和每个容量为 c 的箱子,将每个项目分配给一个箱子,以使总使用的箱子数量最小化。可以假设所有物品的重量都小于箱容量。
例子:
Input: weight[] = {4, 8, 1, 4, 2, 1}
Bin Capacity c = 10
Output: 2
We need minimum 2 bins to accommodate all items
First bin contains {4, 4, 2} and second bin {8, 1, 1}
Input: weight[] = {9, 8, 2, 2, 5, 4}
Bin Capacity c = 10
Output: 4
We need minimum 4 bins to accommodate all items.
Input: weight[] = {2, 5, 4, 7, 1, 3, 8};
Bin Capacity c = 10
Output: 3
下界
我们总是可以找到所需的最少 bin 数量的下限。下界可以表示为:
Min no. of bins >= Ceil ((Total Weight) / (Bin Capacity))
在上面的例子中,第一个例子的下限是“ceil(4 + 8 + 1 + 4 + 2 + 1)/10” = 2,第二个例子的下限是“ceil(9 + 8 + 2 + 2 + 5 + 4)/10” = 3。
这个问题是一个 NP Hard 问题,找到精确的最小 bin 数量需要指数时间。以下是此问题的近似算法。
应用
- 装载卡车等集装箱。
- 将数据放在多个磁盘上。
- 作业调度。
- 在固定长度的广播/电视台休息时间包装广告。
- 将大量音乐收藏到磁带/CD 等上。
在线算法
这些算法用于装箱问题,其中项目一次到达一个(以未知顺序),在考虑下一个项目之前,每个项目都必须放入一个箱子中。
1. 下一个适合:
处理下一个项目时,检查它是否与最后一个项目在同一个 bin 中。仅在没有时才使用新垃圾箱。
下面是该算法的 C++ 实现。
C++
// C++ program to find number of bins required using
// next fit algorithm.
#include
using namespace std;
// Returns number of bins required using next fit
// online algorithm
int nextFit(int weight[], int n, int c)
{
// Initialize result (Count of bins) and remaining
// capacity in current bin.
int res = 0, bin_rem = c;
// Place items one by one
for (int i = 0; i < n; i++) {
// If this item can't fit in current bin
if (weight[i] > bin_rem) {
res++; // Use a new bin
bin_rem = c - weight[i];
}
else
bin_rem -= weight[i];
}
return res;
}
// Driver program
int main()
{
int weight[] = { 2, 5, 4, 7, 1, 3, 8 };
int c = 10;
int n = sizeof(weight) / sizeof(weight[0]);
cout << "Number of bins required in Next Fit : "
<< nextFit(weight, n, c);
return 0;
}
Java
// Java program to find number
// of bins required using
// next fit algorithm.
class GFG {
// Returns number of bins required
// using next fit online algorithm
static int nextFit(int weight[], int n, int c)
{
// Initialize result (Count of bins) and remaining
// capacity in current bin.
int res = 0, bin_rem = c;
// Place items one by one
for (int i = 0; i < n; i++) {
// If this item can't fit in current bin
if (weight[i] > bin_rem) {
res++; // Use a new bin
bin_rem = c - weight[i];
}
else
bin_rem -= weight[i];
}
return res;
}
// Driver program
public static void main(String[] args)
{
int weight[] = { 2, 5, 4, 7, 1, 3, 8 };
int c = 10;
int n = weight.length;
System.out.println("Number of bins required in Next Fit : " + nextFit(weight, n, c));
}
}
// This code has been contributed by 29AjayKumar
Python3
# Python3 implementation for above approach
def nextfit(weight, c):
res = 0
rem = c
for _ in range(len(weight)):
if rem >= weight[_]:
rem = rem - weight[_]
else:
res += 1
rem = c - weight[_]
return res
# Driver Code
weight = [2, 5, 4, 7, 1, 3, 8]
c = 10
print("Number of bins required in Next Fit :",
nextfit(weight, c))
# This code is contributed by code_freak
C#
// C# program to find number
// of bins required using
// next fit algorithm.
using System;
class GFG
{
// Returns number of bins required
// using next fit online algorithm
static int nextFit(int []weight, int n, int c)
{
// Initialize result (Count of bins) and remaining
// capacity in current bin.
int res = 0, bin_rem = c;
// Place items one by one
for (int i = 0; i < n; i++)
{
// If this item can't fit in current bin
if (weight[i] > bin_rem)
{
res++; // Use a new bin
bin_rem = c - weight[i];
}
else
bin_rem -= weight[i];
}
return res;
}
// Driver program
public static void Main(String[] args)
{
int []weight = { 2, 5, 4, 7, 1, 3, 8 };
int c = 10;
int n = weight.Length;
Console.WriteLine("Number of bins required" +
" in Next Fit : " + nextFit(weight, n, c));
}
}
// This code is contributed by Rajput-Ji
Javascript
C++
// C++ program to find number of bins required using
// First Fit algorithm.
#include
using namespace std;
// Returns number of bins required using first fit
// online algorithm
int firstFit(int weight[], int n, int c)
{
// Initialize result (Count of bins)
int res = 0;
// Create an array to store remaining space in bins
// there can be at most n bins
int bin_rem[n];
// Place items one by one
for (int i = 0; i < n; i++) {
// Find the first bin that can accommodate
// weight[i]
int j;
for (j = 0; j < res; j++) {
if (bin_rem[j] >= weight[i]) {
bin_rem[j] = bin_rem[j] - weight[i];
break;
}
}
// If no bin could accommodate weight[i]
if (j == res) {
bin_rem[res] = c - weight[i];
res++;
}
}
return res;
}
// Driver program
int main()
{
int weight[] = { 2, 5, 4, 7, 1, 3, 8 };
int c = 10;
int n = sizeof(weight) / sizeof(weight[0]);
cout << "Number of bins required in First Fit : "
<< firstFit(weight, n, c);
return 0;
}
Java
// Java program to find number of bins required using
// First Fit algorithm.
class GFG
{
// Returns number of bins required using first fit
// online algorithm
static int firstFit(int weight[], int n, int c)
{
// Initialize result (Count of bins)
int res = 0;
// Create an array to store remaining space in bins
// there can be at most n bins
int []bin_rem = new int[n];
// Place items one by one
for (int i = 0; i < n; i++)
{
// Find the first bin that can accommodate
// weight[i]
int j;
for (j = 0; j < res; j++)
{
if (bin_rem[j] >= weight[i])
{
bin_rem[j] = bin_rem[j] - weight[i];
break;
}
}
// If no bin could accommodate weight[i]
if (j == res)
{
bin_rem[res] = c - weight[i];
res++;
}
}
return res;
}
// Driver program
public static void main(String[] args)
{
int weight[] = { 2, 5, 4, 7, 1, 3, 8 };
int c = 10;
int n = weight.length;
System.out.print("Number of bins required in First Fit : "
+ firstFit(weight, n, c));
}
}
// This code is contributed by Rajput-Ji
Python3
# Python program to find number of bins required using
# First Fit algorithm.
# Returns number of bins required using first fit
# online algorithm
def firstFit(weight, n, c):
# Initialize result (Count of bins)
res = 0
# Create an array to store remaining space in bins
# there can be at most n bins
bin_rem = [0]*n
# Place items one by one
for i in range(n):
# Find the first bin that can accommodate
# weight[i]
j = 0
while( j < res):
if (bin_rem[j] >= weight[i]):
bin_rem[j] = bin_rem[j] - weight[i]
break
j+=1
# If no bin could accommodate weight[i]
if (j == res):
bin_rem[res] = c - weight[i]
res= res+1
return res
# Driver program
weight = [2, 5, 4, 7, 1, 3, 8]
c = 10
n = len(weight)
print("Number of bins required in First Fit : ",firstFit(weight, n, c))
# This code is contributed by shubhamsingh10
C#
// C# program to find number of bins required using
// First Fit algorithm.
using System;
class GFG
{
// Returns number of bins required using first fit
// online algorithm
static int firstFit(int []weight, int n, int c)
{
// Initialize result (Count of bins)
int res = 0;
// Create an array to store remaining space in bins
// there can be at most n bins
int []bin_rem = new int[n];
// Place items one by one
for (int i = 0; i < n; i++)
{
// Find the first bin that can accommodate
// weight[i]
int j;
for (j = 0; j < res; j++)
{
if (bin_rem[j] >= weight[i])
{
bin_rem[j] = bin_rem[j] - weight[i];
break;
}
}
// If no bin could accommodate weight[i]
if (j == res)
{
bin_rem[res] = c - weight[i];
res++;
}
}
return res;
}
// Driver code
public static void Main(String[] args)
{
int []weight = { 2, 5, 4, 7, 1, 3, 8 };
int c = 10;
int n = weight.Length;
Console.Write("Number of bins required in First Fit : "
+ firstFit(weight, n, c));
}
}
// This code is contributed by 29AjayKumar
Javascript
C++
// C++ program to find number
// of bins required using
// Best fit algorithm.
#include
using namespace std;
// Returns number of bins required using best fit
// online algorithm
int bestFit(int weight[], int n, int c)
{
// Initialize result (Count of bins)
int res = 0;
// Create an array to store
// remaining space in bins
// there can be at most n bins
int bin_rem[n];
// Place items one by one
for (int i = 0; i < n; i++) {
// Find the best bin that can accommodate
// weight[i]
int j;
// Initialize minimum space left and index
// of best bin
int min = c + 1, bi = 0;
for (j = 0; j < res; j++) {
if (bin_rem[j] >= weight[i] && bin_rem[j] -
weight[i] < min) {
bi = j;
min = bin_rem[j] - weight[i];
}
}
// If no bin could accommodate weight[i],
// create a new bin
if (min == c + 1) {
bin_rem[res] = c - weight[i];
res++;
}
else // Assign the item to best bin
bin_rem[bi] -= weight[i];
}
return res;
}
// Driver program
int main()
{
int weight[] = { 2, 5, 4, 7, 1, 3, 8 };
int c = 10;
int n = sizeof(weight) / sizeof(weight[0]);
cout << "Number of bins required in Best Fit : "
<< bestFit(weight, n, c);
return 0;
}
Java
// Java program to find number
// of bins required using
// Best fit algorithm.
class GFG
{
// Returns number of bins
// required using best fit
// online algorithm
static int bestFit(int weight[], int n, int c)
{
// Initialize result (Count of bins)
int res = 0;
// Create an array to store
// remaining space in bins
// there can be at most n bins
int []bin_rem = new int[n];
// Place items one by one
for (int i = 0; i < n; i++)
{
// Find the best bin that
// can accommodate
// weight[i]
int j;
// Initialize minimum space
// left and index
// of best bin
int min = c + 1, bi = 0;
for (j = 0; j < res; j++)
{
if (bin_rem[j] >= weight[i] &&
bin_rem[j] - weight[i] < min)
{
bi = j;
min = bin_rem[j] - weight[i];
}
}
// If no bin could accommodate weight[i],
// create a new bin
if (min == c + 1)
{
bin_rem[res] = c - weight[i];
res++;
}
else // Assign the item to best bin
bin_rem[bi] -= weight[i];
}
return res;
}
// Driver code
public static void main(String[] args)
{
int []weight = { 2, 5, 4, 7, 1, 3, 8 };
int c = 10;
int n = weight.length;
System.out.print("Number of bins required in Best Fit : "
+ bestFit(weight, n, c));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 program to find number
# of bins required using
# First Fit algorithm.
# Returns number of bins required
# using first fit
# online algorithm
def firstFit(weight, n, c):
# Initialize result (Count of bins)
res = 0;
# Create an array to store
# remaining space in bins
# there can be at most n bins
bin_rem = [0]*n;
# Place items one by one
for i in range(n):
# Find the first bin that
# can accommodate
# weight[i]
j = 0;
# Initialize minimum space
# left and index
# of best bin
min = c + 1;
bi = 0;
for j in range(res):
if (bin_rem[j] >= weight[i] and bin_rem[j] -
weight[i] < min):
bi = j;
min = bin_rem[j] - weight[i];
# If no bin could accommodate weight[i],
# create a new bin
if (min == c + 1):
bin_rem[res] = c - weight[i];
res += 1;
else: # Assign the item to best bin
bin_rem[bi] -= weight[i];
return res;
# Driver code
if __name__ == '__main__':
weight = [ 2, 5, 4, 7, 1, 3, 8 ];
c = 10;
n = len(weight);
print("Number of bins required in First Fit : ",
firstFit(weight, n, c));
# This code is contributed by Rajput-Ji
C#
// C# program to find number
// of bins required using
// Best fit algorithm.
using System;
class GFG {
// Returns number of bins
// required using best fit
// online algorithm
static int bestFit(int[] weight, int n, int c)
{
// Initialize result (Count of bins)
int res = 0;
// Create an array to store
// remaining space in bins
// there can be at most n bins
int[] bin_rem = new int[n];
// Place items one by one
for (int i = 0; i < n; i++) {
// Find the best bin that
// can accommodate
// weight[i]
int j;
// Initialize minimum space
// left and index
// of best bin
int min = c + 1, bi = 0;
for (j = 0; j < res; j++) {
if (bin_rem[j] >= weight[i]
&& bin_rem[j] - weight[i] < min) {
bi = j;
min = bin_rem[j] - weight[i];
}
}
// If no bin could accommodate weight[i],
// create a new bin
if (min == c + 1) {
bin_rem[res] = c - weight[i];
res++;
}
// Assign the item to best bin
else
bin_rem[bi] -= weight[i];
}
return res;
}
// Driver code
public static void Main(String[] args)
{
int[] weight = { 2, 5, 4, 7, 1, 3, 8 };
int c = 10;
int n = weight.Length;
Console.Write(
"Number of bins required in Best Fit : "
+ bestFit(weight, n, c));
}
}
// This code is contributed by 29AjayKumar
Javascript
C++
// C++ program to find number of bins required using
// Worst fit algorithm.
#include
using namespace std;
// Returns number of bins required using worst fit
// online algorithm
int worstFit(int weight[], int n, int c)
{
// Initialize result (Count of bins)
int res = 0;
// Create an array to store remaining space in bins
// there can be at most n bins
int bin_rem[n];
// Place items one by one
for (int i = 0; i < n; i++) {
// Find the best bin that ca\n accommodate
// weight[i]
int j;
// Initialize maximum space left and index
// of worst bin
int mx = -1, wi = 0;
for (j = 0; j < res; j++) {
if (bin_rem[j] >= weight[i] && bin_rem[j] - weight[i] > mx) {
wi = j;
mx = bin_rem[j] - weight[i];
}
}
// If no bin could accommodate weight[i],
// create a new bin
if (mx == -1) {
bin_rem[res] = c - weight[i];
res++;
}
else // Assign the item to best bin
bin_rem[wi] -= weight[i];
}
return res;
}
// Driver program
int main()
{
int weight[] = { 2, 5, 4, 7, 1, 3, 8 };
int c = 10;
int n = sizeof(weight) / sizeof(weight[0]);
cout << "Number of bins required in Worst Fit : "
<< worstFit(weight, n, c);
return 0;
}
// This code is contributed by gromperen
Java
// Java program to find number of bins required using
// Worst fit algorithm.
class GFG
{
// Returns number of bins required using worst fit
// online algorithm
static int worstFit(int weight[], int n, int c)
{
// Initialize result (Count of bins)
int res = 0;
// Create an array to store remaining space in bins
// there can be at most n bins
int bin_rem[]= new int[n];
// Place items one by one
for (int i = 0; i < n; i++)
{
// Find the best bin that ca\n accommodate
// weight[i]
int j;
// Initialize maximum space left and index
// of worst bin
int mx = -1, wi = 0;
for (j = 0; j < res; j++) {
if (bin_rem[j] >= weight[i] && bin_rem[j] - weight[i] > mx) {
wi = j;
mx = bin_rem[j] - weight[i];
}
}
// If no bin could accommodate weight[i],
// create a new bin
if (mx == -1) {
bin_rem[res] = c - weight[i];
res++;
}
else // Assign the item to best bin
bin_rem[wi] -= weight[i];
}
return res;
}
// Driver program
public static void main(String[] args)
{
int weight[] = { 2, 5, 4, 7, 1, 3, 8 };
int c = 10;
int n = weight.length;
System.out.print("Number of bins required in Worst Fit : " +worstFit(weight, n, c));
}
}
// This code is contributed by shivanisinghss2110
C#
// C# program to find number of bins required using
// Worst fit algorithm.
using System;
class GFG
{
// Returns number of bins required using worst fit
// online algorithm
static int worstFit(int []weight, int n, int c)
{
// Initialize result (Count of bins)
int res = 0;
// Create an array to store remaining space in bins
// there can be at most n bins
int []bin_rem= new int[n];
// Place items one by one
for (int i = 0; i < n; i++)
{
// Find the best bin that ca\n accommodate
// weight[i]
int j;
// Initialize maximum space left and index
// of worst bin
int mx = -1, wi = 0;
for (j = 0; j < res; j++) {
if (bin_rem[j] >= weight[i] && bin_rem[j] - weight[i] > mx) {
wi = j;
mx = bin_rem[j] - weight[i];
}
}
// If no bin could accommodate weight[i],
// create a new bin
if (mx == -1) {
bin_rem[res] = c - weight[i];
res++;
}
else // Assign the item to best bin
bin_rem[wi] -= weight[i];
}
return res;
}
// Driver program
public static void Main(String[] args)
{
int []weight = { 2, 5, 4, 7, 1, 3, 8 };
int c = 10;
int n = weight.Length;
Console.Write("Number of bins required in Worst Fit : " +worstFit(weight, n, c));
}
}
// This code is contributed by shivanisinghss2110
Javascript
C++
// C++ program to find number of bins required using
// First Fit Decreasing algorithm.
#include
using namespace std;
/* Copy firstFit() from above */
// Returns number of bins required using first fit
// decreasing offline algorithm
int firstFitDec(int weight[], int n, int c)
{
// First sort all weights in decreasing order
sort(weight, weight + n, std::greater());
// Now call first fit for sorted items
return firstFit(weight, n, c);
}
// Driver program
int main()
{
int weight[] = { 2, 5, 4, 7, 1, 3, 8 };
int c = 10;
int n = sizeof(weight) / sizeof(weight[0]);
cout << "Number of bins required in First Fit "
<< "Decreasing : " << firstFitDec(weight, n, c);
return 0;
}
Java
// Java program to find number of bins required using
// First Fit Decreasing algorithm.
import java.util.*;
class GFG
{
/* Copy firstFit() from above */
// Returns number of bins required using first fit
// decreasing offline algorithm
static int firstFitDec(Integer weight[], int n, int c)
{
// First sort all weights in decreasing order
Arrays.sort(weight, Collections.reverseOrder());
// Now call first fit for sorted items
return firstFit(weight, n, c);
}
// Driver code
public static void main(String[] args)
{
Integer weight[] = { 2, 5, 4, 7, 1, 3, 8 };
int c = 10;
int n = weight.length;
System.out.print("Number of bins required in First Fit " + "Decreasing : "
+ firstFitDec(weight, n, c));
}
}
// This code is contributed by Rajput-Ji
C#
// C# program to find number of bins required using
// First Fit Decreasing algorithm.
using System;
public class GFG
{
/* Copy firstFit() from above */
// Returns number of bins required using first fit
// decreasing offline algorithm
static int firstFitDec(int []weight, int n, int c)
{
// First sort all weights in decreasing order
Array.Sort(weight);
Array.Reverse(weight);
// Now call first fit for sorted items
return firstFit(weight, n, c);
}
static int firstFit(int []weight, int n, int c)
{
// Initialize result (Count of bins)
int res = 0;
// Create an array to store remaining space in bins
// there can be at most n bins
int []bin_rem = new int[n];
// Place items one by one
for (int i = 0; i < n; i++)
{
// Find the first bin that can accommodate
// weight[i]
int j;
for (j = 0; j < res; j++)
{
if (bin_rem[j] >= weight[i])
{
bin_rem[j] = bin_rem[j] - weight[i];
break;
}
}
// If no bin could accommodate weight[i]
if (j == res)
{
bin_rem[res] = c - weight[i];
res++;
}
}
return res;
}
// Driver code
public static void Main(String[] args)
{
int []weight = { 2, 5, 4, 7, 1, 3, 8 };
int c = 10;
int n = weight.Length;
Console.Write("Number of bins required in First Fit " + "Decreasing : "
+ firstFitDec(weight, n, c));
}
}
// This code is contributed by 29AjayKumar
输出:
Number of bins required in Next Fit : 4
Next Fit 是一个简单的算法。它只需要 O(n) 时间和 O(1) 额外空间来处理 n 个项目。
Next Fit 为 2 近似值,即该算法使用的 bin 数量以两倍的最优值为界。考虑任意两个相邻的 bin。这两个 bin 中的项目总和必须 > c;否则,NextFit 会将第二个垃圾箱的所有项目放入第一个垃圾箱。这同样适用于所有其他垃圾箱。因此,最多浪费了一半的空间,因此如果 M 是最佳的,Next Fit 最多使用 2M 个 bin。
2. 首次拟合:
处理下一个项目时,按顺序扫描前面的垃圾箱,并将该项目放入适合的第一个垃圾箱。仅当它不适合任何现有垃圾箱时才启动新垃圾箱。
C++
// C++ program to find number of bins required using
// First Fit algorithm.
#include
using namespace std;
// Returns number of bins required using first fit
// online algorithm
int firstFit(int weight[], int n, int c)
{
// Initialize result (Count of bins)
int res = 0;
// Create an array to store remaining space in bins
// there can be at most n bins
int bin_rem[n];
// Place items one by one
for (int i = 0; i < n; i++) {
// Find the first bin that can accommodate
// weight[i]
int j;
for (j = 0; j < res; j++) {
if (bin_rem[j] >= weight[i]) {
bin_rem[j] = bin_rem[j] - weight[i];
break;
}
}
// If no bin could accommodate weight[i]
if (j == res) {
bin_rem[res] = c - weight[i];
res++;
}
}
return res;
}
// Driver program
int main()
{
int weight[] = { 2, 5, 4, 7, 1, 3, 8 };
int c = 10;
int n = sizeof(weight) / sizeof(weight[0]);
cout << "Number of bins required in First Fit : "
<< firstFit(weight, n, c);
return 0;
}
Java
// Java program to find number of bins required using
// First Fit algorithm.
class GFG
{
// Returns number of bins required using first fit
// online algorithm
static int firstFit(int weight[], int n, int c)
{
// Initialize result (Count of bins)
int res = 0;
// Create an array to store remaining space in bins
// there can be at most n bins
int []bin_rem = new int[n];
// Place items one by one
for (int i = 0; i < n; i++)
{
// Find the first bin that can accommodate
// weight[i]
int j;
for (j = 0; j < res; j++)
{
if (bin_rem[j] >= weight[i])
{
bin_rem[j] = bin_rem[j] - weight[i];
break;
}
}
// If no bin could accommodate weight[i]
if (j == res)
{
bin_rem[res] = c - weight[i];
res++;
}
}
return res;
}
// Driver program
public static void main(String[] args)
{
int weight[] = { 2, 5, 4, 7, 1, 3, 8 };
int c = 10;
int n = weight.length;
System.out.print("Number of bins required in First Fit : "
+ firstFit(weight, n, c));
}
}
// This code is contributed by Rajput-Ji
蟒蛇3
# Python program to find number of bins required using
# First Fit algorithm.
# Returns number of bins required using first fit
# online algorithm
def firstFit(weight, n, c):
# Initialize result (Count of bins)
res = 0
# Create an array to store remaining space in bins
# there can be at most n bins
bin_rem = [0]*n
# Place items one by one
for i in range(n):
# Find the first bin that can accommodate
# weight[i]
j = 0
while( j < res):
if (bin_rem[j] >= weight[i]):
bin_rem[j] = bin_rem[j] - weight[i]
break
j+=1
# If no bin could accommodate weight[i]
if (j == res):
bin_rem[res] = c - weight[i]
res= res+1
return res
# Driver program
weight = [2, 5, 4, 7, 1, 3, 8]
c = 10
n = len(weight)
print("Number of bins required in First Fit : ",firstFit(weight, n, c))
# This code is contributed by shubhamsingh10
C#
// C# program to find number of bins required using
// First Fit algorithm.
using System;
class GFG
{
// Returns number of bins required using first fit
// online algorithm
static int firstFit(int []weight, int n, int c)
{
// Initialize result (Count of bins)
int res = 0;
// Create an array to store remaining space in bins
// there can be at most n bins
int []bin_rem = new int[n];
// Place items one by one
for (int i = 0; i < n; i++)
{
// Find the first bin that can accommodate
// weight[i]
int j;
for (j = 0; j < res; j++)
{
if (bin_rem[j] >= weight[i])
{
bin_rem[j] = bin_rem[j] - weight[i];
break;
}
}
// If no bin could accommodate weight[i]
if (j == res)
{
bin_rem[res] = c - weight[i];
res++;
}
}
return res;
}
// Driver code
public static void Main(String[] args)
{
int []weight = { 2, 5, 4, 7, 1, 3, 8 };
int c = 10;
int n = weight.Length;
Console.Write("Number of bins required in First Fit : "
+ firstFit(weight, n, c));
}
}
// This code is contributed by 29AjayKumar
Javascript
输出:
Number of bins required in First Fit : 4
上述 First Fit 的实现需要 O(n 2 ) 时间,但 First Fit 可以使用自平衡二叉搜索树在 O(n Log n) 时间内实现。
如果 M 是最佳 bin 数,则 First Fit 使用的 bin 永远不会超过 1.7M。因此,就 bin 数量的上限而言,First-Fit 优于 Next Fit。
3. 最适合:
这个想法是将下一个项目放在*紧*的位置。也就是说,将其放入垃圾箱中,以便留下最小的空间。
C++
// C++ program to find number
// of bins required using
// Best fit algorithm.
#include
using namespace std;
// Returns number of bins required using best fit
// online algorithm
int bestFit(int weight[], int n, int c)
{
// Initialize result (Count of bins)
int res = 0;
// Create an array to store
// remaining space in bins
// there can be at most n bins
int bin_rem[n];
// Place items one by one
for (int i = 0; i < n; i++) {
// Find the best bin that can accommodate
// weight[i]
int j;
// Initialize minimum space left and index
// of best bin
int min = c + 1, bi = 0;
for (j = 0; j < res; j++) {
if (bin_rem[j] >= weight[i] && bin_rem[j] -
weight[i] < min) {
bi = j;
min = bin_rem[j] - weight[i];
}
}
// If no bin could accommodate weight[i],
// create a new bin
if (min == c + 1) {
bin_rem[res] = c - weight[i];
res++;
}
else // Assign the item to best bin
bin_rem[bi] -= weight[i];
}
return res;
}
// Driver program
int main()
{
int weight[] = { 2, 5, 4, 7, 1, 3, 8 };
int c = 10;
int n = sizeof(weight) / sizeof(weight[0]);
cout << "Number of bins required in Best Fit : "
<< bestFit(weight, n, c);
return 0;
}
Java
// Java program to find number
// of bins required using
// Best fit algorithm.
class GFG
{
// Returns number of bins
// required using best fit
// online algorithm
static int bestFit(int weight[], int n, int c)
{
// Initialize result (Count of bins)
int res = 0;
// Create an array to store
// remaining space in bins
// there can be at most n bins
int []bin_rem = new int[n];
// Place items one by one
for (int i = 0; i < n; i++)
{
// Find the best bin that
// can accommodate
// weight[i]
int j;
// Initialize minimum space
// left and index
// of best bin
int min = c + 1, bi = 0;
for (j = 0; j < res; j++)
{
if (bin_rem[j] >= weight[i] &&
bin_rem[j] - weight[i] < min)
{
bi = j;
min = bin_rem[j] - weight[i];
}
}
// If no bin could accommodate weight[i],
// create a new bin
if (min == c + 1)
{
bin_rem[res] = c - weight[i];
res++;
}
else // Assign the item to best bin
bin_rem[bi] -= weight[i];
}
return res;
}
// Driver code
public static void main(String[] args)
{
int []weight = { 2, 5, 4, 7, 1, 3, 8 };
int c = 10;
int n = weight.length;
System.out.print("Number of bins required in Best Fit : "
+ bestFit(weight, n, c));
}
}
// This code is contributed by 29AjayKumar
蟒蛇3
# Python3 program to find number
# of bins required using
# First Fit algorithm.
# Returns number of bins required
# using first fit
# online algorithm
def firstFit(weight, n, c):
# Initialize result (Count of bins)
res = 0;
# Create an array to store
# remaining space in bins
# there can be at most n bins
bin_rem = [0]*n;
# Place items one by one
for i in range(n):
# Find the first bin that
# can accommodate
# weight[i]
j = 0;
# Initialize minimum space
# left and index
# of best bin
min = c + 1;
bi = 0;
for j in range(res):
if (bin_rem[j] >= weight[i] and bin_rem[j] -
weight[i] < min):
bi = j;
min = bin_rem[j] - weight[i];
# If no bin could accommodate weight[i],
# create a new bin
if (min == c + 1):
bin_rem[res] = c - weight[i];
res += 1;
else: # Assign the item to best bin
bin_rem[bi] -= weight[i];
return res;
# Driver code
if __name__ == '__main__':
weight = [ 2, 5, 4, 7, 1, 3, 8 ];
c = 10;
n = len(weight);
print("Number of bins required in First Fit : ",
firstFit(weight, n, c));
# This code is contributed by Rajput-Ji
C#
// C# program to find number
// of bins required using
// Best fit algorithm.
using System;
class GFG {
// Returns number of bins
// required using best fit
// online algorithm
static int bestFit(int[] weight, int n, int c)
{
// Initialize result (Count of bins)
int res = 0;
// Create an array to store
// remaining space in bins
// there can be at most n bins
int[] bin_rem = new int[n];
// Place items one by one
for (int i = 0; i < n; i++) {
// Find the best bin that
// can accommodate
// weight[i]
int j;
// Initialize minimum space
// left and index
// of best bin
int min = c + 1, bi = 0;
for (j = 0; j < res; j++) {
if (bin_rem[j] >= weight[i]
&& bin_rem[j] - weight[i] < min) {
bi = j;
min = bin_rem[j] - weight[i];
}
}
// If no bin could accommodate weight[i],
// create a new bin
if (min == c + 1) {
bin_rem[res] = c - weight[i];
res++;
}
// Assign the item to best bin
else
bin_rem[bi] -= weight[i];
}
return res;
}
// Driver code
public static void Main(String[] args)
{
int[] weight = { 2, 5, 4, 7, 1, 3, 8 };
int c = 10;
int n = weight.Length;
Console.Write(
"Number of bins required in Best Fit : "
+ bestFit(weight, n, c));
}
}
// This code is contributed by 29AjayKumar
Javascript
输出:
Number of bins required in Best Fit : 4
也可以使用自平衡二叉搜索树在 O(n Log n) 时间内实现最佳拟合。
如果 M 是最佳 bin 数,则最佳拟合永远不会使用超过 1.7M 的 bin。因此,最佳拟合与 First Fit 相同,并且在 bin 数量的上限方面优于 Next Fit。
4. 最不适合:
这个想法是将下一个项目放在最不紧张的地方,以平衡垃圾箱。也就是说,将其放入垃圾箱,以便留下最多的空白空间。
C++
// C++ program to find number of bins required using
// Worst fit algorithm.
#include
using namespace std;
// Returns number of bins required using worst fit
// online algorithm
int worstFit(int weight[], int n, int c)
{
// Initialize result (Count of bins)
int res = 0;
// Create an array to store remaining space in bins
// there can be at most n bins
int bin_rem[n];
// Place items one by one
for (int i = 0; i < n; i++) {
// Find the best bin that ca\n accommodate
// weight[i]
int j;
// Initialize maximum space left and index
// of worst bin
int mx = -1, wi = 0;
for (j = 0; j < res; j++) {
if (bin_rem[j] >= weight[i] && bin_rem[j] - weight[i] > mx) {
wi = j;
mx = bin_rem[j] - weight[i];
}
}
// If no bin could accommodate weight[i],
// create a new bin
if (mx == -1) {
bin_rem[res] = c - weight[i];
res++;
}
else // Assign the item to best bin
bin_rem[wi] -= weight[i];
}
return res;
}
// Driver program
int main()
{
int weight[] = { 2, 5, 4, 7, 1, 3, 8 };
int c = 10;
int n = sizeof(weight) / sizeof(weight[0]);
cout << "Number of bins required in Worst Fit : "
<< worstFit(weight, n, c);
return 0;
}
// This code is contributed by gromperen
Java
// Java program to find number of bins required using
// Worst fit algorithm.
class GFG
{
// Returns number of bins required using worst fit
// online algorithm
static int worstFit(int weight[], int n, int c)
{
// Initialize result (Count of bins)
int res = 0;
// Create an array to store remaining space in bins
// there can be at most n bins
int bin_rem[]= new int[n];
// Place items one by one
for (int i = 0; i < n; i++)
{
// Find the best bin that ca\n accommodate
// weight[i]
int j;
// Initialize maximum space left and index
// of worst bin
int mx = -1, wi = 0;
for (j = 0; j < res; j++) {
if (bin_rem[j] >= weight[i] && bin_rem[j] - weight[i] > mx) {
wi = j;
mx = bin_rem[j] - weight[i];
}
}
// If no bin could accommodate weight[i],
// create a new bin
if (mx == -1) {
bin_rem[res] = c - weight[i];
res++;
}
else // Assign the item to best bin
bin_rem[wi] -= weight[i];
}
return res;
}
// Driver program
public static void main(String[] args)
{
int weight[] = { 2, 5, 4, 7, 1, 3, 8 };
int c = 10;
int n = weight.length;
System.out.print("Number of bins required in Worst Fit : " +worstFit(weight, n, c));
}
}
// This code is contributed by shivanisinghss2110
C#
// C# program to find number of bins required using
// Worst fit algorithm.
using System;
class GFG
{
// Returns number of bins required using worst fit
// online algorithm
static int worstFit(int []weight, int n, int c)
{
// Initialize result (Count of bins)
int res = 0;
// Create an array to store remaining space in bins
// there can be at most n bins
int []bin_rem= new int[n];
// Place items one by one
for (int i = 0; i < n; i++)
{
// Find the best bin that ca\n accommodate
// weight[i]
int j;
// Initialize maximum space left and index
// of worst bin
int mx = -1, wi = 0;
for (j = 0; j < res; j++) {
if (bin_rem[j] >= weight[i] && bin_rem[j] - weight[i] > mx) {
wi = j;
mx = bin_rem[j] - weight[i];
}
}
// If no bin could accommodate weight[i],
// create a new bin
if (mx == -1) {
bin_rem[res] = c - weight[i];
res++;
}
else // Assign the item to best bin
bin_rem[wi] -= weight[i];
}
return res;
}
// Driver program
public static void Main(String[] args)
{
int []weight = { 2, 5, 4, 7, 1, 3, 8 };
int c = 10;
int n = weight.Length;
Console.Write("Number of bins required in Worst Fit : " +worstFit(weight, n, c));
}
}
// This code is contributed by shivanisinghss2110
Javascript
输出:
Number of bins required in Worst Fit : 4
最差拟合也可以使用自平衡二叉搜索树在 O(n Log n) 时间内实现。
如果 M 是最佳 bin 数,则最佳拟合永远不会使用超过 2M-2 个 bin。因此,就 bin 数量的上限而言,Worst Fit 与 Next Fit 相同。离线算法
在离线版本中,我们预先准备了所有项目。不幸的是离线版本也是 NP Complete,但我们有更好的近似算法。如果最优值是 M,则首次拟合递减最多使用 (4M + 1)/3 个 bin。
4.首次拟合递减:
在线算法的一个问题是打包大件物品很困难,特别是如果它们出现在序列的后期。我们可以通过对输入序列进行*排序* 并首先放置大项目来避免这种情况。通过排序,我们得到首次拟合递减和最佳拟合递减,作为在线首次拟合和最佳拟合的离线模拟。
C++
// C++ program to find number of bins required using
// First Fit Decreasing algorithm.
#include
using namespace std;
/* Copy firstFit() from above */
// Returns number of bins required using first fit
// decreasing offline algorithm
int firstFitDec(int weight[], int n, int c)
{
// First sort all weights in decreasing order
sort(weight, weight + n, std::greater());
// Now call first fit for sorted items
return firstFit(weight, n, c);
}
// Driver program
int main()
{
int weight[] = { 2, 5, 4, 7, 1, 3, 8 };
int c = 10;
int n = sizeof(weight) / sizeof(weight[0]);
cout << "Number of bins required in First Fit "
<< "Decreasing : " << firstFitDec(weight, n, c);
return 0;
}
Java
// Java program to find number of bins required using
// First Fit Decreasing algorithm.
import java.util.*;
class GFG
{
/* Copy firstFit() from above */
// Returns number of bins required using first fit
// decreasing offline algorithm
static int firstFitDec(Integer weight[], int n, int c)
{
// First sort all weights in decreasing order
Arrays.sort(weight, Collections.reverseOrder());
// Now call first fit for sorted items
return firstFit(weight, n, c);
}
// Driver code
public static void main(String[] args)
{
Integer weight[] = { 2, 5, 4, 7, 1, 3, 8 };
int c = 10;
int n = weight.length;
System.out.print("Number of bins required in First Fit " + "Decreasing : "
+ firstFitDec(weight, n, c));
}
}
// This code is contributed by Rajput-Ji
C#
// C# program to find number of bins required using
// First Fit Decreasing algorithm.
using System;
public class GFG
{
/* Copy firstFit() from above */
// Returns number of bins required using first fit
// decreasing offline algorithm
static int firstFitDec(int []weight, int n, int c)
{
// First sort all weights in decreasing order
Array.Sort(weight);
Array.Reverse(weight);
// Now call first fit for sorted items
return firstFit(weight, n, c);
}
static int firstFit(int []weight, int n, int c)
{
// Initialize result (Count of bins)
int res = 0;
// Create an array to store remaining space in bins
// there can be at most n bins
int []bin_rem = new int[n];
// Place items one by one
for (int i = 0; i < n; i++)
{
// Find the first bin that can accommodate
// weight[i]
int j;
for (j = 0; j < res; j++)
{
if (bin_rem[j] >= weight[i])
{
bin_rem[j] = bin_rem[j] - weight[i];
break;
}
}
// If no bin could accommodate weight[i]
if (j == res)
{
bin_rem[res] = c - weight[i];
res++;
}
}
return res;
}
// Driver code
public static void Main(String[] args)
{
int []weight = { 2, 5, 4, 7, 1, 3, 8 };
int c = 10;
int n = weight.Length;
Console.Write("Number of bins required in First Fit " + "Decreasing : "
+ firstFitDec(weight, n, c));
}
}
// This code is contributed by 29AjayKumar
输出:
Number of bins required in First Fit Decreasing : 3
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。