The backtracking method to find the loading problem

Source: Internet
Author: User

1. Backtracking method

(1) Description: Backtracking method is an optimal search method, according to the selection criteria to search forward to achieve the goal. But when the exploration of a step, found that the original choice is not good or not reach the goal, then return to a step back to choose, such a failure to return to go back to the technology of backtracking .

(2) Principle: Backtracking method in the solution space tree of the problem, according to the depth first strategy , from the root node to search the solution space tree. When the algorithm searches to any point in the solution space tree, it first determines whether the node contains the solution of the problem. If it is not included, then the search for the subtree that the node is rooted is skipped, and its ancestor nodes are traced from layer to row; otherwise, go to the subtree and continue searching by depth-first policy .

The basic approach to backtracking is search, or a poorly organized search method that avoids unnecessary searches. This method is suitable for solving the problem that some combinatorial numbers are quite large. There are many problems with backtracking when it is necessary to find out its solution set or ask what solution is the best solution to satisfy certain constraints.

(3) The solution space of the problem

The solution vector of the problem: backtracking expects that the solution of a problem can be expressed as a form of n-ary (x1,x2,..., xn).

Explicit constraint: The value of the component Xi is qualified.

Implicit constraint: A constraint imposed on different components to satisfy the solution of a problem.

Solution space: For an example of a problem, the solution vector satisfies the explicit constraints of all multivariate groups, constituting a solution space for the instance.

Note: The same problem can be expressed in several ways, some of which are simpler and require a smaller state space (less storage and a simple search method).

(4) basic methods for generating problem states

Extension nodes: A node that is producing a son is called an extension node.
Slipknot Point: A node that has been generated by itself but whose son has not yet fully generated is called a biopsy.
Knot point: A knot that all sons have produced is called a knot point.
Depth-First problem state generation method: If an extension node R is created, once it has a son, C, it is treated as a new extension node. After completing the exhaustive search for the subtree C (subtree with root c), turn R into an extension node and continue to generate the next son of R (if present).

The problem state generation method of width first : It is an extension node until an extension node becomes a knot point.
Backtracking: In order to avoid generating problem states that are not likely to produce the best solution, it is necessary to continually use the bounding function to execute Slipknot points that are not actually possible to produce the desired solution, to reduce the computational effort of the problem. the depth-first generation method with the bound function is called backtracking .

(5) The basic idea of backtracking method

Basic idea:

A notable feature of solving problems by backtracking is that the solution space of the problem is generated dynamically during the searching process. At any point in time, the algorithm saves only the path from the root node to the current extension node. If the length of the longest path from the root node to the leaf node in the solution space tree is h (n), the computational space required for backtracking is usually O (h (n)). An O (2h (n)) or O (h (n)) is required to store the entire solution space explicitly. Memory space.

Problem Solving steps:

1) define the solution space of the problem for the given problem;
2) Determine the spatial structure of the solution that is easy to search;
3) Search the solution space in depth first, and use pruning function to avoid invalid search during the search.

Commonly used pruning function: The constraint function is used to cut off the sub-tree that does not satisfy the constraint at the extension node, and the sub-tree without the optimal solution is clipped by the limit function .

Recursive backtracking:

The backtracking method is a depth-first search for the solution space, so the backtracking method is implemented by recursive method in the general case.

 

iterative Backtracking:

using the non-recursive depth-first traversal algorithm of the tree, the backtracking method can be expressed as a non-recursive iterative process.

 

subset Tree : The corresponding solution space is called a subset tree When the given problem is to find a subset that satisfies a certain property from the set S of n elements. For example, the solution space tree for the 0-1 knapsack problem of that item is a subset tree. Such subset problems usually have 2^n leaf nodes, the total number of nodes is 2^ (n+1)-1. Any algorithm that traverses a subset tree requires an O (2^n) calculation time.

The general algorithm for traversing a subset tree by backtracking can be described as follows:

 

Arrange trees : When the given question is to determine the arrangement of n elements satisfying a certain nature, the corresponding solution space tree is called an arrangement tree . An arrangement tree usually has a n! leaf node. So traversing the arrangement tree requires O (n!) The calculation time.

The general algorithm for traversing the arrangement tree by backtracking can be described as follows:

Problem Description : There are a batch of n containers to be loaded with 2 load capacity of C1 and C2 ships, of which the weight of container I is WI, and, loading problems require to determine whether there is a reasonable loading scheme can be installed on these 2 ships. If so, find a loading scheme.

For example: When N=3,c1=c2=50 and w=[10,40,40], containers 1 and 2 can be installed on the first steamer, and container 3 is loaded on the second ship, if W=[20,40,40], the 3 containers will not be installed on the ship.

