DijkstraSmallestPath.js: Standardjs fixes (#147)

This commit is contained in:
Christian Clauss
2020-05-04 22:31:34 +02:00
committed by GitHub
parent ec89a904a2
commit 6483ecc427

View File

@ -1,71 +1,66 @@
// starting at s // starting at s
function solve(graph, s) { function solve (graph, s) {
var solutions = {}; var solutions = {}
solutions[s] = []; solutions[s] = []
solutions[s].dist = 0; solutions[s].dist = 0
while(true) {
var p = null;
var neighbor = null;
var dist = Infinity;
for(var n in solutions) {
if(!solutions[n])
continue
var ndist = solutions[n].dist;
var adj = graph[n];
for(var a in adj) {
if(solutions[a]) while (true) {
continue; var p = null
var neighbor = null
var d = adj[a] + ndist; var dist = Infinity
if(d < dist) {
for (var n in solutions) {
p = solutions[n]; if (!solutions[n]) { continue }
neighbor = a; var ndist = solutions[n].dist
dist = d; var adj = graph[n]
for (var a in adj) {
if (solutions[a]) { continue }
var d = adj[a] + ndist
if (d < dist) {
p = solutions[n]
neighbor = a
dist = d
} }
} }
} }
//no more solutions // no more solutions
if(dist === Infinity) { if (dist === Infinity) {
break; break
} }
//extend parent's solution path // extend parent's solution path
solutions[neighbor] = p.concat(neighbor); solutions[neighbor] = p.concat(neighbor)
//extend parent's cost // extend parent's cost
solutions[neighbor].dist = dist; solutions[neighbor].dist = dist
} }
return solutions; return solutions
} }
//create graph // create graph
var graph = {}; var graph = {}
var layout = { var layout = {
'R': ['2'], R: ['2'],
'2': ['3','4'], 2: ['3', '4'],
'3': ['4','6','13'], 3: ['4', '6', '13'],
'4': ['5','8'], 4: ['5', '8'],
'5': ['7','11'], 5: ['7', '11'],
'6': ['13','15'], 6: ['13', '15'],
'7': ['10'], 7: ['10'],
'8': ['11','13'], 8: ['11', '13'],
'9': ['14'], 9: ['14'],
'10': [], 10: [],
'11': ['12'], 11: ['12'],
'12': [], 12: [],
'13': ['14'], 13: ['14'],
'14': [], 14: [],
'15': [] 15: []
} }
//convert uni-directional to bi-directional graph // convert uni-directional to bi-directional graph
// 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},
@ -77,27 +72,25 @@ var layout = {
// h: {f:1} // h: {f:1}
// }; // };
for(var id in layout) { for (var id in layout) {
if(!graph[id]) if (!graph[id]) { graph[id] = {} }
graph[id] = {}; layout[id].forEach(function (aid) {
layout[id].forEach(function(aid) { graph[id][aid] = 1
graph[id][aid] = 1; if (!graph[aid]) { graph[aid] = {} }
if(!graph[aid]) graph[aid][id] = 1
graph[aid] = {}; })
graph[aid][id] = 1;
});
} }
//choose start node // choose start node
var start = '10'; var start = '10'
//get all solutions // get all solutions
var solutions = solve(graph, start); var solutions = solve(graph, start)
console.log("From '"+start+"' to"); console.log("From '" + start + "' to")
//display solutions // display solutions
for(var s in solutions) { 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 // From '10' to