Week 8 Reading Notes

"Programmer Interview Golden Code" + "Introduction to Algorithms"
PB14214061 Zhang Shilong

Because I may face a wave of interviews recently, I feel that my familiarity with various algorithms and common problems is not enough. However, from the previous code optimization experience, algorithm optimization can be said to be the top priority of code optimization. Appropriate selection of algorithms often leads to many orders of magnitude decrease in running time. It has always been believed that algorithms are the crystallization of human culture. So this week, I found a programmer interview book to read with the introduction to algorithms. Finally, I read the sorting of the introduction to algorithms and the introduction of some graph algorithms and interview questions.


trivial details

Because the interview is not a reflection of pure knowledge and ability, but also a reflection of a person's comprehensive quality. The interviewer's impression will be affected by your words, deeds, and even the clothes you wear. The time for sending emails and how to reject the offer have all been explained. These still need to be paid attention to. I took a moment to look at it casually.

technical details

Basically, it is divided into array string processing, stack and queue, tree and graph, bit operation, recursion and dynamic programming to explain, and finally some language knowledge points (C, C++, JAVA), and some thread locks are explained. Knowledge. Let’s follow along and add some knowledge from the introduction to algorithms.

1 Arrays and Strings

The interview book includes linked lists, hash tables, and ordinary arrays, and talks about the skills of a fast pointer.

  1. In the introduction to the algorithm, this part mainly explains the string matching algorithm, and introduces four kinds, the most common, the Rabin-Karp algorithm, which uses finite automata, and finally the classic kmp algorithm. In application, such algorithms can be used to improve the response speed of input method association, including searching for specific sequences in DNA sequences. The criteria include preprocessing time and matching time. Among them, the KMP algorithm is the most ingenious, which reduces the preprocessing time to O(m) and controls the actual matching time to O(n).
  2. Hash table, through the calculation, the search efficiency of the linked list method is reduced to O(1+C), and C is the load factor, making the open search method O(1/(1-c)), which is carried out in the introduction to the algorithm. To prove it, and make suggestions on how to choose a suitable hash function, for example, the division hash method can choose a prime number that is not close to an integer power of 2 as the divisor, and the multiplication hash method can choose (sqrt(5)-1) /2 generates the fractional part, and also introduces the global hashing method, and gives a construction method. Unfortunately, I have not studied number theory, and I did not understand it, and I am determined to make up some basic CS courses in my senior year. The implementation can use a linked list or an array, the former is more recommended, the load factor can be greater than 1, and the conflict is handled through the slot zipper, the latter load factor has been changed to less than 1, through linear detection, double hashing (here also introduces the second The second hash only produces odd numbers and the divisor is even.

2 stacks and queues

The interview book talks about the java implementation of queues and stacks, and describes several algorithm topics. The more interesting one is to use one array to implement three stacks. The three stacks are required to be dynamic, that is, the space is dynamic. I thought about it for a while. time, but as long as you pay attention to the appropriate moves and pushes to allow for loops, the entire array should be seen as consecutive.

3 Trees and graphs

When I first learned about data structures, I felt that this part of the knowledge was complicated and trivial. Several non-recursive algorithms for tree traversal were almost impossible to write, but recursion always faced the risk of stack explosion, so I reviewed it again. In the interview book, I basically reviewed some basic knowledge points, and then the questions. I did a few questions. One of the questions about finding subtrees in a binary tree with a huge number of nodes is very interesting. The algorithm idea is that the two trees are exactly the same as Pre-order traversal is the same as in-order traversal (note that the condition is that the degree of the tree must be 2 except for the root, and the lack of left and right children should be left blank), otherwise the following situation will occur,

 
 

 

 
 

Determined to be the same tree, and then turned into a string matching problem, but this method will inevitably take up a lot of memory, because it is sometimes unbearable to traverse and store the traversal results. Another method is to directly search for larger The two trees are matched once they have the same root node.

There is too much about this part in the introduction to the algorithm. The tree mainly explains the binary search tree and its insertion and deletion, and proves its average time complexity.
More on the discussion of graphs, the linked list representation of the graph and the adjacency matrix representation, the two kinds of traversal of the graph, depth-first and breadth-first. And each of them made a brief introduction to their applications.

  • BFS search can be used to establish the shortest path from each node to each node. Intuitively, it can be thought like this. BFS is based on the starting node as the center and expands the search circle by circle. In terms of implementation, just keep in mind the queue To ensure that the level of access should be able to be written.
  • Regarding depth-first traversal, we focus on two times. The first is the time vd of the first visit, and the other is the time df after the visit of all descendants under it. Many subsequent algorithms are based on this time.
  1. To determine the relationship between nodes, as long as the interval [vd, vf] has an inclusive relationship with other nodes, it means that one of them is the descendant of the other.
  2. Perform loop detection 3. Perform topological sorting (directed acyclic graph), and insert at the head of the linked list according to the end time.
  3. Find strongly connected components. This algorithm feels very ingenious. The specific proof takes a lot of chapters. It can be understood in this way. First, the strongly connected components are collapsed into a node. Different strongly connected components must have only one-way edges. The collapsed graph is Component graph, the component graph must be a directed acyclic graph, then the .f and .d of the collapse point are defined as the maximum value of .f and the minimum value of .d in the strongly connected component, where the edge of the component graph is the same as the collapse point. The .f of the shrinking point is closely related, that is, the .f of the collapsed point pointed to by the edge must be smaller than the .f of the collapsed point .f issued by the edge, then traverse the graph first, then the point with the largest .f must be topologically sorted in the component graph. The last one, start the transposition of the DFS traversal graph in the reverse order of .f, that is, all the edges of G are turned, and it must not run to other strong connectivity components, otherwise it cannot be an independent strong connectivity component, so go backwards and find it All strongly connected components. Finally, the author asks a question, if the graph is directly accessed in the increasing order of the first access .f, is it correct? If it is correct, can it save the time to find the transpose of the graph (usually O(V+E) ), in fact, this is not the case. .f is consistent with the topological ordering of component graphs, but .d is not. For example, thinking about such a situation,
 
 
 

 

Start traversing from A, and go to B first. If the minimum value of .f is B according to the above algorithm, and traversing from B, ABC is a strongly connected component, which is obviously wrong. This is because there is no edge between .d of the collapse point and the component graph. Confirm the relationship! ! ! !

Summarize

算法真的很有意思,就是有点费脑子,尤其是算法导论的证明比较晦涩,看了一周多,觉察到一个比较功利的看法,先看算法的应用,感兴趣再看实现,然后思考大体的逻辑,如果觉得很对(比如BFS做最短路径计算以及回路检测),就不必关心它的证明。,假如真的完全不理解为什么这种算法是对的(比如利用DFS找寻强连通分量),就要仔细看证明,一般顺着证明过程来就能理清其中的逻辑,还能加深理解。最后唯一不足的是时间有限没来得及写算法的实现,感觉还是要抽空写一下的。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324970599&siteId=291194637