Soft Test road--algorithm is actually very simple

Source: Internet
Author: User

The most difficult part of an exam that is recognized as an intermediate software designer is the algorithm. But since the teacher told us the algorithm, the sense algorithm is actually nothing. Soft examinations, algorithms are divided into divide-and-conquer method, dynamic programming method, greedy algorithm and backtracking method. So, let's talk about these algorithms today.

first, the concept 1. Divide and conquer the law

The basic idea of divide-and-conquer method is to decompose a problem of size n into a small sub-problem of K , which is independent and same as the original problem. Recursive solution of these sub-problems, and then the solution of the sub-problems are combined to obtain the solution of the original problem.

Scope of application:
1) The scale of the problem is reduced to a certain extent and can be easily resolved
2) The problem can be decomposed into several small-scale same problems, that is, the problem has the best substructure properties .
3) The solution of sub-problems decomposed by this problem can be combined into the solution of the problem;
4) The problem is separated from each other sub-problems, that is, the sub-problem does not include the common sub-sub-problem.

2. Dynamic Planning

As with the division method, dynamic programming (programing) solves the whole problem by combining the solution of the sub-problem . But unlike the divide-and-conquer approach, the sub-problems that are decomposed are often not independent , meaning that the same sub-problems may be solved multiple times.

Scope of application:

1) optimal substructure properties: The sub-strategy of an optimization strategy is always optimal .
2) no effect: Each state is a complete summary of past history.
1) sub-problem overlap: Refers to the recursive algorithm from the top-down to solve the problem, each time the sub-problem is not always a new problem, some sub-problems will be repeated calculation multiple times .

3. Greed

The greedy algorithm always makes the best choice in the current view. That is to say, the greedy algorithm does not take the overall optimal consideration, it makes the choice is only in some sense the local optimal choice .

Of course, it is hoped that the final result of greedy algorithm is also the overall optimal. Although the greedy algorithm cannot get the whole optimal solution for all problems, it can produce the whole optimal solution for many problems. such as single source shortest path through problem, minimum spanning tree problem, etc. In some cases, even if the greedy algorithm can not get the overall optimal solution, the final result is a good approximation of the optimal solution.

4. Backtracking

The backtracking algorithm is actually a kind of enumeration-like search attempt process , mainly in search attempts to find the solution of the problem, when the discovery has not satisfied the solution condition, the "backtracking" return, try another path .

Scope of application

in the solution space Tree of all solutions containing the problem, the solution space tree is explored in depth from the root node based on the strategy of depth-first search.

Second, knapsack problem 1. Greedy Thoughts

knapsack problem

knapsack problem: Similar to the 0-1 knapsack problem, the difference is that when you select item I into the backpack, you can select part of the item I, and not necessarily all loaded into the backpack, 1≤i≤n.

given n items and a backpack with a capacity of C, the weight of item I is WI, its value is VI. knapsack problem is how to choose the items into the backpack, so that the total value of the items loaded into the backpack, attention and the difference between the 0/1 backpack, in the knapsack problem can be part of the item into the backpack, but not repeated loading.

three different solutions
(1) Select the most valuable items first , because this can increase the total value of the backpack as soon as possible, but, although each step of the choice to obtain a great increase in the value of the backpack, but the backpack capacity may be consumed too fast, so that the number of items loaded into the backpack is reduced, so that the target function can not achieve maximum.
(2) Select the lightest item , as this can be loaded into as many items as possible, thus increasing the total value of the backpack. However, although each step of the selection makes the capacity of the backpack slow, but the value of the backpack does not guarantee rapid growth, so that the objective function is not guaranteed to reach the maximum.
(3) The most cost-effective, the above two kinds of greedy strategy or only consider the growth of backpack value, or only consider the consumption of backpack capacity, and in order to obtain the best solution of knapsack problem, need in the backpack value growth and backpack capacity consumption between the two to find a balance. The right greedy strategy is to choose items with the highest value per unit weight.

PS: This shows that the highest state of greed is what greed, it is not only greedy for a certain aspect, but the integration of all aspects of greed. This is also the idea of all-greed, it is because we have this greedy idea, only to come up with some of the following algorithms.

2. Backtracking method

0-1 knapsack problem

