-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dijkstra.js
41 lines (41 loc) · 1.17 KB
/
Dijkstra.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
var INF=Number.MAX_SAFE_INTEGER;
function Graph(){
this.graph=[[0,2,4,0,0,0],
[0,0,1,4,2,0],
[0,0,0,0,3,0],
[0,0,0,0,0,2],
[0,0,0,3,0,2],
[0,0,0,0,0,0]];
var minDistance=function(dist,visited){
var min=INF,minIndex=-1;
for(var v=0;v<dist.length;v++){
if(visited[v]==false&&dist[v]<=min){
min=dist[v];
minIndex=v;
}
}
return minIndex;
};
this.dijkstra=function(src){
var dist=[],visited=[],
length=this.graph.length;
for(var i=0;i<length;i++){
dist[i]=INF;
visited[i]=false;
}
dist[src]=0;
for(var i=0;i<length-1;i++){
var u=minDistance(dist,visited);
visited[u]=true;
for(var v=0;v<length;v++){
if(!visited[v]&&this.graph[u][v]!=0&&dist[u]!=INF&&
dist[u]+this.graph[u][v]<dist[v]){
dist[v]=dist[u]+this.graph[u][v];
}
}
}
return dist;
};
}
var graph=new Graph();
var result=graph.dijkstra();