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
+51-23Lines changed: 51 additions & 23 deletions
Original file line number
Diff line number
Diff line change
@@ -1,16 +1,53 @@
1
+
# Weighted graph general cocepts
2
+
3
+
Every weighted graph should contain:
4
+
1. Vertices/Nodes (I will use "vertex" in this readme).
5
+
6
+
<imgsrc="Images/Vertices.png"height="250" />
7
+
8
+
2. Edges connecting vertices. Let's add some edges to our graph. For simplicity let's create directed graph for now. Directed means, that edge have a direction, i.e. vertex, where it starts and vertex, where it ends. But remember VERY IMPORTANT thing:
9
+
* All undirected graphs can be viewed as a directed graph.
10
+
* A directed graph is undirected if and only if every edge is paired with an edge going in the opposite direction.
And once again: An undireceted graph - it is a directed graph with every edge paired with an edge going in the opposite direction. This statement is clear on the image above.
28
+
29
+
Great! Now we are familiar with general concepts about graphs.
30
+
1
31
# Dijkstra's algorithm
32
+
This [algorithm](https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm) was invented in 1956 by Edsger W. Dijkstra.
2
33
3
-
This [algorithm](https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm) was invented in 1956 by Edsger W. Dijkstra.
34
+
It can be used, when you have one source vertex and want to find the shortest paths to ALL other vertices in the graph.
4
35
5
-
It can be used, when you have one source vertex and want to find the shortest paths to all other vertices in the graph.
36
+
The best example is road network. If you wnat to find the shortest path from your house to your job, or if you want to find the closest store to your house, then it is time for the Dijkstra's algorithm.
6
37
7
-
The best example is road network. If you wnat to find the shortest path from your house to your job, then it is time for the Dijkstra's algorithm.
38
+
The algorithm repeats following cycle until all vertices are marked as visited.
39
+
Cycle:
40
+
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)
41
+
2. The algorithm marks picked vertex as visited.
42
+
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.
43
+
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.
8
44
9
-
I have created **VisualizedDijkstra.playground** to improve your understanding of the algorithm's flow. Besides, below is step by step algorithm's description.
45
+
I have created **VisualizedDijkstra.playground**game/tutorial to improve your understanding of the algorithm's flow. Besides, below is step by step algorithm's description.
10
46
47
+
## Example
11
48
Let's imagine, you want to go to the shop. Your house is A vertex and there are 4 possible stores around your house. How to find the closest one/ones? Luckily, you have graph, that connects your house with all these stores. So, you know what to do :)
12
49
13
-
## Initialization
50
+
###Initialization
14
51
15
52
When the algorithm starts to work initial graph looks like this:
16
53
@@ -38,17 +75,8 @@ To initialize our graph we have to set source vertex path length from source ver
Great, now our graph is initialized and we can pass it to the Dijkstra's algorithm.
42
-
43
-
But before we will go through all process side by side read this explanation.
44
-
The algorithm repeats following cycle until all vertices are marked as visited.
45
-
Cycle:
46
-
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)
47
-
2. The algorithm marks picked vertex as visited.
48
-
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.
49
-
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.
78
+
Great, now our graph is initialized and we can pass it to the Dijkstra's algorithm, let's start!
50
79
51
-
Okay, let's start!
52
80
Let's follow the algorithm's cycle and pick the first vertex, which neighbors we want to check.
53
81
All our vertices are not visited, but there is only one has the smallest path length from start - A. This vertex is the first one, which neighbors we will check.
54
82
First of all, set this vertex as visited
@@ -65,7 +93,7 @@ After this step graph has this state:
If checking vertex path length from start + edge weigth is smaller than neighbor's path length from start, then we set neighbor's path length from start new value and append to its pathVerticesFromStart array new vertex: checkingVertex. Repeat this action for every vertex.
| Path Vertices From Start |[A]|[A, B]|[]|[A, D]|[]|
92
120
93
-
## Step 2
121
+
###Step 2
94
122
95
123
From now we repeat all actions again and fill our table with new info!
96
124
@@ -102,7 +130,7 @@ From now we repeat all actions again and fill our table with new info!
102
130
| Path Length From Start | 0 | 3 | inf | 1 | 2 |
103
131
| Path Vertices From Start |[A]|[A, B]|[]|[A, D]|[A, D, E]|
104
132
105
-
## Step 3
133
+
###Step 3
106
134
107
135
<imgsrc="Images/image5.png"height="250" />
108
136
@@ -112,7 +140,7 @@ From now we repeat all actions again and fill our table with new info!
112
140
| Path Length From Start | 0 | 3 | 11 | 1 | 2 |
113
141
| Path Vertices From Start |[A]|[A, B]|[A, D, E, C]|[A, D]|[A, D, E ]|
114
142
115
-
## Step 4
143
+
###Step 4
116
144
117
145
<imgsrc="Images/image6.png"height="250" />
118
146
@@ -122,7 +150,7 @@ From now we repeat all actions again and fill our table with new info!
122
150
| Path Length From Start | 0 | 3 | 8 | 1 | 2 |
123
151
| Path Vertices From Start |[A]|[A, B]|[A, B, C]|[A, D]|[A, D, E ]|
124
152
125
-
## Step 5
153
+
###Step 5
126
154
127
155
<imgsrc="Images/image7.png"height="250" />
128
156
@@ -272,16 +300,16 @@ Also there is a **VisualizedDijkstra.playground**. Use it to figure out algorith
272
300
273
301
It is up to you how to implement some specific parts of algorithm, you can use Array instead of Set, add _visited_ property to Vertex or you can create some local totalVertices Array/Set inside _func findShortestPaths(from startVertex: Vertex)_ to keep totalVertices Array/Set unchanged. This is a general explanation with one possible implementation :)
274
302
275
-
##About this repository
303
+
# About this repository
276
304
277
305
This repository contains to playgrounds:
278
306
* 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.
279
307
* 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.
280
308
281
-
##Demo video
309
+
# Demo video
282
310
283
311
Click the link: [YouTube](https://youtu.be/PPESI7et0cQ)
284
312
285
-
##Credits
313
+
# Credits
286
314
287
315
WWDC 2017 Scholarship Project (Rejected) created by [Taras Nikulin](https://github.com/crabman448)
0 commit comments