Djikstra's Algorithm

We can't all be graph theory geniuses like my friend Nathan Klein, but...we can learn how to make graphs in JavaScript.

Source: Wikipedia

This is what mathematicians refer to as a graph.  In a graph, nodes (the circles above, numbered so we can distinguish them) are linked together by vertices (the lines).  Graphs can be used to store data and relationships, and they can also be used to simulate problems, such as in the Traveling Salesman Problem, which asks you to find the most efficient route through a bunch of different cities.

Quite often, we need to find the shortest path from one node to another (such as in the Traveling Salesman problem. It's a great problem).  What's the best way to do that? Djikstra's algorithm.

Dijkstra’s algorithm has a Big O complexity of log n, which is super efficient.  How does one do Djikstra's algorithm?  It's simple: you start at one node, and then move to the next node that has the shortest distance.  Once you're there, you again consider, from each node you've visited, what the shortest remaining node is.  You mark that one as visited, and continue the process until you've hit your target node.  

[removed]

Comments

Popular posts from this blog

The Sorting Hat

Kadane's Algorithm

Loose Equality, the Null Operator, and Other Oddities