Dijkstra [Dijkstra] algorithm ideas (to find the single point to each of the shortest paths to each point) Floyd algorithm: the shortest distance between any two points __ algorithm

Source: Internet
Author: User

First, give a non-direction graph

Using the Dijkstra algorithm (Dijkstra algorithm) to find the single source shortest path with a starting point is as follows


The process of calculating the shortest path from source vertex 1 to other vertices using the Dijkstra algorithm is listed in the following table.

Iterative process of the Dijkstra algorithm:

Floyd algorithm idea:

1. Start from any single one-way path. The distance between all two points is the right of the edge, if two points between the two sides connected, then the power is infinite. 2, for each pair of vertices u and V, see if there is a vertex w so that from U to W and to V is shorter than the known path. If you are updating it. The graph is represented by the adjacency matrix G, and if there is a path from VI to VJ, the g[i,j]=d,d indicates the length of the road; otherwise, the g[i,j]= is infinitely large. Defines a matrix D used to record the insertion point of information, D[i,j] represents the point to pass from VI to VJ, initializing D[i,j]=j. Insert each vertex into the diagram, compare the distance between the insertion point and the original distance, g[i,j] = min (G[i,j], g[i,k]+g[k,j]), if the value of g[i,j is smaller, then d[i,j]=k. The G contains information about the shortest path between two points, while in D it contains the shortest path information. For example, to find a path from V5 to V1. According to D, if D (5,1) =3 the V3 from V5 to V1, the path is {V5,V3,V1}, if D (5,3) = 3, the V5 is directly connected to V3, and if D (3,1) = 1, the V3 is directly connected to V1.

The shortest distance has three kinds of conditions:
1, two point direct distance is shortest. (Figure <v,x> below)
2, two between the point through only one middle point and the shortest distance. (Figure <v,u>)
3, two points between the above two vertices and the shortest distance. (Figure <v,w>)

For the first case: it was found at the time of initialization and will not be changed to.
For the second case: the basic operation of the Freudian algorithm is for each pair of vertices, iterate through all the other vertices to see if the vertices can be made shorter by this vertex, that is, traversing all the triangles in the graph (the algorithm scans the same triangle nine times, in principle, only three times), but to add a judgment, less efficient).
For the third case: The Pentagon of the following figure, you can find a point (such as x, so that <v,u>=2), it becomes a quadrilateral problem, and then find a point (such as Y, make <u,w&gt;=2), can become a triangular problem (v,u,w), it becomes the second case, Therefore, the N-shape can also be transformed into quadrilateral triangle problem in one step. (There's no need to worry about which point to look for before, because finding any point can make it into a (n-1) edge shape problem).

Combine the code and refer to the image above to simulate execution so we can deepen our understanding:
First key step: when K executes to x,i=v,j=u, the shortest path to calculate v to U is passed X, at this time V, u unicom.
The second key step: when K executes to u,i=v,j=y, the shortest path to the shortest path of V to Y is computed v to u, then to Y (the shortest path of V to u at this time we have computed it, using the last step directly).
The third key step: when K executes to Y, i=v,j=w, then calculates the shortest path of V to Y (at which point the shortest path of V to Y is in the second step we have calculated), and then from Y to W. scanning each point (k) sequentially, and taking the point as the intermediary point, calculates the shortest distance of any other two points (i,j) through K-point , which is the essence of the Floyd algorithm.


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.