迪杰斯特拉算法实现
Posted womendouyiyang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了迪杰斯特拉算法实现相关的知识,希望对你有一定的参考价值。
public class Dijkstra {//节点为A,B,C,D,E,F 以二维数组graph表示连通图 private static int N = 1000; private static int[][] Graph = { { 0,-1,3,N,N}, { N,0,3,2,2 }, { N,N,0,N,N }, { N,1,5,0,N }, { N,N,N,-3,0}, }; public static void dijkstra(int vs, int[][] Graph) { int NUM = Graph[0].length; // System.out.println(NUM); int[] prenode = new int[NUM]; int[] mindist = new int[NUM]; boolean[] find = new boolean[NUM]; int vnear = 0; for(int i = 0; i < mindist.length; i++) { prenode[i] = i; mindist[i] = Graph[vs][i]; find[i] = false; } find[vs] = true; // System.out.println(Graph.length); for(int v = 1; v < Graph.length; v++) { int min = N; for(int j = 0; j < Graph.length; j++) { if(!find[j] && mindist[j] < min) { min = mindist[j]; vnear = j; } } find[vnear] = true; for(int k = 0; k < Graph.length; k++) { if(!find[k] && (min + Graph[vnear][k]) < mindist[k]) { prenode[k] = vnear; mindist[k] = min + Graph[vnear][k]; } } } int t = ‘A‘; for(int i = 0; i < NUM; i++) System.out.println("A" + "::"+(char)(t+prenode[i]) + "->" +(char)(t+i) + ", s=" + mindist[i]); //System.out.println("v" + vs + "...v" + prenode[i] + "->v" + i + ", s=" + mindist[i]); } public static void main(String[] args) { dijkstra(0, Graph); } }
以上是关于迪杰斯特拉算法实现的主要内容,如果未能解决你的问题,请参考以下文章