Current location - Recipe Complete Network - Complete cookbook - Why is the algorithm so difficult?
Why is the algorithm so difficult?
Most code farmers have a * * * knowledge, and the algorithm is a hard nut to crack. Sadly, it may be useless after eating it-except during the interview. In practical engineering, ready-made modules are generally used, and it is generally only necessary to understand the purpose and temporal and spatial complexity of the algorithm.

But then again, the face-to-face teaching algorithms in interviews, including those that are almost impossible to use in face-to-face teaching projects, cannot be said to be unreasonable. Algorithms are often the touchstone of learning and understanding, and difficult things can be mastered, not to mention easy things. Those who aspire to the top will benefit from the middle. The opposite is not the case. On the other hand, although most of the algorithms in textbooks are directly used by modules even if they are used, it is a pity that we brick movers sometimes have to do something as inventors: either we should improve the algorithms into white boxes to meet the specific needs at hand; Or simply invent the wheel. So although the interview algorithm itself may not be available, people who are familiar with various algorithms are usually more familiar with the idea of the algorithm, so it is easier to have the two abilities mentioned here.

So, why is the algorithm difficult? There are only two possible reasons for this problem:

The algorithm itself is difficult. In other words, the algorithm itself is a very difficult thing for the human brain.

That was a terrible speech.

As will be explained below, the reason why the algorithm is considered difficult by most people is because of the above two reasons.

When we say that the algorithm is difficult, there are two situations: one is that the algorithm is difficult to learn. The second is that the algorithm design is difficult. For the former, most people (at least I did it in those days) learn the algorithm by heart, just like memorizing recipes (Recipe is a kind of book that most code farmers like), but the difference between algorithm and cookbook is that the complexity of the details contained in the algorithm is countless times that of the cookbook, the problem description of the algorithm is ever-changing, and the logical process is endless, which often makes people feel sad. In contrast, any recipe involves only a few basic elements (so programmers must have the potential to be good chefs: d) Note that even if you read the proof of the algorithm, it is still "memorized" to some extent (why do you say this, we will talk about it later). I almost always see the proof when I meet a new algorithm myself, but I soon forget it after I find it. This is a typical symptom of rote learning. If you have also chewed the algorithm book, I believe it is very likely that you will feel the same way: Why did you clearly understand it at that time and soon forget it? Why did you know how to prove it at that time, but you wanted to prove it yourself, and it didn't take long to find that you couldn't make up the missing link on the Shanghai Stock Exchange?

Will you be stupid enough to recite a theorem proof when you prove geometry in junior high school and middle school? No, you can only recite the conclusion. Why? On the one hand, it is because the proof process contains many details. On the other hand, the process of proof is interlocking, and it can be deduced by itself as long as one or two key steps are paid attention to. The logical description of the algorithm is like a theorem, and the process of proving the algorithm is like the process of proving the theorem. Unfortunately, unlike many concise basic conclusions in mathematics, the "conclusion" of the algorithm is not so easy to recite. In many cases, the logic of the algorithm itself contains almost the same amount of information as its proof process, and even the logic of the algorithm itself is the proof process (as long as you open a classic algorithm book and read several classic textbook algorithms, you will find how closely the logic of the algorithm is related to the proof of the algorithm). So let's go back to the question just now: can you recite mathematical proofs? Since no one is stupid enough to recite a complete proof, why bother to recite the algorithm stiffly?

So, if you don't recite, you don't recite. How about understanding the proof of the algorithm? Understanding the proof process of the algorithm makes it easier to remember the logical details of the algorithm and understand the memory. Unfortunately, however, most algorithm books do poorly in this respect, and the proofs are complete and the logic is quite rigorous. However, it seems that no author can really restore the thinking process of how the algorithm inventor himself got the algorithm and proved it. Arguably, the proof process should reflect this kind of thinking process, but in the following example about huffman encoding, you will see that, in fact, the highly acclaimed CLRS and algorithms not only failed to restore this process, but covered it up.

It must be noted that no author does this on purpose, but when explaining what he already knows, anyone will unconsciously "linearize" his explanation, such as proving a problem. If you recall the experience of proving plane geometry problems in high school, you will find that the process of proving is actually full of "nonlinearity", such as trial and error, association, reverse derivation, special cases, modifying problem conditions, exhaustive and so on. A chaotic process, not as written in textbooks-Lemma 1, Lemma 2, Theorem 1, Theorem 2, all at once until the final conclusion. This proof process may be easy to understand, but it is definitely not easy to remember. In a few days, you will forget one or several lemmas, one or several key technologies, and then when you want to go back and try to prove it yourself, you will find it stuck in a key place. Why? Because the proof doesn't tell you why the author thought that the proof algorithm needed such a lemma or skill, you will know the reason for the conclusion of the algorithm after reading the proof, but you don't know the reason for the proof process of the algorithm. In the memory system of our brain, new knowledge must be linked with existing knowledge to be easily recalled (how to learn and remember effectively). The more links, the easier it is to recall. However, a flight lemma has nothing to do with our existing knowledge, and children without mothers are easily forgotten. Why is it so difficult to restore the thinking process? I once elaborated in "Knowing Why (I)".

