Commonly used data structure heavy connected graph_topological sort_critical path_shortest path

1. Reconnect graph

1. Re-connected graph : In this article on commonly used data structure graphs , the concept of too strongly connected graphs is introduced. For any two nodes A and B of a directed graph, there is a path connection from A to B, and a path from B to A. Connected, then such a directed graph is called a strongly connected graph. The largest subgraphs in a non-strongly connected graph that are strongly connected are called strongly connected components. This is somewhat like FIG communication with the concept of a weight, the weight means communicating FIG undirected graph in both the at least two paths between any two nodes, such as undirected graph called heavy communicates FIG .
2. Joint point/cut point **: In an undirected graph, after deleting a vertex and its corresponding edge. The original connected graph is divided into two or more connected components, such vertices are called joint points/cut points
3. Connectivity : reconnected graphs have no joint points, that is to say any vertex and its corresponding edge The deletion of the graph will not destroy the connectivity of the graph. But deleting a few more may be enough. So we define that if K vertices and their corresponding edges are deleted, the connectivity of the reconnected graph is destroyed, then the connectivity of the reconnected graph is K. So connectivity can show the robustness of a reconnected graph.
The reconnection graph has a wide range of practical significance: for example, there is a signal network covering multiple cities. If the network is a reconnection graph, then any signal station failure will not cause any other two cities Cannot communicate with each other. The higher the connectivity of this network, the better the overall network robustness.
4. Judging the reconnected graph : A reconnected graph has a property, that is, all vertices are not joint points. Using anyway method, as long as one joint point is found in a graph, the graph is not a reconnected graph. If none of them are found, that is a reconnected graph.
As shown in the figure below, it is a depth-first search tree of a graph. The dotted lines in the tree indicate the edges that exist in the original graph but do not exist in the tree, which are called back edges . We use the back edge to also judge the joint points.
There are two principles:
1. If the root node of the tree has multiple child nodes, that root node is the joint point. (Because after the root node is deleted, the tree becomes multiple subtrees, and the corresponding graph is multiple connected components)
2. For nodes other than the root node and leaf nodes, if at least one node exists in each subtree The back edge to the root node's ancestor node (here the ancestor node is other ancestor nodes that do not include this node) is not a joint point, otherwise it is a joint point.
As shown in the figure below: the joint points have A, B, D, G
A is the root of the tree with two child nodes, so the joint point
B has three subtrees, only C has the back edge to A, and the other two subtrees do not, so it is Key point.
There is no back edge to the ancestor node in the D and G subtrees.
In summary: the undirected graph corresponding to this tree is not a reconnected graph.
Insert picture description here

2. Topological sort

Topological sorting is a sorting that expresses the order of vertices in a directed acyclic graph.
If there is a sequence relationship between the two vertices, it becomes the total order (the second graph in Figure 1), otherwise it is the partial order (the first graph in Figure 1).
If it is used to indicate the sequence of a meaningful activity The order relationship becomes the AOV network. For example
Insert picture description here
, the method of topological sorting for breakfast-lunch-dinner-supper : continuously find the vertex in the graph that has no precursor (the end of the arc corresponding to the arc with the vertex as the arc head, that is, the tail vertex of the arrow), from the graph Delete the vertex and all its corresponding arcs until there are no vertices without predecessors in the graph. As shown below, we first find V1 without the precursor, and delete V1 and its corresponding edge; there are two vertices V2 and V3 without the precursor, we randomly select V2 and delete it. Then V3, V4, knowing that there is no vertex without a precursor.
Insert picture description here
If it is a partially ordered graph, there is more than one topological sequence. For example, in the above figure, the order of vertex 2 and vertex 3 cannot be determined. So the following two topological sequences are generated.
Insert picture description here

3. Critical path

In the previous section, we said that a graph that can express the sequence of a certain meaningful activity is called an AOV network. Then, adding its own weight to each edge of the AOV network becomes an AOE network. The weight indicates the duration of the activity. For example, a1 is equal to 6, which means that the completion of a1 takes 6 days (days are written randomly). Each vertex indicates that the previous activity has been completed, for example, V5 indicates that a4 and a5 have been completed.
The V1 in degree is 0, which is called the source point; the V9 out degree is 0, which is called the sink point.
Insert picture description here
The critical path is the longest path to ensure that all activities can be completed. If the above picture is a project, the lines in the picture are sequential production lines, and the critical path is the production line to ensure that the entire project can be completed.
To find the critical path, you need to master 4 key indicators:
For each vertex, there are two critical times, Ve(j) the earliest occurrence time and Vl(j) the latest occurrence time.
For each line, there are also two critical times e (i) Earliest start time and l(i) latest start time
Ve(j) Earliest occurrence time: the longest time from the origin to the vertex of j. For example, the earliest occurrence time of V5 node is a1+a4=7, not a2+a5=5.
Vl(j) latest occurrence time: the latest start time of the vertex under the premise of guaranteeing the construction period. If the total construction period is 18, then the latest occurrence time of V7 is 18-2=16
e(i) The earliest start time: if the arc ai is represented as <Vk,Vj>, the earliest start time of ai is the earliest occurrence of Vk Time; the earliest occurrence time of the vertex of the arc tail.
l(i) Latest start time: If arc ai is expressed as <Vk,Vj>, then the latest start time of ai is Vj-len(ai), that is, the latest occurrence time of the arc head vertex minus the time required for ai.
If the earliest start time of an edge is equal to the latest start time, then this edge is called a key activity, and the path composed of key activities is called a critical path.
Solving process :
So the process of solving the critical path is to count the above four components, and then find all the edges where e(i) is equal to l(i), and then the critical path is found. Take the above diagram as an example:
Ve(j) earliest occurrence time: find the longest path
Insert picture description here
Vl(j) corresponding to the vertex and latest occurrence time: find the latest occurrence time of each vertex, and push forward.
Insert picture description here
e(i) earliest start time: the earliest occurrence time of the corresponding arc tail vertex
Insert picture description here
l(i) latest start time: find the latest start time of each edge.
Insert picture description here
By comparing e(i) and I(i), we find a1, a4, a7, a8, a10, and a11 are all equal, so we found the two maximum paths in the AOE network as shown in the figure below.
Insert picture description here

