1. Introduction to the branch definition method before you need to know breadth First search breadth-first-search (BFS)
1.A vertex V0, and accesses this vertex; In order of layers, one layer at a down
2.From V0, access each of the V0 's inaccessible adjacency points w1,w2,...,wk; then, from W1,w2,..., Wk, access their inaccessible adjacency points
The width-First search algorithm (also known as breadth-first search) is one of the simplest algorithms for graph search, and this algorithm is also a prototype of many important graph algorithms. Dijkstra single-source Shortest path algorithm and prim minimum spanning tree algorithm both use the same idea as width-first search. Its alias, also known as BFS, belongs to a blind search method, which is designed to systematically unfold and examine all the nodes in the graph to find the results. In other words, it does not take into account the possible location of the results and thoroughly searches the entire graph until the results are found.
BFS, whose English name is breadth first Search. BFS does not use the rule of thumb algorithm. From the point of view of the algorithm, all child nodes resulting from the expansion of nodes are added to a FIFO queue. In a general experiment, a node whose neighbor node has not been tested is placed in a container called open (such as a queue or a list), a vector or a small Gangan of the optimal value, and the tested node is placed in a container called closed. (open-closed table)
The judgment repetition of breadth search if direct judgment is time consuming, we generally use a hash table to optimize the complexity of time.
Branch-defining method is generally the optimization of breadth-first search
2. Branch Definition method
The branch-defining algorithm is a solution to search for problems in the solution space tree of the problem. Generally, if there is n layer, then the leaf node of the solution space tree has 2 power N, then the branch definition method is to use the finite conditions, constantly pruning the solution space tree, until the last layer.
The branch definition method searches the solution space tree by the method of breadth first or least cost first. Also, in the decomposition definition method, only one chance of each Slipknot point becomes an extension node. His search strategy is to:
1. Generate all child nodes of the current extension node, and delete the current expansion node in the Slipknot point list; for the general optimal value and resource problem is two fork tree, for the shortest path such graph theory problem, is the general tree
2. In the resulting child node, cut out those nodes that are not likely to produce a viable solution (or optimal solution);
3. Add all the remaining child nodes to the list of live nodes;
4. Select the next Slipknot point from the Slipknot Point table as the new expansion node;
5. Encounter layer end tag (different data structure implementation may be different, add new closing tag)
This loops until a workable solution (or optimal solution) is found for the problem, or the Slipknot point table is empty.
The idea of branch-defining method is to determine the upper and lower bounds of the target value first, and to reduce some branches of the search tree while searching, and improve the search efficiency.
The branch definition method accelerates the search efficiency of the algorithm in two aspects, one is to select a minimum cost node when selecting an extension node, and to enter the branch which is most likely to be the optimal solution as early as possible, and the other is to extend the process of the node, discarding the sub-nodes which cause the non-feasible solution or cause the suboptimal solution.
Select the next extension node e-node has the following methods
FIFO Search LIFO Search priority Queue Search For example, a container with a capacity of 10, with a weight of 4,8,5 goods, how to install the most
3. Using C + + to implement FIFO branch-defining algorithm, container problem
Now there is a container, can be installed 30 capacity, there are 10, 15, 20 of the goods, ask how to maximize the container load cargo??
We know the answer is 30.
The following uses C + + implementation algorithm
#include <iostream>#include<list>using namespacestd; #defineMAX 3Const intcap= -;//Maximum CapacityConst intbox[max]={Ten, the, -};//Three BoxesintMain () {inttemp=0, level=-1, best=0; intCurval=0, parentval=0, expectval=0; List<int>queue; Queue.push_back (-1);//-1 indicates a layerQueue.push_back (Parentval); Do{parentval=queue.front ();// first In first outQueue.pop_front ();//the first element is removed according to FIFO, and the if(parentval!=-1) { // Left ChildCurval=parentval+box[level];//one aspect of improving efficiency if(CURVAL>BEST&&CURVAL<=CAP)//The boundary condition records a maximum and the condition is within the cap and does not satisfy the pruning of the condition{ Best=Curval; Std::cout<<"Bestvalue"<<best<<Endl; //last layer node does not join queue if(level<max-1) {queue.push_back (curval); } } // Right Childtemp =0; Curval=Parentval; for(inti=level+1; i<max;i++) {Temp+=Box[i]; } expectval=curval+temp;//The expected value is not added to the value of the current level, the remaining possible maximum, the second aspect of improving efficiencystd::cout<<"Expect:"<<expectVal<<Endl; //if the maximum expected value is greater than the current best value, join the queue; if(expectval>best&&level<max-1) {queue.push_back (curval); } for(list<int>::iterator ite = Queue.begin (); Ite! = Queue.end (); ite++) {Std::cout<<" "<<*ite; } } Else//deal with hierarchy issues, join layer end flag, go to next layer { if(level<max-1) {Queue.push_back (-1); } Level++; } } while(Level!=max&&queue.empty ()! =true);//to the last layer or the queue is empty .Std::cout<<best<<Endl; System ("Pause"); return 0; }
If the subdivision, the above algorithm is actually FIFO by adding "gauge" strategy to speed up the search.
The search order still uses a FIFO branch search, but when it encounters: if the current branch's "Mount Upper bound" is smaller than the existing maximum load, the branch does not need to continue searching. "Upper bound", easy to solve, is assumed to load all items after the current item.
4. FIFO for Branch Search
There are two ships and n crates. The load capacity of the first vessel is C1, the second ship's load is C2,wi is the weight of the container I, and
W1+w2+......+wn≤c1+c2.
Determine if there is a way to ship all n containers in full. If so, find out the method.
1) Implement algorithm using FIFO branch search
#include <iostream>#include<queue>using namespacestd; intw[ -]; intN; intbestw=0; Queue<int> queue1;//store current or nodevoidAddlivenode (intWtinti) {if(i==n)//It's the leaves that need to end { if(wt>BESTW) BESTW=wt; } Else //not leaves .{queue1.push (WT); } } intMaxloading (intc) {//initializing Slipknot point queues, marking hierarchiesQueue1.push (-1); intLevel=0; intcurrentw=0; while(!Queue1.empty ()) { if(Currentw!=-1) { if(CURRENTW+W[LEVEL]<=C)//left node, limited to current + WI < C1{addlivenode (currentw+w[level],level);//item I can be loaded} addlivenode (Currentw,level); //right child is always feasible, do not load item I//Remove an e-nodeCURRENTW =Queue1.front (); Queue1.pop (); cout<<" Current"<<currentw<<Endl; } Else //the tail of the arrival layer { if(Queue1.empty ()) {cout<<" Best"<<bestw<<Endl; returnBESTW; } //Add a hierarchy tagQueue1.push (-1); //Remove an e-nodeCURRENTW =Queue1.front (); Queue1.pop (); cout<<"Current next level"<<currentw<<Endl; level++; } //EW's } } voidMain () {intC1,C2;intsum=0; scanf ("%d", &C1); scanf ("%d", &C2); scanf ("%d", &N); cout<<"N"<<n<<Endl; for(intI=0; i<n;i++) {scanf ("%d", w+i); Sum=sum+W[i]; } if(Sum<=c1 | | sum<=C2) {cout<<"need only one ship"<<Endl; return; } if(sum>c1+C2) {cout<<"No solution"<<Endl; return; } maxloading (C1); if((SUM-BESTW) <=C2) {cout<<"The first ship loading"<<bestw<<Endl; cout<<"The second ship loading"<< sum-bestw<<Endl; } Else{cout<<"No solution"<<Endl; } System ("Pause"); }
5. Optimal solution for Priority queue construction
Data structure Design:
1) in order to output solutions, we still need to generate the solution tree during the search process, and its node information includes pointers to the parent node and identifying the item trade-offs (or the left and right children of the parent node). (left node 1, right node 0)
2) The heap node includes node priority information: the upper bound of the branch of the node is uweight, and the hierarchical information (level) of the nodes can not be reflected in the heap, but only stored in the nodes;
Addlivenode is used to add Slipknot points to the subtree and to insert the Slipknot point of the Heapnode type into the maximum heap.
3) different from the algorithm 2, because the expansion node is not based on the layer, the calculation node is the branch of the load upper bound (expectval>best), to use the array variable R to record the maximum weight below the current layer, so it is convenient to use each layer node loading upper bound.
6. Complexity of the algorithm
No matter how the complexity of the algorithm is still O (2n), but through the clearance strategy, and there is no search for all nodes in the subset tree, and because each time is selected closest to the optimal solution of the node expansion, so once the search to the leaf node as an e node when the algorithm can be ended. The heap is not necessarily empty at the end of the algorithm.
Reprinted from: http://blog.csdn.net/yinlili2010/article/details/39313035
Branch definition Method Branch-and-bound analysis and Implementation) (reproduced)