Basic idea : It is easy to prove that if a given loading problem has a solution, the following strategy can be used to obtain the optimal loading scheme.
(1) First ship as full as possible;
(2) Attach the remaining container to the second ship.
Fill the first ship as full as possible equivalent to selecting a subset of the container, so that the sub-centralized container weight of the nearest C1. The loading problem is equivalent to the following special 0-1 knapsack problem.

An O (2^n) Computational time algorithm for solving loading problems is designed by backtracking method. In some cases, the algorithm is better than the dynamic programming algorithm.

Algorithm design:

When the problem is solved by backtracking, it is obviously most suitable to use subset tree to represent its solution space. The sub-tree which does not meet the constraint condition can be cut by using the feasible constraint function. At the node z of the j+1 layer of the subset tree, the current load weight is recorded with CW, i.e., cw=, when the CW>C1, all nodes in the subtree with node Z root do not satisfy the constraints, so the solution in the subtree is not a solution, so the subtree can be cut off. ( the constraint function removes the infeasible solution and obtains all feasible solutions .)

an upper bound function can be introduced to cut down the subtree without the optimal solution, thus improving the operation efficiency of the algorithm on average . Set Z is the current extension node on the first layer of the space tree. CW is the current load capacity, BESTW is the current optimal load capacity, R is the weight of the remaining container, that is, r=. Defines the upper bound function as cw+r. The weight of any leaf node in a subtree with the root of z does not exceed cw+r. Therefore, when CW+R<=BESTW, you can cut the right subtree of Z.

    1. #include "stdafx.h"
    2. #include <iostream>
    3. Using namespace std;
    4. template<class type>
    5. Type maxloading (type w[], type C, int n, int bestx[]);
    6. int main ()
    7. {
    8. int n=3,m;
    9. int c=50,c2=50;
    10. int w[4]={0,10,40,40};
    11. int bestx[4];
    12. M=maxloading (W, c, N, BESTX);
    13. cout<<"The load capacity of the ship is:" <<endl;
    14. cout<<"C (1) =" <<c<<", C (2) =" <<c2<<endl;
    15. cout<<"The weight of the container to be loaded is:" <<endl;
    16. cout<<"W (i) =";
    17. For (int i=1;i<=n;i++)
    18. {
    19. cout<<w[i]<<"";
    20. }
    21. cout<<endl;
    22. cout<<"Retrospective selection result:" <<endl;
    23. cout<<"m (1) =" <<m<<endl;
    24. cout<<"x (i) =";
    25. For (int i=1;i<=n;i++)
    26. {
    27. cout<<bestx[i]<<"";
    28. }
    29. cout<<endl;
    30. int m2=0;
    31. For (int j=1;j<=n;j++)
    32. {
    33. m2=m2+w[j]* (1-bestx[j]);
    34. }
    35. cout<<"m (2) =" <<m2<<endl;
    36. if (M2>C2)
    37. {
    38. cout<<"because M (2) is greater than C (2), so the original problem no solution!"  "<<endl;
    39. }
    40. return 0;
    41. }
    42. Template <class type>
    43. Type maxloading (type W[],type c,int n,int bestx[])//iterative backtracking, returns the optimal load weight and its corresponding solution, initializes the root node.
    44. {
    45. int i=1; //Current layer, x[1:i-1] is the current path
    46. int *x=new int[n+1];
    47. Type bestw=0, //Current optimal load weight
    48. Cw=0, //Current load weight
    49. r=0; //remaining container weight
    50. For (int j=1;j<=n;j++)
    51. {
    52. R+=W[J];
    53. }
    54. While (true)//Search subtree
    55. {
    56. While (i<=n &&cw+w[i]<=c)//Enter the left sub-tree
    57. {
    58. R-=w[i];
    59. Cw+=w[i];
    60. X[i]=1;
    61. i++;
    62. }
    63. if (i>n)//Reach Leaf junction
    64. {
    65. For (int j=1;j<=n;j++)
    66. {
    67. BESTX[J]=X[J];
    68. }
    69. BESTW=CW;
    70. }
    71. Else//Enter Right sub-tree
    72. {
    73. R-=w[i];
    74. x[i]=0; i++;
    75. }
    76. While (CW+R<=BESTW)
    77. { //pruning backtracking
    78. i--;
    79. While (i>0 &&!x[i])
    80. {
    81. R+=w[i];
    82. i--;
    83. }
    84. //Return from right subtree
    85. if (i==0)
    86. {
    87. delete []x;
    88. return BESTW;
    89. }
    90. x[i]=0;
    91. Cw-=w[i];
    92. i++;
    93. }
    94. }
    95. }

The backtracking method to find the loading problem

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.