Dijkstra's algorithm for finding the shortest path

content

Algorithm introduction

Applications

Algorithm step

Code


 

Algorithm introduction

Dijkstra's algorithm is a typical shortest path algorithm , which is used to calculate the shortest path from one node to other nodes. Its main feature is that it expands from the starting point to the outer layer (the idea of ​​breadth-first search) until it reaches the end point.

Applications

watermark,type_d3F5LXplbmhlaQ,shadow_50,text_Q1NETiBA5rGC5LiN6ISx5Y-R,size_20,color_FFFFFF,t_70,g_se,x_16

 

Algorithm step

1) Set the starting vertex as v, the vertex set VfvI, v2, vi.), the distance from v to each vertex of V constitutes the distance set Dis, Dis ( dI, d2, di.), Dis set records v to each vertex in the graph The distance of (to itself can be regarded as 0, the distance from v to vi corresponds to di)
2) Select the di with the smallest value from Dis and move out of the Dis set, and at the same time move out the corresponding item point vi in ​​the V set, at this time v to vi That is, the shortest path
3) Update the Dis set. The update rule is: compare the distance value from v to the vertices in the V set, and the distance from v to the vertices in the V set through vi, and keep
the one with the smaller value (the vertex should also be updated at the same time). The predecessor node of is vi, indicating that it is reached through vi)
4) Repeat the two steps until the shortest path vertex is the target vertex.

Code


import java.util.Arrays;

public class _最短路{
	static int[] vis;//标记已经访问的顶点  0未访问 1 访问
	static int[] dis;//出发顶点到各个下标对应顶点的最短距离
	static int[] pre;//每个下标对应的上一个顶点下标
	static char[] vertex;//顶点
	static int[][] matrix;//邻接矩阵
	public static void main(String[] args) {
		vertex= new char[]{'A','B','C','D','E','F','G'};
		matrix = new int[vertex.length][vertex.length];
		chushihua(matrix);//初始化邻接矩阵
		djstl(vertex.length,6);//调用算法
	}
	public static void djstl(int length,int start) {
		vis=new int[length];
		dis=new int[length];
		pre=new int[length];
		Arrays.fill(dis, 9999);//初始化距离为较大值
		dis[start] = 0;//初始化出发顶点到自身的距离0
		
		/*  先将起始点到与其连通的顶点的路径及pre前一个顶点进行更新*/
		update(start);
		//在以与起始点相连的顶点为起点  更新距离和路径
		for (int i = 1; i < vertex.length; i++) {
			int minIndex = -1;
			int mindis=9999;
			//找到一个最短路径
			for (int j = 0; j < vertex.length; j++) {
				if(vis[j]==0 && dis[j] < mindis) {
					minIndex = j;
					mindis = dis[j];
				}
			}
			vis[minIndex] = 1;
			update(minIndex);//继续更新
		}
		
		System.out.println(Arrays.toString(dis));
	}
	/**
	 * 以index顶点向下查找!!!以起点start到index附近的邻接结点的最短路径!!!
	 * @param index
	 */
	public static void update(int index) {
		vis[index] = 1;//index标记为已访问
		int len= 0;//len:从start顶点到index顶点的距离+上从index再到i顶点的距离
		//循环遍历每个邻接结点顶点,找到真正意义上的最短路径
		for (int i = 0; i < matrix[index].length; i++) {
			//记录从start顶点到index顶点的距离+上从index再到i顶点的距离
			len = dis[index] + matrix[index][i];
			//将dis[i] 即从start直接到i 的距离 与len进行比较 
			if(vis[i] == 0 && len < dis[i]) {
				dis[i] = len;//更新最短路径
				pre[i] = index;//更新前置顶点
			}
		}
	}
	/**
	 * 初始化邻接矩阵
	 * @param matrix
	 */
	public static void chushihua(int[][] matrix) {
		final int N = 9999;
		matrix[0]=new int[]{N,5,7,N,N,N,2};
		matrix[1]=new int[]{5,N,N,9,N,N,3};
		matrix[2]=new int[]{7,N,N,N,8,N,N};
		matrix[3]=new int[]{N,9,N,N,N,4,N};
		matrix[4]=new int[]{N,N,8,N,N,5,4};
		matrix[5]=new int[]{N,N,N,4,5,N,6};
		matrix[6]=new int[]{2,3,N,N,4,6,N};
	}
}

 

 

 

Guess you like

Origin blog.csdn.net/qq_52360069/article/details/123686090