Backtracking回溯

Introduction

The Backtracking is known as the "universal problem-solving method". It can be used to systematically search for all or one solution to a problem. It is a search method that is both systematic and jumping.

Basic Idea of Backtracking

   Depth first search for trees+bounds (pruning)

Basic Elements
  Tree representation of solution space
  Pruning operations (constrained pruning, optimized pruning
Steps for designing a backtracking algorithm
Represent the solution space as a tree T (solution space tree).
Define pruning operations (considering both constraint conditions and objective value optimization)
Backtracking algorithm form and termination conditions
Algorithm form: recursive form and non recursive form (iterative form)
Conditions for algorithm termination:

1) When seeking all solutions, tracing back to the root node and all subtrees of the root have been searched all over is the end.

2) When seeking a solution, the search terminates as soon as it reaches a solution to the problem.

The 3-Coloring Problem 

Problem

     Given an undirected graph G = (V,E), it is required to color each vertex in V with one of three colors, say 1, 2, and 3, such that no two adjacent vertices have the same color.  We call such a coloring legal; otherwise, if two adjacent vertices have the same color, it is illegal. A coloring can be represented by an n-tuple (c1, c2,...,cn) such that ci ∈ {1, 2, 3},   1≤ i ≤ n. For example, (1, 2, 2, 3, 1) denotes a coloring of a graph with five vertices. There are 3n possible colorings (legal and illegal) to color a graph with n vertices. The set of all possible colorings can be represented by a complete ternary tree called the search tree. In this tree, each path from the root to a leaf node represents one coloring assignment. Figure 12.1 shows such a tree for the case of a graph with three vertices.

 Idea

     Let us call an incomplete coloring of a graph partial if no two adjacent colored vertices have the same color. Backtracking works by generating the underlying tree one node at a time. If the path from the root to the current node corresponds to a legal coloring, the process is terminated (unless more than one coloring is desired). If the length of this path is less than n and the corresponding coloring is partial, then one child of the current node is generated and is marked as the current node. If, on the other hand, the corresponding path is not partial, then the current node is marked as a dead node and a new node corresponding to another color is generated. If, however, all three colors have been tried with no success, the search backtracks to the parent node whose color is changed, and so on.

Example 12.1 Consider the graph shown in Fig. 12.2(a), where we are interested in coloring its vertices using the colors {1, 2, 3}. Figure 12.2(b) shows part of the search tree generated during the process of searching for a legal coloring.

依照DFS算法思想遍历整个prossible 3-coloring graph 有边连接的节点颜色不能相同称为非法 非法在赋予颜色节点打× 如下图是求解一种合法解 

It is interesting to note that we have arrived at the solution after generating only 10 nodes out of the 364 nodes comprising the search tree.

        There are two important observations to be noted in Example 12.1, which generalize to all backtracking algorithms.

First, the nodes are generated in a depth-first-search manner.

Second, there is no need to store the whole search tree; we only need to store the path from the root to the current active node. In fact, no physical nodes are generated at all; the whole tree is implicit. In our example above, we only need to keep track of the color assignment.

3**0+……+3**n n为节点个数

Time Complexity

       As to the time complexity of these two algorithms, we note that O(3n) nodes are generated in the worst case. For each generated node, O(n) work is required to check whether the current coloring is legal, partial, or neither. Hence, the overall running time is O(n3n) in the worst case.

The 8-Queens Problem

Problem

       How can we arrange eight queens on an 8×8 chessboard so that no two queens can attack each other? Two queens can attack each other if they are in the same row, column, or diagonal. The n-queens problem is defined similarly, where in this case we have n queens and an n × n chessboard for an arbitrary value of n ≥ 1. To simplify the discussion, we will study the 4-queens problem, and the generalization to any arbitrary n is straightforward.、

同一行同一列同一斜线(对角线)都不能存在两个皇后

