Dijkstra’s algorithm 2018 – Explained with example!

If you studied high school or college in Computer Science major you will definitely come across this algorithm. So what is Dijkstra’s algorithm and why we need this algorithm, let us look deep about this in this article

Dijkstra’s Algorithm is an algorithm which is used to find the shortest distance between two nodes in a graph. This algorithm was developed by Dijkstra in 1959 to minimize the amount of wire needed to connect the pins in the back every machine in his institution. Dijkstra’s ultimate aim is to create the shortest path tree. Before looking into the actual algorithm let us see some of the practical usages of this algorithm:

You all guys are using Google maps, Right? Have you ever think why Google is recommending this route to reach your destination? Google maps use purely this Dijkstra’s algorithm to decide a route for your travel. Google Maps marks your location as node A and your destination as node b. After, with this algorithm, it goes through all the shortest nodes (Paths) from the previous node to reach the destination.

Dijkstra’s algorithm is majorly used in routing protocols which is essential for any router to generate their forwarding table. By using this algorithm the router can update the shortest path from one router to another in the network.

It is widely used in the telephone network for cost-effectiveness.

This algorithm plays a very big role in Flight agenda.

Now, we understand this algorithm has an impact on our current life. Let us see this algorithm to find the shortest Path tree.

Dijkstra’s ALGORITHM:

STEP 1: Initially create a set that monitors the vertices which are included in the Shortest path tree. Make this set as empty first.

STEP 2: Initialize the value ‘0’ for the source vertex to make sure this is not picked first.

STEP 3: Other than the source node makes all the nodes distance as infinite.

STEP 4: Now select a vertex X which is not in the tree set and has minimum values.

STEP 5: Now include the vertex X in the Shortest path tree set.

STEP 6: Now start iterating through all the nodes of the vertex X. For every adjacent vertex Y, the distance from the source and the weight of edge X-Y is lesser than the distance of Y then update the value of Y in the shortest path tree set.

Let us see how to implement this algorithm in Java programming. If you need to implement this algorithm in any other programming languages let us know in the comment section.

The output of this program will be:

Use Your favoriteIDE to run this Program OR Just copy this code into notepad and save the file as Solution.java [Class name.java]. Then run this program using CMD. [Javac Solution.java and java Solution]

Dijkstra’s Algorithm- Output Java

Conclusion

So that’s done with Dijkstra algorithm using java. Hope this article will help you to understand the Dijkstra’s Algorithm in 2018.Join