Skip to content

Commit a88971d

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

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

src/algorithms/graph/dijkstra/README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ Each neighbor of the pulled (from the queue) node is being traversed to calculat
4545

4646
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.
4747

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-
5048
![Dijkstra step 2](./images/dijkstra-02.png)
5149

5250
![Dijkstra step 3](./images/dijkstra-03.png)
@@ -55,8 +53,12 @@ If the node is already in the queue, it means we already calculated the distance
5553

5654
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.
5755

56+
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+
5858
![Dijkstra step 5](./images/dijkstra-05.png)
5959

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.
61+
6062
![Dijkstra step 6](./images/dijkstra-06.png)
6163

6264
![Dijkstra step 7](./images/dijkstra-07.png)

0 commit comments

Comments
 (0)