算法简介:
Floyd算法又称为弗洛伊德算法,插点法,是一种用于寻找给定的加权图中顶点间最短路径的算法。
算法过程:
1.从任意一条单边路径开始。所有两点之间的距离是边的权,如果两点之间没有边相连,则权为无穷大。
2.对于每一对顶点 i 和 j,看看是否存在一个顶点 k 使得从 i 到 k 再到 j 比已知的路径更短。如果是更新它。
代码说明:
dist[i][j] : 记录从顶点i到顶点j的最短路径长度,初始化为INF
path[i][j] : 记录从顶点i到顶点j的最短路径所经过的顶点k,初始化为0
时间复杂度:O(n^3);
空间复杂度:O(n^2);
代码:
package Algorithm; import java.util.Scanner; public class Floyd { public static final int INF = 100000; public Graph initGraph() { @SuppressWarnings("resource") Scanner scanner = new Scanner(System.in); System.out.println("Please input the number of the vertex: "); int vertexNum = scanner.nextInt(); System.out.println("Please input the number of the edge: "); int edgeNum = scanner.nextInt(); Graph graph = new Graph(vertexNum, edgeNum); for(int i = 1; i <= vertexNum; i++) { for(int j = 1; j <= vertexNum; j++) { int w = (i == j) ? 0 : INF; graph.addEdge(i, j, w); graph.addPath(i, j); } } for(int i = 1; i <= edgeNum; i++) { System.out.println("Please input the vertexs and weight of the edge " + i); int v1 = scanner.nextInt(); int v2 = scanner.nextInt(); int weight = scanner.nextInt(); graph.addEdge(v1, v2, weight); } return graph; } public void floyd(Graph graph) { int vertexNum = graph.getVertexNum(); int dist[][] = graph.getDist(); int path[][] = graph.getPath(); for(int i = 1; i <= vertexNum; i++) { for(int j = 1; j <= vertexNum; j++) { for(int k = 1; k <= vertexNum; k++) { if(dist[i][k] + dist[k][j] < dist[i][j]) { dist[i][j] = dist[i][k] + dist[k][j]; path[i][j] = k; } } } } } private void printPath(int i, int j, int path[][]) { int k = path[i][j]; if(k == 0) return; printPath(i, k, path); System.out.print(" " + k); printPath(k, j, path); } public void showAllPath(Graph graph) { System.out.println("Print all path:"); int vertexNum = graph.getVertexNum(); int dist[][] = graph.getDist(); int path[][] = graph.getPath(); for(int i = 1; i <= vertexNum; i++) { for(int j = 1; j <= vertexNum; j++) { System.out.print("From vertex " + i + " to " + j + " --- Length: " + dist[i][j] + " Path: " + i); printPath(i, j, path); System.out.println(" " + j); } } } public static void main(String args[]) { Floyd floyd = new Floyd(); Graph graph = floyd.initGraph(); floyd.floyd(graph); floyd.showAllPath(graph); } }
package Algorithm; public class Graph { private int vertexNum; private int edgeNum; private int[][] dist; private int[][] path; public Graph(int vertexNum, int edgeNum) { this.vertexNum = vertexNum; this.edgeNum = edgeNum; int i = vertexNum + 1; dist = new int[i][i]; path = new int[i][i]; } public void addEdge(int i, int j, int w) { dist[i][j] = w; } public void addPath(int i, int j) { path[i][j] = 0; } public int getVertexNum() { return vertexNum; } public void setVertexNum(int vertexNum) { this.vertexNum = vertexNum; } public int getEdgeNum() { return edgeNum; } public void setEdgeNum(int edgeNum) { this.edgeNum = edgeNum; } public int[][] getDist() { return dist; } public void setDist(int[][] dist) { this.dist = dist; } public int[][] getPath() { return path; } public void setPath(int[][] path) { this.path = path; } }