### WEBINAR:

On-Demand

Full Text Search: The Key to Better Natural Language Queries for NoSQL in Node.js

**Solving a Simple 2BP Problem**
This article discusses some approaches for solving one of the simpler variations of 2BP, using these assumptions:

- The shapes are rectangles with sides of rational lengths.
- You cannot rotate the shapes.
- The stock has a set width but is infinitely long. You can think of it as an idealized bolt of cloth or roll of paper.

One way to solve this problem is to exhaustively try every possible arrangement for the shapes. If their sides have rational lengths, then you can find the smallest distance that divides evenly into all the shapes' side lengths, and use that to determine all the positions where the shapes might need to go.

For example, if every shape has sides measured in an integer number of inches, you can divide the stock into a grid of one-inch squares. To enumerate every possible location for the shapes, you would try placing each shape so its upper-left corner lies in every square.

Here's

*why *the problem is difficult. The number of possible allowed combinations depends on the size of the stock and the exact sizes of the shapes, but you can get some idea of how big the problem is by ignoring the sizes of the shapes altogether! For example, suppose the stock is 10 inches wide, you have 8 shapes, and you consider only a 20-inch length of the stock. Then the stock contains 10 * 20 = 200 squares where you could (theoretically) place each of the 8 shapes. There are 200 possible positions for the first shape, 199 for the second, 198 for the third, and so forth. The total number of combinations is 200 * 199 * ... * (200 - 7), which is roughly 2.2E18. If you could evaluate one million of those combinations per second, it would take you more than seventy thousand years to check them all.

In practice the situation isn't quite that grim. For example, the preceding calculation doesn't exclude overlapping shapes, or that shapes placed too close to the right or bottom edges of the stock will lie partly off the edge. However, it does give you a sense of how many possible solutions there might be. If you eliminate all the illegal arrangements, you may be able to exhaustively examine all of the possible solutions for eight shapes—but you still won't be able to handle problems that are much larger. In other words, an exhaustive search is possible only when there are fewer than eight or nine shapes; for larger problems, you'll need to use

*heuristics*—algorithms that are likely to give you a good solution but are not guaranteed to produce the best possible result.

For example, one heuristic for getting somewhere as quickly as possible is to drive five miles per hour over the speed limit. There's some chance you'll get a ticket for speeding, but the odds are you're safe as long as you're not doing anything else naughty at the time. You might be able to go a little faster, but then the odds of your being pulled over increase—and a traffic ticket will definitely delay your arrival.

| |

Figure 2. **Two Columns Heuristic**: This heuristic divides the stock into two columns, placing the widest rectangles first, and attempting to place the next rectangle in the shorter of the two columns. |

The 2BP program example (see

Figure 2) that you'll find in the

downloadable code in both Visual Basic and C# versions, demonstrates several heuristics for solving 2BP. Enter the width of the stock in the "Bin W" text box on the upper left. Enter the dimensions of the shapes in the "Inputs" text box. Then click one of the buttons to run a particular algorithm.

This article describes most of these algorithms in words rather than code, but you can

download the project example and look at the code to learn the programming details.

Figure 2 shows the solution found by the "Two Columns" heuristic described in the following section. You can easily see that this solution isn't perfect. You could slide the 3x2 rectangle at the bottom into the open area to the right of the 6x6 rectangle to reduce the total height by 2 units. You could then move the 4x2 rectangle next to the 6x6 rectangle and move the 2x3 rectangle into the upper right corner, shortening the solution to a height of 14. You may want to experiment for a few minutes to see if you can come up with a better solution.

**Two Columns Heuristic**
The Two Columns heuristic is one of the classic algorithms for solving 2BP. It was written up by Stanford's Daniel Sleator in

*Information Processing Letters* way back in 1980.

First the heuristic removes any shapes that are wider than half of the stock's width and places them at the top of the stock. In

Figure 2 these are the rectangles with dimensions 8x3, 7x1, and 6x6.

Next the algorithm divides the stock into two equally sized vertical columns. It then sorts the remaining shapes by height with the tallest first.

The algorithm places the tallest shape in the first column, which begins a horizontal strip with the height of this first item within the column. The program places the subsequent shapes next to the first one (in order of decreasing height) until no more shapes will fit in the strip.

Now the program considers the two columns and determines which one has the smaller total height so far. It places the next item in that column and starts a new strip. The program continues filling strips and then starting new strips in the shorter column until it runs out of items.

Figure 2 shows the result. The red "18" at the bottom describes the total length of stock used.

This algorithm isn't the best possible alternative, but it is useful because it sets an upper bound on how tall the piece of stock must be. Sleator's paper shows that, if

Halg is the height given by the algorithm,

Hopt is the optimal solution, and

Htall is the height of the tallest rectangle, then:

```
Halg <= 2 * Hopt + Htall / 2
```

The algorithm provides only a loose boundary, but it's better than simply adding up the heights of all of the rectangles.