Review of Introduction to Algorithms


The final exam has reached QAQ, so I sorted out the relevant content. The content of the article is the collation of relevant materials. The blogs referenced are listed at the end of the article. If there is any infringement, please contact me to ensure that it is deleted immediately.

Sort

Insert picture description here

Insertion sort

Insert picture description here
Insert picture description here

Merge sort

Insert picture description here
Merging steps of merge sort
Insert picture description here
Insert picture description here

Heap sort

Insert picture description here
Insert picture description here
Insert picture description here

Progressive symbols, functions, and runtime

We use progressive notation to characterize the running time of an algorithm. Sometimes we are interested in the worst-case running time, but we often want to characterize the running time of any input. In other words, we want to make a statement that comprehensively covers all inputs and not just the worst-case scenario.
Insert picture description here

Recursive solution

Insert picture description here
Insert picture description here

heap

Maximum heap & minimum heap
Insert picture description here

Maintain maximum heap

Insert picture description here
Insert picture description here

tree

AVL

AVL (Adelson-Velskii and Landis) tree is a binary search tree with a balance condition. The height difference between left and right subtrees of any node in the tree does not exceed 1.
This balance condition must be easy to maintain, and the depth of the tree must be O(logN).
Insert picture description here

LL right hand

LL means to insert a new node into the left child (L) of the left subtree (L), resulting in imbalance. In this case, a right-handed operation is required
Insert picture description here
Insert picture description here

RR left hand

Insert picture description here
Insert picture description here

LR

Insert picture description here
For the third module in the figure below, X and Z need to be changed.
Insert picture description here

RL

Insert picture description here
In the figure below, the left child of the second module y is T1, and the third module x and z are reversed. The children are T1, T2, T3, and T4 from left to right to
Insert picture description here
assist understanding. Example: The
initial AVL tree has two nodes , A and b, the tree is in balance at this time.
Insert picture description here
Insert node c, if node c is smaller than b, it becomes the left child node of b. At this time, the left subtree depth of a is 2, and the right subtree depth is 0, which is in an unbalanced state. At this time, a right-handed operation is performed.
Insert picture description here
Right-rotation is to make a and b perform a right-rotation 1/4 circle, making node b as the parent node, node a becomes the right node of node b, and the left subtree of b is still the left subtree of b after the right rotation operation .
If b has a right subtree. Then the right subtree of b after right rotation becomes the left subtree of a.
Insert picture description here
This is the concrete realization of right-handed operation, left-handed is a process of right-handed symmetry. When a node is inserted or deleted in a binary tree, if the balance of the tree is destroyed, the rotation operation will be performed according to the unbalance of the tree. If the left subtree is higher than the right subtree, it will be rotated right. If the right subtree is higher than the left subtree The tree is left-handed.
In the above, the tree is restored from an unbalanced state to a balanced state with only one rotation operation. In fact, there is a slightly more complicated situation that requires two rotations to restore the balance of the tree.
If the node is inserted into the right node of the left subtree of a, it needs to be rotated left and then right to rebalance the tree. Similarly, if a node is inserted into the left child node of the right subtree of a, it needs to be rotated right and then left to rebalance the tree. If right-handed directly, the resulting tree will still be unbalanced.
Insert picture description here
The (binary) heap is an array, which can be regarded as an approximate complete binary tree, and each node on the tree corresponds to an element in the array. Except for the bottom layer, the tree is completely full and filled from left to right.
Insert picture description here

Figure

Find path

BFS

Insert picture description here
Insert picture description here
Insert picture description here
Example:
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Breadth-first search
starting from vertex 1 : Initial state, starting from vertex 1, queue = {1}
visits the adjacent vertex of 1, 1 goes out of the queue and turns black, 2, 3 enters the queue, queue = {2,3,}
Visit the adjacent node of 2, 2 dequeue, 4 enqueue, queue={3,4}
visit the adjacent node of 3, 3 dequeue, queue={4}
visit the adjacent node of 4, 4 dequeue, queue ={Empty}
Node 5 is unreachable to 1.

DFS

Insert picture description here
Insert picture description here
Insert picture description here
Example
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here
Start a deep search from vertex 1: the
initial state, starting from vertex 1
, after visiting vertices 1, 2 and 3 in turn, ending at
vertex 3, backtracking from vertex 3 to vertex 2, continue visiting vertex 5, and ending at vertex 5
from Vertex 5 traces back to vertex 2, and ends at vertex 2,
traces back from vertex 2 to vertex 1, and ends at vertex 1
, visits from vertex 4, and ends at vertex 4

Time complexity of BFS and DFS

Because in the book Introduction to Algorithms for graphs, the storage structure uses adjacency linked lists, so the time complexity of BFS and DFS is O(N+E),
but for graphs, there is another storage form. That is, the adjacency matrix, the time complexity at this time is O(n 2 )
DFS
Insert picture description here
BFS
Insert picture description here
Insert picture description here

Find the shortest path

Dijkstra

Greedy thinking is achieved. First, save the distance from the starting point to all points to find the shortest one, and then traverse it to see if the point with the shortest distance just found as the transit station will be closer. If it is closer, update the distance. After searching all the points, the shortest distance from the starting point to all other points is saved.
Specify the shortest path from a point (source point) to the rest of the vertices, also called "single source shortest path". For example, find the shortest path from vertex 1 to vertex 2, 3, 4, 5, and 6 in the figure below.
Insert picture description here
Use a matrix to store the graph information.
Insert picture description here
Use a one-dimensional array to store the initial path from node 1 to the rest of the vertices.
Insert picture description here
The process of the algorithm. The
Insert picture description here
time complexity is O(n 2 )
. The overall time complexity of the algorithm reaches O((n+m)logm). The
disadvantage is that it cannot handle negative weight edges.

Bellman-Ford

The single-source shortest path problem with negative values ​​on the edges can be solved
. However, it is required that the graph cannot contain loops with negative weight sums (negative weight loops).
If there are negative weight loops reachable from the source point, the shortest path is not Exist, because this loop can be repeated to make the path infinitesimal.
Insert picture description here
Insert picture description here
Whether there is a negative weight loop judgment
Insert picture description here
algorithm complexity
Insert picture description here

Dijkstra&Bellman-Ford difference

Insert picture description here

Quote blog

Detailed graphics
and text-AVL tree teaches hands-on , hand-written AVL tree
Dijkstra algorithm graphics and text detailed
BFS and DFS algorithms

Guess you like

Origin blog.csdn.net/weixin_39333120/article/details/103679065