"Practical strategy of algorithmic problem"-chaper7--poor lifting method

Source: Internet
Author: User

In this chapter, "Algorithmic Combat strategy" has an overview of the problem, I think for the programmer is very valuable, so here is the following excerpt:

The idea of algorithms is a very difficult task. Compared to what we have experienced, the face of complex requirements just silly staring at the display, or not after careful consideration of the beginning of the keyboard, the result will be hard to repair the change of a mess of code. After these hardships, you will be able to realize the importance of design algorithms.

Unlike what is often thought, dominating the design algorithm is not a momentary inspiration, but a lot of strategic choices. The conception algorithm not only needs to understand the characteristics of the problem, but also understands the antagonism between execution time and memory space, and chooses the appropriate data structure.

The algorithm design paradigm refers to the strategy or viewpoint that the algorithm uses to solve a given problem. Some algorithms share the most important understanding of solving problems, accumulating them and understanding a pattern. In this sense, the understanding is to solve the problem by the same strategy or the same paradigm.

The algorithm design paradigm can be used as a framework for design algorithms, so learning these paradigms will help improve the ability of algorithmic design.

                                                                                                                                                                                         --from "Algorithmic Combat strategy"

About the exhaustive method, it can be said that the computer field is the most well-known but there is a large neglect of a basic algorithm. Many people will know that the idea is to list the solution space and then count it or something else. People with better algorithm fundamentals will know that search (DFS, BFS) is essentially a poor lift, but that they are designed for different types of problems based on recursive and search tree structure of the exhaustive method, here has actually begun to reflect the poor lifting method to solve the problem of flexibility, not many people impression that simple. While the use of the strategy to solve the problem, there will be some of the following major problems:

(1) The calculation of repetition in the process of poor lifting: that is, we should design the process must be "orderly", so-called "orderly" is to do "not heavy not leak" list all the circumstances.

(2) The analysis of the complexity of the exhaustive process: in the specific problem-solving we often encounter time, space overflow situation, when we need to do is optimization, mapping to the above mentioned search is actually pruning strategy.

Then we combine these two aspects, through the specific problems to experience this paradigm design thinking.

Q: Given the number of students N (n≤10) and their friend relationship, now 22 pairing, how many different ways of pairing?

Analysis:

First of all we think about this problem will have a repeat of the pit, we can see that we have a pair of pairs, such as the formation of the two yuan in view of disorder, that is (and 2,1) is the same, for this we in the programming design, the "pairing" of the time can not be designed to design a two-layer cycle, Instead, the duplication is ruled out by stipulating a b>a of two yuan (a, b).

The second is the analysis of the complexity of the exhaustive process, based on the maximum value of the problem data n, we can see that at most the number of poor lifting is 9*7*5*3*1 = 945, the computer is more than enough to deal with, so there is no need to consider optimization.

Another aspect of the problem is that in the design of the poor, if a situation to be divided into multiple decision-making process, then the best way is based on recursion and search tree deep Search strategy, it more emphasis on the relationship between the generation of various situations, and if the situation is relatively independent, then a simple brute force enumeration can be.

The function code of this problem is as follows:

 intN;BOOLarefriend[Ten][Ten];intCountpairings (BOOLtaken[Ten])//array of token pairing cases{    intFirstfree =-1;  for(inti =0; i < n;i++)//find the first person in the current bool table that is not paired    {          if(!Taken[i]) {Firstfree=i;  Break; }    }    if(Firstfree = =-1)return 1;//Everyone has finished pairing, get a pairing method, return 1       intRET =0;  for(intPairwith = First +1;p Airwith < n;++Pairwith) {         if(!taken[pairwith] &&Arefriend[firstfree][pairwith]) {Taken[firstfree]=true; Taken[pairwith]=true; RET+=countpairings (taken); Taken[firstfree]=false; Taken[pairwith]=false; }            }    returnret;}//because the data is small, for the convenience of processing, for the n<10 situation, we can remember the missing person as already paired, and in arefriend[][] need to initialize to 0, and this step in the above part of the code is not reflected in the//readers need to be careful when completing the code. 

"Practical strategy of algorithmic problem"-chaper7--poor lifting method

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.