"My First Algorithm Book" Reading Notes

"My First Algorithm Book" Reading Notes

Author: Miyazaki Shuichi Ishida Yasuki

◆ 1-3 array

In linked lists and arrays, data is arranged linearly in a column. Accessing data in a linked list is more complicated, and adding and deleting data is relatively simple; while accessing data in an array is relatively simple, adding and deleting data is more complicated.

◆ 1-6 hash tables

In the process of storing data, if a conflict occurs, the linked list can be used to insert new data behind the existing data to resolve the conflict. This method is called "chain address method". In addition to the chain address method, there are several ways to resolve conflicts. Among them, the "open address method" is widely used. This method means that when a conflict occurs, an alternate address (position on the array) is immediately calculated and the data is stored in it. If there is still a conflict, continue to calculate the next candidate address until there is an empty address. Candidate addresses can be calculated by multiple uses of a hash function or by methods such as "linear probing".

◆ 1-8 binary search tree

Binary search trees have two properties. The first is that the value of each node is greater than the value of any node on its left subtree.

The second is that the value of each node is less than the value of any node on its right subtree.

insert image description here

According to these two properties, the following conclusions can be drawn. First of all, the smallest node of the binary search tree starts from the top and searches for its lower left end. The minimum value here is 3.

insert image description here

Conversely, the largest node of the binary search tree starts from the top and searches for its lower right end. Here the maximum value is 28.

insert image description here

◆ 2-7 quick sort

Quick sort is a "divide and conquer method". It divides the original problem into two sub-problems (numbers smaller than the benchmark value and numbers larger than the benchmark value), and then solve these two problems separately. After the sub-problems, that is, after the sub-sequences are sorted, they are merged into a sequence as explained at the beginning, and then the sorting of the original sequence is completed.

◆ 4-2 Breadth-first search

The characteristic of breadth-first search is to start from the starting point and search extensively from near to far. Therefore, the closer the target vertex is to the starting point, the faster the search will end.

◆ 4-3 Depth-first search

The characteristic of depth-first search is to search continuously along a path. Although breadth-first search and depth-first search have great differences in search order, there is only one difference in operation steps, that is, which candidate vertex is selected as the benchmark for the next vertex. The breadth-first search selects the vertex that becomes the earliest candidate, because the closer the vertex is to the starting point, the earlier it becomes a candidate, so it will search in order from the place closest to the starting point; while the depth-first search selects the vertex that becomes the latest candidate , so it will go all the way down and continue to search deeply along the newly discovered path.

◆ 4-5 Dijkstra Algorithm

Compared with the Bellman-Ford algorithm, which needs to repeatedly calculate weights and update weights for all edges, Dijkstra's algorithm has an extra step of selecting vertices, which makes it more efficient in finding the shortest path.

In general, when there are no negative weights, it is more suitable to use the more efficient Dijkstra algorithm, and when there are negative weights, even if it is time-consuming, you should use the Bellman-Ford algorithm that can get the correct answer .

◆ 4-6 A* algorithm

If we can get some heuristic information, that is, the approximate distance from each vertex to the end point (this distance does not need to be an exact value), we can use the A algorithm. Of course, sometimes this kind of information cannot be estimated at all, and the A algorithm cannot be used at this time.

The closer the distance estimate is to the actual value from the current vertex to the end point, the higher the search efficiency of the A* algorithm is; conversely, if the distance estimate is far from the actual value, the algorithm may be more efficient than Dixter The pull algorithm is even lower. If the gap is larger, it may not even be possible to get the correct answer.

◆ 5-3 Hash function

Inputting similar data does not result in a similar output hash.

◆ 6-2 k-means algorithm

In the k-means algorithm, as the operation is repeated, the position of the center point must converge somewhere

Since the k-means algorithm needs to determine the number of clusters in advance, if the set number is unreasonable, the running results may not meet our needs.

In a hierarchical clustering algorithm, initially each data is a class of its own. That is to say, n data will form n clusters. Then repeat the operation of "merging the two closest clusters into one" n-1 times. Each time it is executed, the number of clusters will decrease by one. After executing n-1 times, all the data are divided into one cluster. In this process, the number of clusters in each stage is different, and the corresponding clustering results are also different. Just choose one of the most reasonable results.

◆ 7-2 Primality test

In fact, it is not just 5, for any prime number p, the following formula is true. This is "Fermat's Little Theorem". The method of judging whether a number is prime according to whether it satisfies Fermat's little theorem is the "Fermat test".

insert image description here

Guess you like

Origin blog.csdn.net/Gherbirthday0916/article/details/130151662