Floyd's algorithm to find the shortest path - Java

The two algorithms that use the greedy algorithm to solve the shortest path are described above, namely BFS and Dijkstra's algorithm. The next one to be introduced is a dynamic programming algorithm - Freud's algorithm.

In plain language, first of all our goal is to find the shortest path from point i to point j. From the perspective of dynamic programming, we need to reinterpret this goal. There are two possibilities for the shortest path from any node i to any node j, 1 is directly from i to j, 2 is from i through several nodes k to j. So, we assume that Dis(i,j) is the distance of the shortest path from node u to node v, and for each node k, we check that Dis(i,k) + Dis(k,j) < Dis(i,j) Is it true? If true, it proves that the path from i to k to j is shorter than the path from i to j directly, we set Dis(i,j) = Dis(i,k) + Dis(k,j), so that As a result, when we have traversed all nodes k, Dis(i,j) records the distance of the shortest path from i to j.

Algorithm Description:
  1. Start with any one-sided path. The distance between all two points is the weight of the edge. If there is no edge connected between the two points, the weight is infinite, which is also the so-called initialization work;
  2. For each pair of vertices u and v, see if there is a vertex w such that the path from u to w to v is shorter than the known path. If yes update it.

The following figure is an example to demonstrate the Floyd algorithm.

write picture description here

The first is the initial state of the algorithm: the matrix S is the matrix that records the shortest paths between the vertices.

Step 1:
Initialize S. The distance of vertex a[i][j] in matrix S is the weight of vertex i to vertex j; if i and j are not adjacent, then a[i][j]=∞. In effect, the original matrix of the graph is copied into S.
Note: a[i][j] represents the distance from vertex i (the ith vertex) to vertex j (the jth vertex) in the matrix S.

Step 2:
Take vertex A (the first vertex) as the intermediate point, if a[i][j] > a[i][0]+a[0][j], set a[i][j ]=a[i][0]+a[0][j].
Taking vertex a[1][6], after the previous operation, a[1][6]=∞; and when using A as the intermediate point, (B,A)=12, (A,G)=14, so The distance between B and G can be updated to 26.
Similarly, the vertices B, C, D, E, F, G are used as intermediate points in turn, and the size of a[i][j] is updated.

Below we give the implemented code:

public class MatrixUDG {
    private int mEdgNum;        // 边的数量
    private char[] mVexs;       // 顶点集合
    private int[][] mMatrix;    // 邻接矩阵
    private static final int INF = Integer.MAX_VALUE;   // 最大值

    ...//省略部分代码

    /**
    * floyd最短路径。
    * 即,统计图中各个顶点间的最短路径。
    * 参数说明:
    * path -- 路径。path[i][j]=k表示,"顶点i"到"顶点j"的最短路径会经过顶点k。
    * dist -- 长度数组。即,dist[i][j]=sum表示,"顶点i"到"顶点j"的最短路径的长度是sum。
    */
    public void floyd(int[][] path, int[][] dist) {

        // 初始化
        for (int i = 0; i < mVexs.length; i++) {
            for (int j = 0; j < mVexs.length; j++) {
                dist[i][j] = mMatrix[i][j];    // "顶点i"到"顶点j"的路径长度为"i到j的权值"。
                path[i][j] = j;                // "顶点i"到"顶点j"的最短路径是经过顶点j。
            }
        }

        // 计算最短路径
        for (int k = 0; k < mVexs.length; k++) {
            for (int i = 0; i < mVexs.length; i++) {
                for (int j = 0; j < mVexs.length; j++) {

                    // 如果经过下标为k顶点路径比原两点间路径更短,则更新dist[i][j]和path[i][j]
                    int tmp = (dist[i][k]==INF || dist[k][j]==INF) ? INF : (dist[i][k] + dist[k][j]);
                    if (dist[i][j] > tmp) {
                        // "i到j最短路径"对应的值设,为更小的一个(即经过k)
                        dist[i][j] = tmp;
                        // "i到j最短路径"对应的路径,经过k
                        path[i][j] = path[i][k];
                    }
                }
            }
        }

        // 打印floyd最短路径的结果
        System.out.printf("floyd: \n");
        for (int i = 0; i < mVexs.length; i++) {
            for (int j = 0; j < mVexs.length; j++)
                System.out.printf("%2d  ", dist[i][j]);
            System.out.printf("\n");
        }
    }
}

This article is reproduced from: http://www.cnblogs.com/skywang12345/p/3711532.html

The Freud source code of "adjacency matrix graph" and "adjacency list graph" are given below.

  1. Adjacency matrix source code (MatrixUDG.java)
  2. Adjacency list source code (ListUDG.java)

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324603525&siteId=291194637