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
+9-7Lines changed: 9 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -2,13 +2,13 @@
2
2
3
3
This [algorithm](https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm) was invented in 1956 by Edsger W. Dijkstra.
4
4
5
-
This algorithm can be used, when you have one source vertex and want to find the shortest paths to all other vertices in the graph.
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.
6
6
7
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.
8
8
9
-
I have created **VisualizedDijkstra.playground** to help you to understand, how this algorithm works. Besides, I have described below step by step how does it works.
9
+
I have created **VisualizedDijkstra.playground** to improve your understanding of the algorithm's flow. Besides, below is step by step algorithm's description.
10
10
11
-
So let's imagine, that your house is "A" vertex and your job is "B" vertex. And you are lucky, you have graphwith all possible routes.
11
+
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
12
13
13
## Initialization
14
14
@@ -30,7 +30,7 @@ The table below represents graph state:
30
30
31
31
>T states for True
32
32
33
-
To initialize out graph we have to set source vertex path length from source vertex to 0, and append itself to path vertices ffrom start.
33
+
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.
@@ -40,7 +40,7 @@ To initialize out graph we have to set source vertex path length from source ver
40
40
41
41
Great, now our graph is initialized and we can pass it to the Dijkstra's algorithm.
42
42
43
-
But before we will go through all process side by side let me explain how algorithm works.
43
+
But before we will go through all process side by side read this explanation.
44
44
The algorithm repeats following cycle until all vertices are marked as visited.
45
45
Cycle:
46
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)
@@ -50,7 +50,7 @@ When all vertices are marked as visited, the algorithm's job is done. Now, you c
50
50
51
51
Okay, let's start!
52
52
Let's follow the algorithm's cycle and pick the first vertex, which neighbors we want to check.
53
-
All our vertices are not visited, but there is only one have the smallest path length from start - A. This vertex is th first one, which neighbors we will check.
53
+
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
54
First of all, set this vertex as visited
55
55
56
56
A.visited = true
@@ -268,7 +268,9 @@ public class Dijkstra {
268
268
269
269
That's all! Now you can check this algorithm in the playground. On the main page there is a code for creating random graph.
270
270
271
-
Also there is a **VisualizedDijkstra.playground**. Use it to figure out algorithm flow in real (slowed :)) time.
271
+
Also there is a **VisualizedDijkstra.playground**. Use it to figure out algorithm's flow in real (slowed :)) time.
272
+
273
+
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 :)
0 commit comments