Pruning algorithm--optimized search (reprint)

Source: Internet
Author: User

Reproduced in: http://princetonboy.ycool.com/post.2805302.html

"Abstract" This paper discusses the common optimization techniques of "pruning" in search algorithms. First, the retrospective approach to solve the maze problem unfolds, introduced what is pruning; Then, the three principles of pruning were correct, accurate and efficient, and the two ways of pruning were as follows: feasibility pruning and optimization pruning, combined with examples for further elaboration; Finally, the pruning optimization method is summarized.

"keywords" search, optimization, pruning, time complexity

Introduction

In the contest, we sometimes encounter some problems, they can not be solved by building a mathematical model, there is no ready-made algorithm can be applied, or do not traverse all conditions to obtain the correct results. At this point, we have to use the search algorithm to solve the problem.

Search algorithms are divided into two categories according to search, one is depth-first search, and the other is breadth-first search. We know that the deep search programming is simple, the program is easy to understand, the space requirement is also relatively low, but the time complexity of this method often refers to the number of levels, if not optimized, its time efficiency simply unbearable; The breadth-first search, although the time complexity is lower than the former, but its huge space requirements are often prohibitive.

Therefore, the optimization of the program, it becomes the search algorithm programming the most critical link.

In this paper, we will discuss a basic method of "pruning" of the optimization program in the search algorithm.

What is pruning

I believe that people who have just started to touch the search algorithm have done a maze-like problem. When we "Walk the Maze", the general backtracking approach is this:

1, this direction has the way to go, I did not pass

2. Go in this direction

3, is a dead end, go back to the last intersection

4. Repeat the first step until you find the exit

This kind of thinking is very well understood, programming is also relatively easy. But when the size of the maze was large, the drawback of backtracking was revealed: The search was too time-consuming and unbearable.

Can we move forward in a certain direction, first to determine whether such a walk will come to a dead end? In this way, the search time can not be reduced?

The answer is: yes.

The concept of pruning, in fact, with the maze to avoid the dead end of the same. If we think of the search process as a traversal of a tree, then pruning as the name implies, is the tree "dead End", can not reach the solution we need to "cut" off, to reduce the time to search.

Search algorithm, most need to use pruning. However, not all branches can be cut off, it is necessary to design a reasonable judgment method to determine the choice of a branch. In the design of the method of judgment, we need to follow certain principles.

The principle of pruning

1. Correctness

As mentioned above, the branches are not cut by the love shears. Pruning also loses its meaning if the branch with the optimal solution is cut off casually. Therefore, the premise of pruning is to ensure that the correct results are not lost.

2. Accuracy

On the basis of guaranteeing correctness, we should analyze the specific problems and adopt appropriate judgment means so that the branches that do not contain the optimal solution are cut as much as possible to achieve the goal of "optimization" of the program. It can be said that the accuracy of pruning is to measure the quality of an optimization algorithm standard.

3. High efficiency

The fundamental purpose of the design optimizer is to reduce the number of searches and to reduce the time it takes to run the program. However, in order to reduce the number of searches as much as possible, we must also spend time to design a high accuracy optimization algorithm, and when the accuracy of the algorithm increases, the number of judgments will certainly increase, and thus lead to increased costs, which leads to contradictions. Therefore, it is also very important to find a balance between optimization and efficiency, which makes the time complexity of the program as low as possible. If a pruning judgment effect is very good, but it needs to spend a lot of time to judge, compare, the result of the whole program is not optimized to run with no difference, so it is too much worth the candle.

To sum up, we can boil down the main principles of pruning optimization to six words: correct, accurate and efficient.

The pruning algorithm can be broadly divided into two categories: feasibility pruning and optimal pruning.

These two methods are described in the following examples.

Feasibility pruning

Can you walk in this direction? Will you run into a dead end? This is a brief judgment process for the feasibility pruning of a branch.

Let's look at this question now.

