O'Reilly logo

Python Data Structures and Algorithms by Benjamin Baka

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

Dijkstra's shortest path algorithm

We introduce and study Dijkstra's algorithm. This algorithm is an example of a greedy algorithm. It finds the shortest distance from a source to all other nodes or vertices in a graph. By the end of this section, you will come to understand why it is classified as a greedy algorithm.

Consider the following graph:

By inspection, the first answer to the question of finding the shortest path between node A and node D that comes to mind is the edge with value or distance 9. From the diagram, it would seem that the straight path from node A to D would also yield the shortest route between the two nodes. But the ...

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