Skip to main content

Python library that implements Dijkstra's shortest path algorithm.

Project description

graphs_AustynGriegoMSU

A Python library that implements Dijkstra's shortest path algorithm for finding shortest paths in weighted graphs.

The Shortest Path Problem

A graph is a data structure that connects nodes identified by labels. In a graph, a node is normally called vertex and the connections between two vertices is called edge. The cost to travel through an edge is its weight. Below is an example of a weighted graph with 9 vertices.

Dijkstra's Shortest Path Algorithm

Edsger W. Dijkstra was a Dutch computer scientist that made many contributions to the field, including a solution to the shortest path problem in a graph. The solution discussed here uses a min-heap to store the shortest known distances from the source. Let's look at a step-by-step execution of the algorithm for the example above.

The algorithm begins by adding the cost to reach the source vertex, which is (of course) 0. We will use the notation (source, weight) to represent that. In the solution for the shortest path problem, the heap uses the weight of an edge to sort the values. A distance list is created to record the cost to reach each of the destinations from source. The distance list is initialized with the maximum possible value for the weight, represented as ∞, except to reach the source, which should be set to zero.

The distances are initialized as: [0, ∞, ∞, ∞, ∞, ∞, ∞, ∞, ∞]

Hint: to represent ∞ in your code use sys.maxsize.

Next, the algorithm will run a loop until the heap becomes empty. At each iteration of that loop, the following steps are taken:

  • the top of the heap (i.e., its smaller element) is removed;
  • refer to that edge as (u, w), where w represents the best (known) cost to reach node u from source;
  • next, the algorithm evaluates if the cost to reach each of u's neighbors is better than what is known so far;
  • for example, if v is one of u's neighbor, then the algorithm checks if the cost to reach node u plus the weight from u to v is smaller than what is recorded in the distance array;
  • if that is the case, the distance array is updated;
  • also, if the cost is updated, the algorithm needs to update that cost if edge v is found in the heap.

For the example, (u, w) = (0, 0) in the first iteration. Nodes 1 and 7 can be reached from u=0 with costs 4 and 8, respectively. Therefore, the distances are updated to [0, 4, ∞, ∞, ∞, ∞, ∞, 8, ∞]. Also, (1, 4) and (7, 8) are added to the heap, which should now look like:

    (1, 4)
(7, 8)

On the second iteration, (u, w) = (1, 4). Nodes 0, 2 and 7 can be reached from u=1 with costs 4, 8 and 11, respectively. The distance to node 2 is updated (with a cost of 4+8=12) and the pair (2, 12) is added to the heap. The distance to node 0 does not get updated because travelling to node 0 through node 1 didn't improve the cost: 4 vs. 0. Also, the distance to node 7 does not get updated because travelling to node 7 through node 1 didn't improve the cost: 15 vs. 8. The heap should now look like:

    (7, 8)
(2, 12)    

The distances are now: [0, 4, 12, ∞, ∞, ∞, ∞, 8, -]

On the third iteration, (u, w) = (7, 8). Nodes 0, 1, 6, and 8 can be reached from u=7 with costs 8, 11, 1 and 7, respectively. The distance to node 6 is updated (with a cost of 8+1=9) and the pair (6, 9) is added to the heap. The distance to node 8 is also updated (with a cost of 8+7=15) and the pair (8, 15) is added to the heap. The heap should now look like:

    (6, 9)
(2, 12) (8, 15)  

The distances are now: [0, 4, 12, ∞, ∞, ∞, 9, 8, 15]

The algorithm repeats until the heap becomes empty. At the end of the loop the array of distances should have the lowest costs to reach each of the destinations from the source.

👨‍💻 Author

Austyn Griego
Email: agriego4@msudenver.edu
GitHub: AustynGriegoMSU

📈 Version History

  • 0.0.2: Current version with Dijkstra's algorithm implementation
  • 0.0.1: Initial release

🔗 Links


This package was created as part of a Python packaging exercise and demonstrates graph algorithms implementation.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

graphs_austyngriegomsu-0.0.3.tar.gz (11.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

graphs_austyngriegomsu-0.0.3-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

Details for the file graphs_austyngriegomsu-0.0.3.tar.gz.

File metadata

  • Download URL: graphs_austyngriegomsu-0.0.3.tar.gz
  • Upload date:
  • Size: 11.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for graphs_austyngriegomsu-0.0.3.tar.gz
Algorithm Hash digest
SHA256 c2ce36139f73579644b50c8e1c10cd1dd69fbecb0a7fccb4b1ad45fed516e0cc
MD5 14b851875bee627b0cad25b59bdb1737
BLAKE2b-256 6226d7c55ee8fb2b747f8d85735b79d8935c97185f1391959dfc5140c0cb003c

See more details on using hashes here.

File details

Details for the file graphs_austyngriegomsu-0.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for graphs_austyngriegomsu-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 000f22ad9c79ca54ebe84b304b3a8cd0b323d6a49fe16d6fe6716c17674c5926
MD5 8a87afead09ab133b8d9b3f7c678a9c9
BLAKE2b-256 b03a6d8eab2434c969995677187b887c6268d9debf0a12cb1c7284f240fa2f65

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page