How to Implement Dijkstra’s Algorithm in JavaScript


I’ve been reading Grokking Algorithms, which I recommend to anyone new to algorithms. It’s basically the introduction I wish I had a few months ago! The examples in the book are written in Python, so I’d like to share a JavaScript version of Dijkstra’s algorithm. This algorithm uses a directed, weighted graph to determine the “cheapest” path to reach a node.

I’ll be breaking each part into a few steps, with some background information. If you’d prefer to just look at the code, here is the link to the gist.

Background: What is a graph?

A graph is an abstract data type used to model a set of connections. For example, say Bob follows Sarah and Lou on Twitter. Sarah follows Lin. Lou follows Lin and Mark. We can represent these connections using a graph.

Read more at HackerNoon