Dijkstra's Algorithm - is the one of the most classical algorithm for shortest path finding. If someone has to find the shortest path of a given problem, it's the way to go algorithm.

It's used to find shortest distance of nodes in a graph from the source node. That means it finds shortest distance for each node from the source node.

Here we will use the above graph to demonstrate Dijkstra-s Algorithm - Shortest Path Finding.

Here there are few terms used for the algorithm

- visited
- unvisited
- marked updated

Initially everything is started unvisited and we can pick any node in the graph as source node. For convenience we will mark node A source and unvisited as well. Apart from source node other nodes values are marked as infinity.

Here if we visit from A to B we will be counting cost. Here you may also call it * distance*. So from A to B the cost or distance is 4. In this case, this is also the shortest distance.

Visit node C

Now visit node * C* becomes interesting. There are two ways to visit node

This is where Dijkstra-s Algorithm shines and finds the shortest path and update the value. This kind of updating shortest value to a node is called relaxation. Let's get started and find the shortest path for all the nodes.

Relaxation

Here we use a terminology called relaxation, we also say we relax nodes to go to other nodes to find the shortest path. For example, when we go to * C* node from

Relaxation is trying different edges to find the shortest path.

Let's find the shortest path

Initially all the vertices are set to infinity except from the source node. In our case this is the first one with value 0.

So initially from * A* to

Learn more about edge relaxation here

```
if ( d[u] + c(u, v) < d[v] ) then
{
d[v] = d[u] + c(u, v)
}
```

The above is the pseudo code for edge relaxation and updating path value. Here we will understand it by below example

In the above example we have three nodes. And we will see use node A as source node. So from A to A distance is 0, and other node's distance is set to infinity.

From A to B or to C, the shortest edge is 4. So we will go to B first and set the distance as shortest distance. From B we will reach out the neighboring nodes. We have only one node C. So here B is marked as visited. And the total edge distance is 4+5 which 9. So from A to C the distance is 9 if we go through B. C does not have any unvisited nodes.

Now once again from A, we can visit C and but we see that here the edge is 11 which is greater than 9. So the shorted distance would be 9 to C going through B.

Algorithm

- Mark the source node with a current distance of 0 and the rest with infinity.
- Set the non-visited node with the smallest current distance as the current node, lets say C.
- For each neighbor N of the current node C: add the current distance of C with the weight of the edge connecting C-N. If it is smaller than the current distance of N, set it as the new current distance of N.
- Mark the current node C as visited.
- Go to step 2 if there are any nodes are unvisited.

Let's see how it all work

Lets assume the below graph as our input with the vertex A being the source.

- Initially all the vertices are marked unvisited.
- The path to A is 0 and for all the other vertices it is set to infinity.
- Now the source vertex A is marked as visited.Then its neighbours are accessed(only accessed and not visited).
- The path to B is updated to 4 using relaxation as the path to A is 0 and path from A to B is 4, so min((0+4),∞) is 4.
- The path to C is updated to 5 using relaxation as the path to A is 0 and path from A to C is 5, so min((0+5),∞) is 5.Both the neighbours of A are relaxed so we move ahead.
- Then the next unvisited vertex with least path is picked and visited. So vertex B is visited and its unvisited neighbours are relaxed.After relaxing path to C remains 5, path to E becomes 11 and path to D becomes 13.
- Then vertex C is visited and its unvisited neighbour E is relaxed. While relaxing E, we find that the path to E via is smaller than its current path, so the path to E is updated to 8.All neighbours of C are now relaxed.
- Vertex E is visited and its neighbours B,D and F are relaxed. As only vertex F is unvisited only, F is relaxed. Path of B remains 4, path to D remains 13 and path to F becomes 14(8+6).
- Then vertex D is visited and only F is relaxed. .The path to vertex F remains 14.
- Now only vertex F is remaining so it is visited but no relaxations are performed as all of its neighbours are already visited.
- As soon as all the vertices become visited the program stops.

- React Native E-commerce App 10+ hours (Beginners friendly)
- Complete Beginners Course 3+ Hours (Beginners SwiftUI)
- Complete Beginners Course 7+ Hours (Beginners)
- Beginners Course 12+ Hours (Beginners-Riverpod)
- Complete Firebase Chat App 12+ Hours (Intermediate-Getx)
- Complete Flutter BLoC App Course 29 Hours (Advanced-BLoC)
- Flutter Audio Video Chat App Course 24 Hours (Advanced-Getx)
- Flutter Nodejs App Course 13+ Hours (More Advanced-Provider)
- Flutter BLoC TDD & Clean Architecture | Project Based (Not for beginners)

- PHP - Stripe Payment
- Flutter Clean Architecture and TDD
- React Native Travel App Tutorial
- Flutter Clean Architecture With TDD | BLoC | Cubit
- Riverpod AsyncNotifier Explained
- Flutter BLoC Update List | CRUD
- Go Lang JWT Authentication
- Go Run Your First Program The Right Way
- Flutter Riverpod Latest Guide
- Flutter Dio Complete Guide