mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-07 11:08:54 +08:00
DijkstraSmallestPath
after fixing some errors.
This commit is contained in:
@ -1,11 +1,12 @@
|
|||||||
|
// starting at s
|
||||||
function solve(graph, s) {
|
function solve(graph, s) {
|
||||||
var solution = {};
|
var solutions = {};
|
||||||
solutions[s] = [];
|
solutions[s] = [];
|
||||||
solutions[s].dist = 0;
|
solutions[s].dist = 0;
|
||||||
|
|
||||||
while(true) {
|
while(true) {
|
||||||
var parent = null;
|
var p = null;
|
||||||
var nearest = null;
|
var neighbor = null;
|
||||||
var dist = Infinity;
|
var dist = Infinity;
|
||||||
|
|
||||||
|
|
||||||
@ -14,39 +15,38 @@ function solve(graph, s) {
|
|||||||
continue
|
continue
|
||||||
var ndist = solutions[n].dist;
|
var ndist = solutions[n].dist;
|
||||||
var adj = graph[n];
|
var adj = graph[n];
|
||||||
//for each of its adjacent nodes...
|
|
||||||
for(var a in adj) {
|
for(var a in adj) {
|
||||||
//without a solution already...
|
|
||||||
if(solutions[a])
|
if(solutions[a])
|
||||||
continue;
|
continue;
|
||||||
//choose nearest node with lowest *total* cost
|
|
||||||
var d = adj[a] + ndist;
|
var d = adj[a] + ndist;
|
||||||
if(d < dist) {
|
if(d < dist) {
|
||||||
//reference parent
|
|
||||||
parent = solutions[n];
|
p = solutions[n];
|
||||||
nearest = a;
|
neighbor = a;
|
||||||
dist = d;
|
dist = d;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//no more solutions
|
||||||
if(dist === Infinity) {
|
if(dist === Infinity) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
solutions[nearest] = parent.concat(nearest);
|
//extend parent's solution path
|
||||||
|
solutions[neighbor] = p.concat(neighbor);
|
||||||
solutions[nearest].dist = dist;
|
//extend parent's cost
|
||||||
|
solutions[neighbor].dist = dist;
|
||||||
}
|
}
|
||||||
|
|
||||||
return solutions;
|
return solutions;
|
||||||
}
|
}
|
||||||
|
//create graph
|
||||||
var graph = {};
|
var graph = {};
|
||||||
|
|
||||||
\\create graph
|
|
||||||
|
|
||||||
var layout = {
|
var layout = {
|
||||||
'R': ['2'],
|
'R': ['2'],
|
||||||
'2': ['3','4'],
|
'2': ['3','4'],
|
||||||
@ -66,7 +66,6 @@ var layout = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
//convert uni-directional to bi-directional graph
|
//convert uni-directional to bi-directional graph
|
||||||
// needs to look like: where: { a: { b: cost of a->b }
|
|
||||||
// var graph = {
|
// var graph = {
|
||||||
// a: {e:1, b:1, g:3},
|
// a: {e:1, b:1, g:3},
|
||||||
// b: {a:1, c:1},
|
// b: {a:1, c:1},
|
||||||
@ -100,3 +99,20 @@ for(var s in solutions) {
|
|||||||
if(!solutions[s]) continue;
|
if(!solutions[s]) continue;
|
||||||
console.log(" -> " + s + ": [" + solutions[s].join(", ") + "] (dist:" + solutions[s].dist + ")");
|
console.log(" -> " + s + ": [" + solutions[s].join(", ") + "] (dist:" + solutions[s].dist + ")");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// From '10' to
|
||||||
|
// -> 2: [7, 5, 4, 2] (dist:4)
|
||||||
|
// -> 3: [7, 5, 4, 3] (dist:4)
|
||||||
|
// -> 4: [7, 5, 4] (dist:3)
|
||||||
|
// -> 5: [7, 5] (dist:2)
|
||||||
|
// -> 6: [7, 5, 4, 3, 6] (dist:5)
|
||||||
|
// -> 7: [7] (dist:1)
|
||||||
|
// -> 8: [7, 5, 4, 8] (dist:4)
|
||||||
|
// -> 9: [7, 5, 4, 3, 13, 14, 9] (dist:7)
|
||||||
|
// -> 10: [] (dist:0)
|
||||||
|
// -> 11: [7, 5, 11] (dist:3)
|
||||||
|
// -> 12: [7, 5, 11, 12] (dist:4)
|
||||||
|
// -> 13: [7, 5, 4, 3, 13] (dist:5)
|
||||||
|
// -> 14: [7, 5, 4, 3, 13, 14] (dist:6)
|
||||||
|
// -> 15: [7, 5, 4, 3, 6, 15] (dist:6)
|
||||||
|
// -> R: [7, 5, 4, 2, R] (dist:5)
|
||||||
|
Reference in New Issue
Block a user