Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 6483ecc

Browse files
authored
DijkstraSmallestPath.js: Standardjs fixes (TheAlgorithms#147)
1 parent ec89a90 commit 6483ecc

File tree

1 file changed

+67
-74
lines changed

1 file changed

+67
-74
lines changed

‎maths/DijkstraSmallestPath.js

Lines changed: 67 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,66 @@
11
// starting at s
2-
function solve(graph, s) {
3-
var solutions = {};
4-
solutions[s] = [];
5-
solutions[s].dist = 0;
6-
7-
while(true) {
8-
var p = null;
9-
var neighbor = null;
10-
var dist = Infinity;
11-
12-
13-
for(var n in solutions) {
14-
if(!solutions[n])
15-
continue
16-
var ndist = solutions[n].dist;
17-
var adj = graph[n];
18-
19-
for(var a in adj) {
2+
function solve (graph, s) {
3+
var solutions = {}
4+
solutions[s] = []
5+
solutions[s].dist = 0
206

21-
if(solutions[a])
22-
continue;
23-
24-
var d = adj[a] + ndist;
25-
if(d < dist) {
26-
27-
p = solutions[n];
28-
neighbor = a;
29-
dist = d;
7+
while (true) {
8+
var p = null
9+
var neighbor = null
10+
var dist = Infinity
11+
12+
for (var n in solutions) {
13+
if (!solutions[n]) { continue }
14+
var ndist = solutions[n].dist
15+
var adj = graph[n]
16+
17+
for (var a in adj) {
18+
if (solutions[a]) { continue }
19+
20+
var d = adj[a] + ndist
21+
if (d < dist) {
22+
p = solutions[n]
23+
neighbor = a
24+
dist = d
3025
}
3126
}
3227
}
33-
34-
//no more solutions
35-
if(dist === Infinity) {
36-
break;
28+
29+
//no more solutions
30+
if(dist === Infinity) {
31+
break
3732
}
38-
39-
//extend parent's solution path
40-
solutions[neighbor] = p.concat(neighbor);
41-
//extend parent's cost
42-
solutions[neighbor].dist = dist;
33+
34+
//extend parent's solution path
35+
solutions[neighbor] = p.concat(neighbor)
36+
//extend parent's cost
37+
solutions[neighbor].dist = dist
4338
}
44-
45-
return solutions;
39+
40+
return solutions
4641
}
47-
//create graph
48-
var graph = {};
42+
//create graph
43+
var graph = {}
4944

5045
var layout = {
51-
'R': ['2'],
52-
'2': ['3','4'],
53-
'3': ['4','6','13'],
54-
'4': ['5','8'],
55-
'5': ['7','11'],
56-
'6': ['13','15'],
57-
'7': ['10'],
58-
'8': ['11','13'],
59-
'9': ['14'],
60-
'10': [],
61-
'11': ['12'],
62-
'12': [],
63-
'13': ['14'],
64-
'14': [],
65-
'15': []
46+
R: ['2'],
47+
2: ['3','4'],
48+
3: ['4','6','13'],
49+
4: ['5','8'],
50+
5: ['7','11'],
51+
6: ['13','15'],
52+
7: ['10'],
53+
8: ['11','13'],
54+
9: ['14'],
55+
10: [],
56+
11: ['12'],
57+
12: [],
58+
13: ['14'],
59+
14: [],
60+
15: []
6661
}
6762

68-
//convert uni-directional to bi-directional graph
63+
//convert uni-directional to bi-directional graph
6964
// var graph = {
7065
// a: {e:1, b:1, g:3},
7166
// b: {a:1, c:1},
@@ -77,27 +72,25 @@ var layout = {
7772
// h: {f:1}
7873
// };
7974

80-
for(var id in layout) {
81-
if(!graph[id])
82-
graph[id] = {};
83-
layout[id].forEach(function(aid) {
84-
graph[id][aid] = 1;
85-
if(!graph[aid])
86-
graph[aid] = {};
87-
graph[aid][id] = 1;
88-
});
75+
for (var id in layout) {
76+
if (!graph[id]) { graph[id] = {} }
77+
layout[id].forEach(function (aid) {
78+
graph[id][aid] = 1
79+
if (!graph[aid]) { graph[aid] = {} }
80+
graph[aid][id] = 1
81+
})
8982
}
9083

91-
//choose start node
92-
var start = '10';
93-
//get all solutions
94-
var solutions = solve(graph, start);
84+
//choose start node
85+
var start = '10'
86+
//get all solutions
87+
var solutions = solve(graph, start)
9588

96-
console.log("From '"+start+"' to");
97-
//display solutions
98-
for(var s in solutions) {
99-
if(!solutions[s]) continue;
100-
console.log(" -> " + s + ": [" + solutions[s].join(", ") + "] (dist:" + solutions[s].dist + ")");
89+
console.log("From '"+start+"' to")
90+
//display solutions
91+
for(var s in solutions) {
92+
if(!solutions[s]) continue
93+
console.log(' -> ' + s + ': [' + solutions[s].join(', ') + '] (dist:' + solutions[s].dist + ')')
10194
}
10295

10396
// From '10' to

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /