Soft test path-the algorithm is actually very simple, but the soft test algorithm is actually very simple

Source: Internet
Author: User

Soft test path-the algorithm is actually very simple, but the soft test algorithm is actually very simple

Algorithms are recognized as the most difficult part of intermediate software designer exams. However, since the teacher gave us an algorithm, I feel that the algorithm is actually nothing. In the soft test, algorithms are divided into divide and conquer, dynamic programming, greedy algorithms, and backtracking. Let's talk about these algorithms today.

I. Concept 1. Divide and conquer Law

The basic idea of the division and control law is to break down a problem of n into k subproblems of small scale. These subproblems are independent of each other and are the same as the original problems. Recursively solve these subproblems, and then combine the solutions of each subproblem to obtain the original solution.

Applicability:
1) The problem can be easily solved by narrowing down to a certain extent.
2) The problem can be divided into several smaller issues, that is, the problem has the optimal substructure.
3) The solutions of subproblems decomposed by this problem can be combined into the solutions of this problem;
4) Each subproblem identified by the problem is independent of each other, that is, the subproblem does not include a public subproblem.

2. Dynamic Planning

As with the Division and Control Law, dynamic planning solves the entire problem by combining subproblems. However, what is different from the divide and conquer method is that the subproblems obtained by decomposition are often not independent, that is, the same subproblems may be solved multiple times.

Applicability:

1) optimal sub-structure: the sub-strategy of an optimization strategy is always optimal.
2) No aftereffect: each state is a complete summary of past history.
1) Overlapping subproblem: When a top-down problem is solved using a recursive algorithm, the subproblem generated each time is not always a new problem, some sub-problems are repeatedly calculated.

3. Greedy

Greedy algorithms always make the best choice for the moment. That is to say, the greedy algorithm does not take the overall optimization into consideration. All it makes is a local optimal choice in a certain sense.

Of course, we hope that the final result obtained by the greedy algorithm will be the optimal as a whole. Although greedy algorithms cannot obtain the overall optimal solution for all problems, they can produce the overall optimal solution for many problems. Such as the single-source shortest path and the shortest tree. In some cases, even if the greedy algorithm cannot obtain the overall optimal solution, the final result is a good approximation of the optimal solution.

4. Backtracking

In fact, the backtracking algorithm is a search attempt process similar to enumeration. It is mainly used to find a solution to the problem during the search attempt process. When it finds that the solution condition is no longer met, it will return "backtracking, try another path.

Applicability

In the solution space tree that contains all solutions to the problem, the depth of the solution space tree is explored from the root node according to the depth-first search policy.

Ii. Knapsack Problem 1. Greedy Thoughts

Backpack Problems

Backpack problem: similar to the 0-1 backpack problem, the difference is that when you choose item I to load the backpack, you can choose part of item I, instead of all to load the backpack, 1 ≤ I ≤ n.

Given n items and a C-sized backpack, the weight of item I is Wi and its value is Vi. The problem with a backpack is how to choose the items that are included in the backpack, so that the total value of the items in the backpack is the largest. Pay attention to the difference with the 0/1 backpack. In the backpack problem, you can mount part of the items into the backpack, but it cannot be loaded repeatedly.

Three Solutions
(1) Select the item with the greatest value first, because it can increase the total value of the backpack as quickly as possible. However, although each step of the selection leads to a significant increase in the value of the backpack, however, the size of the backpack may be consumed too quickly, reducing the number of items loaded into the backpack, which cannot guarantee that the target function reaches the maximum.
(2) Select the lightest item because it can be loaded with as many items as possible to increase the total value of the backpack. However, although each step of the selection slows down the Capacity consumption of the backpack, the value of the backpack does not guarantee rapid growth, thus the maximum target function is not guaranteed.
(3) The most cost-effective model. The above two greedy strategies only consider the increase of the value of the backpack, or only consider the consumption of the capacity of the backpack. In order to obtain the optimal solution of the knapsack problem, we need to find a balance between the increase in backpack value and the consumption of backpack capacity. The correct greedy strategy is to select the item with the largest unit weight value.

PS: it can be seen that the highest realm of greed is greed. It is not only greedy for one aspect, but also greedy for all aspects. This is also everyone's idea-Greed. It is precisely because we have this greedy idea that we can come up with some subsequent algorithms.

2. Backtracking

0-1 backpack Problems

