## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

No credit card required

# Implementing Dijkstra's algorithm

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 ...`

## With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

No credit card required