The
Implementation code is as follows:
var n = 3;
var dis = [0];
var mark = [];
var arr = [];
var inf = 9999;
var I, J, K;
init for (i = 0; i < n; i++) {Mark.push (1);
Arr[i] = [];
for (j = 0; J < N; j +) {Arr[i][j] = inf;//i->j,init 9999}} arr[0][1] = 3;
ARR[1][2] =-2;
ARR[0][2] = 6;
init dis dis[0] = 0;
for (j = 1; J < Arr.length J + +) {Dis[j] = arr[0][j];
} Mark[0] = 0; for (i = 0; i < arr.length-1; i++) {//Remove the source point itself, select the point from the remaining point to the smallest distance from the source point, so just do the n-1 wheel lookup var min = [0, 0, inf];//src,dis,w Eight for (j = 0; J < Arr.length; J +) {//Find the minimum if (Mark[j]) {if (arr I
[j] < min[2]) {min = [I, J, Arr[i][j]]; }} Mark[min[1]] = 0;//the dis smallest point min[1] marked as selected for (k = 0; k < arr.length; k++) {//ratio
Distance from min[1]->k + source point to min[1] dis[[min[1]], less than dis[k if (Arr[min[1]][k] + dis[min[1]] < Dis[k]) {dis[k] = Arr[min[1]][k] + dis[min[1]];//Slack} } console.log (DIS);
The
Dijkstra algorithm is to find a single source shortest path, the idea is to use a dis array to save the source point to the distance (the source point to its own distance of 0), and then each take the point of the smallest distance, all the edges of this point to relax.
The so-called relaxation (relaxation), such as 1->3=10,1->2->3=5, then 1->3 can actually be shortened to 5, which is called the Edge 1->3 for a round of slack operation.
I've been watching "aha." Algorithm, the book says Dijkstra algorithm can not have negative edge, I use JS to write this, try the negative right side seems to be possible, just can not have negative power loop, and then read the book Bellman-ford algorithm, I think in addition to the negative weight loop judgment as if and Dijkstra algorithm is no different ... Well, I guess I didn't understand. = =
I used JS to achieve the judgement of the negative power loop, the code is as follows:
var n = 3;
var dis = [0];
var mark = [];
var arr = [];
var inf = 9999;
var I, J, K;
init for (i = 0; i < n; i++) {Mark.push (1);
Arr[i] = [];
for (j = 0; J < N; j +) {Arr[i][j] = inf;//i->j,init 9999}} arr[0][1] = 3;
ARR[1][2] = 2;
ARR[0][2] = 6;
Arr[2][0] =-1;
init dis dis[0] = 0;
for (j = 1; J < Arr.length J + +) {Dis[j] = arr[0][j];
} Mark[0] = 0; for (i = 0; i < arr.length-1; i++) {//Remove the source point itself, select the point from the remaining point to the smallest distance from the source point, so just do the n-1 wheel lookup var min = [0, 0, inf];//src,dis,w Eight for (j = 0; J < Arr.length; J +) {//Find the minimum if (Mark[j]) {if (arr I
[j] < min[2]) {min = [I, J, Arr[i][j]]; }} Mark[min[1]] = 0;//the dis smallest point min[1] marked as selected for (k = 0; k < arr.length; k++) {//ratio than min[1]->k + source point to min[1] distance dis[[min[1]], is it more than dis[K] Distance to small if (Arr[min[1]][k] + dis[min[1]] < Dis[k]) {dis[k] = Arr[min[1]][k] + dis[min[1]];
Slack Arr[0][k] = dis[k];//modifies the array value at the same time to determine whether there is a negative loop}} console.log (dis); /* Detect if there is a negative right loop * * for (i = 0; i < arr.length; i++) {for (j = 0; J < Arr.length; j) {if arr[
0][J] > Dis[i] + arr[i][j]) {console.log ("negative loop", "0->", I, "->", j);
Break }
}
}