Shortest Path-based Floyd algorithm

Source: Internet
Author: User

The time complexity of Dijkstra algorithm is O (n ^ 2), but if this algorithm is used to find the shortest path from a source point to a specific end point, the complexity is still O (n ^ 2 ).

Find the shortest path between each pair of vertices:

(1) the Dijkstra algorithm is repeatedly executed n times each time a vertex is taken as the source point. The total time complexity is O (n ^ 3 );

(2) Floyd algorithm: time complexity is also O (n ^ 3), but the form is simpler.

Take the following wired network as an example:

The adjacent matrix is:

Shows the process of obtaining the shortest path and path length between each pair of vertices using the floyd algorithm:


The specific Algorithm Implementation Code is as follows:

// Floyd algorithm, find the Shortest Path P [v] [w] And D [v] [w] void ShortestPath_FLOYD (Graph * G, pathMatrix p [] [MAX_VEX], ShortPathTable d [] [MAX_VEX]) {int v, w, k; for (v = 0; v <g-> vexNum; v ++) for (w = 0; w <g-> vexNum; w ++) {d [v] [w] = g-> arc [v] [w]; // initialize the weights d ^-1 and path p ^-1 p [v] [w] = w;} for (k = 0; k <g-> vexNum; k ++) for (v = 0; v <g-> vexNum; v ++) for (w = 0; w <g-> vexNum; w ++) if (d [v] [k] + d [k] [w] <d [v] [w]) // a shorter path from v to w by k {d [v] [w] = d [v] [k] + d [k] [w]; p [v] [w] = p [v] [k]; // The path is set to a vertex whose subscript is k} // ShortestPath_FLOYD
The following describes the execution process in detail:

(1) The program starts to run. Line 05-10 of the code is used to initialize D and P, making them the upper and lower matrices of the first column.
(2) Row 12-19 is the main loop of the algorithm, with a total of three layers nested. k Represents the subscript of the intermediate vertex. V indicates the start vertex, and w indicates the end vertex.
(3) When k = 0, the shortest distance between all vertex pairs passes through vertex.

Because there are only three vertices A, B, and C:

View B-> A-> C, d-1 [1] [0] + D-1 [0] [2] = 6 + 11 = 17> D-1 [1] [2] = 2, => D0 [1] [2] = D-1 [1] [2], that is, the value of D0 [1] [2] does not change.

View C-> A-> B, d-1 [2] [0] + D-1 [0] [1] = 3 + 4 = 7 <D-1 [2] [1] = ∞, => D0 [1] [2] = D-1 [2] [0] + D-1 [0] [1] = 7. Therefore, the array D0 is obtained. At the same time, the value of array P-1 [2] [1] is changed to the subscript 0 of the current intermediate vertex, that is, p0 [2] [1] = 0, in this way, the array p0 is obtained.

That is to say:

D0 [I] [j] = Min {D-1 [I] [j], D-1 [I] [k] + D-1 [k] [j]}

For example:


Typically, Dk [I] [j] = Min {Dk-1 [I] [j], Dk-1 [I] [k] + Dk-1 [k] [j]}, 0 <= k <= n-1, where D-1 [I] [j] = G. arcs [I] [j].

(4) k = 1. The shortest distance between all vertex pairs passes through vertex B:

View A-> B-> C, d0 [0] [1] + D0 [1] [2] = 4 + 2 = 6 <D0 [0] [2] = 11, => D1 [0] [2] = D0 [0] [1] + D0 [1] [2] = 6, modify the value of array p1 [0] [2] to the subscript 1 of the current intermediate vertex, that is, p1 [0] [2] = 1.

View C-> B->, d0 [2] [1] + D0 [1] [0] = ∞ + 6 = ∞> D0 [2] [0] = 3, => D1 [2] [0] = D0 [2] [0] = 3, not changed.


(5) k = 2. The shortest distance between all vertex pairs passes through vertex C:

Check whether A-> C-> B, D2 [0] [1] does not change.

View B-> C->, d1 [1] [2] + D1 [2] [0] = 2 + 3 = 5 <D1 [1] [0] = 6, => D2 [1] [0] = D1 [1] [2] + D1 [2] [0], modify the value of p2 [1] [0] To the subscript 2 of the intermediate vertex, that is, p2 [1] [0] = 2.

Ps: D0 is based on D-1, D1 is based on D0, D2 is based on D1. Finally, when k = 2, the two matrices are:


Verify that the 1st rows of the D2 array are the same as the number of results of the D array calculated by Dijkstra.

So how can we get the specific shortest path from the P path array? It is consistent with the Dijkstra algorithm to obtain a path sequence after matrix p is obtained:

Take A to C as an example. In Column 2nd of the P matrix in the lower half, P [0] [2] = 1, we get the vertex B and replace 1 with 0, P [1] [2] = 2 (this is equivalent to the subscript of end C, indicating that the vertex sequence in the middle is only vertex B with the subscript of 1 ), the final shortest path is A-> B-> C.

The Code is as follows:

// Find the path from the Source Vertex v to the destination vertex w, and output void SearchPath (VertexType vex [], PathMatrix p [] [MAX_VEX], int v, int w) {int que [MAX_VEX]; int tot = 0; que [tot ++] = w; // end wint tmp = p [v] [w]; // The Last vertex subscript on the path to vertex subscript w while (tmp! = W) {que [tot ++] = tmp; tmp = p [tmp] [w]; // The Last vertex subscript on the path to the vertex subscript tmp} que [tot] = v; for (int I = tot; I> = 0; -- I) if (I! = 0) printf ("% c->", vex [que [I]); elseprintf ("% c", vex [que [I]);}
The preceding Directed Network Diagram is used as an example. The program runs as follows:



Refer to: blog.chinaunix.net/uid-26548237-id-3834873.html. (If the url cannot be opened, an invalid url is always prompted during Publishing. If you delete the http: // at the beginning of the url, you will not be prompted again, but the link will, I don't know how to solve it ~)


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.