It is precisely because of the algorithmic proof process of tragedy in most algorithmic books that many people find that the proof itself is not easy to remember, so they prefer to remember the conclusion directly. At that time, I was in the department of mathematics, and the examination proof process, but it seems that the examination algorithm proof process in the department of computer science is ridiculous? As an "engineering" program design, it seems to pay more attention to use and results. But what if you need to design an algorithm yourself in the project? At this time, the least you have to do is to prove the correctness of the algorithm. When we interview, we often encounter some problems in algorithm design. I always ask candidates to prove the correctness of the algorithm, because even an algorithm that looks "correct" is often not so easy to prove.

Therefore, the vast majority of algorithm books fail from the perspective of cultivating algorithm designers, even more than mathematics education. Most people will do proof problems after finishing plane geometry in junior high school (math books don't require you to remember all the theorems of geometry), but many people are still in a mess after reading an algorithm book, and some basic algorithms can't be proved. We have a conclusion and a conclusion on the back, not only a lot of useless, but also will not prove it when used together. Why is there such a difference? Because the ideal purpose of mathematics education is to make you a scientist who can discover new theorems, but the purpose of algorithm education in Code Agriculture Department is more realistic, in order to make you an engineer who can do things with algorithms. However, is it really that simple? If so, you don't even have to memorize the conclusion of the algorithm, just know what the algorithm is doing and what the spatio-temporal complexity is.

If the difficulty of the algorithm mentioned above (the difficulty of interpretation and memory) belongs to accidental complexity, then another difficulty of the algorithm is the essential complexity: algorithm design. Or take mathematical proof as an analogy (if you have read Introduction to Algorithms: A Creative Method, you will know how similar an algorithm is to mathematical proof. ), compared with just proving, the difficulty in designing an algorithm is that both theorems and proofs need you to explore, especially the former-you need to discover the key theorems yourself. The complexity of this matter is often an order of magnitude more difficult than proving a known conclusion (you know that the conclusion is correct, you just need to logically link the conclusion with the condition).

An interesting fact is that the process of algorithm exploration often includes the process of algorithm proof. The ideal algorithm book should restore the process of algorithm exploration, so that readers can not only deduce the process of proof by themselves, but also have the ability to explore new algorithms. I say this because I am a lazy person. Lazy people always dream of learning something to achieve the following two purposes:

Once and for all: Programmers know the benefits of "write once and run everywhere", which is much simpler. If you learn, you forget, and if you forget, you have to learn again, wasting your life again and again. Why can't you just watch it once and never forget it? Is it bad teaching or learning?

Get twice the result with half the effort: In fact, programmers not only pay attention to writing once and running everywhere, but also pay attention to "writing once and using everywhere" (also known as "reuse"). If the experience gained from learning an algorithm can be used everywhere, learning one as ten, and by extension, the efficiency of time utilization will be greatly improved. How can we learn to maximize the efficiency of extrapolating experience?

To achieve these two points, we must start from the "root node" of the knowledge tree as much as possible. Although this is a beautiful dream, for example, the dream of finding the "root node" in mathematics has a long history ("a little history of solving problems with Paulia"), Godel's proof makes the dream go up in smoke ("eternal golden diagonal"); However, this does not prevent us from looking for higher-level nodes-more general principles and methods to solve problems. Therefore, the ideal algorithm book or algorithm explanation should proceed from the most general thinking law and logically deduce the algorithm. This process should try to restore the thinking process of an "ordinary person" instead of making people feel "how can this be thought of?"

Take huffman encoding mentioned in the first part of this article as an example. When I first saw huffman encoding, I only saw the algorithm description, which was quite intuitive. After two years, I forgot, because I don't know why the frequencies of two nodes add up to one node-if I don't know "why", I can't remember well. Knowing "why" provides inevitability for this matter. If you don't know why, you can choose this or that. Our brains often confuse other things, and it is easier to remember those things that are justified (from the perspective of information theory, the probability of an inevitable thing is 1, the information content of an alternative thing is 0, and the information content of an alternative thing is greater than 0). The second time I saw it was after work, and finally I knew I needed to see the certificate. I took out the famous algorithm and nodded while watching it. It feels really good. At first glance, I can understand why we should construct such an optimal coding tree. But before long, I forgot again! This time, I forgot not to add up the frequencies of two nodes to calculate one, but to forget why I did it, because I don't understand why Hoffman could think of why I constructed the optimal coding tree like that. As a result, I only know one thing, but I don't know the other.

It must be noted that if you only care about the conclusion of the algorithm (that is, the algorithm logic), then it is enough to understand the proof of the algorithm. The logic of the bare back algorithm is difficult to remember, and it is easy to remember a lot after understanding the proof. But to remember the proof of the algorithm, we should not only understand the proof, but also understand the thinking behind it, that is, why behind it. The latter is generally difficult to find in books and materials, and only you can figure it out for yourself. Why bother this god? As long as we don't forget the conclusion, isn't it over? It depends on what you want to do. If you really want to understand the idea behind the algorithm design, it is impossible not to figure out how the original author of the algorithm came up with it.