|
3 | 3 | _Read this in other languages:_ |
4 | 4 | [_한국어_](README.ko-KR.md) |
5 | 5 |
|
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, |
14 | 14 | producing a shortest-path tree. |
15 | 15 |
|
16 | 16 |  |
17 | 17 |
|
18 | 18 | 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, |
21 | 21 | and updates the neighbor's distance if smaller. Mark visited |
22 | 22 | (set to red) when done with neighbors. |
23 | 23 |
|
| 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 | + |
| 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 | + |
| 51 | + |
| 52 | + |
| 53 | + |
| 54 | + |
| 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 | + |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | + |
| 63 | + |
| 64 | + |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | + |
| 73 | + |
| 74 | + |
| 75 | + |
| 76 | + |
| 77 | + |
| 78 | + |
| 79 | + |
| 80 | + |
| 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 | + |
24 | 84 | ## References |
25 | 85 |
|
26 | 86 | - [Wikipedia](https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm) |
|
0 commit comments