Skip to content

Commit 3755036

Browse files
author
Taras Nikulin
committed
Still improving description
1 parent 8e0d77f commit 3755036

File tree

1 file changed

+54
-19
lines changed

1 file changed

+54
-19
lines changed

Dijkstra Algorithm/README.md

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,40 @@
1-
# Dijkstra-algorithm
1+
# Dijkstra's algorithm
22

33
This algorithm was invented in 1956 by Edsger W. Dijkstra.
44

55
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.
66

77
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.
88

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+
1115
When the algorithm starts to work initial graph looks like this:
1216

1317
<img src="Images/image1.png" height="250" />
1418

15-
The table below will represent graph state
19+
The table below represents graph state:
1620

1721
| | A | B | C | D | E |
1822
|:------------------------- |:---:|:---:|:---:|:---:|:---:|
1923
| Visited | F | F | F | F | F |
2024
| Path Length From Start | inf | inf | inf | inf | inf |
2125
| Path Vertices From Start | [ ] | [ ] | [ ] | [ ] | [ ] |
2226

23-
Graph's array contains 5 vertices [A, B, C, D, E].
24-
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.
32+
33+
| | A | B | C | D | E |
34+
|:------------------------- |:---:|:---:|:---:|:---:|:---:|
35+
| Visited | F | F | F | F | F |
36+
| Path Length From Start | 0 | inf | inf | inf | inf |
37+
| Path Vertices From Start | [ ] | [ ] | [ ] | [ ] | [ ] |
2838

2939
Great, now our graph is initialized and we can pass it to the Dijkstra's algorithm.
3040

@@ -37,34 +47,51 @@ Cycle:
3747
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.
3848

3949
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.
52+
First of all, set this vertex as visited
53+
54+
A.visited = true
55+
56+
<img src="Images/image2.png" height="250" />
57+
58+
After this step graph has this state:
4659

4760
| | A | B | C | D | E |
4861
|:------------------------- |:---:|:---:|:---:|:---:|:---:|
4962
| Visited | T | F | F | F | F |
5063
| Path Length From Start | 0 | inf | inf | inf | inf |
5164
| Path Vertices From Start | [A] | [ ] | [ ] | [ ] | [ ] |
5265

53-
checkingVertex.visited = true
54-
55-
<img src="Images/image2.png" height="250" />
66+
> Step 1
5667
5768
Then we check all of its neighbors.
58-
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 {
74+
B.pathLengthFromStart = A.pathLengthFromStart + AB.weight
75+
B.pathVerticesFromStart = A.pathVerticesFromStart
76+
B.pathVerticesFromStart.append(B)
77+
}
78+
```
79+
And now our graph looks like this one:
5980

6081
<img src="Images/image3.png" height="250" />
6182

83+
And its state is here:
84+
6285
| | A | B | C | D | E |
6386
|:------------------------- |:----------:|:----------:|:----------:|:----------:|:----------:|
6487
| Visited | T | F | F | F | F |
6588
| Path Length From Start | 0 | 3 | inf | 1 | inf |
6689
| Path Vertices From Start | [A] | [A, B] | [ ] | [A, D] | [ ] |
6790

91+
> Step 2
92+
93+
From now we repeat all actions again and fill our table with new info!
94+
6895
<img src="Images/image4.png" height="250" />
6996

7097
| | A | B | C | D | E |
@@ -73,6 +100,8 @@ If neighbor's path length from start is bigger than checking vertex path length
73100
| Path Length From Start | 0 | 3 | inf | 1 | 2 |
74101
| Path Vertices From Start | [A] | [A, B] | [ ] | [A, D] | [A, D, E] |
75102

103+
> Step 3
104+
76105
<img src="Images/image5.png" height="250" />
77106

78107
| | A | B | C | D | E |
@@ -81,6 +110,8 @@ If neighbor's path length from start is bigger than checking vertex path length
81110
| Path Length From Start | 0 | 3 | 11 | 1 | 2 |
82111
| Path Vertices From Start | [A] | [A, B] |[A, D, E, C]| [A, D] | [A, D, E ] |
83112

113+
> Step 4
114+
84115
<img src="Images/image6.png" height="250" />
85116

86117
| | A | B | C | D | E |
@@ -89,8 +120,12 @@ If neighbor's path length from start is bigger than checking vertex path length
89120
| Path Length From Start | 0 | 3 | 8 | 1 | 2 |
90121
| Path Vertices From Start | [A] | [A, B] | [A, B, C]| [A, D] | [A, D, E ] |
91122

123+
> Step 5
124+
92125
<img src="Images/image7.png" height="250" />
93126

127+
> Step 6
128+
94129
| | A | B | C | D | E |
95130
|:------------------------- |:----------:|:----------:|:----------:|:----------:|:----------:|
96131
| Visited | T | T | T | T | T |

0 commit comments

Comments
 (0)