create()
{
scanf(n);
scanf(k); d=0;
Maybe you are interested!
-
Algorithm Design and Evaluation - 13 -
Algorithm Design and Evaluation - 8 -
Evaluation of Research Situation and Issues Raised for the Thesis -
Design and construction of color TV PAN model - 2 -
Evaluation of Online Marketing Activities of Philip Entertainment Media and Entertainment Company Limited - 1
for(i=1;i<=n;i++) b[i]=0;
}

try(i)
{
for(j=1;j<=n;j++) if(b[j]==0)
{
c[i]=a[j]; b[j]=1;
if(i==k) ht(); else
try(i+1);
b[j]=0;
}
}
5. A magic square of order n is a square matrix of order n with elements being natural numbers from 1 to n 2 satisfying the property: The sum of the elements in each row, each column and each diagonal is equal. List the magic squares of order 3.
Instruct :
- The elements of the third-order square matrix A are stored in elements x[1] to x[9] of the array x:
a 11 is stored in x[1] a 12 is stored in x[2]
a 13 is stored in x[3] a 21 is stored in x[4] a 22 is stored in x[5] a 23 is stored in x[6] a 31 is stored in x[7] a 32 is stored in x[8] a 33 is stored in x[9]
- Use the backtracking algorithm to construct all permutations of the 9 elements 1, 2, 3,..., 9. Each permutation will be stored in the elements of the array x.
- Every time we get a permutation, check the condition, that is, check the sums: x[1] + x[2] + x[3]; x[4] + x[5] + x[6]; x[7] + x[8] + x[9] (sum of rows)
x[1] + x[4] + x[7]; x[2] + x[5] + x[8]; x[3] + x[6] + x[9] (sum of columns) x[1] + x[5] + x[9]; x[3] + x[5] + x[7] (sum of diagonals)
If the sums are equal, display the elements of array x on the screen as a matrix (If i mod 3 = 0, go to the next line)
5.1. Technical content
Chapter 5
BRANCH AND NEAR TECHNIQUE
The backtracking and exhaustive techniques can be used to solve combinatorial optimization problems by going through the solutions of the problem, for each solution we calculate the value of the objective function at it, then compare the value of the objective function at all the solutions to find the optimal solution. However, in practice, with many problems, the method of applying this technique is very difficult to accept in terms of time. Therefore, it is necessary to have measures to limit the search in order to have hope of solving practical combinatorial optimization problems. Of course, in order to propose such measures, it is necessary to carefully study the nature of the specific combinatorial optimization problem. Thanks to such studies, in some specific cases we can build effective algorithms to solve the problem. At that time, a problem arises that in the process of listing solutions, we need to take advantage of the information found to eliminate solutions that are certainly not optimal. The branch and bound technique (abbreviated as the branch and bound) is an improvement of the backtracking technique in this direction.
We will describe the content of the branch-bound technique on the following general combinatorial optimization problem model:
min { f( x ): where D is a finite set of elements. Suppose D is described as follows:
x D }
D = { x = (x 1 , x 2 , ..., xn) A 1 A 2 ... An: x satisfies property P}, with A 1 , A 2 , ..., An being finite sets, and P being a property given on the Cartesian product
A 1 A 2 ... An.
With the above assumption about the set D , we can use the backtracking algorithm to list the solutions of the problem. During the process of listing by the backtracking algorithm, we will gradually build the components of the solution. A set of k components (a 1 , a 2 , ..., ak) appearing during the execution of the algorithm will be called a k- level partial solution .
The branch-bound technique can be applied to solve the given problem if it is possible to find a function g defined on the set of all partial solutions of the problem that satisfies the following inequality:
g(a 1 , a 2 , ..., ak) min{f(x): x D, x i =a i , i= 1,2, ..., k } (*)
for all partial solutions ( a 1, a 2 , ..., a k ) , and for all k = 1,2,...
The inequality (*) means that the value of the function g at the partial solution (a 1 , a 2 ,
..., a k ) is not greater than the minimum value of the problem's objective function on the subset of solutions
D(a 1 , a 2 , ..., a k ) = { x D :x i = a i , i = 1, 2, ..., k}
or in other words, g(a 1 , a 2 , ..., a k ) is the lower bound of the objective function value on the set D(a 1 , a 2 ,..., a k ). For that reason, the function g is called the lower bound function , and the value g(a 1 , a 2 , ..., a k ) is called the lower bound of the set D(a 1 , a 2 ,..., a k ) . Since we can identify the set D(a 1 , a 2 ,..., a k ) with the partial solution (a 1 , a 2 ,..., a k ) , we also call the value g(a 1 , a 2 , ..., a k ) the lower bound of the partial solution (a 1 , a 2 , ..., a k ).
Suppose we have a function g. We consider how to use this function to reduce the amount of searching in the process of searching all solutions according to the backtracking algorithm. In the process of listing the solutions, some solutions to the problem may have been obtained. Let x * be the solution with the smallest objective function value among the solutions found,
f
denoted by f * = f(x * ) . We will call x * the best available solution, and
is a record. Suppose
we have f * . Then if
g(a 1 , a 2 , ..., a k ) > f *
then from inequality (*) we get
f * < g(a 1 , a 2 , ..., a k ) min {f(x): x D, x i =a i , i= 1,2,..., k } ,
Therefore, the subset of solutions to the problem D(a 1 , a 2 ,...,a k ) certainly does not contain an optimal solution. In this case, we do not need to continue developing the partial solution (a 1 , a 2 ,...,a k ) , in other words, we can eliminate the solutions in the set D(a 1 , a 2 ,...,a k ) from the search process.
The backtracking algorithm lists the options that need to be revised as follows Try(k)
/* Develop partial solutions (a 1 , a 2 ,...,ak) using the backtracking algorithm with lower bound checking before continuing to develop the solution */
{
for a k A k do
if (<accept a k >)
{
x k = a k ;
if(k == n)
<update record>; else
if(g((a 1 , a 2 ,...,a k ) f * )
Try(k+1)
}
}
Then, the branch technique is performed as follows: Branch_near()
{
f * = ;
/* If we know a solution x * we can set f * = f(x * ) */ Try(1);
if (f * < )
< f * is the optimal value, x * is the optimal solution > else
<problem has no solution>;
}
Note that if in the Try function we replace the statement if(k == n)
<update record>; else
if(g((a 1 , a 2 ,...,a k ) f * )
Try(k+1)
by :
if(k == n)
<update record>; else
Try(k+1)
The Try function will list all the solutions of the problem, and we get the exhaustive search algorithm. The construction of the g function depends on each specific combinatorial optimization problem.
body. Usually we try to build it so that:
Calculating the value of g must be simpler than solving the combinatorial optimization problem on the right side of (*).
The value of g ( a 1 , a 2 ,..., a k ) must be close to the value of the right side of (*). However, these two requirements are often contradictory in practice.
5.2. Application examples
5.2.1. The tourist problem
1) Problem
A tourist wants to visit n cities numbered 1, 2, ...,
n. Starting from a certain city, the tourist wants to visit all the remaining cities, each city exactly once, and then return to the starting city. Knowing cij is the cost of going from city i to city j (i, j = 1, 2,…, n) , find the itinerary (a way of going that satisfies the given conditions) with the smallest total cost.
Problem analysis :
With such content of the problem, it is very difficult to visualize its model, we will analyze and bring it to a general form. There are two factors that need to be determined and pointed out: Set D and objective function F(x).
We can set up a 1-1 correspondence between the journeys.
(1) -> (2) ->…-> (n)-> (1)
with a permutation = ( (1), (2) ,…, (n)) of n natural numbers 1, 2, …, n. Let F( ) = c (1, (2) + ... + c (n-1), (n) + c (n), (1)
and denote D by the set of all permutations = ( (1), (2),…, (n)) of n natural numbers 1, 2,
…, n. Then the problem is reduced to the following general form:
min { F( ) : D }.
2) Algorithm design
The tourist problem leads to the problem: Find the minimum of the function:
f(x 1, x 2 , x 3 ,...,x n ) = c[x 1 , x 2 ] + c[ x 2 , x 3 ] +...+ c[ x n-1 + x n ] + c[ x n , x 1 ]
Given that (x 1 , x 2 , x 3 ,...,x n ) is a permutation of the numbers 1, 2, 3,..., n.
input: cost matrix C = (c ij )
output: optimal solution x * and f * =f(x * ) The try() function can be sketched as follows: Try (i)
{
for (j = 1 -> n)
if( Accept J )
{
Determine xi according to j; Record state; if(i == n)
Update optimal solution;
else
{
}
Determine the limit g(x 1 ,..., xi );
if( g(x 1 ,..., xi ) ≤ f* ) Try (i+1);
Return status;
}
}
- Determine the limit g( x1 ,..., xi )
The notation Cmin = min{c[ i, j ], i, j = 1,2,...,n, i j } is the minimum travel cost between cities.
Suppose we have a partial plan (u 1 , u 2 ,...,u i ). This plan corresponds to a partial trip through i cities:
u 1 u 2 ... u i-1 u i
So the cost to pay for this part of the journey will be
= c[u 1 ,u 2 ] + c[u 2 ,u 3 ] + ...+ c[u i-1 ,u i ].
To develop this partial journey into a complete journey, we still have to go through nk remaining cities and then return to city u1 , which means we still have to go through n-i+1 more roads.
Since the cost of going through each of the remaining n-i+1 segments is not less than cmin, the lower bound for the partial solution (u 1 ,u 2 ,..,ui) can be calculated by the formula
g(u 1 , u 2 ,..., ui) = + (n-i+1).cmin
- The acceptable condition of j is that city j has not been through. We use an array Daxet[] to represent this state with the convention:
+ Daxet[j] = 0 : city j has not been visited
+ Daxet[j] = 1: city j has been passed through
The Daxet[] array is initialized to 0 (all elements are 0)
- Determine xi according to j using the command: xi = j
- Record status: Daxet[j] = 1.
- Update the cost when xi is determined: S = S+ C xi −1,xi
- Update optimal solution:
Calculate the cost of the trip just found: Tong = S + C xn, 1 ;
If (Tong < f*) then
x * = x;
f* = Total;
- Restore status:
Daxet[j] = 0.
Return old cost: S = S- C xi −1,xi
- We fix the starting city as city 1.
Functions : Init()
/* Function to calculate cmin, Initially all cities are unvisited; Initialize optimal values, starting from city 1*/
{
Cmin = maxint;//Minimum cost between 2 cities for(i = 1; i <= n; i++)
Daxet[i] = 0; for(i = 1; i <= n; i++)





