Loading problem | Backtracking: 01 selection (maximum pruning)

table of Contents

1. Comparison with optimal loading problem

2. How to choose the cargo of the first ship

3. Backtracking algorithm of selection tree

4. Pruning operation-optimization of backtracking algorithm


Loading problem: There are n containers to be loaded on 2 ships with deadweight c1 and c2 , where the weight of container i is wi and ∑wi <= c1 + c2.

Ask if there is a reasonable loading plan to load these n containers on these 2 ships. If so, find a loading plan.

Problem analysis: In fact, it can be understood as whether it is possible to load all the goods on the first ship and then the second ship and give a solution.

The main thing to consider is how to install the first ship? After this problem is solved, the rest can be put into the second ship. 


1. Comparison with optimal loading problem

First, let's take a look at another similar problem:

Optimal loading problem:  There is a batch of containers to be loaded on a ship with a deadweight c . Wherein the weight of the container i is the Wi . The optimal loading problem requires determining that as many containers as possible are loaded on the ship without restrictions on the loading volume .

       Obviously, the optimal loading problem can be solved greedily . Greedy choice: Choose the container with the smallest weight each time until it can't fit. 

       However, the loading problem cannot be as greedy as above ! If we let the first ship load as many containers as possible and use the above greedy selection strategy, it is easy to waste space on the first ship, and the result is not optimal. You can look at the following counterexample:


2. How to choose the cargo of the first ship

       Since it has been explained above that the first ship cannot be greedy, it will cause a waste of space and result in suboptimal results! So how should the cargo of the first ship be selected?

       It is not that the larger the quantity of goods loaded on the first ship, the better, but the greater the weight of the goods loaded on the first ship, the better. That is , the first ship should be filled as much as possible, and all the remaining cargo should be handed over to the second ship .

       Then the realization process of the first ship is actually a  backpack DP|01 backpack problem  : each cargo has only two choices of boarding or not boarding. Under the condition that the backpack size is c1, choose items with both value and weight wi , Making the maximum value possible within the capacity range!

       In addition to dynamic programming, there is actually another method, which is the backtracking algorithm. Explain in detail below!


3. Backtracking algorithm of selection tree

        Let's look at the solution of violent enumeration first : for n items, we enumerate all the n-digit binary numbers, and whether each digit corresponds to the minor container is on the ship, which includes all enumeration methods. While enumerating one by one, record the choice that can fill the ship most! ( Record the total weight under this selection in cw , and bestw record the largest cw in the loading range )

  [Take n = 3 as an example] The order of enumeration can be selected:

  • Dictionary order: 000, 001, 010, 011, 100, 101, 110, 111
  • Inverse sub-dictionary order: opposite to dictionary order
  • Gray code sequence: 000, 001, 011, 010, 110, 111, 101, 100 (reduce the calculation amount of cw)

       The shortcoming of the brute force enumeration algorithm is obvious: it traverses many unnecessary choices, but it is not easy to prune. 


        The backtracking algorithm can be said to be a rationalized realization of violent enumeration. First, draw all our enumeration schemes as a selection decision tree (as shown in the figure below, the leaf part is the total decision), each decision corresponds to an edge of the tree, and the node of the tree is the result of the selection. Then the essence of the backtracking algorithm is to search this tree deeply ~

The specific process of the backtracking algorithm is shown in the figure below, which is actually a simple dfs~ 

  • Initialization: cw = bestW = 0
  • Call: backstrack(1)

       It can be found that the tree structure is not actually reflected in the code. But the essence of our decision-making can be embodied in a tree, so the traversal of the entire code is a deep search of the tree. The backtracking algorithm is amazing! The following is an example to understand the entire backtracking process. Conditions: W[16,15,15], c = 30.


The code implementation of this backtracking algorithm:

int cw;  //当前重量
int bestW;  //最优重量
int c;   //船的最大承载量
int n;  //货物的数量
int w[100];   //对应 n 个集装箱的重量

/* 尽量装满第一艘船的回溯算法
 * step:层数 */
void backtrack(int step) {
    /* 到达了树叶 */
    if(step > n) {
        if(bestW < cw && cw <= c)
            bestW = cw;
        return;
    }

    cw += w[step];
    backtrack(step + 1);
    cw -= w[step];

    backtrack(step + 1);
}

4. Pruning operation-optimization of backtracking algorithm

        The above backtracking algorithm is not pruned, in fact, there is no difference between the overall complexity and brute force enumeration. Next, we optimize the pruning algorithm for the backtracking algorithm.


Pruning operation 1: For subtrees that do not meet our final constraints, skip traversal. 【Restrictions】


Pruning operation 2: For the subtree that cannot find the optimal solution, skip the traversal. 【Limited Conditions】

(The pseudo code below also adds the x array to record specific choices, and maintain the optimal solution while updating the optimal value)

Usually, the bounding conditions need to be constructed by ourselves. For example, r is introduced to record.


Pruning operation 3: Update the optimal value in advance.

       No longer only update the optimal value on the leaf node, update it in every other node, and return directly to the leaf node. In this way, the pruning range of the boundary condition can be wider.


Code implementation of backtracking algorithm with pruning operation:

        coding...

Guess you like

Origin blog.csdn.net/weixin_43787043/article/details/105815795