Idea

       Consider a chessboard of size 4×4. Since no two queens can be put in the same row, each queen is in a different row. Since there are four positions in each row, there are 44 possible configurations. Each possible configuration can be described by a vector with four components x = (x1, x2, x3, x4). For example, the vector (2, 3, 4, 1) corresponds to the configuration shown in Fig. 12.3(a). A component is zero (and hence not included explicitly in the vector) if there is no queen placed in its corresponding row. For example, the partial vector (3, 1) corresponds to the configuration shown in Fig. 12.3(b). In fact, since no two queens can be placed in the same column, a legal placement corresponds to a permutation of the numbers 1, 2, 3, and 4. This reduces the search space from 44 to 4!.

In the algorithm, we used the term legal to mean a placement of four queens that do not attack each other, and the term partial to mean a placement of less than four queens that do not attack each other. Clearly, two queens placed at positions xi and xj are in the same column if and only if xi = xj . It is not hard to see that two queens are in the same diagonal if and only if

xixj = i − j or xixj = j − i.

Example 12.2  Applying the algorithm produces the solution shown in Fig. 12.4. 

与三色问题一样 依照DFS算法思路

x1=2相当于第一个皇后放在(1,2)第一行第二列 每次依次从xn=1一直到xn=n遍历n为行列数

Time Complexity

       Now, consider a brute-force method to solve the general n-queens problem. As mentioned before, since no two queens can be placed in the same column, the solution vector must be a permutation of the numbers 1, 2,...,n. Thus, the brute-force method can be improved to test n! configurations instead of nn . However, the following argument shows that backtracking drastically cuts the number of tests. Consider the (n − 2)! vectors corresponding to those configurations in which the first two queens are placed in the first column. The brute-force method blindly tests all these vectors, whereas in backtracking these tests can be avoided using O(1) tests. Although the backtracking method to solve the n-queens problem costs O(nn+1) in the worst case, it empirically far exceeds the O(n!) brute-force method in efficiency, as its expected running time is generally much faster. For example, the algorithm discovered the solution shown in Fig. 12.4 after generating 27 nodes out of a total of 341 possible nodes

Basic Elements
  The form of solution:

 X= (x1,x2,…,xn)1£xi£n( 1£i £n)

Tree representation of solution: full n-branch tree

 Pruning operation:

   Placing the i-th queen in column xi is partial Û xi¹xj  and |i-j|¹|xi-xj| (1£j <i)

 Backtracking
 iteration form

 Time Complexity: O(n{n**n})

The General Backtracking Method

Example 12.3 Consider a variant of the partition problem defined as follows. Given a set of n integers X = {x1, x2,...,xn} and an integer y, find a subset Y of X whose sum is equal to y.

For instance, if X = {10, 20, 30, 40, 50, 60} and y = 60, then there are three solutions of different lengths, namely

{10, 20, 30}, {20, 40}, and {60}.

It is not hard to devise a backtracking algorithm to solve this problem. Note that this problem can be formulated in another way so that the solution is a boolean vector of length n in the obvious way. Thus, the above three solutions may be expressed by the boolean vectors {1, 1, 1, 0, 0, 0, }, {0, 1, 0, 1, 0, 0}, and {0, 0, 0, 0, 0, 1}.

We describe the general backtracking algorithm as a systematic search method that can be applied to a class of search problems whose solution consists of a vector (x1, x2,...,xi) satisfying some predefined constraints. Here i is some integer between 0 and n, where n is a constant that is dependent on the problem formulation.

       In backtracking, each xi in the solution vector belongs to a finite linearly ordered set Xi. Thus, the backtracking algorithm considers the elements of the Cartesian product X1×X2×· · ·×Xn in lexicographic order. Initially, the algorithm starts with the empty vector. It then chooses the least element of X1 as x1. If (x1) is a partial solution, the algorithm proceeds by choosing the least element of X2 as x2. If (x1, x2) is a partial solution, then the least element of X3 is included; otherwise, x2 is set to the next element in X2 .

