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
+54-19Lines changed: 54 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,30 +1,40 @@
1
-
# Dijkstra-algorithm
1
+
# Dijkstra's algorithm
2
2
3
3
This algorithm was invented in 1956 by Edsger W. Dijkstra.
4
4
5
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.
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 a gif example, which will show you how algorithm works. If this is not enough, then you can play with my **VisualizedDijkstra.playground**.
10
-
So let's image, that your house is "A" vertex and your job is "B" vertex. And you are lucky, you have graph with all possible routes.
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.
10
+
11
+
So let's imagine, that your house is "A" vertex and your job is "B" vertex. And you are lucky, you have graph with all possible routes.
12
+
13
+
> Initialization
14
+
11
15
When the algorithm starts to work initial graph looks like this:
Let's assume, that edge weight it is path length in kilometers between vertices.
25
-
A vertex has neighbors: B(path from A: 5.0), C(path from A: 0.0), D(path from A: 0.0)
26
-
And because algorithm has done nothing, then all vertices' path length from source vertex values are infinity (think about infinity as "I don't know, how long will it takes to get this vertex from source one")
27
-
Finally we have to set source vertex path length from source vertex to 0.
27
+
_inf is equal infinity, which basically means, that algorithm doesn't know how far away is this vertex from start one._
28
+
_F states for False_
29
+
_T states for True_
30
+
31
+
To initialize out graph we have to set source vertex path length from source vertex to 0.
Great, now our graph is initialized and we can pass it to the Dijkstra's algorithm.
30
40
@@ -37,34 +47,51 @@ Cycle:
37
47
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.
38
48
39
49
Okay, let's start!
40
-
From now we will keep array which contains non visited vertices. Here they are:
41
-
var nonVisitedVertices = [A, B, C, D, E]
42
-
Let's follow the algorithm and pick the first vertex, which neighbors we want to check.
43
-
Imagine that we have function, that returns vertex with smallest path length from start.
44
-
var checkingVertex = nonVisitedVertices.smallestPathLengthFromStartVertex()
45
-
Then we set this vertex as visited
50
+
Let's follow the algorithm's cycle and pick the first vertex, which neighbors we want to check.
51
+
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.
If neighbor's path length from start is bigger than checking vertex path length from start + edge weigth, 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.
69
+
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.
70
+
71
+
for clarity:
72
+
```swift
73
+
if (A.pathLengthFromStart+ AB.weight) < B.pathLengthFromStart {
0 commit comments