单源最短路径( Dijkstra算法)JAVA实现

单源最短路径( Dijkstra算法)JAVA实现

package dijkstra;


public class Graph {
	final int max=100;
	/*
	 * 顶点节点
	 */
	public class VexNode{
			int adjvex;
			int data;
	}
	
	VexNode[] vexNodes;
	int[] thevexs; //顶点集合
	int[][] edges = new int[max][max]; //边集合
	
	
	/*
	 * 创建图
	 */
	public void createGraph(Graph graph,int[][] A,int[] vexs) {
		thevexs=vexs;
		for (int i = 0; i < vexs.length; i++) {
			  for (int j = 0; j < vexs.length; j++) {
				  graph.edges[i][j] = A[i][j];
			}
		}
	}
	
	/*
	 * 输出图
	 */
	public void printGraph(Graph graph) {
		for (int i = 0; i < graph.thevexs.length; i++) {
			for (int j = 0; j < graph.thevexs.length; j++) {
				//没有路径则输出/
				if (graph.edges[i][j]==1000) {
					System.out.printf("%4s","/");
					 
				}else {
					System.out.printf("%4d",graph.edges[i][j]);
				}
				
			}
			System.out.println("\n");
		}
	}
}
package dijkstra;

public class DijkStra {
			final int max = 100;

		
			
			 public static int[] Dijsktra(Graph graph,int start){  
			     //接受一个有向图的权重矩阵,和一个起点编号start(从0编号,顶点存在数组中)  
			        //返回一个int[] 数组,表示从start到它的最短路径长度  
			        int n = graph.thevexs.length;        //顶点个数  
			        int[] shortPath = new int[n];    //存放从start到其他各点的最短路径  
			        String[] path=new String[n]; //存放从start到其他各点的最短路径的字符串表示  
			         for(int i=0;inew String(start+"-->"+i);  
			        int[] visited = new int[n];   //标记当前该顶点的最短路径是否已经求出,1表示已求出  
			          
			        //初始化,第一个顶点求出  
			        shortPath[start] = 0;  
			        visited[start] = 1;  
			  
			        for(int count = 1;count <= n - 1;count++)  //要加入n-1个顶点  
			        {  
			   
			            int k = -1;    //选出一个距离初始顶点start最近的未标记顶点  
			            int dmin = Integer.MAX_VALUE;  
			            for(int i = 0;i < n;i++)  
			            {  
			            	
			                if(visited[i] == 0 && graph.edges[start][i]"k="+k);  
			               
			            //将新选出的顶点标记为已求出最短路径,且到start的最短路径就是dmin  
			            shortPath[k] = dmin;  
			  
			            visited[k] = 1;  
			    
			            //以k为中间点,修正从start到未访问各点的距离  
			            for(int i = 0;i < n;i++)  
			            {                 // System.out.println("k="+k);  
			                if(visited[i] == 0 && graph.edges[start][k] + graph.edges[k][i] < graph.edges[start][i]){  
			                	graph.edges[start][i] = graph.edges[start][k] + graph.edges[k][i];  
			                     
			                     path[i]=path[k]+"-->"+i;  
			                      
			                }  
			                  
			            }    
			       
			        }  
			         for(int i=0;i"从"+start+"出发到"+i+"的最短路径为:"+path[i]);    
			         System.out.println("=====================================");  
			        
			        return shortPath;  
			    }  
			
}
package dijkstra;

public class Test {

	public static void main(String[] args) {
			final int INF = 1000;
			int[] vexs = {0,1,2,3,4,5};
			int[][] A ={
					{0,50,10,INF,INF,INF},
					{INF,0,15,50,10,INF},
					{20,INF,0,15,INF,INF},
					{INF,20,INF,0,35,INF},
					{INF,INF,INF,30,0,INF},
					{INF,INF,INF,3,INF,0},
					
			};
			Graph graph = new Graph();
			graph.createGraph(graph, A, vexs);
			graph.printGraph(graph);
			DijkStra dijkStra = new DijkStra();
			int[] shortPath = dijkStra.Dijsktra(graph, 2);
			
			
		      for(int i = 0;i < shortPath.length;i++)  
		    	  if (shortPath[i]!=INF) {
		             System.out.println("从"+"2"+"出发到"+i+"的最短距离为:"+shortPath[i]);    
		    	  }
		    }  
		     
	

}

你可能感兴趣的:(Java学习)