You are on page 1of 13

Chapter 6.

3
More approximation algorithms

Dept of Computer Science, University of Bristol.

COMS21101. Chapter 6.3 Slide 1

 The bin-packing problem has been shown to be intractable. COMS21101. The number of possible partitions of the n objects into n or fewer subsets is exponential in n. Chapter 6. They have to be packed into bins each of which has capacity of 1.  Solution: the minimum number of bins required to pack the n objects.3 Slide 2 . University of Bristol.Bin Packing  The bin-packing problem is stated as follows:  There are n objects whose sizes range between 0 and 1.  (see Baase and Van Gelder. chapter 13) Dept of Computer Science.

University of Bristol. COMS21101.Applications of Bin-packing Packing data in computers with minimum empty storage space placing files in disk blocks/tracks program segments in memory pages Filling orders for a product to be cut from large standard-size pieces e.g. cutting lengths of fabric or timber with minimum waste Dept of Computer Science.3 Slide 3 . Chapter 6.

3 Slide 4 . COMS21101.The First-Fit Strategy Simple first-fit strategy: take the objects in any order place each object in the first bin in which it fits. University of Bristol. First-fit decreasing (FFD) strategy: Sort the objects into descending order Then apply the simple strategy above. Chapter 6. Dept of Computer Science.

2.2. 0. 0.FFD-strategy: example Given objects 0. 0.5 0.5. 0.2.3 Sorted into decreasing order 0.4 0. 0.4.2 0. 0. 0. University of Bristol.4.2 Note: this is not optimal.8.4. COMS21101.4.3 0. 0.2.8 0. 0. The objects would fit into 3 bins.4 0. 0.2. 0. 0.8. Dept of Computer Science. 0.3. Chapter 6.2 0.5. 0.3 Slide 5 .2 0.

Chapter 6.3 Slide 6 . since n2 + n log n is O(n2).FFD complexity  The objects are sorted in O(n log n)  The first fit strategy is O(n2). it usually gives solutions much closer to the optimal one.e. In practice. since each of the n objects must be ³tried´ in at most n bins. University of Bristol.  It has been proved that the FFD strategy gives a solution that is at worst about 22% sub-optimal (i. requires at most 22% more bins than necessary). Dept of Computer Science.  Hence overall complexity is O(n2). COMS21101.

Dept of Computer Science. University of Bristol. we could put it in the fullest of those bins in which it fits. it works only about as well as the FFD strategy.3 Slide 7 . COMS21101. Chapter 6. in practice. Intuitively. it attempts to fill bins as much as possible. this ³best-fit´ strategy might be expected to give better solutions.  However.Best-fit strategy Instead of placing an object in the first bin in which it fits. since.

University of Bristol. Chapter 6. COMS21101. given available resources C (e. and a set of n objects each of which has a size and a value. on a space flight). Dept of Computer Science.g. Applications:  creating an investment portfolio for a sum of money C to achieve the greatest profit.3 Slide 8 .The Knapsack problem Given a ³knapsack´ with capacity C. Find the ³most valuable´ set of objects that fit in the knapsack.  selecting the most ³profitable´ tasks.

3 Slide 9 .  Hence. Chapter 6. we consider only the size.  The general knapsack algorithm as stated above can be obtained by extending this simple version (see Baase and Van Gelder. Chapter 13). where we assume that the value of each object is proportional to its size. University of Bristol. Dept of Computer Science.An approximate knapsack algorithm  We present an algorithm for simple version of the knapsack algorithm. COMS21101.

k-th approximation  Let S be the given set of n objects. COMS21101.  For fixed k < n. for k=2.  The maximum value so obtained is the k-th approximation. we then try to fill up the knapsack by picking the objects in some fixed order until it is full.g. we first consider all subsets of S with at most k elements. Chapter 6. However. University of Bristol.  For each subset of k objects which fits in the knapsack.  Clearly. by increasing k. Let C be the size of the knapsack. we get a better and better approximation.  E. whose total size is less than C.3 Slide 10 . the complexity is O(knk+1) Dept of Computer Science. we consider all pairs of objects that fit in the knapsack.

. Chapter 6. COMS21101..} } if (maxSum < sum) {maxSum = sum} } } return maxSum.. University of Bristol.n} with k elements { sum = the sum of the values S[i] such that i  T. T = T Š {j}. j.S) // S an array with n values (the sizes) int maxSum. for each subset T  {1.. take = ˆ.3 Slide 11 . maxSum = 0. if (sum e C) { for each j not in T { if (sum + S[j] e C){ sum = sum + S[j]. // C is the size of the knapsack Set T = new Set.Algorithm for k-th approximation knapSack_k(C. Dept of Computer Science. sum.

1} {54.23.29.1} {54.3 Slide 12 .1} {54. Chapter 6.1} {54.45.14.29.Example: 1-approximation  Consider objects {54.43.1} {54.45} sum 100 100 98 107 107 105 98 100 Thus maxSum = 107. COMS21101.1}  Let C = 110  Consider all singleton sets (k=1) Subset {54} {45} {43} {29} {23} {21} {14} {1} Objects added by inner ³for´ loop {45.21. Dept of Computer Science.29.23.29.1} {54.1} {54. University of Bristol.

1} or {43.29} ..14.21.Example .29. {45.1} 107 {21.45} {54.3 Slide 13 . {23.1} 110 110 106 Since C=110. Objects added by inner ³for´ loop sum {1} 100 {23.1} {23.14} ..1} {54.29} .14.23... Some examples of the 28 pairs considered are Subset {54..1} completely fill the knapsack and hence either is optimal (assuming size and value are the same). {45.43.continued  In fact the optimal solution to the above example is found by the approximation with k=2.14. COMS21101. University of Bristol.43} {43.. Dept of Computer Science. Chapter 6.