当前位置:   article > 正文

Java使用Jgrapht,求无向(有向)加权图的最短路径

java jgrapht 定义无向图

把有向图相邻顶点之间添加方向相反的两条边相当于无向图

先上代码,后面有空再添加注释

根据文末图4.2对应的例题,可以验证程序结果

 

 

 

 

 

 
  
  1 package com.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 class CalShortestPath {
 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     private  String[] str ;
 16     private int[] startPoint ;
 17     private int[] endPoint ;
 18     private double[] weights ;
 19     private Double[][] Dij;
 20     /**
 21      * @param str 顶点集合
 22      * 用三个一维数据保存图Graph的信息
 23      * @param startPoint 边的起点
 24      * @param endPoint 边的终点
 25      * @param weights 对应边的权值
 26      * @param Dij 任意两点之间的最短距离
 27      */
 28     public CalShortestPath(String[] str, int[] startPoint, int[] endP
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/152141
推荐阅读
相关标签
  

闽ICP备14008679号