Skip to content

Commit 5428e53

Browse files
Language fixes
1 parent 3f25101 commit 5428e53

File tree

1 file changed

+57
-57
lines changed

1 file changed

+57
-57
lines changed

Dijkstra Algorithm/README.md

Lines changed: 57 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
# Weighted graph general cocepts
1+
# Weighted graph general concepts
22

33
Every weighted graph should contain:
44
1. Vertices/Nodes (I will use "vertex" in this readme).
55

66
<img src="Images/Vertices.png" height="250" />
77

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:
99
* All undirected graphs can be viewed as a directed graph.
1010
* A directed graph is undirected if and only if every edge is paired with an edge going in the opposite direction.
1111

@@ -24,30 +24,30 @@ Undirected weighted graph:
2424

2525
<img src="Images/WeightedUndirectedGraph.png" height="250" />
2626

27-
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.
2828

2929
Great! Now we are familiar with general concepts about graphs.
3030

31-
# Dijkstra's algorithm
31+
# The Dijkstra's algorithm
3232
This [algorithm](https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm) was invented in 1956 by Edsger W. Dijkstra.
3333

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.
3535

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.
3737

3838
The algorithm repeats following cycle until all vertices are marked as visited.
3939
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)
4141
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.
4343
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.
4444

4545
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.
4646

4747
## 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 :)
4949

50-
### Initialization
50+
### Initialisation
5151

5252
When the algorithm starts to work initial graph looks like this:
5353

@@ -61,25 +61,25 @@ The table below represents graph state:
6161
| Path Length From Start | inf | inf | inf | inf | inf |
6262
| Path Vertices From Start | [ ] | [ ] | [ ] | [ ] | [ ] |
6363

64-
>inf is equal infinity, which basically means, that algorithm doesn't know how far away is this vertex from start one.
64+
>inf is equal infinity which basically means that algorithm doesn't know how far away is this vertex from start one.
6565
6666
>F states for False
6767
6868
>T states for True
6969
70-
To initialize our graph we have to set source vertex path length from source vertex to 0, and append itself to path vertices from start.
70+
To initialize our graph we have to set source vertex path length from source vertex to 0 and append itself to path vertices from start.
7171

7272
| | A | B | C | D | E |
7373
|:------------------------- |:---:|:---:|:---:|:---:|:---:|
7474
| Visited | F | F | F | F | F |
7575
| Path Length From Start | 0 | inf | inf | inf | inf |
7676
| Path Vertices From Start | [A] | [ ] | [ ] | [ ] | [ ] |
7777

78-
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!
7979

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.
8383

8484
A.visited = true
8585

@@ -95,8 +95,8 @@ After this step graph has this state:
9595

9696
### Step 1
9797

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.
100100