given n kinds of items and a backpack. The weight of item I is WI, its value is VI, the capacity of the backpack is C. How do you choose which items are loaded into your backpack so that the total value of the items loaded into your backpack is greatest? When selecting items to be loaded into the backpack, there are only 2 options for each item I pack (1) or not to pack in the backpack (0). item I cannot be loaded into the backpack multiple times, nor can I only load part of the item I.

Solution Solutions

(1) Not optimized

Although the 0-1 knapsack problem is not the same as the knapsack problem, they are all greedy to make the total value of the package the most. Because, in the greedy algorithm, we already know that according to the value of the maximum unit weight first put will make the total value of the package, so in the backtracking method, the same weight value of the largest first put, however, each item is divided into two cases, put (in 1) or not put (in 0).

Therefore, according to these conditions, a binary tree can be constructed, the algorithm traverses each path (from the root node to the bottom of a road), and then calculates the total value of each path to the package, and then the maximum total value.

(2) Optimization

Because the binary tree will produce many branches, if each is traversed, the efficiency is very low, so in order to improve the efficiency of the algorithm, we can use the limit function, its role is to narrow the scope of our traversal.

In the 0-1 backpack, we are in accordance with the greedy algorithm to achieve the limit function, each time into or not put in the backpack, we find the greedy optimal solution after putting or not into the backpack (greedy algorithm to deal with knapsack problem of the optimal solution), and then according to the comparison results, we traverse the larger side of greedy optimal solution. After traversing to the end, backtrack until the other traversed points are found, then continue to compare, loop.

In this way, we can ensure that each time the greedy optimal solution of the larger nodes to start the traversal, so that our traversal closer to the optimal solution .

3. Dynamic Planning

For the 0-1 knapsack problem, dynamic planning is divided into a method to consider, we can imagine that when the backpack capacity is very small, may enter the backpack is relatively less items (because some items weight has exceeded the backpack capacity), then in order to make the total value of the backpack, we choose it into the backpack, it is very easy. But in short, when the backpack capacity is large, there are many options, when the backpack capacity is small, optional (can choose to enter items) less, so the problem has been simplified.

How do we simplify the knapsack problem? That is to put the knapsack problem into the problem of taking out knapsack, what does it mean? That is, we first assume that all the items are in the backpack, then randomly come up with one, and then according to the following conditions to find the optimal solution, the optimal solution is also in the remaining several items to solve, so we continue to take, so recursion, until the item in the backpack is 0.
here is the formula in the book,

PS: I indicates the total number of items in the backpack, W indicates the maximum weight that the backpack can bear, and WI indicates the weight of the item I. C[I,W] means that the backpack weighs w and puts the maximum value of the item I get.


In fact, this is not a formula, this is only divided into three kinds of situations .

(1) The first case 0, needless to say, is when the item in the backpack is 0, or the backpack weighs 0 o'clock. At this point the value is naturally 0.
(2) The second case is c[i-1,w], stating that after removing the item I, the backpack weight is the same, that is, the item is not in the backpack. This is wi>w, that is, the weight of the item I was removed is greater than the weight of the backpack, that is, the item is not in the backpack.

(3) The third case is that the weight of the item is less than the backpack, it may be in the backpack, may not be in the backpack, and then compare the total value of the two cases, respectively.


For the above expression, emphasize two points :
1th: We use the divide-and- conquer method for two different situations, but the sub-problems in these two cases are not the same, because the constraints of the two sub-problems are different, one backpack capacity is W-WI, the other is W.
2nd: We see that in order to find out who has the total value of the two cases, we must rely on the optimal solution of the previous sub-problem . The current problem can only be resolved after the previous sub-problem has been solved. When considering the optimal solution of the previous sub-problem, we still choose one of the items to be considered in the same way, to see whether it is the value of the sub-problem caused by the knapsack, or the value of the sub-problem which is not inside.
PS: Dynamic Programming algorithm lies in dynamic and planning two words, dynamic means, the calculation process with the goods, the weight of the package decreases dynamically; planning means, in the process of calculation, through the comparison of the previous optimal solution, the present optimal solution is obtained.


Summary:In conclusion , we know that each algorithm has its own application, we should choose the appropriate algorithm according to the actual situation. Of course, I personally think that all the algorithms are from greed, to divide and conquer the way to solve. For a deeper understanding of the algorithm, we also need to be in the process of practical use in the future slowly experience.


Soft Test road--algorithm is actually very simple

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.