# Weighted graph general concepts
Every weighted graph should contain:
1. Vertices/Nodes (I will use "vertex" in this readme).
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:
* All undirected graphs can be viewed as a directed graph.
* A directed graph is undirected if and only if every edge is paired with an edge going in the opposite direction.
3. Weights for every edge.
Final result.
Directed weighted graph:
Undirected weighted graph:
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.
Great! Now we are familiar with general concepts about graphs.
# The Dijkstra's algorithm
This [algorithm](https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm) was invented in 1956 by Edsger W. Dijkstra.
It can be used when you have one source vertex and want to find the shortest paths to ALL other vertices in the graph.
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.
The algorithm repeats following cycle until all vertices are marked as visited.
Cycle:
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)
2. The algorithm marks picked vertex as visited.
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.
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.
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.
A short sidenote. The Swift Algorithm Club also contains the A* algorithm, which essentially is a faster version of Dijkstra's algorithm for which the only extra prerequisite is you have to know where the destination is located.
## Example
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 :)
### Initialisation
When the algorithm starts to work initial graph looks like this:
The table below represents graph state:
| | A | B | C | D | E |
|:------------------------- |:---:|:---:|:---:|:---:|:---:|
| Visited | F | F | F | F | F |
| Path Length From Start | inf | inf | inf | inf | inf |
| Path Vertices From Start | [ ] | [ ] | [ ] | [ ] | [ ] |
>inf is equal infinity which basically means that algorithm doesn't know how far away is this vertex from start one.
>F states for False
>T states for True
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.
| | A | B | C | D | E |
|:------------------------- |:---:|:---:|:---:|:---:|:---:|
| Visited | F | F | F | F | F |
| Path Length From Start | 0 | inf | inf | inf | inf |
| Path Vertices From Start | [A] | [ ] | [ ] | [ ] | [ ] |
Great, now our graph is initialised and we can pass it to the Dijkstra's algorithm, let's start!
Let's follow the algorithm's cycle and pick the first vertex which neighbours we want to check.
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.
First of all, set this vertex as visited.
A.visited = true
After this step graph has this state:
| | A | B | C | D | E |
|:------------------------- |:---:|:---:|:---:|:---:|:---:|
| Visited | T | F | F | F | F |
| Path Length From Start | 0 | inf | inf | inf | inf |
| Path Vertices From Start | [A] | [ ] | [ ] | [ ] | [ ] |
### Step 1
Then we check all of its neighbours.
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.
for clarity:
```swift
if (A.pathLengthFromStart + AB.weight) < B.pathLengthFromStart {
B.pathLengthFromStart = A.pathLengthFromStart + AB.weight
B.pathVerticesFromStart = A.pathVerticesFromStart
B.pathVerticesFromStart.append(B)
}
```
And now our graph looks like this one:
And its state is here:
| | A | B | C | D | E |
|:------------------------- |:----------:|:----------:|:----------:|:----------:|:----------:|
| Visited | T | F | F | F | F |
| Path Length From Start | 0 | 3 | inf | 1 | inf |
| Path Vertices From Start | [A] | [A, B] | [ ] | [A, D] | [ ] |
### Step 2
From now we repeat all actions again and fill our table with new info!
| | A | B | C | D | E |
|:------------------------- |:----------:|:----------:|:----------:|:----------:|:----------:|
| Visited | T | F | F | T | F |
| Path Length From Start | 0 | 3 | inf | 1 | 2 |
| Path Vertices From Start | [A] | [A, B] | [ ] | [A, D] | [A, D, E] |
### Step 3
| | A | B | C | D | E |
|:------------------------- |:----------:|:----------:|:----------:|:----------:|:----------:|
| Visited | T | F | F | T | T |
| Path Length From Start | 0 | 3 | 11 | 1 | 2 |
| Path Vertices From Start | [A] | [A, B] |[A, D, E, C]| [A, D] | [A, D, E ] |
### Step 4
| | A | B | C | D | E |
|:------------------------- |:----------:|:----------:|:----------:|:----------:|:----------:|
| Visited | T | T | F | T | T |
| Path Length From Start | 0 | 3 | 8 | 1 | 2 |
| Path Vertices From Start | [A] | [A, B] | [A, B, C]| [A, D] | [A, D, E ] |
### Step 5
| | A | B | C | D | E |
|:------------------------- |