"Data structure" topological ordering, Shortest path algorithm, Dijkstra algorithm, no-loop diagram, etc.

Source: Internet
Author: User

definition of Diagram

Figure (graph) G = (v,e) consists of the set of V and Edge (edge) of the vertex (vertex). Sometimes the edge is called arc (ARC), if the point pair (V,W) is ordered, then the graph is called the direction of the graph (the direction of the graph). Vertex V and W adjacency (adjacent) when and only if (v,w) belongs to E.

If there is a path from each vertex to every other vertex in the undirected graph, the undirected graph is said to be connected (connected). An undirected graph of this nature is called strong-connected (strongly connected). If a directed graph is not strongly connected, but its underlying diagram (underlying graph) (i.e. the graph on which the direction is removed from the arc) is connected, the directed graph is weakly connected (weakly connected). A complete graph is a graph of the existence of an edge between each pair of vertices.

A forward graph with 7 vertices and 12 edges is shown below.

Topological sorting

Topological ordering (topological sort) is a sort of vertex with a direction-free graph, so that if there is a path from VI to VJ, VJ appears behind VI in the sort. Because of this feature, topological sequencing is not possible if the graph contains loops.

A simple way to find a topological sorting algorithm: Select a vertex without an edge, display the point, delete it along with its edges, and then apply the same method to the rest of the diagram.

The so-called penetration (indegree) refers to the number of edges (u,v) of the vertex v. Assuming that the degree of each vertex is stored and the graph is read into an adjacency table, the following code can generate a topological sort.

The results of sorting the application topology are as follows:

Shortest Path Algorithm

Single Source Shortest path problem: Given a weighted graph g= (v,e) and a specific vertex s as input, find the shortest weighted path from S to G for each other point.

As shown, the value of the shortest weighted path from v1 to V6 is 6 (V1-V4-V7-V6), and the value of the shortest weighted path from V2 to V5 is 5 (V2-V4-V5).

The following figure from V5 to v4 the shortest weighted path can be interesting, it is 1? No. Following the path of V5-V4-V2-V5-V4 is a shorter path, as this is a graph with a negative loop. And because of the loops introduced with negative values, this loop is called a negative loop (negative-cost cycle), and when it appears in the diagram, the shortest path problem is uncertain. Negative edges may not be bad, but they make the problem even more difficult.

If the graph is weighted, then the path is weighted if it is not indicated whether the weighted path or the unauthorized path is being discussed.

Breadth First Search

Please ignore all of the subscript, let us start from the beginning. Randomly select a point where V3 is selected as the entry point. Thus the distance to the V3 is 0. From V3, the distance of 1 is V1 and V6; The next step is that V6 has no way to go, while the V1 distance of 1 is v2 and V4, so Mark 2 on them; go on, V2 and V4 take one step to v5,v4, so V7 and V5 are labeled 3. This is the end of the search.

This is the breadth-first search, which processes vertices by layer breadth-first. The vertices closest to the starting point are evaluated first, and the farthest point is finally evaluated, much like the sequence traversal of the tree (Level-order traversal).

Dijkstra Algorithm

The graph in the previous breadth-first search is not an entitlement graph, and if it becomes a weighted graph, then the problem becomes difficult.

For each vertex, we are marked as known and unknown, and as above, there is a distance of DV. Like the no-right shortest path, the Dijkstra algorithm is staged, selecting a vertex v at each stage, which has the smallest DV in all unknown vertices, while the algorithm declares that the shortest path from S to V is known. Then, then, continue to go on.

So what's going on with this algorithm? Please look.

The figure has been tagged with v1 as the starting point, so the initial situation is as follows left figure.

V1 is now known, and it has 2 adjacency points v2 and v4, so it can be adjusted to the right image as follows. Positive Infinity icon identity is not connected. PV represents the previous adjacency point.

There is no doubt that this will go to V4 next, because the weight of V4 1 than v2 weight of 2 smaller. Adjust to the left image below.

You may have seen the right figure in the picture and wondered why the next step was v2, but V4 could not go to V2 at all. Because V4 can go to, such as V3, the weight from V1 began altogether is 3, this is bigger than from V1 to v2. So he jumps back to the V2.

The next step is to go to V5, because only the weight of the value of 3, the same V3 is also, so they are both marked as known. This is shown in the diagram on the left.

Then went to the V7, while V6 down to 5+1=6 got the following right figure. As for why this adjustment is made, it is because the weighting of V1 to V7 at this time is 1+4=5, and the weight of V7 to V6 is 1, so there is this adjustment.

Finally, the trend came to V6 completed the entire Dijkstra algorithm, they have been marked as known.

There will be a Fibonacci heap in the back, optimized for the Dijkstra algorithm, and we welcome your continued attention.

with a negative edge worth a picture

If a graph has a negative boundary value, then the Dijkstra algorithm will not work. This is because after a vertex u is declared as known, it is possible to have a negative path back to u from some other unknown vertex v. and "Back" means the loop, in the previous example we already know how the loop is ...

The problem is not a solution, if we have a constant X, add it to each side of the value, so that the negative edges, and then calculate the shortest path of the new graph, and finally apply the results to the original. The solution was then covered with thorns because the paths of many of the edges became heavier than those with very few sides. If we put s in the queue, and then each phase let a vertex v out of the team, find all the vertices adjacent to the V W, make DW>DV+CVW, and then update to the DW and PW, and when W is not in the queue, put it into the queue, You can set one bit (bit) for each vertex to indicate what it will appear in the queue.

no-loop diagram

If the graph is non-ring, you can improve the Dijkstra algorithm by changing the order in which the vertices are declared known, or by the vertex selection rule. This method selects vertices by topological sorting, and since selection and update can be performed during the topology sort execution, the new algorithm can be completed in just one trip.

The following Action node graph (Activity-node graph) explains what a critical path analysis (critical path Analyst) can do. An edge (V,W) indicates that the action V must be done before the action W begins, as described earlier, which means that the diagram must be non-circular.

In order to perform these operations, we convert the Action node graph to the event node graph (event-node graph), each event corresponds to an action and all the actions associated with it are completed.

So now we need to find out the earliest time to finish the event, just find the longest path from the first event to the last one. It is often meaningless to have the longest path problem with a positive loop (Positive-cost cycle). Because the event node diagram is a no-ring diagram, then there is no need to worry about the circuit, so that there is no scruples.

The following is the earliest finish time.

The following is the latest finish time.

The earliest finish time is calculated by using the topological ordering of the vertices, and the latest finish is computed by reversing the topology sort.

The relaxation time of each edge in the event node graph (slack-hour) represents the amount of time that the corresponding action can be delayed without delaying the overall completion, the earliest completion time, the latest finish time, and the relaxation time as shown below.

Some actions have a relaxation time of 0, which are critical actions that must end as planned. There is at least one path to complete the 0-slack edge, such that the path is the critical path (critical path).

Thank you for your support, we welcome your attention or collection.

To make this article get treatise and ask questions, reprint please indicate the source:
Http://blog.csdn.net/nomasp

"Data structure" topological ordering, Shortest path algorithm, Dijkstra algorithm, no-loop diagram, etc.

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.