You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/algorithms/graph/dijkstra/README.md
+45-7Lines changed: 45 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -33,55 +33,93 @@ and updates the neighbor's distance if smaller. Mark visited
33
33
34
34
## Step-by-step Dijkstra's algorithm example
35
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.
36
+
Let's say we have a weighted 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` (or just `7m` for brevity), and so on.
37
37
38
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
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.
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
+
The rest of the nodes will be added to the priority queue later during the graph traversal (while visiting the neighbors).
41
43
42
44

43
45
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.
46
+
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 `0m + 7m = 7m`, and so on.
47
+
48
+
Every time we visit a not-yet-seen neighbor, we add it to the priority queue, where the priority is the distance to the neighbor node from the origin node.
45
49
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.
50
+
The node `B` is being added to the min priority queueto be traversed later.
47
51
48
52

49
53
54
+
Visiting the next neighbor `C` of the node `A`. The distance from the origin node `A` to `C` is `0m + 9m = 9m`.
55
+
56
+
The node `C` is being added to the min priority queue to be traversed later.
57
+
50
58

51
59
60
+
Same for the node `F`. The current distance to `F` from the origin node `A` is `0m + 14m = 14m`.
61
+
62
+
The node `F` is being added to the min priority queue to be traversed later.
63
+
52
64

53
65
54
-
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.
66
+
Once all the neighbors of the current node were checked, the current node was added to the `visited` set. We don't want to visit such nodes once again during the upcoming traverses.
55
67
56
68
Now, let's pull out the next node from the priority queue that is closest to the origin (has a shorter distance). We start visiting its neighbors as well.
57
69
58
70

59
71
60
-
If the node that we're visiting (in this case the node `C`) is already in the queue, it means we already calculated the distance to it before but from another node/path (`A → C`). If the current distance to it (from the current neighbor node `A → B → C`) 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. If the distance from the current neighbor is actually longer that the once was already calculated, we leave it like this without updating the `C` node distance in the queue.
72
+
If the node that we're visiting (in this case, the node `C`) is already in the queue, it means we already calculated the distance to it before, but from another node/path (`A → C`). If the current distance to it (from the current neighbor node `A → B → C`) 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. If the distance from the current neighbor is actually longer than the one that was already calculated, we leave it like this without updating the `C` node distance in the queue.
73
+
74
+
While visiting the node `C` via `B` (the path `A → B → C`), we see that the distance to it would be `7m + 10m = 17m`. This is actually longer than the already recorded distance of `9m` for the path `A → C`. In such cases, we just ignore the longest distance and don't update the priority (the minimum found distance at the moment from the origin node).
61
75
62
76

63
77
78
+
Visiting another neighbor of `B`, which is `D`. The distance to `D` equals `7m + 15m = 22m`. Since we didn't visit `D` yet and it is not in the min priority queue, let's just add it to the queue with a priority (distance) of `22m`.
79
+
64
80

65
81
82
+
At this point, all of the neighbors of `B` were traversed, so we add `B` to the `visited` set. Next, we pull the node that is closest to the origin node from the priority queue.
83
+
66
84

67
85
86
+
Traversing the unvisited neighbors of the node `C`. The distance to node `F` via `C` (the path `A → C → F`) is `9m + 2m = 11m`. This is actually shorter than the previously recorded path `A → F` of `14m` length. In such cases, we update the distance to `F` to `11m` and update its priority in the queue from `14m` to `11m`. We've just found a shorter path to `F`.
87
+
68
88

69
89
90
+
The same goes for `D`. We've just found a shorter path to `D`, where `A → C → D` is shorter than `A → B → D`. Updating the distance from `22m` to `20m`.
91
+
70
92

71
93
94
+
All neighbors of `C` were traversed, so we may add `C` to the `visited` set. Pulling the next closest node from the queue, which is `F`.
95
+
72
96

73
97
98
+
Recording the distance to `E` as `11m + 9m = 20m`.
99
+
74
100

75
101
102
+
Adding the node `F` to the `visited` set, and pulling the next closest node `D` from the queue.
103
+
76
104

77
105
106
+
Distance to `E` via `D` is `20m + 6m = 26m`. This is longer than the already calculated distance to `E` from `F`, which is `20m`. We can discard the longer distance.
107
+
78
108

79
109
110
+
Node `D` is visited now.
111
+
80
112

81
113
114
+
Node `E` is visited now as well. We've finished the graph traversal.
115
+
82
116

83
117
84
-
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.
118
+
Now we know the shortest distances to each node from the start node `A`.
119
+
120
+
In practice, during the distance calculations, we also record the `previousVertices` for each node to be able to show the exact sequence of nodes that form the shortest path.
121
+
122
+
For example, the shorter path from `A` to `E` is `A → C → F → E`.
0 commit comments