kopia lustrzana https://github.com/animator/learn-python
Update Dijkstra's_algorithm.md
rodzic
d98376b7da
commit
85ecb2cabc
|
@ -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).
|
||||

|
||||

|
||||
|
||||
- **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.
|
||||

|
||||

|
||||
|
||||
- **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.
|
||||

|
||||

|
||||
|
||||
- **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.
|
||||

|
||||

|
||||
|
||||
- **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.
|
||||

|
||||

|
||||
|
||||
- **Step5:** Similarly, both the elements of the pq will be popped one by one without any new addition.
|
||||

|
||||

|
||||

|
||||

|
||||
|
||||
- The distance list we get at the end will be our answer.
|
||||
- `Output` `dist=[1, 3, 7, 1, 6]`
|
||||
|
|
Ładowanie…
Reference in New Issue