题目
The following list of eleven numbers is to be packed into bins of size 14
(a) Use the first-fit bin packing algorithm to determine how the eleven numbers listed above can be packed into bins of size 14.
(b) The list of numbers is to be sorted into ascending order. Use a quick sort to obtain the sorted list. You should show the result of each pass and identify your pivots clearly.
(c) Apply the first-fit decreasing bin packing algorithm to the sorted list to pack the numbers into bins of size 14.
(d) Explain why the number of bins used in part (c) is optimal.
(e) Use the binary search algorithm to try to locate 3.0 in the list of numbers. Clearly indicate how you choose your pivots and which part of the list is rejected at each stage.
题目中文翻译
以下十一个数字列表需要装入容量为 14 的箱子中
(a) 使用首次适应装箱算法确定上述十一个数字如何装入容量为 14 的箱子。
(b) 数字列表需要按升序排列。使用快速排序获得排序后的列表。应显示每轮后的结果并清楚标明基准元素。
(c) 对排序后的列表应用首次适应递减装箱算法,将数字装入容量为 14 的箱子。
(d) 解释为什么 (c) 中使用的箱子数量是最优的。
(e) 使用二分搜索算法尝试在数字列表中定位 3.0。清楚说明如何选择基准元素以及每个阶段被排除的列表部分。
解答
(a)
解法一
思路
展开
保持原顺序逐个处理数字。每个数字都从第一个箱子开始检查,并放入第一个剩余容量足够的箱子;只有所有已有箱子都放不下时才开启新箱子。
答题过程
展开
Applying first-fit in the given order gives
| Number | First available bin | Load after placement |
|---|---|---|
| 5.2 | 1 | 5.2 |
| 4.7 | 1 | 9.9 |
| 6.5 | 2 | 6.5 |
| 4.5 | 2 | 11.0 |
| 3.1 | 1 | 13.0 |
| 5.1 | 3 | 5.1 |
| 1.8 | 2 | 12.8 |
| 2.9 | 3 | 8.0 |
| 3.4 | 3 | 11.4 |
| 3.8 | 4 | 3.8 |
| 1.2 | 2 | 14.0 |
Thus the packing is
| Bin | Numbers | Total |
|---|---|---|
| 1 | 5.2, 4.7, 3.1 | 13.0 |
| 2 | 6.5, 4.5, 1.8, 1.2 | 14.0 |
| 3 | 5.1, 2.9, 3.4 | 11.4 |
| 4 | 3.8 | 3.8 |
(b)
解法一
思路
展开
采用官方评分资料中的 middle-right 规则:偶数长度子列选择中间两个元素中的右侧元素作基准。每轮同时处理所有尚未排好的子列,把较小元素放在基准左侧、较大元素放在右侧,直至得到升序列表。
答题过程
展开
Using middle-right pivots, begin with pivot 5.1.
Pass 1:
4.7 4.5 3.1 1.8 2.9 3.4 3.8 1.2 |5.1| 5.2 6.5
The next pivots are 2.9 and 6.5.
Pass 2:
1.8 1.2 |2.9| 4.7 4.5 3.1 3.4 3.8 |5.1| 5.2 |6.5|
The next nontrivial pivots are 1.2 and 3.1; the one-element partition 5.2 is already sorted.
Pass 3:
|1.2| 1.8 |2.9| |3.1| 4.7 4.5 3.4 3.8 |5.1| 5.2 6.5
The next nontrivial pivot is 3.4; the one-element partition 1.8 is already sorted.
Pass 4:
1.2 1.8 2.9 3.1 |3.4| 4.7 4.5 3.8 5.1 5.2 6.5
The next pivot is 4.5.
Pass 5:
1.2 1.8 2.9 3.1 3.4 3.8 |4.5| 4.7 5.1 5.2 6.5
Hence the ascending list is
解法二
思路
展开
也可采用官方认可的 middle-left 规则:偶数长度子列选择中间两个元素中的左侧元素。后续每个子列持续使用同一规则,也会得到相同的升序结果。
答题过程
展开
Using middle-left pivots, begin with pivot 5.1.
Pass 1:
4.7 4.5 3.1 1.8 2.9 3.4 3.8 1.2 |5.1| 5.2 6.5
The next pivots are 1.8 and 5.2.
Pass 2:
1.2 |1.8| 4.7 4.5 3.1 2.9 3.4 3.8 |5.1| |5.2| 6.5
The next nontrivial pivot is 3.1; the one-element partitions 1.2 and 6.5 are already sorted.
Pass 3:
1.2 1.8 2.9 |3.1| 4.7 4.5 3.4 3.8 5.1 5.2 6.5
The next nontrivial pivot is 4.5; the one-element partition 2.9 is already sorted.
Pass 4:
1.2 1.8 2.9 3.1 3.4 3.8 |4.5| 4.7 5.1 5.2 6.5
The next nontrivial pivot is 3.4; the one-element partition 4.7 is already sorted.
Pass 5:
1.2 1.8 2.9 3.1 |3.4| 3.8 4.5 4.7 5.1 5.2 6.5
Therefore,
(c)
解法一
思路
展开
首次适应递减算法先把数字改为降序,再按首次适应规则依次装箱。每个数字仍须从第一个箱子开始检查,不能任意重排以凑出组合。
答题过程
展开
Use the descending order
First-fit decreasing gives
| Bin | Numbers | Total |
|---|---|---|
| 1 | 6.5, 5.2, 1.8 | 13.5 |
| 2 | 5.1, 4.7, 3.8 | 13.6 |
| 3 | 4.5, 3.4, 3.1, 2.9 | 13.9 |
| 4 | 1.2 | 1.2 |
(d)
解法一
思路
展开
用全部数字的总和除以每箱容量,得到任何装箱方案都不能突破的箱数下界。若此下界向上取整正好等于 (c) 使用的箱数,就证明该方案最优。
答题过程
展开
The total size of all the items is 42.2, and
Therefore at least four bins are required. Since the packing in part (c) uses four bins, it is optimal.
(e)
解法一
思路
展开
在 (b) 的升序列表中进行二分搜索。每次选择当前子列的中间项;若剩余项数为偶数,则采用 middle-right。将 3.0 与基准比较并舍弃不可能包含它的一半,直到只剩一个仍不相等的数。
答题过程
展开
Number the ascending list from 1 to 11.
- The first pivot is item 6, which is 3.8. Since , reject items 6 to 11.
- From items 1 to 5, the pivot is item 3, which is 2.9. Since , reject items 1 to 3.
- From items 4 and 5, take the middle-right pivot, item 5, which is 3.4. Since , reject item 5.
- The only remaining item is item 4, which is 3.1. Since ,