In general, suppose that the algorithm has detected the partial solution (x1, x2,...,xj). It then considers the vector v = (x1, x2,...,xj , xj+1). We have the following cases:

(1) If v represents a final solution to the problem, the algorithm records it as a solution and either terminates in case only one solution is desired or continues to find other solutions. (2) (The advance step). If v represents a partial solution, the algorithm advances by choosing the least element in the set Xj+2.

(3) If v is neither a final nor a partial solution, we have two subcases:

(a) If there are still more elements to choose from in the set Xj+1, the algorithm sets xj+1 to the next member of Xj+1.

(b) (The backtrack step) If there are no more elements to choose from in the set Xj+1, the algorithm backtracks by setting xj to the next member of Xj. If again there are no more elements to choose from in the set Xj, the algorithm backtracks by setting xj-1 to the next member of Xj-1, and so on.

Now, we describe the general backtracking algorithm formally using two algorithms: one recursive (backtrackrec) and the other iterative (backtrackiter).

  Worst case time and space complexity of backtracking method (theoretical analysis)
 Time performance :

---- The worst-case involves searching the whole solution space, so the complexity is exponential. If the pruning operation is strong, the average performance is often good.

 Space performance :

---- The additional space of the backtracking method is O (h), where h is the height of the search tree.

 Branch and Bound

        Branch-and-bound design technique is similar to backtracking in the sense that it generates a search tree and looks for one or more solutions. However, while backtracking searches for a solution or a set of solutions that satisfy certain properties (including maximization or minimization), branchand-bound algorithms are typically concerned with only maximization or minimization of a given function. Moreover, in branch-and-bound algorithms, a bound is calculated at each node x on the possible value of any solution given by nodes that may later be generated in the subtree rooted at x. If the bound calculated is worse than a previous bound, the subtree rooted at x is blocked, i.e., none of its children are generated.

        Henceforth, we will assume that the algorithm is to minimize a given cost function; the case of maximization is similar. In order for branch and bound to be applicable, the cost function must satisfy the following property. For all partial solutions (x1, x2,...,xk−1) and their extensions (x1, x2,...,xk), we must have

 cost (x1, x2,...,xk−1) ≤ cost(x1, x2,...,xk) .

        Given this property, a partial solution (x1, x2,...,xk) can be discarded once it is generated if its cost is greater than or equal to a previously computed solution. Thus, if the algorithm finds a solution whose cost is c, and there is a partial solution whose cost is at least c, no more extensions of this partial solution are generated.

Example 12.4  Given a set of cities and a cost function that is defined on each pair of cities, find a tour of minimum cost. Here a tour is a closed path that visits each city exactly once. The cost function may be the distance, travel time, air fare, etc. An instance of the traveling salesman is given by its cost matrix whose entries are assumed to be nonnegative. The matrix A in Fig. 12.5 is an example of such an instance. With each partial solution (x1, x2,...,xk) we associate a lower bound y which is interpreted as follows. The cost of any complete tour that visits the cities x1, x2,...,xk  in this order must be at least y.

         In Fig. 12.5, matrix B is the reduction of matrix A. Matrix B in the figure results from subtracting the shown amounts from each row and from column 4. The total amount subtracted is 63. It is not hard to see that the cost of any tour is at least 63. In general, let (r1, r2, . . . , rn) and (c1, c2, . . . , cn) be the amounts subtracted from rows 1 to n and columns 1 to n, respectively, in an n × n cost matrix A. Then

y = S 1£ i £ n ri + S 1£ i £ n ci

is a lower bound on the cost of any complete tour.

        Now, we proceed to describe a branch-and-bound algorithm to solve the traveling salesman problem through an example. Our example is to find an optimal tour for the instance given in Fig. 12.5. The search tree, which is a binary tree, is depicted in Fig. 12.6.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值