The concrete realization of the branch-bound method of the algorithm _c language

Source: Internet
Author: User

First, let's focus on a problem:

Problem Description:

Wiring problem: Printed circuit board divides the wiring area into the NXM grid array, which requires to determine the shortest wiring scheme of the midpoint of the grid A in the grid array connected to Grid B. In wiring, the circuit can only be along a straight line or right angle wiring, in order to avoid line intersection, has been cloth line of the box to do a blockade mark, other lines are not allowed through the blocked squares. As shown in the following illustration:

Algorithm idea:

The solution space for a cabling problem is a graph, which starts with a start position A as the first extension node. The squares adjacent to and accessible to the extended node become viable nodes and are added to the slip-point queue, and the squares are labeled 1, that is, the distance from the starting lattice A to the squares is 1. The first node of the queue is then taken out as the next extended node, and the unmarked squares adjacent to the current extended node are labeled as 2 and deposited into the slip-point queue. This process continues until the algorithm searches for the target square B or the slip point queue as empty.

When the above algorithm is implemented,

(1) Define a class position representing the position of the squares on the circuit board.

Its 2 members row and Col respectively represent the rows and columns in which the squares are located. In the square, the wiring can be along the right, down, left, 4 directions. The movements along these 4 directions are recorded separately as 0,1,2,3. In the following table, Offset[i].row and Offset[i].col (i= 0,1,2,3) give the relative displacements of the 1-step phase to the current grid along these 4 directions respectively.

(2) A two-dimensional array grid is used to represent the given square array.

Initially, grid[i][j] = 0, indicating that the square allows wiring, and grid[i][j] = 1 means that the box is blocked and cabling is not allowed.

Algorithm diagram:

Code posted:

Copy Code code as follows:

#include <stdio.h>
typedef struct {
int row;
int col;
}position;
int Findpath (Position start, Position finish, int &pathlen, Position *&path)
{//calculates the shortest routing path from start to start to the target location, finds return 1, otherwise, returns 0
int i;
if ((Start.row = = Finish.row) && (Start.col = = = Finish.col)) {
PathLen = 0; return 0; }//start = Finish
Set the grid "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
Initializing relative displacements
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; Under
Offset[0].row = 0;  Offset[0].col =-1; Left
Offset[0].row =-1;  Offset[0].row = 0; On
Here.row = Start.row;
Here.col = Start.col;
Linkedqueue <Position> Q; Mark up to square position
do {
for (i = 0; i< Numofnbrs; i++) {//tag 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) {//The 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 wiring
Q.add (NBR);
}
}
if ((Nbr.row = = Finishi.row) && (Nbr.col = = Finish.col)) break;//Complete wiring
if (Q.isempty ())//live queue is empty
return 0; No solution
Q.delete (here); Remove an extension node
}while (1);
Constructing Shortest routing path
PathLen = Grid[finish.row][finish.col]-2;
Path = new Position[pathlen];
here = finish;
for (int j = pathlen–1 J >= 0; j--) {//Find precursor position
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;


Findpath (Start, Finish, PathLen, path);
}

Code posted:

Copy Code code as follows:

#include <stdio.h>
typedef struct {
int row;
int col;
}position;
int Findpath (Position start, Position finish, int &pathlen, Position *&path)
{//calculates the shortest routing path from start to start to the target location, finds return 1, otherwise, returns 0
int i;
if ((Start.row = = Finish.row) && (Start.col = = = Finish.col)) {
PathLen = 0; return 0; }//start = Finish
Set the grid "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
Initializing relative displacements
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; Under
Offset[0].row = 0;  Offset[0].col =-1; Left
Offset[0].row =-1;  Offset[0].row = 0; On
Here.row = Start.row;
Here.col = Start.col;
Linkedqueue <Position> Q; Mark up to square position
do {
for (i = 0; i< Numofnbrs; i++) {//tag 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) {//The 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 wiring
Q.add (NBR);
}
}
if ((Nbr.row = = Finishi.row) && (Nbr.col = = Finish.col)) break;//Complete wiring
if (Q.isempty ())//live queue is empty
return 0; No solution
Q.delete (here); Remove an extension node
}while (1);
Constructing Shortest routing path
PathLen = Grid[finish.row][finish.col]-2;
Path = new Position[pathlen];
here = finish;
for (int j = pathlen–1 J >= 0; j--) {//Find precursor position
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;


Findpath (Start, Finish, PathLen, path);
}


Well, the problem has been solved. Hey, what's the method we use? Oh, yes, this is the branch-bound algorithm.


Algorithm Summary:

The basic idea of branch-bound method:

The branch-and-bound method often searches the solution space tree of the problem in breadth first or in the least cost (maximum benefit) priority way.

• In the branch-bound method, each slip point has only one chance to become an extension node. Once the knot is expanded, all of its sons ' nodes are produced at a single point.

• In these son nodes, the son node that causes the irreducible solution or causes the suboptimal solution is discarded, and the remaining son nodes are added to the slip-point table.

• Thereafter, a node is removed from the table of the knot to become the current expansion node, and the above node extension process is repeated. This process continues until the desired solution is found or the slip table is empty.

The difference between the branch-bound method and the backtracking method:

(1) The target is different: The backtracking method is to find all the solutions that satisfy the constraints in the space tree, and the branch-and-bound method is to find a solution satisfying the constraint condition, or to find the optimal solution in some sense in the solution satisfying the constraint condition.

(2) Different search methods: The Backtracking method searches the solution space tree in depth first, and the branch-bound rule searches for the solution space tree in breadth first or in the least-cost-priority way.

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.