101101
for clarity:
102102
```swift
@@ -162,41 +162,41 @@ From now we repeat all actions again and fill our table with new info!
162162

163163

164164
## 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.
166166
It is pretty simple
167167
```swift
168168
open class Vertex {
169169

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
171171
open var identifier: String
172172

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-
open var neighbors: [(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+
open var neighbours: [(Vertex, Double)] = []
177177

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.
180180
open var pathLengthFromStart = Double.infinity
181181

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.
184184
open var pathVerticesFromStart: [Vertex] = []
185185

186186
public init(identifier: String) {
187187
self.identifier = identifier
188188
}
189189

190190
//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.
192192
open func clearCache() {
193193
pathLengthFromStart = Double.infinity
194194
pathVerticesFromStart = []
195195
}
196196
}
197197
```
198198

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.
200200
```swift
201201
extension Vertex: Hashable {
202202
open var hashValue: Int {
@@ -212,11 +212,11 @@ extension Vertex: Equatable {
212212
```
213213

214214
We've created a base for our algorithm. Now let's create a house :)
215-
Dijkstra's realization is really straightforward.
215+
Dijkstra's realisation is really straightforward.
216216
```swift
217217
public class Dijkstra {
218218
//This is a storage for vertices in the graph.
219-
//Assuming, that our vertices are unique, we can use Set instead of array. This approach will bring some benefits later.
219+
//Assuming that our vertices are unique we can use Set instead of array. This approach will bring some benefits later.
220220
private var totalVertices: Set<Vertex>
221221

222222
public init(vertices: Set<Vertex>) {
@@ -230,52 +230,52 @@ public class Dijkstra {
230230
}
231231

232232
public func findShortestPaths(from startVertex: Vertex) {
233-
//Before we start searching shortes path from startVertex,
234-
//we need to clear vertices cache just to be sure, that out graph is as clean as a baby.
233+
//Before we start searching the shortest path from startVertex,
234+
//we need to clear vertices cache just to be sure that out graph is clean.
235235
//Remember that every Vertex is a class and classes are passed by reference.
236-
//So whenever you change vertex outside of this class, it will affect this vertex inside totalVertices Set
236+
//So whenever you change vertex outside of this class it will affect this vertex inside totalVertices Set
237237
clearCache()
238-
//Now all our vertices have Double.infinity pathLengthFromStart and empty pathVerticesFromStart array.
238+
//Now all our vertices have Double.infinity pathLengthFromStart and an empty pathVerticesFromStart array.
239239

240240
//The next step in the algorithm is to set startVertex pathLengthFromStart and pathVerticesFromStart
241241
startVertex.pathLengthFromStart = 0
242242
startVertex.pathVerticesFromStart.append(startVertex)
243243

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

248248
while let vertex = currentVertex {
249249

250-
//Nex line of code is an implementation of setting vertex as visited.
250+
//Next line of code is an implementation of setting vertex as visited.
251251
//As it has been said, we should check only unvisited vertices in the graph,
252252
//So why don't just delete it from the set? This approach let us skip checking for *"if !vertex.visited then"*
253253
totalVertices.remove(vertex)
254254

255-
//filteredNeighbors is and arrray, that contains current vertex neighbors, which aren't yet visited
256-
let filteredNeighbors = vertex.neighbors.filter { totalVertices.contains($0.0) }
255+
//filteredNeighbours is an array that contains current vertex neighbours which aren't yet visited
256+
let filteredNeighbours = vertex.neighbours.filter { totalVertices.contains($0.0) }
257257

258258
//Let's iterate through them
259-
for neighbor in filteredNeighbors {
260-
//These variable are more representative, than neighbor.0 or neighbor.1
261-
let neighborVertex = neighbor.0
262-
let weight = neighbor.1
259+
for neighbour in filteredNeighbours {
260+
//These variable are more representative, than neighbour.0 or neighbour.1
261+
let neighbourVertex = neighbour.0
262+
let weight = neighbour.1
263263

264-
//Here we calculate new weight, that we can offer to neighbor.
264+
//Here we calculate new weight, that we can offer to neighbour.
265265
let theoreticNewWeight = vertex.pathLengthFromStart + weight
266266

267-
//If it is smaller than neighbor's current pathLengthFromStart
267+
//If it is smaller than neighbour's current pathLengthFromStart
268268
//Then we perform this code
269-
if theoreticNewWeight < neighborVertex.pathLengthFromStart {
269+
if theoreticNewWeight < neighbourVertex.pathLengthFromStart {
270270

271271
//set new pathLengthFromStart
272-
neighborVertex.pathLengthFromStart = theoreticNewWeight
272+
neighbourVertex.pathLengthFromStart = theoreticNewWeight
273273

274274
//set new pathVerticesFromStart
275-
neighborVertex.pathVerticesFromStart = vertex.pathVerticesFromStart
275+
neighbourVertex.pathVerticesFromStart = vertex.pathVerticesFromStart
276276

277-
//append current vertex to neighbor's pathVerticesFromStart
278-
neighborVertex.pathVerticesFromStart.append(neighborVertex)
277+
//append current vertex to neighbour's pathVerticesFromStart
278+
neighbourVertex.pathVerticesFromStart.append(neighbourVertex)
279279
}
280280
}
281281

@@ -294,17 +294,17 @@ public class Dijkstra {
294294
}
295295
```
296296

297-
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.
298298

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.
300300

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 :)
302302

303303
# About this repository
304304

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.
308308

309309
# Demo video
310310

0 commit comments

Comments
 (0)