Skip to content

DijkstraSmallestPath.js: Standardjs fixes #147

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 4, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 67 additions & 74 deletions maths/DijkstraSmallestPath.js
Original file line number Diff line number Diff line change
@@ -1,71 +1,66 @@
// starting at s
function solve(graph, s) {
var solutions = {};
solutions[s] = [];
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) {
function solve (graph, s) {
var solutions = {}
solutions[s] = []
solutions[s].dist = 0

if(solutions[a])
continue;

var d = adj[a] + ndist;
if(d < dist) {

p = solutions[n];
neighbor = a;
dist = d;
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]) { continue }

var d = adj[a] + ndist
if (d < dist) {
p = solutions[n]
neighbor = a
dist = d
}
}
}
//no more solutions
if(dist === Infinity) {
break;

// no more solutions
if (dist === Infinity) {
break
}
//extend parent's solution path
solutions[neighbor] = p.concat(neighbor);
//extend parent's cost
solutions[neighbor].dist = dist;

// extend parent's solution path
solutions[neighbor] = p.concat(neighbor)
// extend parent's cost
solutions[neighbor].dist = dist
}
return solutions;

return solutions
}
//create graph
var graph = {};
// create graph
var graph = {}

var layout = {
'R': ['2'],
'2': ['3','4'],
'3': ['4','6','13'],
'4': ['5','8'],
'5': ['7','11'],
'6': ['13','15'],
'7': ['10'],
'8': ['11','13'],
'9': ['14'],
'10': [],
'11': ['12'],
'12': [],
'13': ['14'],
'14': [],
'15': []
R: ['2'],
2: ['3', '4'],
3: ['4', '6', '13'],
4: ['5', '8'],
5: ['7', '11'],
6: ['13', '15'],
7: ['10'],
8: ['11', '13'],
9: ['14'],
10: [],
11: ['12'],
12: [],
13: ['14'],
14: [],
15: []
}

//convert uni-directional to bi-directional graph
// convert uni-directional to bi-directional graph
// var graph = {
// a: {e:1, b:1, g:3},
// b: {a:1, c:1},
Expand All @@ -77,27 +72,25 @@ var layout = {
// h: {f:1}
// };

for(var id in layout) {
if(!graph[id])
graph[id] = {};
layout[id].forEach(function(aid) {
graph[id][aid] = 1;
if(!graph[aid])
graph[aid] = {};
graph[aid][id] = 1;
});
for (var id in layout) {
if (!graph[id]) { graph[id] = {} }
layout[id].forEach(function (aid) {
graph[id][aid] = 1
if (!graph[aid]) { graph[aid] = {} }
graph[aid][id] = 1
})
}

//choose start node
var start = '10';
//get all solutions
var solutions = solve(graph, start);
// choose start node
var start = '10'
// get all solutions
var solutions = solve(graph, start)

console.log("From '"+start+"' to");
//display solutions
for(var s in solutions) {
if(!solutions[s]) continue;
console.log(" -> " + s + ": [" + solutions[s].join(", ") + "] (dist:" + solutions[s].dist + ")");
console.log("From '" + start + "' to")
// display solutions
for (var s in solutions) {
if (!solutions[s]) continue
console.log(' -> ' + s + ': [' + solutions[s].join(', ') + '] (dist:' + solutions[s].dist + ')')
}

// From '10' to
Expand Down