Several classic algorithms you must memorize [2nd]

Source: Internet
Author: User
(3) The Red/black tree has excellent balance and efficient retrieval speed. It is suitable for organizing and searching weighted data. The red/black tree is a binary search tree, which has

(3) The Red/black tree has excellent balance and efficient retrieval speed. It is suitable for organizing and searching weighted data. The red/black tree is a binary search tree, which has

(3) red and black trees

The red/black tree has excellent balance and efficient retrieval speed. It is suitable for organizing and searching weighted data. The red/black tree is a binary search tree. Therefore, it has the property of "minimum left and maximum right. Each node of the red/black tree has at least five fields: parent node pointer, left child pointer, right child pointer, keyword, and color. The red/black tree has the following features, to make it so excellent:

[1] nodes of the red/black tree are either black or red.
[2] The root node is black.
[3] The leaf node is black (the root node and the leaf node are usually represented by a null node with a unique domain value of null)
[4] the child with red nodes must be black
[5] The number of black nodes passing through from any node to the leaf node is equal
(The black height can be used to specify the number of black nodes on the path of a node to its leaf node, excluding its own)
The creation time of a red/black tree is O (n * lg (n), the insertion time is O (lg (n), and the deletion time is O (lg (n )), the search time is O (lg (n )).
Rotate the Binary Search Tree:
X
/\
A Y
/\
B r
Rotation:
Y
/\
X r
/\
A B


The rotation can be performed without an additional pointer. The procedure is as follows:
[1] y = x. right two pointers are used here, including a parameter
[2] x. p. left_or_right = y; y. p = x. p;
[3] x. right = y. left; y. left. p = x;
[3] y. left = x; x. p = y;
Insert a red/black tree:
Step 1: insert nodes in red Based on the binary search tree method, regardless of the violation of the four properties
Step 2: because the property may be violated 2, (non-root) must be violated 4, color adjustment. Call the RB-INSERT-FIXUP in three cases:
(1) The node is red, and the node itself is left or right
(2) The uncle node is black and left child
(3) The node is black and the node itself is the right child.


(1) Turn the into red, distribute the to the parent node and the uncle node, and continue recursion for the grandfather node, so that the black height will "shift" down and remain unchanged, the operation only changes the color.

(2) Perform a right-hand operation on the parent node and maintain the color distribution of the tree. In this way, the color distribution remains unchanged, but only the parent node performs a right-hand operation.


(4) Base sorting

The principle of base sorting is to map n elements to be arranged to a multi-digit integer with a total of d bits, each of which can be set to k values; A stable sorting of each bit as the standard BIT. The entire element is ordered according to the standard BIT. The elements are sorted from the low bit to the high bit and re-ordered; if the time for each stable sorting is O (n + k), the final time is O (d * (n + k )).


(5) Sort buckets

Bucket sorting sorts data evenly distributed (or approximately evenly distributed) within a certain range. Therefore, it is a special (non-universal) algorithm. We assume that the distribution interval is [0, 1). The interval with the length of 1 is divided into n cells (we call it a bucket). The length of each interval is 1/n, each cell corresponds to a linked list. When new data is input, we compare the input data one by one with the standard demarcation value of the bucket. When the input data falls into a certain range, insert it into the corresponding linked list, and compare it during insertion so that the data in the linked list is ordered. After all input is complete, all linked lists (except the first node) are linked to form a general linked list.

(6) Dynamic Planning

The basic idea of an algorithm is to divide the problem into several sub-problems, but records the intermediate results (that is, the solution of the sub-problems, the preceding intermediate results are used multiple times to combine the solutions of these subproblems in an orderly manner. Generally, a two-dimensional array can be formed and used for saving. This is the biggest feature of dynamic planning. It can be seen that dynamic planning is at the cost of sacrificing space for time, and the intermediate results are placed in the storage table. This method is used to improve the recursive method to obtain the memory type recursion, that is, each Recursion to a subproblem first queries whether there is an intermediate solution, and does not continue recursion; otherwise, the obtained result is directly used, such as matrix chain multiplication.

The use of dynamic planning also has an important principle: that is, the necessary condition for the optimal solution of the original problem of the subproblem, that is, if the final solution is optimal, therefore, each subdivision (by subproblem) or step is optimal. For example, if the shortest path is used, its sub-step is the path segment that reaches the end point, that is, the selection of the sub-path containing the end point (from the back to the front). Make sure that each word segment is the best. That is, if it is optimal, it is optimal. Here, the 0-1 knapsack problem and the shortest path problem are both optimal solutions.

(7) 0-1 backpack Problems

The idea of algorithms is simple. It is a basic recursive idea. When every layer is recursive, consider the trade-off between the next item and whether the next item is "Overweight ", always follow the principle of "accumulative" maximum value and "no overweight", rather than "can be installed on demand ".

However, you need more skills when constructing a storage table. This is a matrix (two-dimensional array). I subscript represents the number of items remaining, and j subscript represents the remaining capacity. Because the subscript of the Two-dimensional array is continuously changing, the storage space and initial value must be allocated for the remaining weight of each unit. The critical conditions considered here are clever, the initial values assigned to array elements have been carefully designed:. for the last item, that is, the initial value of the remaining n items, assume W [n] <= C, that is, when the last item that can be loaded (the last item refers to the nth item instead of the time concept) is mapped to this redundant storage array, from M [n] [W [n] to M [n] [C], the same initial value must be assigned, because at this time, only one item can be obtained n, after obtaining the data, make M [n] [.] (. representing the value in the above range) is the maximum value that can be obtained after the n-th item is selected, that isM [n] [W [n]... C] = W [n],; Here we further consider the case where the element subscript j is 0 to W [n]-1 for the nth item, although it is not possible at present, because our item n weight W [n] M [n] [0... W [n]-1] = 0That is, you cannot select an item. After considering item n, the total maximum value is 0. B. if W [n]> C cannot fit the last item, then only 0 can be assigned to these values, that is, after considering the trade-off between the n items, the maximum value is still 0, that is M [n] [0 .. C] = 0. To sum up the above two cases, make MaxJ = min (W [n]-1, C), with M [n] [0... maxJ] = 0; M [n] [W [n]... c] = W [n]; (Note: W [n] here... C In the case of W [n]> C, it is deemed that the assignment will not be executed)

Repeat the above ideas for 0... C (... the table "to") refers to the total weight range. No matter how many items with less than N-1 are left, it cannot be estimated. Considering these items, the maximum value can be obtained; because when considering these items, you must at least consider whether to select the nth item, the biggest value arising from this comparison problem (the n-th item may not be selected when it cannot be installed), thus the initial value cannot be assigned. There are two possible cases:. W [I] <= C: You can place the I-th item. In this case, it is different from the n-th item. Consider whether to take the I-th item or not, this directly affects the selection of sub-problems, therefore, M [I] [j] = max (M [I + 1] [j-W [I] + V [I], M [I + 1] [j]), j = W [I]... c; M [I] [j] = M [I + 1] [j], j = 0... W [I]-1 (here considering that the remaining capacity may be limited after the next item is selected, and the available value is stored, that is, the capacity remains unchanged when item I is not selected ). B. W [I]> C. I, M [I] [j] = M [I + 1] [j], j = 0... C. MaxJ = min (W [I]-1, C), with M [I] [j] = M [I + 1] [j], j = 0... maxJ; M [I] [j] = max (M [I + 1] [j-W [I] + V [I], M [I + 1] [j]), j = W [I]... c.

Here, there is another key issue of detail. In this algorithm, what affects the I-th item is not an item that has not yet been obtained, but the biggest value of its own problems, here, we need to consider the maximum value of the sub-problem when selecting item I to reduce the remaining space, and the maximum value of the sub-problem under the remaining space without Selecting item I, instead of the I + 1 item,There is no positive correlation between the remaining capacity and the increase or decrease of I.The only difference is that the sub-Problem of the I + 1 item provides the option to solve the sub-Problem with the greatest value in different spaces. Specifically, the reverse thinking method used here is, that is to say, the optimal solution of our problem depends on the optimal solution of the sub-problem. The optimal solution of the sub-problem is the combination of the solution of the solved problem and the situation processing corresponding to the new elements currently considered, instead of simply choosing the best value solution in the sub-problem solution, we need to choose between the values of Two Different Storage values of the remaining capacity on the considerations of the I + 1 item, select M [I + 1] [j] or M [I + 1] [j-W [I]. This is contrary to normal thinking, because we may think that the I + 1 item has been selected, but we still do not choose it. In consideration of this, the remaining space of the backpack should be fixed, this is affected by the trade-off between the I-th item. Here, it is the essence of this algorithm, that is, when considering an item, we need to consider all the capacity spaces. Under these capacity spaces, the total value that can be obtained by choosing an item must be calculated. In this way, when the I-th item is selected, the selection of the I-th item directly affects the Selection Range of the I + 1 subproblem solution, the range of selection here is the limit on the remaining capacity caused by taking or not taking the I-th item. Specifically, this is the solution to the sub-problem that gets the maximum value of the I-th item under the remaining capacity, this is the solution of the sub-problem that obtains the maximum value under the remaining capacity when the I-th item is not obtained. On this basis, the value brought by the selection of I-th item is considered, and thus the choice is made.

The mathematical expression is as follows:

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.