Skip to content

Commit 5a65dcb

Browse files
committed
Add Dijkstra algorithm illustrations and explanations
1 parent c80e10c commit 5a65dcb

17 files changed

+70
-10
lines changed

src/algorithms/graph/dijkstra/README.md

Lines changed: 70 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,84 @@
33
_Read this in other languages:_
44
[_한국어_](README.ko-KR.md)
55

6-
Dijkstra's algorithm is an algorithm for finding the shortest
7-
paths between nodes in a graph, which may represent, for example,
8-
road networks.
9-
10-
The algorithm exists in many variants; Dijkstra's original variant
11-
found the shortest path between two nodes, but a more common
12-
variant fixes a single node as the "source" node and finds
13-
shortest paths from the source to all other nodes in the graph,
6+
Dijkstra's algorithm is an algorithm for finding the shortest
7+
paths between nodes in a graph, which may represent, for example,
8+
road networks.
9+
10+
The algorithm exists in many variants; Dijkstra's original variant
11+
found the shortest path between two nodes, but a more common
12+
variant fixes a single node as the "source" node and finds
13+
shortest paths from the source to all other nodes in the graph,
1414
producing a shortest-path tree.
1515

1616
![Dijkstra](https://upload.wikimedia.org/wikipedia/commons/5/57/Dijkstra_Animation.gif)
1717

1818
Dijkstra's algorithm to find the shortest path between `a` and `b`.
19-
It picks the unvisited vertex with the lowest distance,
20-
calculates the distance through it to each unvisited neighbor,
19+
It picks the unvisited vertex with the lowest distance,
20+
calculates the distance through it to each unvisited neighbor,
2121
and updates the neighbor's distance if smaller. Mark visited
2222
(set to red) when done with neighbors.
2323

24+
## Practical applications of Dijkstra's algorithm
25+
26+
- GPS / navigation systems
27+
- Public transit and airline route optimization
28+
- Internet routing (OSPF, IS-IS protocols)
29+
- Network traffic & latency optimization
30+
- Game pathfinding (shortest path on maps)
31+
- Logistics & delivery route optimization
32+
- Supply chain & transportation network design
33+
34+
## Step-by-step Dijkstra's algorithm example
35+
36+
Let's say we have a graph of nodes, where each edge has a distance parameter between nodes. Let's say the distance from node `A` and node `B` is `7 meters`, and so on.
37+
38+
The algorithm uses [Priority Queue](../../../data-structures/priority-queue/) to always pull the next unvisited vertex that has the smallest distance from the origin node.
39+
40+
The start node by definition has a distance of `0m` from itself. So we start from it, the only node in the priority queue.
41+
42+
![Dijkstra step 1](./images/dijkstra-01.png)
43+
44+
Each neighbor of the pulled (from the queue) node is being traversed to calculate the distance to it from the origin. For example the distance from `A` to `B` is `0 + 7 = 7m`, and so on.
45+
46+
Every time we visit a not yet seen neighbor, we add it to the priority queue, where the priority is a distance to the neighbor node from the origin node.
47+
48+
If the node is already in the queue, it means we already calculated the distance to it before but from another node/path. If the current distance to it (from the current neighbor node) is shorter than the one that was calculated before, we update the distance (in the priority queue) to the shortest one, since we're searching for the shortest paths.
49+
50+
![Dijkstra step 2](./images/dijkstra-02.png)
51+
52+
![Dijkstra step 3](./images/dijkstra-03.png)
53+
54+
![Dijkstra step 4](./images/dijkstra-04.png)
55+
56+
Once all the neighbors of the current nodes were checked, the current node is being put to the `visited` set. We don't want to visit such nodes ones again during the upcoming traverses.
57+
58+
![Dijkstra step 5](./images/dijkstra-05.png)
59+
60+
![Dijkstra step 6](./images/dijkstra-06.png)
61+
62+
![Dijkstra step 7](./images/dijkstra-07.png)
63+
64+
![Dijkstra step 8](./images/dijkstra-08.png)
65+
66+
![Dijkstra step 9](./images/dijkstra-09.png)
67+
68+
![Dijkstra step 10](./images/dijkstra-10.png)
69+
70+
![Dijkstra step 11](./images/dijkstra-11.png)
71+
72+
![Dijkstra step 12](./images/dijkstra-12.png)
73+
74+
![Dijkstra step 13](./images/dijkstra-13.png)
75+
76+
![Dijkstra step 14](./images/dijkstra-14.png)
77+
78+
![Dijkstra step 15](./images/dijkstra-15.png)
79+
80+
![Dijkstra step 16](./images/dijkstra-16.png)
81+
82+
Now we now the shortest distances to ech node from the start node `A`. In practice, during the distance calculations we also record the the `previousVertices` for each node to be able to show the exact sequence of nodes that forms the shortest path.
83+
2484
## References
2585

2686
- [Wikipedia](https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm)
41.8 KB
Loading
82.6 KB
Loading
85.5 KB
Loading
90 KB
Loading
88.3 KB
Loading
95.3 KB
Loading
95.7 KB
Loading
89.5 KB
Loading
99.2 KB
Loading

0 commit comments

Comments
 (0)