Skip to content

Commit 35db21b

Browse files
author
Taras Nikulin
committed
Updated description
1 parent 0a813ba commit 35db21b

6 files changed

+42
-14
lines changed
67.1 KB
Loading
35.1 KB
Loading
Loading
Loading
Loading

Dijkstra Algorithm/README.md

+42-14
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,50 @@
1+
#Weighted graph general cocepts
2+
3+
Every weighted graph should contain:
4+
1. Vertices/Nodes (I will use "vertex" in this readme).
5+
6+
<img src="Images/Vertices.png" height="250" />
7+
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:
9+
* All undirected graphs can be viewed as a directed graph.
10+
* A directed graph is undirected if and only if every edge is paired with an edge going in the opposite direction.
11+
12+
<img src="Images/DirectedGraph.png" height="250" />
13+
14+
3. Weights for every edge.
15+
16+
<img src="Images/WeightedDirectedGraph.png" height="250" />
17+
18+
Final result.
19+
Directed weighted graph:
20+
21+
<img src="Images/WeightedDirectedGraphFinal.png" height="250" />
22+
23+
Undirected weighted graph:
24+
25+
<img src="Images/WeightedUndirectedGraph.png" height="250" />
26+
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.
28+
29+
Great! Now we are familiar with general concepts about graphs.
30+
131
# Dijkstra's algorithm
32+
This [algorithm](https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm) was invented in 1956 by Edsger W. Dijkstra.
233

3-
This [algorithm](https://en.wikipedia.org/wiki/Dijkstra%27s_algorithm) was invented in 1956 by Edsger W. Dijkstra.
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.
435

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

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.
38+
The algorithm repeats following cycle until all vertices are marked as visited.
39+
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)
41+
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.
43+
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.
844

9-
I have created **VisualizedDijkstra.playground** to improve your understanding of the algorithm's flow. Besides, below is step by step algorithm's description.
45+
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.
1046

47+
#Example
1148
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 :)
1249

1350
## Initialization
@@ -38,17 +75,8 @@ To initialize our graph we have to set source vertex path length from source ver
3875
| Path Length From Start | 0 | inf | inf | inf | inf |
3976
| Path Vertices From Start | [A] | [ ] | [ ] | [ ] | [ ] |
4077

41-
Great, now our graph is initialized and we can pass it to the Dijkstra's algorithm.
42-
43-
But before we will go through all process side by side read this explanation.
44-
The algorithm repeats following cycle until all vertices are marked as visited.
45-
Cycle:
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)
47-
2. The algorithm marks picked vertex as visited.
48-
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.
49-
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.
78+
Great, now our graph is initialized and we can pass it to the Dijkstra's algorithm, let's start!
5079

51-
Okay, let's start!
5280
Let's follow the algorithm's cycle and pick the first vertex, which neighbors we want to check.
5381
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.
5482
First of all, set this vertex as visited

0 commit comments

Comments
 (0)