N items and a backpack are given. The Weight of item I is Wi, its value is Vi, and the size of the backpack is C. How should we select the items that are loaded into the backpack to maximize the total value of the items in the backpack? When you choose to attach a backpack to an item, you only have two options for each type of item I, that is, loading a backpack (1) or not loading a backpack (0 ). You cannot attach item I to a backpack multiple times or just part of item I.

 Solution

(1) not optimized

Although the problem of 0-1 backpacks is different from that of backpacks, they are greedy to maximize the total value of the package. Because in the greedy algorithm, we already know that the first release with the largest unit weight value will maximize the total value of the package, so in the Backtracking Method, the first release with the greatest weight value is also the first release. However, each item can be divided into two situations: Put (represented by 1) or not put (represented by 0 ).

Therefore, we can construct a binary tree based on these conditions. This algorithm traverses each path (from the root node to the bottom layer, calculate the path strength) and then calculates the total value of the packages obtained from each path, then compare to get the maximum total value.

(2) Optimization

A binary tree generates many branches. If every one of them is traversed, the efficiency is very low. Therefore, to improve the efficiency of the algorithm, we can use the bounded function, its function is to narrow down our traversal scope.

In a 0-1 backpack, we implement a bounded function based on the greedy algorithm. Every time we put or not put it into a backpack, we can find the greedy optimal solution (the optimal solution for the greedy algorithm to deal with the problem of the backpack) that is placed after or not put into the backpack. Then, based on the comparison results, we traverse the large side of the greedy optimal solution. After the traversal ends, the system traces back until other traversal points are found, and then continues the comparison and loop.

In this way, we can ensure that each time we traverse from a node with a greedy optimal solution, so that our traversal is closer to the optimal solution.

3. Dynamic Planning

We can imagine that when the size of a backpack is small, the number of items that may enter the backpack is also relatively small (because the weight of some items has exceeded the size of the backpack), so in order to maximize the total value of the backpack, we can choose not to enter the backpack, it is very easy. But in short, when the backpack capacity is large, there are many options, and when the backpack capacity is small, there are fewer options (you can choose to enter items), so the problem is simplified.

How can we simplify the problem of backpacks? That is to convert the problem of putting a backpack into a backpack to the problem of getting a backpack out. What does that mean? That is to say, let's assume that all the items are in the backpack, and then take out one randomly. Then, we can find the optimal solution based on the following situations. The optimal solution should be solved among the remaining items, so we continue to take it out, so recursively until the item in the backpack is 0.
Here is the form in the book,

PS: I indicates the total number of items in the backpack, w indicates the maximum weight that the backpack can afford, and Wi indicates the weight of the I-th item. C [I, w] indicates that the weight of a backpack is w, and the maximum value obtained by placing one item in it.


In fact, this is not a formula, it is just dividedThree cases.

(1)Case 1: 0Needless to say, it is when the content in the backpack is 0, or the weight of the backpack is 0. At this time, the value is naturally 0.
(2)The second case is C [I-1, w]Indicates that the weight of the first item remains the same after being taken out. That is to say, the item is not included in the backpack. At this time, it is Wi> W, that is, the weight of the I-th item is greater than the weight of the backpack, that is, the item is not in the backpack.

(3)The third case is:When the weight of the retrieved item is smaller than that of the backpack, it may have been in the backpack or not, and then compare the total values of the two cases respectively.


For the above expression,Emphasize two points:
First, we use the divide and conquer method for two different cases, but the subproblems are different in these two cases, because the constraints of these two subproblems are different, the size of one backpack is W-Wi, and that of the other is W.
Second, we can see that in the two cases, the total value of the two depends on the optimal solution of the previous subproblem. The current problem can be solved only after the previous subproblem is solved. When considering the optimal solution of the previous sub-problem, we choose an item to be considered in the same way to see the value of the sub-problem caused by it in the backpack, it is still a great value for sub-problems that do not cause in it.
PS:The dynamic planning algorithm consists of two words: dynamic and planning. Dynamic refers to the dynamically decreasing weight of the package as the item is generated during the calculation process, through the comparison of the previous optimal solution, the current optimal solution is obtained.


Conclusion: In summary, we know that each algorithm has its own applicability. We need to select an appropriate algorithm based on the actual situation. Of course, I personally think that all algorithms are based on greed and managed separately. For a deeper understanding of algorithms, we also need to gradually understand the actual use process in the future.


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.