4. The shortest path

Gothe is in hand, and he walks away. For people traveling in cities today, a mobile phone can reach their destination as quickly as possible. We regard the map as a network, and people travel from one vertex to another. How to make people travel with the lowest time cost involves the shortest path problem.

1. Dijkstra's algorithm

The Dijkstra algorithm finds the shortest path from a vertex to all other vertices. When the destination of people’s travel has not been determined, we need to give them multiple candidate locations. At this time, it is necessary to list the shortest distances from travelers to all other candidate locations.
The flow of the algorithm is: continuously find the next closest point, use the distance from the closest point to other points, update the shortest distance from the initial point to other points, until all points have the closest distance.
We use the following directed graph to demonstrate the specific work flow:
Insert picture description here
If our initial point is V0, ask for the shortest distance from V0 to all other points. We see that V2, V4 and V5 are directly connected to V0. The distance of each point is shown in the following table, and the distance corresponding to the vertex without edges between V0 is infinite. We see that the point closest to V0 is V2.
Insert picture description here
Then find the distance from V2 to other points, from V2 can only reach V3, the distance from V0V2-V3 is smaller than the distance from V0-V3, so update the distance and path of V0 corresponding to V3.
Insert picture description here
After the update, we found that the distance between V0-V4 is the closest (V2 has been used, just exclude), so V4 is the next closest point. From V4 to V3 and V5, after comparison, it is found that the distance between V0-V4-V3 is less than V0-V2-V3 in the original table, so update the distance and path from V0 to V3 to
Insert picture description here
find the next closest point as V3, and find V0 after comparison -The distance of V4-V3-V5 is less than the distance from V0 to V5 directly, so update V5.
Insert picture description here
The next closest point is V5, and the distance between V5-V1 is infinity, so the distance of V1 will not be updated, and it remains the original infinity. At this point, the closest distances of all points have been obtained, and the algorithm flow ends. Of course, Dijkstra's algorithm is not only suitable for directed graphs, but also for undirected graphs. The specific operation process is the same.

2. Freud algorithm

If the shortest path between any two points is required, Dijkstra's algorithm needs to be executed once for each point as the initial point. This section introduces another way of solving Freud's algorithm.
The idea of ​​Freud's algorithm is: take each point as an intermediate point to see if it will affect the shortest path between the original point and the point.
Insert picture description here
Let's learn the flow of Freud's algorithm from the directed graph in the previous section. First, the pairwise distances between initialization points are shown in the following table:
Insert picture description here
From the original picture, it can be seen that V0 and V1 have only out-degrees and no in-degrees. So being in the middle will not contribute. We directly start with V2 as the intermediate point and see that the distance between V0-V3 and V1-V5 in the figure has been updated.
Insert picture description here
Then take V3 as the center point to see that the distances of V0-V5, V1-V5, V2-V5 and V4-V5 are updated.
Insert picture description here
Then take V4 as the center point, and the distances of V0-V3 and V0-V5 are updated.
Insert picture description here
Because the out-degree of V5 is 0, taking V5 as the center point will not contribute much. It is equivalent to that all the points have been centered, the overall traversal is completed, and the results are shown in the above table.
To find the distance from any point to a point, the time complexity of Freud's algorithm is the same as that of Dijkstra's algorithm is O(n^3)

5. Summary

In this article, we talked about reconnected graphs. There are 2 or more paths between any two points in an undirected graph. Topological sorting, arranging the vertices according to the sequence relationship, if the vertices are meaningful events, we become an AOV network; adding weight to the AOV network (representing the length of time the activity is executed) is called the AOE network. Then introduced the way to find the critical path in the AOE network, the critical path can ensure that the entire project is completed on time. Finally, two ways to find the optimal path, Freud’s algorithm and Dijkstra’s algorithm are introduced. Dijkstra’s algorithm is to constantly find the next closest point and update the initial point by using the distance from the closest point to other points. The shortest distance to other points. Freud's algorithm uses each point as an intermediate point to update the pairwise distance matrix.

6. I will be reborn in the shadows

The night is so clean, everything becomes the purest. I listen to the song, let my thoughts fly. Only when the sun appeared, the so-called colorfulness was revealed. It seems to me that everything is messy, messy during the day, all kinds of chaos, all kinds of ugliness! There are few pieces of beauty found all over the world, but they are also pitiful. Dark night is a road, full of the Yu Lie of pepper; Dark night is a beauty, with the fragrance of orchids. The undead flying all over the sky, elegant, beautiful, like a phoenix rebirth from the ashes! They have been suppressed for thousands of years. This world is originally dark, and the sun is the invader, plundering everything without reservation. Our great power is hidden in a corner of the night, to make the world blow up with an unprecedented whirlwind, let everything fly. The dead in the night will never be slaves!

[HD Remake] Overture of the Dead-The most powerful and shocking version in history

Guess you like

Origin blog.csdn.net/gaobing1993/article/details/108933643