Skip to content

Commit 2e7c34c

Browse files
author
Taras Nikulin
committed
Probably final updates
1 parent 5e5e7a9 commit 2e7c34c

File tree

1 file changed

+4
-30
lines changed

1 file changed

+4
-30
lines changed

Dijkstra Algorithm/README.md

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ I have created **VisualizedDijkstra.playground** to help you to understand, how
1010

1111
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.
1212

13-
> Initialization
13+
## Initialization
1414

1515
When the algorithm starts to work initial graph looks like this:
1616

@@ -130,43 +130,17 @@ From now we repeat all actions again and fill our table with new info!
130130
| Path Length From Start | 0 | 3 | 8 | 1 | 2 |
131131
| Path Vertices From Start | [A] | [A, B] | [A, B, C]| [A, D] | [A, D, E ] |
132132

133-
## About
133+
## About this repository
134134

135135
This repository contains to playgrounds:
136136
* 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.
137137
* 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.
138138

139-
140-
141-
142-
143-
[Wikipedia](https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm)'s explanation:
144-
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-
167139
## Demo video
140+
168141
Click the link: [YouTube](https://youtu.be/PPESI7et0cQ)
169142

170143
## Credits
144+
171145
WWDC 2017 Scholarship Project
172146
Created by [Taras Nikulin](https://github.com/crabman448)

0 commit comments

Comments
 (0)