Skip to content

Commit b6c5b82

Browse files
Update README.md
1 parent edbccd3 commit b6c5b82

File tree

1 file changed

+40
-40
lines changed

1 file changed

+40
-40
lines changed

Dijkstra Algorithm/README.md

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -139,28 +139,28 @@ It is pretty simple
139139
```swift
140140
open class Vertex {
141141

142-
//Every vertex should be unique, that's why we set up identifier
142+
//Every vertex should be unique, that's why we set up identifier
143143
open var identifier: String
144144

145-
//For dijkstra every vertex in the graph should be connected with at least one other vertex. But there can be some use cases,
146-
//when you firstly initialize all vertices without neighbors. And then on next interation set up thei neighbors. So, initially neighbors is an empty array.
147-
//Array contains tuples (Vertex, Double). Vertex is a neighbor and Double is as edge weight to that neighbor.
145+
//For dijkstra every vertex in the graph should be connected with at least one other vertex. But there can be some use cases,
146+
//when you firstly initialize all vertices without neighbors. And then on next interation set up thei neighbors. So, initially neighbors is an empty array.
147+
//Array contains tuples (Vertex, Double). Vertex is a neighbor and Double is as edge weight to that neighbor.
148148
open var neighbors: [(Vertex, Double)] = []
149149

150-
//As it was mentioned in algorithm description, default path length from start for all vertices should be as much as possible.
151-
//It is var, because we will update it during algorithm execution.
150+
//As it was mentioned in algorithm description, default path length from start for all vertices should be as much as possible.
151+
//It is var, because we will update it during algorithm execution.
152152
open var pathLengthFromStart = Double.infinity
153-
154-
//This array containt vertices, which we need to go through to reach this vertex from starting one
155-
//As with path length from start, we will change this array during algorithm execution.
153+
154+
//This array containt vertices, which we need to go through to reach this vertex from starting one
155+
//As with path length from start, we will change this array during algorithm execution.
156156
open var pathVerticesFromStart: [Vertex] = []
157157

158158
public init(identifier: String) {
159159
self.identifier = identifier
160160
}
161161

162-
//This function let us use the same array of vertices again and again to calculate paths with different starting vertex.
163-
//When we will need to set new starting vertex andd recalculate paths, then we will simply clear graph vertices' cashes.
162+
//This function let us use the same array of vertices again and again to calculate paths with different starting vertex.
163+
//When we will need to set new starting vertex andd recalculate paths, then we will simply clear graph vertices' cashes.
164164
open func clearCache() {
165165
pathLengthFromStart = Double.infinity
166166
pathVerticesFromStart = []
@@ -187,79 +187,79 @@ We've created a base for our algorithm. Now let's create a house :)
187187
Dijkstra's realization is really straightforward.
188188
```swift
189189
public class Dijkstra {
190-
//This is a storage for vertices in the graph.
191-
//Assuming, that our vertices are unique, we can use Set instead of array. This approach will bring some benefits later.
190+
//This is a storage for vertices in the graph.
191+
//Assuming, that our vertices are unique, we can use Set instead of array. This approach will bring some benefits later.
192192
private var totalVertices: Set<Vertex>
193193

194194
public init(vertices: Set<Vertex>) {
195195
totalVertices = vertices
196196
}
197197

198-
//Remember clearCache function in the Vertex class implementation?
199-
//This is just a wrapper that cleans cache for all stored vertices.
198+
//Remember clearCache function in the Vertex class implementation?
199+
//This is just a wrapper that cleans cache for all stored vertices.
200200
private func clearCache() {
201201
totalVertices.forEach { $0.clearCache() }
202202
}
203203

204204
public func findShortestPaths(from startVertex: Vertex) {
205-
//Before we start searching shortes path from startVertex,
206-
//we need to clear vertices cache just to be sure, that out graph is as clean as a baby.
207-
//Remember that every Vertex is a class and classes are passed by reference.
208-
//So whenever you change vertex outside of this class, it will affect this vertex inside totalVertices Set
205+
//Before we start searching shortes path from startVertex,
206+
//we need to clear vertices cache just to be sure, that out graph is as clean as a baby.
207+
//Remember that every Vertex is a class and classes are passed by reference.
208+
//So whenever you change vertex outside of this class, it will affect this vertex inside totalVertices Set
209209
clearCache()
210-
//Now all our vertices have Double.infinity pathLengthFromStart and empty pathVerticesFromStart array.
210+
//Now all our vertices have Double.infinity pathLengthFromStart and empty pathVerticesFromStart array.
211211

212-
//The next step in the algorithm is to set startVertex pathLengthFromStart and pathVerticesFromStart
212+
//The next step in the algorithm is to set startVertex pathLengthFromStart and pathVerticesFromStart
213213
startVertex.pathLengthFromStart = 0
214214
startVertex.pathVerticesFromStart.append(startVertex)
215215

216-
//Here starts the main part. We will use while loop to iterate through all vertices in the graph.
217-
//For this purpose we define currentVertex variable, which we will change in the end of each while cycle.
216+
//Here starts the main part. We will use while loop to iterate through all vertices in the graph.
217+
//For this purpose we define currentVertex variable, which we will change in the end of each while cycle.
218218
var currentVertex: Vertex? = startVertex
219219

220220
while let vertex = currentVertex {
221-
222-
//Nex line of code is an implementation of setting vertex as visited.
223-
//As it has been said, we should check only unvisited vertices in the graph,
224-
//So why don't just delete it from the set? This approach let us skip checking for *"if !vertex.visited then"*
221+
222+
//Nex line of code is an implementation of setting vertex as visited.
223+
//As it has been said, we should check only unvisited vertices in the graph,
224+
//So why don't just delete it from the set? This approach let us skip checking for *"if !vertex.visited then"*
225225
totalVertices.remove(vertex)
226226

227-
//filteredNeighbors is and arrray, that contains current vertex neighbors, which aren't yet visited
227+
//filteredNeighbors is and arrray, that contains current vertex neighbors, which aren't yet visited
228228
let filteredNeighbors = vertex.neighbors.filter { totalVertices.contains($0.0) }
229229

230-
//Let's iterate through them
230+
//Let's iterate through them
231231
for neighbor in filteredNeighbors {
232-
//These variable are more representative, than neighbor.0 or neighbor.1
232+
//These variable are more representative, than neighbor.0 or neighbor.1
233233
let neighborVertex = neighbor.0
234234
let weight = neighbor.1
235235

236-
//Here we calculate new weight, that we can offer to neighbor.
236+
//Here we calculate new weight, that we can offer to neighbor.
237237
let theoreticNewWeight = vertex.pathLengthFromStart + weight
238238

239-
//If it is smaller than neighbor's current pathLengthFromStart
240-
//Then we perform this code
239+
//If it is smaller than neighbor's current pathLengthFromStart
240+
//Then we perform this code
241241
if theoreticNewWeight < neighborVertex.pathLengthFromStart {
242242

243-
//set new pathLengthFromStart
243+
//set new pathLengthFromStart
244244
neighborVertex.pathLengthFromStart = theoreticNewWeight
245245

246-
//set new pathVerticesFromStart
246+
//set new pathVerticesFromStart
247247
neighborVertex.pathVerticesFromStart = vertex.pathVerticesFromStart
248248

249-
//append current vertex to neighbor's pathVerticesFromStart
249+
//append current vertex to neighbor's pathVerticesFromStart
250250
neighborVertex.pathVerticesFromStart.append(neighborVertex)
251251
}
252252
}
253253

254-
//If totalVertices is empty, i.e. all vertices are visited
255-
//Than break the loop
254+
//If totalVertices is empty, i.e. all vertices are visited
255+
//Than break the loop
256256
if totalVertices.isEmpty {
257257
currentVertex = nil
258258
break
259259
}
260260

261-
//If loop is not broken, than pick next vertex for checkin from not visited.
262-
//Next vertex pathLengthFromStart should be the smallest one.
261+
//If loop is not broken, than pick next vertex for checkin from not visited.
262+
//Next vertex pathLengthFromStart should be the smallest one.
263263
currentVertex = totalVertices.min { $0.pathLengthFromStart < $1.pathLengthFromStart }
264264
}
265265
}

0 commit comments

Comments
 (0)