Dijkstra貪心演算法求單源最短路徑
阿新 • • 發佈:2019-01-06
給定一個帶權有向圖G=(V,E),其中每條邊的權是一個非負實數。另外,還給定V中的一個頂點,稱為源。現在要計算從源到其他所有各頂點的最短路徑長度。這裡的長度就是指路上各邊權之和。
Dijkstra演算法是解單源最短路徑的貪心演算法。
public static void dijkstra(int sourcePoint, float[][] weightMatrix, float[] distance, int[] previousPoints) { // the numbers of points; int pointNumber = distance.length; if (sourcePoint <0 || sourcePoint >= pointNumber) { // the sourcePoint exceed the valid point index; return ; } boolean[] selectedPoints = new boolean[pointNumber]; // initialize three array: // 1. initialize the array of distance from source point to each point; // 2. initialize the array of selected points with false; // 3. initialize the array of previous points with 0; for(int i=0; i< pointNumber; i++) { distance[i] = weightMatrix[sourcePoint][i]; selectedPoints[i] = false; if (Math.abs(distance[i]- Float.MAX_VALUE) < 0.0001f ) { previousPoints[i] = -1; }else { // there is a line link between sourcePoint and currentPoint; previousPoints[i] = sourcePoint; } } // step 1: put source point into selected set; selectedPoints[sourcePoint] = true; // this variable store next candidate point which has shortest distance; for(int i=0; i< pointNumber; i++) { // a temp variable that store the shortest distance; initialize value with float max. float tempShortestDist = Float.MAX_VALUE; int candidatePoint = sourcePoint; // step 2: find a point which has shortest distance; // traverse the set of All Points minus Selected Points and calculate the shorted distance from source to each point for (int j=0; j<pointNumber; j++) { if(!selectedPoints[j] && distance[j] < tempShortestDist) { candidatePoint = j; tempShortestDist = distance[j]; } } // step3 : add the candidate point to selected set; selectedPoints[candidatePoint] = true; // step 4: calculate the distance of SPECIAL Path that start from candidatePoint to each point; for (int j=0; j<pointNumber; j++) { if (!selectedPoints[j] && weightMatrix[candidatePoint][j] < Float.MAX_VALUE) { // point j is not in selected points set and there is a line between point candidatePoint and j; float newDistance = distance[candidatePoint] + weightMatrix[candidatePoint][j]; if (newDistance < distance[j]) { // the distance from cadidatePoint is less than before(start from previous point); distance[j] = newDistance; previousPoints[j] = candidatePoint; } } }// end of for }// end of for }