Shortest path algorithm between all vertices: Floyd algorithm.

Source: Internet
Author: User

The basic idea of the Floyd algorithm is: Set the initial state of the set S is empty, then add vertex 0,1,...,n-1 to the set S in turn, add one vertex at a time, and save the length of the shortest path with two-dimensional array d, where d[i][j] is the length of the shortest path of vertex i to Vertex J.
Detailed Description:
The most important method in the Floyd algorithm is that the two-dimensional array d[i][j],d[i][j] is the length of the shortest path in all possible paths from I to j only through the vertices in S. If the node in S from I to J is unreachable, the set D[I][J] is positive infinity. The length of the current shortest path from I to J, which can be called D[i][j], is stored. And as the vertices are constantly added to the S set, D[I][J] is constantly revised, and the last all vertices are added, the d[i][j] value is the shortest path from I to J.
The most critical part of the Floyd algorithm is similar to the Dijkstra algorithm, whenever a new point K is added to the S collection, updating the D array, obtaining the shortest path of the S collection after adding a new point, has
D[I][J] = Min{d[i][j], d[i][k] + d[k][j]}
Where the formula to the left D is the updated value, and the right is the shortest path to the pre-update stage, obviously the right two d[i][k] and D[k][j] have K, in the case of K, these two values are definitely not changed, do not worry.
The Floyd algorithm uses a two-dimensional array path to record the shortest path, and its path record is similar to the Dijkstra algorithm, and the data recorded by the Path[i][j] is the last vertex of the point J Shortest path.

Next, implement the Floyd algorithm in Java:


private void Floyd () {int I, j, k, n = pointset.size (); for (i = 0; i < n; i++) {for (j = 0; J < N; j + +) {D[i][j] = A I [J];if (I! = J && A[i][j] < Infty) {path[i][j] = i;} else {path[i][j] =-1;}}} for (k = 0, k < n; k++) {for (i = 0, i < n; i++) {for (j = 0; J < N; j + +) {if (D[i][k] + d[k][j] < D[i][j]) {D I [j] = D[i][k] + d[k][j];//The previous point of the new path is the previous point of the path of the K-point to J-point. PATH[I][J] = Path[k][j];}}}}


Shortest path algorithm between all vertices: 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.