FIG single source shortest path (the Dijkstra algorithm)

Single-source shortest path problem

        If the path to the other vertex (end point) of a vertex from the graph (source) may be more than one, how to find a path such that the sum of the weights of each edge along this path is minimized.

Dijkstra's algorithm Origin

        Dijkstra (Dijkstra) was presented in 1959 by the Dutch computer scientist Dick Stella, so called Dijkstra algorithm. The rest is from one vertex to the vertices of the shortest path algorithm to solve the shortest path problem is entitled to the figure. Dijkstra's algorithm is the main features from the starting point, using the greedy algorithm strategy, each traversed node adjacent to the apex of the starting point and not the nearest visited until extended to the end point.

Specific idea of ​​the algorithm

Auxiliary array

① using a path [] array to store path: path [a] = b represents the vertex shortest path vertex a is B;
② the shortest distance a info [] array to store the respective vertex to the source point, and this also has a marked effect of the array, i.e., info [x] = 0, x denotes a vertex it has in the path.

specific process

① auxiliary array initialization, determining source points (orign)

Structure ② in FIG shortest distance to be updated auxiliary array:
Ⅰ, to a point that can be reached: info [x] = map [ orign] [x]; these points and the pre-set source point : path [X] = orign
ⅱ, for inaccessible points: info [x] = ∞

③ Location info minimum point in the array weights (except 0,0 indicates addition) (min), remove the minimum weight minInfo, the points added to the path, provided info [min] = 0, and the vertex according min, auxiliary not added vertex array is updated:
If minInfo + map [min] [x ] <info [x], so that the info [x] = minInfo + map [min] [x], and forwards the vertex of the path x top point is set to min vertices
Ps: this step may be right minInfo stores a value corresponding to the size, i.e. the source point to the point of the shortest path length.

Repeat steps ④ ③, until all vertices have come to the shortest path, i.e., the array info [] are all zero values

Output path

Specific information stored in the route path [] array: the shortest path in front of the current path vertex
may be taken using the characteristics of the stack wherein the data out:

path[] = dijkstra(sMap, 1);
int end = 5;
//借助栈的特性
int stack[] = new int[sMap.num];
int top = 0;
end--;	//从零开始
while(end != -1) {
   	stack[top++] = end;
   	end = path[end];
}
System.out.print(stack[--top]+1);
while(top > 0) {
   	//位置比索引大一
   	System.out.print("---->"+(stack[--top]+1));
}

Code

/**
  * 得出最小路径表
  * @param map 用二维数组存储的图结构
  * @return 路径数组
  */
 public static int[] dijkstra(SimpleMap sMap,int first) {
  	//路径数组
  	int[] path = new int[sMap.num];
  	//标记与权值数组(权值为0,表示已加入路径)
  	int[] markAndInfo = new int[sMap.num];
  	//辅助数组初始化
  	for(int i=0; i<sMap.num; i++) {
   		path[i] = -1;
   		markAndInfo[i]= CreateAMap.MAX;
 	}
  	//定位初始位置(从零开始)
  	first = first-1;
  	for(int j=0; j<sMap.num; j++) {
   		if(j != first) {
    			path[j] = first;
   		}
   		markAndInfo[j]= sMap.map[first][j];
  	}
  	//已经加入到路径的顶点
  	int judge = 1;
  	while(judge < sMap.num) {
   		//从权值数组中找出最小的的值
   		int index = 0;
   		int min = CreateAMap.MAX;
   		for(int i=0; i<sMap.num; i++) {
    			//需判断当前点是否已经在路径中
    			if(markAndInfo[i] != 0 && markAndInfo[i] < min) {
     				index = i;
     				min = markAndInfo[i];
    			}
	   	}	
   		//更新权值数组以及路径数组
   		markAndInfo[index] = 0;
   		for (int i = 0; i < sMap.num; i++) {    
    				if(markAndInfo[i] !=0 && min+sMap.map[index][i] < markAndInfo[i]) {
     					markAndInfo[i] =  min+sMap.map[index][i];
     					path[i] = index;
    				}
   		}
   		judge++;
  	}
  	return path;
 }

And FIG test results:
Here Insert Picture Description
source 1, the end 5:
Here Insert Picture Description

Published 88 original articles · won praise 2 · Views 1734

Guess you like

Origin blog.csdn.net/qq_41891805/article/details/105308790