Linchakin

8th Sep - Dijkstra Algorithm Java

 September 08, 2021     No comments   

Dijkstra algorithm is one of the prominent algorithms to find the shortest path from the source node to a destination node. It uses the greedy approach to find the shortest path. The concept of the Dijkstra algorithm is to find the shortest distance (path) starting from the source point and to ignore the longer distances while doing an update.

In this section, we will implement the Dijkstra algorithm in Java program. Also, we will discuss its usage and limitations.

Dijkstra Algorithm Steps

Step1: All nodes should be marked as unvisited.

Step2: All the nodes must be initialized with the "infinite" (a big number) distance. The starting node must be initialized with zero.

Step3: Mark starting node as the current node.

Step4: From the current node, analyze all of its neighbors that are not visited yet, and compute their distances by adding the weight of the edge, which establishes the connection between the current node and neighbor node to the current distance of the current node.

Step5: Now, compare the recently computed distance with the distance allotted to the neighboring node, and treat it as the current distance of the neighboring node,

Step6: After that, the surrounding neighbors of the current node, which has not been visited, are considered, and the current nodes are marked as visited.

Step7: When the ending node is marked as visited, then the algorithm has done its job; otherwise,

Step8: Pick the unvisited node which has been allotted the minimum distance and treat it as the new current node. After that, start again from step4.

Dijkstra Algorithm Pseudo Code

Implementation of Dijkstra Algorithm

The following code implements the Dijkstra Algorithm using the diagram mentioned below.

Dijkstra Algorithm Java

FileName: DijkstraExample.java

Output:

The shortest Distance from source 0th node to all other nodes are: 
To 0 the shortest distance is: 0
To 1 the shortest distance is: 3
To 2 the shortest distance is: 8
To 3 the shortest distance is: 10
To 4 the shortest distance is: 18
To 5 the shortest distance is: 10
To 6 the shortest distance is: 9
To 7 the shortest distance is: 7
To 8 the shortest distance is: 7

The time complexity of the above code is O(V2), where V is the total number of vertices present in the graph. Such time complexity does not bother much when the graph is smaller but troubles a lot when the graph is of larger size. Therefore, we have to do the optimization to reduce this complexity. With the help of the priority queue, we can decrease the time complexity. Observe the following code that is written for the graph depicted above.

FileName: DijkstraExample1.java

Output:

The shortest path from the node:
0 to 0 is 0
0 to 1 is 3
0 to 2 is 8
0 to 3 is 10
0 to 4 is 18
0 to 5 is 10
0 to 6 is 9
0 to 7 is 7
0 to 8 is 7

The time complexity of the above implementation is O(V + E*log(V)), where V is the total number of vertices, and E is the number of Edges present in the graph.

Limitations of Dijkstra Algorithm

The following are some limitations of the Dijkstra Algorithm:

  1. The Dijkstra algorithm does not work when an edge has negative values.
  2. For cyclic graphs, the algorithm does not evaluate the shortest path. Hence, for the cyclic graphs, it is not recommended to use the Dijkstra Algorithm.

Usages of Dijkstra Algorithm

A few prominent usages of the Dijkstra algorithm are:

  1. The algorithm is used by Google maps.
  2. The algorithm is used to find the distance between two locations.
  3. In IP routing also, this algorithm is used to discover the shortest path.

Adblock test (Why?)


You may be interested in:
>> Is a Chromebook worth replacing a Windows laptop?
>> Find out in detail the outstanding features of Google Pixel 4a
>> Top 7 best earbuds you should not miss

Related Posts:
>> Recognizing 12 Basic Body Shapes To Choose Better Clothes
>>Ranking the 10 most used smart technology devices
>> Top 5+ Best E-readers: Compact & Convenient Pen
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook

Related Posts:

  • Windows 11 Activator + Product Key [Latest-2022] DownloadWindows 10 Product Key + Crack Full 2022  Final Download Windows 10 Activator With Product Key Generator 2022 is here to help you activate Wind… Read More
  • CyberLink PowerDirector 20 Crack With Keygen [2022]CyberLink PowerDirector 20 Crack [Keygen] + Torrent 2021 CyberLink PowerDirector 20.0 Crack build 2204 is a very good program that helps you to… Read More
  • TIWAP - Totally Insecure Web Application Project TIWAP is a web security testing lab made using Flask for budding security enthusiasts to learn about various web vulnerabilities. Inspired by DVWA, t… Read More
  • 8 Truth Bombs of Launching your Product on Product Hunt🚀 November 3rd 2021 new story Wylo, an interest-based social network on Product Hunt, launched last month. We managed to secure … Read More
  • On Food Waste and Relevance of Privacy: Noonies Nominee Paran SonthaliaParan Sonthalia is nominated for a 2021 Noonies award. She is the CEO of DeWaste, a company that analyzes food waste using computer vision. She says s… Read More
Newer Post Older Post Home

0 Comments:

Post a Comment


Copyright © 2025 Linchakin | Powered by Blogger
Design by Hardeep Asrani | Blogger Theme by NewBloggerThemes.com | Distributed By Gooyaabi Templates