Problem brief: A regular rectangular network-like city, the city center coordinates (0,0). The city contains M impassable roadblocks (m<=50), using the following rules to travel the city: the first step to walk 1 blocks, the second step to walk 2, and so on, nth step N (n<=20), in addition to the first step has four directions to go, the remaining steps must be on the basis of the previous step left or right 90 degrees, Finally back to the starting point (0,0). For a given n, M, programming to find all feasible paths.

The simplification of the title of "Golden Figure" in the ACM competition, which was GDKOI98 in the "mathematician tourism".

The answer in the book uses a simple backtracking method, because the problem itself has a strong pruning judgment. So let's first analyze the idea of solving problems with backtracking method:

Use x, y two variables to store the current coordinates, each step to the value of x, Y to modify, no obstacles to continue to walk, walk through the N-step to see if there is no return (0,0), no word backtracking search, until all the paths are found.

Next, let's look at the time complexity of this algorithm.

Take a total of n steps, each step to search four directions, assuming that in the worst case, without any obstructions, then its time complexity should be: O (4n).

Obviously, the efficiency of such algorithms is not very high, so we have to prune the program, before the completion of the N-step to determine whether this method is feasible.

When going to step O, assuming the current coordinate is (xo,yo), then the farthest distance from (0,0) should be Max (XO, Yo), while the remaining n-o step can walk the furthest distance is (o+1) + (o+2) +......+n. So, if (o+1) + (o+2) +......+n < Max (xo,yo), it means that even now "back" there is no way to reach the starting point, that is to say that this branch even if the search continues to find no solution, then we can abandon this branch and backtrack.

This pruning seems to be good, but its effect is obvious only when the data is large. In addition to the above optimization, there is no other way?

We can think of this, the city is a regular rectangular network-like, east, south, west, North Four directions are symmetrical. For example, with (1,0) This point symmetrical can have ( -1,0), (0,1), (0,-1) these three points. Can it be imagined that, when starting from One direction and looking for a solution, the solution is rotated 90o, 180o, 270o, and the rest of the three solutions are obtained? Wouldn't that save 3/4 of the search?

From this scenario, we can design the following optimizations:

Ignoring all obstacles, the first step is to fix the direction a (such as the East), on this basis to search the path, each found a path will be the remaining three "symmetric path" together to determine whether there is a barrier, if not, the path is one of the solution.

Through the above analysis, we have been able to compile a more efficient search program. Take a look at the following table:

Test situation of "Golden graph" (unit: seconds)

Test Result Analysis:

1. The general backtracking method, when processing relatively small data, time is relatively low, but when the scale to a certain extent, the time complexity of the exponential rise, so the competition should try to avoid the use of simple non-optimized backtracking method.

2. Using the first pruning method, when the data is small and the normal backtracking time is considerable, the data scale is gradually increasing, and the time lag of the backtracking method is gradually opened up, because the pruning is proper, the number of searches is less than the optimization of at least half.

3. Using symmetry to reduce the time complexity of a number of points, from the table can be clearly seen in the optimization of the program and the time spent simply not optimized, compared with the previous pruning method, according to the principle of accuracy in pruning principles to judge, this method is better than the former.

4. Combining two pruning methods, the accuracy is improved and time consuming is very low. In order to clearly compare the advantages and disadvantages of the various algorithms, I will increase the value of N to 24, the results of the comprehensive optimization of the program only 21 seconds will be solved, time-consuming for the common backtracking method one-tenth.

5. Both pruning, together with a comprehensive pruning method, follow the principle of correctness. The difference between them is mainly in the accuracy and efficiency of two points. It can be said that the last optimization algorithm synthesized the first two, both improve the accuracy, but also ensure the high efficiency, so that the two pruning complementary advantages, and achieved very good results. The search program in competition often uses more than one optimization method, which is required to achieve this effect.

Pruning algorithm--optimized search (reprint)

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.