迪杰特斯拉求最短路径 JAVA描述

package test;


import java.net.Socket;
import java.util.ArrayList;


public class one {
 
   public static final int M=65535;
   public static void main(String[] args)
   {
  int [][] temp=
  {
       {0,1,5,M,M,M,M,M,M},
       {1,0,3,7,5,M,M,M,M},
       {5,3,0,M,1,7,M,M,M},
       {M,7,M,0,2,M,3,M,M},
       {M,5,1,2,0,3,6,9,M},
       {M,M,7,M,3,0,M,5,M},
       {M,M,M,3,6,M,0,2,7},
       {M,M,M,M,9,5,2,0,4},
       {M,M,M,M,M,M,7,4,0},
  };
  String[] temp2={"s0","s1","s2","s3","s4","s5","s6","s7","s8"};
  /*
  int[][]temp=new[temp2.length][temp2.length];
  for(int i=0;i<temp.length;i++)
     for(int j=0;j<temp.length;j++)
  {  
      temp[i][j]=M;
      if(i==j)
      temp[i][j]=0;
      else
      {
      if(table.get(temp[i].temp[j])!=null)
      temp[i][j]=table.get(temp[i].temp[j]).getnum();
      }
      
      
  }
   
   
   */
  Graph test=new Graph(temp,temp2);
  
  DijKstraShortestPath first=new DijKstraShortestPath(test,"s3","s1");

  first.calculate();
  System.out.println(first.getShortestDistance());




  
  
   }

}













package test;


public class DijKstraShortestPath {





public static final int INFINITY=65535;
private Graph G;
private int shortestDistance;
private String shortestPath;
private String start;
private String end;

public DijKstraShortestPath(Graph G,String start,String end) 
{
this.shortestDistance=0;
this.shortestPath=null;
this.start=start;
this.end=end;
this.G=G;
}

public Graph getG() {
return G;
}


public void setG(Graph g) {
G = g;
}


public String getStart() {
return start;
}


public void setStart(String start) {
this.start = start;
}


public String getEnd() {
return end;
}


public void setEnd(String end) {
this.end = end;
}


public void calculate()
{
   int startNum=-1;
   int endNum=-1;
   for(int i=0;i<G.numVertexes;i++)
   {
  if(G.vexs[i].equals(start))
  {
  startNum=i; 
  }
  if(G.vexs[i].equals(end))
  {
  endNum=i; 
  }
   }
   ShortestPath_Dijkstra(G,startNum,endNum);

}
private void ShortestPath_Dijkstra(Graph G,int start,int end ) throws ArrayIndexOutOfBoundsException
{   
boolean flag1=start<0||start>=G.numVertexes;
boolean flag2=end<0||end>=G.numVertexes;
if(flag1||flag2||!G.IsParaCorrect())
throw new ArrayIndexOutOfBoundsException();
if(start==end)
this.shortestDistance=0;
//当前 已知距离起点的最近距离 
int min=INFINITY;
//final[w]=1表示已经求得v0至Vw最短路径
   int[] finished=new int[G.numVertexes];
   //带权长度
   int[] Distance=new int[G.numVertexes];
   int[] Path=new int[G.numVertexes];
   // 每次遍历最小值的下标
   int index=0;
   //初始化 
   for(int i=0;i<G.numVertexes;i++)
   {
    finished[i]=0;
    Distance[i]=G.arc[start][i];
    Path[i]=start;
   }
       finished[start]=1;
       Distance[start]=0;
    for(int i=1;i<G.numVertexes;i++)
{    
   min=INFINITY;
   for(int j=0;j<G.numVertexes;j++)
   {
    if(finished[j]==0&&Distance[j]<min)
    {
       index=j;
    min=Distance[j];
    }
   }
   finished[index]=1;
   
   if(index==end)
   {   
    this.shortestDistance=Distance[index];
    break;
   }
   for(int j=0;j<G.numVertexes;j++)
{   
    if((finished[j]==0)&&(min+G.arc[index][j]<Distance[j]))
    {
    Distance[j]=min+G.arc[index][j];
    Path[j]=index;
    }
}
}


}


public int getShortestDistance()
{
return shortestDistance;
}



public String getshortestPath()
{
return shortestPath;
}







}










package test;
public class Graph
{
String []vexs;
int [][] arc;
int numVertexes;




public Graph(int[][] arc,String[] vexs) {
super();
this.arc = arc;
this.numVertexes = arc.length;
   this.vexs=vexs;
}

public boolean IsParaCorrect()
{
   
for(int i=0;i<numVertexes;i++)
{

}
   if(arc.length==0||arc.length!=vexs.length||arc.length!= arc[0].length)
    return false;
   for(int i=0;i<numVertexes;i++)
{
if(arc[i][i]!=0)
return false;
}
       return true;
}
  

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值