Dijkstra Shortest Path
Project description
Dijkstra's Shortest Path Algorithm is an algorithm for finding the shortest paths between nodes in a weighted graph
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file graphs_gamebox62-0.0.1.tar.gz
.
File metadata
- Download URL: graphs_gamebox62-0.0.1.tar.gz
- Upload date:
- Size: 9.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fb99390532424615b36a4565a91971f1c19e05894ddcc569e44263258648d448 |
|
MD5 | dff6a18a4a439bd94acfcacfd08bd7d0 |
|
BLAKE2b-256 | 8fa725dc3588d21ca3b4d1207af42d497ea3c5db6a4f1594b07d2830066bc21a |
File details
Details for the file graphs_gamebox62-0.0.1-py3-none-any.whl
.
File metadata
- Download URL: graphs_gamebox62-0.0.1-py3-none-any.whl
- Upload date:
- Size: 10.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ac592cc71f2ec638857151bef0bd1cfcecc92472cb521c2a4f7bc8c352371812 |
|
MD5 | 0644a00ed563e24e1c182a4b97c0d0d5 |
|
BLAKE2b-256 | d75845d76c0f84dd855d980025c950a42fb223c3abd0d0a7a976d99b7c609634 |