Update Dijkstra's_algorithm.md

pull/847/head
yashasvi232 2024-06-13 22:33:49 +05:30 zatwierdzone przez GitHub
rodzic d98376b7da
commit 85ecb2cabc
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: B5690EEEBB952194
1 zmienionych plików z 9 dodań i 7 usunięć

Wyświetl plik

@ -13,23 +13,25 @@ Dijkstra's algorithm is a graph algorithm that gives the shortest distance of ea
## Dry Run
We will now do a manual simulation using an example graph given. First, (0, a) is pushed to the priority queue (pq).
![gssoc photo 1](https://github.com/animator/learn-python/assets/154299201/713cf7dd-fa8a-4f81-829f-00ac33eb12f4)
![Photo 1](images/Dijkstra's_algorithm_photo1.png)
- **Step1:** The lowest element is popped from the pq, which is (0, a), and all its neighboring nodes are added to the pq while simultaneously checking the distance list. Thus (3, b), (7, c), (1, d) are added to the pq.
![gssoc photo2](https://github.com/animator/learn-python/assets/154299201/9456b701-7877-44dc-854b-da416c5e2e9b)
![Photo 2](images/Dijkstra's_algorithm_photo2.png)
- **Step2:** Again, the lowest element is popped from the pq, which is (1, d). It has two neighboring nodes, a and e, from which
(0 + 1, a) will not be added to the pq as dist[a] = 0 is less than 1.
![gssoc photo3](https://github.com/animator/learn-python/assets/154299201/83990a40-f3fc-4173-9e21-10eb9e2003cb)
![Photo 3](images/Dijkstra's_algorithm_photo3.png)
- **Step3:** Now, the lowest element is popped from the pq, which is (3, b). It has two neighboring nodes, a and c, from which
(0 + 1, a) will not be added to the pq. But the new distance to reach c is 5 (3 + 2), which is less than dist[c] = 7. So (5, c) is added to the pq.
![gssoc photo4](https://github.com/animator/learn-python/assets/154299201/b47eb063-8ce7-4e68-85a5-da98f5ac9a3d)
![Photo 4](images/Dijkstra's_algorithm_photo4.png)
- **Step4:** The next smallest element is (5, c). It has neighbors a and e. The new distance to reach a will be 5 + 7 = 12, which is more than dist[a], so it will not be considered. Similarly, the new distance for e is 5 + 3 = 8, which again will not be considered. So, no new tuple has been added to the pq.
![gssoc photo5](https://github.com/animator/learn-python/assets/154299201/c0ef3c2a-1938-4c33-9e07-0929e8e5e631)
![Photo 5](images/Dijkstra's_algorithm_photo5.png)
- **Step5:** Similarly, both the elements of the pq will be popped one by one without any new addition.
![gssoc photo6](https://github.com/animator/learn-python/assets/154299201/828739c6-413b-41f6-a09f-b702802b20e1)
![gssoc photo7](https://github.com/animator/learn-python/assets/154299201/488d4c7b-d7bf-4887-a9c5-04accc6c6c89)
![Photo 6](images/Dijkstra's_algorithm_photo6.png)
![Photo 7](images/Dijkstra's_algorithm_photo7.png)
- The distance list we get at the end will be our answer.
- `Output` `dist=[1, 3, 7, 1, 6]`