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
@@ -44,7 +44,9 @@ When all vertices are marked as visited, the algorithm's job is done. Now, you c
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
-
## Example
47
+
A short sidenote. The Swift Algorithm Club also contains the A* algorithm, which essentially is a faster version of Dijkstra's algorithm for which the only extra prerequisite is you have to know where the destination is located.
48
+
49
+
## Example
48
50
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
51
50
52
### Initialisation
@@ -95,7 +97,7 @@ After this step graph has this state:
95
97
96
98
### Step 1
97
99
98
-
Then we check all of its neighbours.
100
+
Then we check all of its neighbours.
99
101
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
102
101
103
for clarity:
@@ -120,7 +122,7 @@ And its state is here:
120
122
121
123
### Step 2
122
124
123
-
From now we repeat all actions again and fill our table with new info!
125
+
From now we repeat all actions again and fill our table with new info!
124
126
125
127
<imgsrc="Images/image4.png"height="250" />
126
128
@@ -161,20 +163,20 @@ From now we repeat all actions again and fill our table with new info!
161
163
| Path Vertices From Start |[A]|[A, B]|[A, B, C]|[A, D]|[A, D, E ]|
162
164
163
165
164
-
## Code implementation
166
+
## Code implementation
165
167
First of all, let’s create class that will describe any Vertex in the graph.
166
168
It is pretty simple
167
169
```swift
168
170
openclassVertex {
169
171
170
172
//Every vertex should be unique that's why we set up identifier
171
173
openvar identifier: String
172
-
173
-
//For Dijkstra every vertex in the graph should be connected with at least one other vertex. But there can be some usecases
174
+
175
+
//For Dijkstra every vertex in the graph should be connected with at least one other vertex. But there can be some usecases
174
176
//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
177
//Array contains tuples (Vertex, Double). Vertex is a neighbour and Double is as edge weight to that neighbour.
176
178
openvar neighbours: [(Vertex, Double)] = []
177
-
179
+
178
180
//As it was mentioned in the algorithm description, default path length from start for all vertices should be as much as possible.
179
181
//It is var because we will update it during the algorithm execution.
180
182
openvar pathLengthFromStart =Double.infinity
@@ -215,77 +217,77 @@ We've created a base for our algorithm. Now let's create a house :)
215
217
Dijkstra's realisation is really straightforward.
216
218
```swift
217
219
publicclassDijkstra {
218
-
//This is a storage for vertices in the graph.
220
+
//This is a storage for vertices in the graph.
219
221
//Assuming that our vertices are unique we can use Set instead of array. This approach will bring some benefits later.
220
222
privatevar totalVertices: Set<Vertex>
221
223
222
224
publicinit(vertices: Set<Vertex>) {
223
225
totalVertices = vertices
224
226
}
225
227
226
-
//Remember clearCache function in the Vertex class implementation?
228
+
//Remember clearCache function in the Vertex class implementation?
227
229
//This is just a wrapper that cleans cache for all stored vertices.
0 commit comments