Fixed formatting problems, added JSDoc

This commit is contained in:
Alexandrshy
2019-07-25 08:53:03 +04:00
parent bac32b613b
commit 2216e23b6b
2 changed files with 38 additions and 30 deletions

View File

@@ -21,21 +21,25 @@ costs.fin = Infinity;
// the parents table // the parents table
const parents = {}; const parents = {};
parents.a = 'start'; parents.a = "start";
parents.b = 'start'; parents.b = "start";
parents.fin = null; parents.fin = null;
let processed = []; let processed = [];
/**
const findLowestCostNode = (itCosts) => { * Find the lowest node
* @param {Object} itCosts Hash table
* @returns {(string|null)} The lowest node
*/
const findLowestCostNode = itCosts => {
let lowestCost = Infinity; let lowestCost = Infinity;
let lowestCostNode = null; let lowestCostNode = null;
Object.keys(itCosts).forEach((node) => { Object.keys(itCosts).forEach(node => {
const cost = itCosts[node]; const cost = itCosts[node];
// If it's the lowest cost so far and hasn't been processed yet... // If it's the lowest cost so far and hasn't been processed yet...
if (cost < lowestCost && (processed.indexOf(node) === -1)) { if (cost < lowestCost && processed.indexOf(node) === -1) {
// ... set it as the new lowest-cost node. // ... set it as the new lowest-cost node.
lowestCost = cost; lowestCost = cost;
lowestCostNode = node; lowestCostNode = node;
@@ -50,7 +54,7 @@ while (node !== null) {
const cost = costs[node]; const cost = costs[node];
// Go through all the neighbors of this node // Go through all the neighbors of this node
const neighbors = graph[node]; const neighbors = graph[node];
Object.keys(neighbors).forEach((n) => { Object.keys(neighbors).forEach(n => {
const newCost = cost + neighbors[n]; const newCost = cost + neighbors[n];
// If it's cheaper to get to this neighbor by going through this node // If it's cheaper to get to this neighbor by going through this node
if (costs[n] > newCost) { if (costs[n] > newCost) {
@@ -68,5 +72,5 @@ while (node !== null) {
node = findLowestCostNode(costs); node = findLowestCostNode(costs);
} }
console.log('Cost from the start to each node:'); console.log("Cost from the start to each node:");
console.log(costs); // { a: 5, b: 2, fin: 6 } console.log(costs); // { a: 5, b: 2, fin: 6 }

View File

@@ -1,4 +1,4 @@
'use strict'; "use strict";
// the graph // the graph
const graph = {}; const graph = {};
@@ -17,44 +17,48 @@ graph["fin"] = {};
// The costs table // The costs table
const costs = {}; const costs = {};
costs['a'] = 6; costs["a"] = 6;
costs['b'] = 2; costs["b"] = 2;
costs['fin'] = Infinity; costs["fin"] = Infinity;
// the parents table // the parents table
const parents = {}; const parents = {};
parents['a'] = 'start'; parents["a"] = "start";
parents['b'] = 'start'; parents["b"] = "start";
parents['fin'] = null; parents["fin"] = null;
let processed = []; let processed = [];
/**
function find_lowest_cost_node(costs) { * Find the lowest node
let lowest_cost = Infinity; * @param {Object} itCosts Hash table
let lowest_cost_node = null; * @returns {(string|null)} The lowest node
*/
function findLowestCostNode(costs) {
let lowestCost = Infinity;
let lowestCostNode = null;
// Go through each node // Go through each node
for (let node in costs) { for (let node in costs) {
let cost = costs[node]; const cost = costs[node];
// If it's the lowest cost so far and hasn't been processed yet... // If it's the lowest cost so far and hasn't been processed yet...
if (cost < lowest_cost && (processed.indexOf(node) === -1)) { if (cost < lowestCost && processed.indexOf(node) === -1) {
// ... set it as the new lowest-cost node. // ... set it as the new lowest-cost node.
lowest_cost = cost; lowestCost = cost;
lowest_cost_node = node; lowestCostNode = node;
} }
} }
return lowest_cost_node; return lowestCostNode;
} }
let node = find_lowest_cost_node(costs); let node = findLowestCostNode(costs);
while (node !== null) { while (node !== null) {
let cost = costs[node]; const cost = costs[node];
// Go through all the neighbors of this node // Go through all the neighbors of this node
let neighbors = graph[node]; const neighbors = graph[node];
Object.keys(neighbors).forEach(function(n) { Object.keys(neighbors).forEach(function(n) {
let new_cost = cost + neighbors[n]; const new_cost = cost + neighbors[n];
// If it's cheaper to get to this neighbor by going through this node // If it's cheaper to get to this neighbor by going through this node
if (costs[n] > new_cost) { if (costs[n] > new_cost) {
// ... update the cost for this node // ... update the cost for this node
@@ -66,9 +70,9 @@ while (node !== null) {
// Mark the node as processed // Mark the node as processed
processed = processed.concat(node); processed = processed.concat(node);
// Find the next node to process, and loop // Find the next node to process, and loop
node = find_lowest_cost_node(costs); node = findLowestCostNode(costs);
} }
console.log("Cost from the start to each node:"); console.log("Cost from the start to each node:");