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: Dijkstra Algorithm/README.md
+4-30Lines changed: 4 additions & 30 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,7 +10,7 @@ I have created **VisualizedDijkstra.playground** to help you to understand, how
10
10
11
11
So let's imagine, that your house is "A" vertex and your job is "B" vertex. And you are lucky, you have graph with all possible routes.
12
12
13
-
>Initialization
13
+
##Initialization
14
14
15
15
When the algorithm starts to work initial graph looks like this:
16
16
@@ -130,43 +130,17 @@ From now we repeat all actions again and fill our table with new info!
130
130
| Path Length From Start | 0 | 3 | 8 | 1 | 2 |
131
131
| Path Vertices From Start |[A]|[A, B]|[A, B, C]|[A, D]|[A, D, E ]|
132
132
133
-
## About
133
+
## About this repository
134
134
135
135
This repository contains to playgrounds:
136
136
* To understand how does this algorithm works, I have created **VisualizedDijkstra.playground.** It works in auto and interactive modes. Moreover there are play/pause/stop buttons.
137
137
* If you need only realization of the algorithm without visualization then run **Dijkstra.playground.** It contains necessary classes and couple functions to create random graph for algorithm testing.
Let the node at which we are starting be called the initial node. Let the distance of node Y be the distance from the initial node to Y. Dijkstra's algorithm will assign some initial distance values and will try to improve them step by step.
145
-
146
-
1. Assign to every node a tentative distance value: set it to zero for our initial node and to infinity for all other nodes.
147
-
2. Set the initial node as current. Mark all other nodes unvisited. Create a set of all the unvisited nodes called the unvisited set.
148
-
3. For the current node, consider all of its unvisited neighbors and calculate their tentative distances. Compare the newly calculated tentative distance to the current assigned value and assign the smaller one. For example, if the current node A is marked with a distance of 6, and the edge connecting it with a neighbor B has length 2, then the distance to B (through A) will be 6 + 2 = 8. If B was previously marked with a distance greater than 8 then change it to 8. Otherwise, keep the current value.
149
-
4. When we are done considering all of the neighbors of the current node, mark the current node as visited and remove it from the unvisited set. A visited node will never be checked again.
150
-
5. If the destination node has been marked visited (when planning a route between two specific nodes) or if the smallest tentative distance among the nodes in the unvisited set is infinity (when planning a complete traversal; occurs when there is no connection between the initial node and remaining unvisited nodes), then stop. The algorithm has finished.
151
-
6. Otherwise, select the unvisited node that is marked with the smallest tentative distance, set it as the new "current node", and go back to step 3.
152
-
153
-
Explanation, that can be found in the **VisualizedDijkstra.playground**
154
-
Algorithm's flow:
155
-
First of all, this program randomly decides which vertex will be the start one, then the program assigns a zero value to the start vertex path length from the start.
156
-
157
-
Then the algorithm repeats following cycle until all vertices are marked as visited.
158
-
Cycle:
159
-
1. From the non-visited vertices the algorithm picks a vertex with the shortest path length from the start (if there are more than one vertex with the same shortest path value, then algorithm picks any of them)
160
-
2. The algorithm marks picked vertex as visited.
161
-
3. The algorithm check all of its neighbors. If the current vertex path length from the start plus an edge weight to a neighbor less than the neighbor current path length from the start, than it assigns new path length from the start to the neihgbor.
162
-
When all vertices are marked as visited, the algorithm's job is done. Now, you can see the shortest path from the start for every vertex by pressing the one you are interested in.
163
-
164
-
## Usage
165
-
This algorithm is popular in routing. For example, biggest Russian IT company Yandex uses it in [Яндекс.Карты](https://yandex.ru/company/technologies/routes/)
166
-
167
139
## Demo video
140
+
168
141
Click the link: [YouTube](https://youtu.be/PPESI7et0cQ)
169
142
170
143
## Credits
144
+
171
145
WWDC 2017 Scholarship Project
172
146
Created by [Taras Nikulin](https://github.com/crabman448)
0 commit comments