# java implementation of dijastra shortest path algorithm

Algorithmic idea

Extend layer by layer from the starting point to the end point.

Main steps of the algorithm

1. Construct a two-dimensional array weight to store the undirected graph. weight[i][j] represents the weight from node i to node j, that is, the distance from node i to node j (hereinafter, dij).

2. Build the array shortpath, store the shortest distance from the starting node (0) to each node, that is, d0j(j is all nodes), initialize the shortpath[0] = 0, and other values are infinite.

3. Build the array visited, mark whether each node has been extended (assume 0 is extended, 1 is extended), initialize visited[0] = 1, and other values are zero.

4. The iterative algorithm traverses the two-dimensional array weight, selects the unmarked node K which is the shortest distance from the start node, records d0k to the short path, and marks K as extended,

Update the distance from the start node to other nodes through K. if d0j > d0k + DKJ, d0j = d0k + dkj.

Code implementation

``````public static  int[] Dijsktra(int [][] weight,int start){

int length = weight.length;//Get the number of vertices

int[] shortPath = new int[length];//Shortest path array

shortPath[0] = 0;//

String[] path = new String[length];//Record the shortest path from the starting point to each point

for(int i = 0 ; i < length ; i++){

path[i] = start + "->" + i;
}

int[] visited = new int[length];//Record whether the shortest path of the current vertex has been found. 1 means it has been found

for(int i = 0 ; i < length ; i++){

visited[i] = 0;
}

visited[0] = 1;//The shortest path to the starting point has been found

for(int m = 1 ; m < length ; m ++){

int k = -1;

int dmin = Integer.MAX_VALUE;

//Select an unmarked vertex closest to the starting point, and the shortest path to the starting point is dmin
for(int n = 0 ; n < length ; n++){

if(visited[n] == 0 && weight[start][n] < dmin){

dmin = weight[start][n];

k = n;
}
}

shortPath[k] = dmin;

visited[k] = 1;

//with k For the middle point, update the distance from the starting point to other unmarked points
for(int j = 0 ; j < length ; j++){

if(visited[j] == 0 && weight[k][j] != Integer.MAX_VALUE && weight[start][k] + weight[k][j] < weight[start][j]){

weight[start][j] = weight[start][k] + weight[k][j];

path[j] = path[k] + "->" + j;
}
}
}

for(int i = 1 ; i < length ; i ++){

System.out.println("Starting point to" + i + "The shortest path for is:" + path[i] + "The distance is:" + shortPath[i]);
}
return shortPath;
}``````

Declare constant

``public static final int MAX = Integer.MAX_VALUE;``

Code test

``````public static void main(String[] args) {
int[][] weigth = {{0,50,70,MAX,MAX},
{50,0,15,30,MAX},
{70,15,0,MAX,40},
{MAX,30,MAX,0,20},
{MAX,MAX,40,20,0}};

Dijsktra(weigth,0);
}``````

Operation results

The shortest path from the starting point to 1 is: 0 - > 1 the distance is: 50
The shortest path from the starting point to 2 is: 0 - > 1 - > 2 and the distance is: 65
The shortest path from the starting point to 3 is: 0 - > 1 - > 3 the distance is: 80
The shortest path from the starting point to 4 is: 0 - > 1 - > 3 - > 4 the distance is: 100

Posted by sxiix on Mon, 11 May 2020 09:30:13 -0700