mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-05 16:26:47 +08:00
Changed var to const/let (#322)
* varToLet * Update DijkstraSmallestPath.js Co-authored-by: Aayushadh <ayushharwani2011@gmail.com>
This commit is contained in:
@ -1,23 +1,23 @@
|
|||||||
// starting at s
|
// starting at s
|
||||||
function solve (graph, s) {
|
function solve (graph, s) {
|
||||||
var solutions = {}
|
const solutions = {}
|
||||||
solutions[s] = []
|
solutions[s] = []
|
||||||
solutions[s].dist = 0
|
solutions[s].dist = 0
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
var p = null
|
let p = null
|
||||||
var neighbor = null
|
let neighbor = null
|
||||||
var dist = Infinity
|
let dist = Infinity
|
||||||
|
|
||||||
for (var n in solutions) {
|
for (const n in solutions) {
|
||||||
if (!solutions[n]) { continue }
|
if (!solutions[n]) { continue }
|
||||||
var ndist = solutions[n].dist
|
const ndist = solutions[n].dist
|
||||||
var adj = graph[n]
|
const adj = graph[n]
|
||||||
|
|
||||||
for (var a in adj) {
|
for (const a in adj) {
|
||||||
if (solutions[a]) { continue }
|
if (solutions[a]) { continue }
|
||||||
|
|
||||||
var d = adj[a] + ndist
|
const d = adj[a] + ndist
|
||||||
if (d < dist) {
|
if (d < dist) {
|
||||||
p = solutions[n]
|
p = solutions[n]
|
||||||
neighbor = a
|
neighbor = a
|
||||||
@ -40,9 +40,9 @@ function solve (graph, s) {
|
|||||||
return solutions
|
return solutions
|
||||||
}
|
}
|
||||||
// create graph
|
// create graph
|
||||||
var graph = {}
|
const graph = {}
|
||||||
|
|
||||||
var layout = {
|
const layout = {
|
||||||
R: ['2'],
|
R: ['2'],
|
||||||
2: ['3', '4'],
|
2: ['3', '4'],
|
||||||
3: ['4', '6', '13'],
|
3: ['4', '6', '13'],
|
||||||
@ -61,7 +61,7 @@ var layout = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// convert uni-directional to bi-directional graph
|
// convert uni-directional to bi-directional graph
|
||||||
// var graph = {
|
// let 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},
|
||||||
// c: {b:1, d:1},
|
// c: {b:1, d:1},
|
||||||
@ -72,7 +72,7 @@ var layout = {
|
|||||||
// h: {f:1}
|
// h: {f:1}
|
||||||
// };
|
// };
|
||||||
|
|
||||||
for (var id in layout) {
|
for (const id in layout) {
|
||||||
if (!graph[id]) { graph[id] = {} }
|
if (!graph[id]) { graph[id] = {} }
|
||||||
layout[id].forEach(function (aid) {
|
layout[id].forEach(function (aid) {
|
||||||
graph[id][aid] = 1
|
graph[id][aid] = 1
|
||||||
@ -82,13 +82,13 @@ for (var id in layout) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// choose start node
|
// choose start node
|
||||||
var start = '10'
|
const start = '10'
|
||||||
// get all solutions
|
// get all solutions
|
||||||
var solutions = solve(graph, start)
|
const solutions = solve(graph, start)
|
||||||
|
|
||||||
console.log("From '" + start + "' to")
|
console.log("From '" + start + "' to")
|
||||||
// display solutions
|
// display solutions
|
||||||
for (var s in solutions) {
|
for (const 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 + ')')
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user