Summary of basic algorithm series: branch Limit algorithm

Source: Internet
Author: User

Today, I am not the norm, othersAlgorithmSeriesArticleI first introduced the theory of the algorithm, and then talked about the specific problem. Then someone gave me a response. For those who just looked at it, I am particularly disgusted with the theoretical text I posted elsewhere, and I don't want to continue reading it below. For the branch Limit algorithm, I use the problem-first summary method. First, let's focus on the following:

Problem description:

Wiring Problem: The printed circuit board divides the wiring area into n × m square arrays. It is required to determine the shortest Wiring Scheme connecting the midpoint of Square A in the square array to the midpoint of square B. During wiring, the circuit can only be routed along a straight line or right angle. To avoid line intersection, the square of the line is blocked and other lines cannot pass through the blocked square. As shown in:

Problem solving result

Algorithm Concept:

The space for resolving the wiring problem is a graph, which is used as the first expansion node starting from position. The squares adjacent to the extended node become feasible nodes are added to the active node queue and marked as 1, that is, the distance from the starting Square A to the square is 1. Then, the first node of the team is taken from the active node queue as the next expansion node, and the square adjacent to the current expansion node and unmarked is marked as 2, and stored in the active node queue. This process continues until the algorithm searches for the target square B or the active node queue is empty.

When implementing the above algorithm,

(1) define a class position that represents the square position on the circuit board.

Its two member row and Col represent the row and column of the square respectively. On the square, cabling can be performed in four directions: Right, bottom, left, and top. The four directions are marked as 0, 1, 2, and 3 respectively. In the following table, offset [I]. Row and offset [I]. COL (I =,) give the relative displacement of the first step forward along the four directions relative to the current square.

(2) Use a two-dimensional array grid to represent the given square array.

Initially, grid [I] [J] = 0 indicates that the square can be cabled, while grid [I] [J] = 1 indicates that the square is blocked and cannot be cabled.

Algorithm diagram:

CodePost it:

View code

# Include < Stdio. h >
Typedef Struct {
Int Row;
Int Col;
} Position;
Int Findpath (position start, position finish, Int   & Pathlen, position *& Path)
{ // Calculate the shortest path from start to finish at the starting position, and 1 is returned. Otherwise, 0 is returned.
Int I;
If (Start. Row =   = Finish. Row) && (Start. Col =   = Finish. col )){
Pathlen =   0 ; Return   0 ;} // Start = finish
// Set the square array "wall"
For (I =   0 ; I <= M + 1 ; I ++ )
Grid [ 0 ] [I] = Grid [n + 1 ] [I] =   1 ; // Top and bottom
For (I =   0 ; I <= N + 1 ; I ++ )
Grid [I] [ 0 ] = Grid [I] [m + 1 ] =   1 ; // Left and right
// Initialize relative displacement
Int Numofnbrs =   4 ; // Number of adjacent squares
Position offset [ 4 ], Here, NBR;
Offset [ 0 ]. Row =   0 ; Offset [ 0 ]. Col =   1 ; // Right
Offset [ 0 ]. Row =   1 ; Offset [ 0 ]. Col =   0 ; // Lower
Offset [ 0 ]. Row =   0 ; Offset [ 0 ]. Col =   - 1 ; // Left
Offset [ 0 ]. Row =   - 1 ; Offset [ 0 ]. Row =   0 ; // Upper
Here. Row = Start. row;
Here. Col = Start. Col;
Linkedqueue < Position > Q; // Mark reachable square location
Do {
For (I =   0 ; I < Numofnbrs; I ++ ){ // Mark up to adjacent squares
NBR. Row = Here. Row + Offset [I]. row;
NBR. Col = Here. Col + Offset [I]. Col;
If (Grid [NBR. Row] [NBR. Col] =   =   0 ){ // This square is not marked
Grid [NBR. Row] [NBR. Col] = Grid [here. Row] [here. Col] +   1 ;
If (NBR. Row =   = Finish. Row) && (NBR. Col =   = Finish. col )) Break ; // Complete cabling
Q. Add (NBR );
}
}
If (NBR. Row =   = Finishi. Row) && (NBR. Col =   = Finish. col )) Break ; // Complete cabling
If (Q. isempty ()) // Whether the live queue is empty
Return   0 ; // Unsolved
Q. Delete (here ); // Next extension Node
} While ( 1 );
// Construct the Shortest Path
Pathlen = Grid [finish. Row] [finish. Col]- 2 ;
Path =   New Position [pathlen];
Here = Finish;
For ( Int J = Pathlen- 1 ; J > =   0 ; J -- ){ // Locate the Precursor
Path [J] = Here;
For (I =   0 ; I < Numofnbrs; I ++ ){
NBR. Row = Here. Row + Offset [I]. row;
NBR. Col = Here. Col + Offset [I]. Col;
If (Grid [NBR. Row] [NBR. Col] =   = J + 2 ) Break ;
}
Here = NBR; // Move forward
}
Return   1 ;
}
Void Main ()
{
Int Grid [ 8 ] [ 8 ];
Int Pathlen, * Path;
Position start, finish;
Start. Row =   3 ; Start. Col =   2 ;
Finish. Row =   4 ; Finish. Col =   6 ;< P>

findpath (START, finish, pathlen, PATH );
}

the problem is solved. What method do we use? Yes, this is the branch restriction algorithm.

algorithm summary:

basic idea of the branch restriction method:

• The Branch restriction method is usually used to search the space tree for solving the problem in a breadth-first or minimum-cost (maximum-benefit)-First manner.

• in the branch restriction method, each active node has only one chance to become an extension node. Once a dynamic node becomes an extended node, all its son nodes are generated at one time.

• in these son nodes, the son nodes of the non-optimal solution are discarded, and other son nodes are added to the dynamic node table.

• after that, a node is removed from the active node table to become the current expansion node, and the above node expansion process is repeated. This process continues until you find that the desired solution or dynamic knots table is null.

the branch restriction method differs from the Backtracking Method:

for more information about the Backtracking Method, see the summary of the algorithm series: backtracking algorithms (solving the Fire Network problem)

(1) different solutions: the goal of the backtracking method is to find all solutions that meet the constraints in the solution space tree, while the goal of the branch restriction method is to find a solution that meets the constraints, or find the optimal solution in a certain sense in the solution that meets the constraints.

(2) Different search methods: the Backtracking Method searches for a spatial tree in depth-first mode, in contrast, the branch restriction rule searches for a spatial tree in the breadth-first or minimum-cost-first mode.

algorithm series Directory:

1. algorithm series Summary: grouping algorithm

2. summary of algorithm series: greedy algorithm

3. algorithm series Summary: Dynamic Planning (solving outsourcing cost issues)

4. algorithm series Summary: backtracking algorithm (solving the problem of Fire Network)

5. algorithm series Summary: branch Limit algorithm

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.