赞
踩
1 packagecom.sun.GraphTheoryReport;2 import org.jgrapht.*;3 import org.jgrapht.alg.connectivity.*;4 import org.jgrapht.alg.interfaces.ShortestPathAlgorithm.*;5 import org.jgrapht.alg.interfaces.*;6 import org.jgrapht.alg.shortestpath.*;7 import org.jgrapht.graph.*;8
9 import java.util.*;10 public classCalShortestPath {11 //private String[] str={"u0","u1","u2","u3","u4","u5","u6","u7"};12 //private int[] startPoint={0,0,0,1,1,3,3,3,3,2,4,5,5,4,6};13 //private int[] endPoint={1,3,2,4,3,4,5,6,2,6,5,6,7,7,7};14 //private double[] weights={2,8,1,1,6,5,1,2,7,9,3,4,6,9,3};
15 privateString[] str ;16 private int[] startPoint ;17 private int[] endPoint ;18 private double[] weights ;19 privateDouble[][] Dij;20 /**
21 *@paramstr 顶点集合22 * 用三个一维数据保存图Graph的信息23 *@paramstartPoint 边的起点24 *@paramendPoint 边的终点25 *@paramweights 对应边的权值26 *@paramDij 任意两点之间的最短距离27 */
28 public CalShortestPath(String[] str, int[] startPoint, int[] endPoint,double[] weights) {29 super();30 this.str =str;31 this.startPoint =startPoint;32 this.endPoint =endPoint;33 this.weights=weights;34 this.Dij=newDouble[str.length][str.length];35 }36
37 /**
38 * 主方法39 *@paramargs40 */
41 public static voidmain(String[] args) {42 String[] str={"u0","u1","u2","u3","u4","u5","u6","u7"};43 int[] startPoint={0,0,0,1,1,3,3,3,3,2,4,5,5,4,6};44 int[] endPoint={1,3,2,4,3,4,5,6,2,6,5,6,7,7,7};45 double[] weights={2,8,1,1,6,5,1,2,7,9,3,4,6,9,3};46 CalShortestPath calShortestPath = newCalShortestPath(str, startPoint, endPoint, weights);47 calShortestPath.getShortestPath();48 Double[][] dij2 =calShortestPath.getDij();49 for (int i = 0; i < dij2.length; i++) {50 System.out.print("[");51 for (int j = 0; j < dij2.length; j++) {52 System.out.print(Integer.parseInt(dij2[i][j].toString().substring(0, dij2[i][j].toString().indexOf(".")))+",");53 }54 System.out.println("]\n");55 }56 }57 /**
58 * 得到一个有向带权图,本来是做无向图的还没写好,这个每队相邻顶点之间用两条方向相反的边表示59 *@return返回一个带权有向图60 */
61 public SimpleDirectedWeightedGraphgenSimpleDirectedWeightedGraph(){62 SimpleDirectedWeightedGraph directedGraph =
63 new SimpleDirectedWeightedGraph(DefaultWeightedEdge.class);64 for (int i = 0; i < str.length; i++) {65 directedGraph.addVertex(str[i]);66 }67 DefaultWeightedEdge[] addEdgeUp=newDefaultWeightedEdge[startPoint.length];68 DefaultWeightedEdge[] addEdgeDown=newDefaultWeightedEdge[startPoint.length];69 for (int i = 0; i < endPoint.length; i++) {70 addEdgeUp[i] =directedGraph.addEdge(str[startPoint[i]], str[endPoint[i]]);71 addEdgeDown[i] =directedGraph.addEdge(str[endPoint[i]], str[startPoint[i]]);72 directedGraph.setEdgeWeight(addEdgeUp[i], weights[i]);73 directedGraph.setEdgeWeight(addEdgeDown[i], weights[i]);74 }75 returndirectedGraph;76 }77 /**
78 * 根据得到的带权有向图,Dijkstra计算最短路径,并保存他们的路径权值之和到数组Dij中79 */
80 public voidgetShortestPath(){81 SimpleDirectedWeightedGraph directedGraph =genSimpleDirectedWeightedGraph();82
83 //System.out.println("Shortest path from u0 to u7:");
84 DijkstraShortestPath dijkstraAlg =
85 new DijkstraShortestPath<>(directedGraph);86 //SingleSourcePaths iPaths = dijkstraAlg.getPaths("u0");87 //System.out.println(iPaths.getPath("u7") + "\n");
88
89 for (int i = 0; i < str.length; i++) {90 for (int j = 0; j < str.length; j++) {91 GraphPath path =dijkstraAlg.getPath(str[i], str[j]);92 //System.out.println(path + ":" + path.getWeight());
93 Dij[i][j]=path.getWeight();94 }95 }96 //FloydWarshallShortestPaths floydWarshallShortestPaths = new FloydWarshallShortestPaths<>(directedGraph);97 //System.out.println(floydWarshallShortestPaths.getPath("u0","u7"));
98 }99
100 publicDouble[][] getDij() {101 returnDij;102 }103
104 }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。