January 2018
Intermediate to advanced
332 pages
7h 36m
English
Let's break down the implementation of Dijkstra's algorithm based on the pseudo code described in the preceding section. The first step is to initialize all the variables. We will use one to track the costs of going through each node, one for tracking the path we are taking, and one more to track the already visited nodes to avoid recalculations:
var _ = require('lodash');class Dijkstra { solve (graph, start, end) { // track costs of each node const costs = graph[start]; // set end to infinite on 1st pass costs[end] = Infinity; // remember path from // which each node was visited const paths = {}; // add path for the start nodes neighbors _.forEach(graph[start], (dist, city) => { // e.g. city SJ was visited ...
Read now
Unlock full access