O'Reilly logo

Clojure for Data Science by Henry Garner

Stay ahead with the world's most comprehensive technology and business learning platform.

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

Start Free Trial

No credit card required

Finding the shortest path

The algorithms presented earlier traversed the graph vertex by vertex and returned a lazy sequence of all the nodes in the graph. They were convenient for illustrating the two primary ways of navigating the graph structures. However, a more common task would be to find the shortest path from one vertex to another. This means that we'll be interested only in the sequence of nodes that lie between them.

If we have an unweighted graph, such as the previous graphs, we'll usually count the distance as the number of "hops": a hop being the step between two neighboring nodes. The shortest path will have the fewest number of hops. Breadth-first search is, in general, a more efficient algorithm to use in this case.

Loom implements ...

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

Start Free Trial

No credit card required