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
+57-57Lines changed: 57 additions & 57 deletions
Original file line number
Diff line number
Diff line change
@@ -1,11 +1,11 @@
1
-
# Weighted graph general cocepts
1
+
# Weighted graph general concepts
2
2
3
3
Every weighted graph should contain:
4
4
1. Vertices/Nodes (I will use "vertex" in this readme).
5
5
6
6
<imgsrc="Images/Vertices.png"height="250" />
7
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:
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 has a direction, i.e. vertex, where it starts and vertex, where it ends. But remember a VERY IMPORTANT thing:
9
9
* All undirected graphs can be viewed as a directed graph.
10
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.
27
+
And once again: An undirected 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
28
29
29
Great! Now we are familiar with general concepts about graphs.
30
30
31
-
# Dijkstra's algorithm
31
+
# The Dijkstra's algorithm
32
32
This [algorithm](https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm) was invented in 1956 by Edsger W. Dijkstra.
33
33
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.
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.
35
35
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.
36
+
The best example is a road network. If you want 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.
37
37
38
38
The algorithm repeats following cycle until all vertices are marked as visited.
39
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)
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
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.
42
+
3. The algorithm checks all of its neighbours. If the current vertex path length from the start plus an edge weight to a neighbour less than the neighbour current path length from the start than it assigns new path length from the start to the neighbour.
43
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.
44
44
45
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.
46
46
47
47
## Example
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 :)
48
+
Let's imagine that 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 a graph that connects your house with all these stores. So, you know what to do :)
49
49
50
-
### Initialization
50
+
### Initialisation
51
51
52
52
When the algorithm starts to work initial graph looks like this:
53
53
@@ -61,25 +61,25 @@ The table below represents graph state:
Great, now our graph is initialized and we can pass it to the Dijkstra's algorithm, let's start!
78
+
Great, now our graph is initialised and we can pass it to the Dijkstra's algorithm, let's start!
79
79
80
-
Let's follow the algorithm's cycle and pick the first vertex, which neighbors we want to check.
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.
82
-
First of all, set this vertex as visited
80
+
Let's follow the algorithm's cycle and pick the first vertex which neighbours we want to check.
81
+
All our vertices are not visited but there is only one has the smallest path length from start. It is A. This vertex is the first one which neighbors we will check.
82
+
First of all, set this vertex as visited.
83
83
84
84
A.visited = true
85
85
@@ -95,8 +95,8 @@ After this step graph has this state:
95
95
96
96
### Step 1
97
97
98
-
Then we check all of its neighbors.
99
-
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.
98
+
Then we check all of its neighbours.
99
+
If checking vertex path length from start + edge weight is smaller than neighbour's path length from start then we set neighbour's path length from start new value and append to its pathVerticesFromStart array new vertex: checkingVertex. Repeat this action for every vertex.
100
100
101
101
for clarity:
102
102
```swift
@@ -162,41 +162,41 @@ From now we repeat all actions again and fill our table with new info!
162
162
163
163
164
164
## Code implementation
165
-
First of all, lets create class, that will describe any Vertex in the graph.
165
+
First of all, let’s create class that will describe any Vertex in the graph.
166
166
It is pretty simple
167
167
```swift
168
168
openclassVertex {
169
169
170
-
//Every vertex should be unique, that's why we set up identifier
170
+
//Every vertex should be unique that's why we set up identifier
171
171
openvar identifier: String
172
172
173
-
//For dijkstra every vertex in the graph should be connected with at least one other vertex. But there can be some use cases,
174
-
//when you firstly initialize all vertices without neighbors. And then on next interation set up thei neighbors. So, initially neighbors is an empty array.
175
-
//Array contains tuples (Vertex, Double). Vertex is a neighbor and Double is as edge weight to that neighbor.
176
-
openvarneighbors: [(Vertex, Double)] = []
173
+
//For Dijkstra every vertex in the graph should be connected with at least one other vertex. But there can be some usecases
174
+
//when you firstly initialize all vertices without neighbours. And then on next iteration you set up their neighbours. So, initially neighbours is an empty array.
175
+
//Array contains tuples (Vertex, Double). Vertex is a neighbour and Double is as edge weight to that neighbour.
176
+
openvarneighbours: [(Vertex, Double)] = []
177
177
178
-
//As it was mentioned in algorithm description, default path length from start for all vertices should be as much as possible.
179
-
//It is var, because we will update it during algorithm execution.
178
+
//As it was mentioned in the algorithm description, default path length from start for all vertices should be as much as possible.
179
+
//It is var because we will update it during the algorithm execution.
180
180
openvar pathLengthFromStart =Double.infinity
181
181
182
-
//This array containt vertices, which we need to go through to reach this vertex from starting one
183
-
//As with path length from start, we will change this array during algorithm execution.
182
+
//This array contains vertices which we need to go through to reach this vertex from starting one
183
+
//As with path length from start, we will change this array during the algorithm execution.
184
184
openvar pathVerticesFromStart: [Vertex] = []
185
185
186
186
publicinit(identifier: String) {
187
187
self.identifier= identifier
188
188
}
189
189
190
190
//This function let us use the same array of vertices again and again to calculate paths with different starting vertex.
191
-
//When we will need to set new starting vertex andd recalculate paths, then we will simply clear graph vertices' cashes.
191
+
//When we will need to set new starting vertex and recalculate paths then we will simply clear graph vertices' cashes.
192
192
openfuncclearCache() {
193
193
pathLengthFromStart =Double.infinity
194
194
pathVerticesFromStart = []
195
195
}
196
196
}
197
197
```
198
198
199
-
Because every vertex should be unique it is usefull to make them Hashable and according Equatable. We use identifier for this purposes.
199
+
As every vertex should be unique it is useful to make them Hashable and according Equatable. We use an identifier for this purposes.
That's all! Now you can check this algorithm in the playground. On the main page there is a code for creating random graph.
297
+
That's all! Now you can check this algorithm in the playground. On the main page there is a code for creating a random graph.
298
298
299
-
Also there is a **VisualizedDijkstra.playground**. Use it to figure out algorithm's flow in real (slowed :)) time.
299
+
Also there is a **VisualizedDijkstra.playground**. Use it to figure out the algorithm's flow in real (slowed :)) time.
300
300
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 :)
301
+
It is up to you how to implement some specific parts of the 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 :)
302
302
303
303
# About this repository
304
304
305
-
This repository contains to playgrounds:
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.
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.
305
+
This repository contains two playgrounds:
306
+
* To understand how does this algorithm works, I created **VisualizedDijkstra.playground.** It works in auto and interactive modes. Moreover, there are play/pause/stop buttons.
307
+
* If you need only realisation of the algorithm without visualisation then run **Dijkstra.playground.** It contains necessary classes and couple functions to create random graph for algorithm testing.
0 commit comments