170 likes | 461 Views
Best-fit bin packing in O(n log n) time. The search tree will contain one element for each bin which is in use and has non-zero available capacity. Notice! It is possible that for two or more bins to have the same available capacity. Best-fit bin packing in O(n log n) time. Example
E N D
Best-fit bin packing inO(n log n) time • The search tree will contain one element for each bin which is in use and has non-zero available capacity • Notice! It is possible that for two or more bins to have the same available capacity
Best-fit bin packing inO(n log n) time • Example • Suppose that when object i is to be packed, there are nine bins (a through i) in use. • Available capacity of these bins are 1, 3, 12, 6, 8, 1, 20, 6 and 5, respectively. • The nine bins may be stored in a binary tree with duplicates, using as key the available capacity of a bin.
e 8 Best-fit bin packing inO(n log n) time • A possible binary search tree. h 6 b c 3 12 a i g d 20 1 5 6 f 1
e 8 Best-fit bin packing inO(n log n) time • Object I that is to be packed requires s[i] = 4. h • Root h fits, proceeds to the left subtree. 6 b c 3 12 a i g d 20 1 5 6 f 1
e 8 Best-fit bin packing inO(n log n) time • Object I that is to be packed requires s[i] = 4. h 6 b c 3 12 a i g d 20 1 5 6 f 1
e 8 Best-fit bin packing inO(n log n) time • Object I that is to be packed requires s[i] = 4. h • b fails, proceeds to the right subtree of b. 6 b c 3 12 a i g d 20 1 5 6 f 1
e 8 Best-fit bin packing inO(n log n) time • Object I that is to be packed requires s[i] = 4. h 6 b c 3 12 a i g d 20 1 5 6 f 1
e 8 Best-fit bin packing inO(n log n) time • Object I that is to be packed requires s[i] = 4. h • i fits, proceeds to the left subtree of i. 6 b c 3 12 a i g d 20 1 5 6 f 1 • idoesn’t have any left child, it is the best candidate.
e 8 Best-fit bin packing inO(n log n) time • Another example, object I that is to be packed requires s[i] = 7. h • h fails, proceeds to the right subtree of h. 6 b c 3 12 a i g d 20 1 5 6 f 1
e 8 Best-fit bin packing inO(n log n) time • Another example, object I that is to be packed requires s[i] = 7. h 6 b c 3 12 a i g d 20 1 5 6 f 1
Best-fit bin packing inO(n log n) time • Another example, object I that is to be packed requires s[i] = 7. h • c fits, proceeds to the left subtree of c. 6 b c 3 12 a i g d 20 1 5 6 f e 8 1
Best-fit bin packing inO(n log n) time • Another example, object I that is to be packed requires s[i] = 7. h 6 b c 3 12 a i g d 20 1 5 6 f e 8 1
Best-fit bin packing inO(n log n) time • Another example, object I that is to be packed requires s[i] = 7. h • d fails, proceeds to the right subtree of d. 6 b c 3 12 a i g d 20 1 5 6 f e 8 1
Best-fit bin packing inO(n log n) time • Another example, object I that is to be packed requires s[i] = 7. h 6 b c 3 12 a i g d 20 1 5 6 f e 8 1
Best-fit bin packing inO(n log n) time • Another example, object I that is to be packed requires s[i] = 7. h • e fits, proceeds to the left subtree of e. 6 b c 3 12 a i g d 20 1 5 6 f e 8 1 • e has no left child, so e is the best candidate.
Best-fit bin packing inO(n log n) time • When we find the best bin, we can delete it from the search tree, reduce its capacity by s[i], and reinsert it (unless its remaining capacity is zero). • If we do not find a bin with enough capacity, we can start a new bin.