Dijkstra Algorithm Java10 May 2025 | 9 min read 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 StepsStep1: 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 CodeImplementation of Dijkstra AlgorithmThe following code implements the Dijkstra Algorithm using the diagram mentioned below. ![]() 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 AlgorithmThe following are some limitations of the Dijkstra Algorithm:
Usages of Dijkstra AlgorithmA few prominent usages of the Dijkstra algorithm are:
Next TopicPair-sum-closest-to-0-problem-in-java |
In the world of programming, dealing with large numbers is a common occurrence. When it comes to handling massive numerical values, Java provides a powerful class called BigInteger. In this section, we will explore how to convert strings into BigInteger objects in Java enabling us to...
2 min read
Java, a strong and flexible programming language, has long been a mainstay of the software development sector. Java has remained relevant and well-liked since its introduction in the middle of the 1990s, making it a great option for anybody wishing to enter the programming profession or...
4 min read
The Two Sum - Pairs with Zero Sum is another algorithmic problem referred to as a problem of identifying pairs of integers in an array that sum up to zero. This problem is quite common among coding interviews and competitive programming since it requires not only...
5 min read
A Java default keyword is an access modifier. If we do not assign any access modifier to variables, methods, constructors, and classes, by default, it is considered as default access modifier. The default keyword is a versatile and powerful tool that plays a crucial role...
10 min read
(Usage and Examples) The Java `new` keyword creates a class instance by allocating dynamic memory for a new object and returns a reference to that memory. It can also be used to create an array object. When the `new` keyword is employed, it executes the class...
6 min read
Apache Maven is used as a project management tool based on a project object model (POM). It is useful for dependency management, project build, and document. To add any dependency in our project, we need to maintain a pom.xml file that contains the dependencies in the...
5 min read
Sylvester's sequence is a mathematical sequence where each term is derived from the product of all ious terms plus one. It starts with 2, and subsequent terms grow rapidly. This sequence has applications in number theory and combinatorics. Implementing it in Java involves recursive or iterative...
8 min read
Prime factorization is a fundamental concept in number theory that involves breaking down a composite number into its smallest prime factors. This process is invaluable in various fields of mathematics and computer science, including cryptography and number theory. In this section, we will explore how to...
4 min read
The bully algorithm is a type of Election algorithm which is mainly used for choosing a coordinate. In a distributed system, we need some election algorithms such as bully and ring to get a coordinator that performs functions needed by other processes. Election algorithms select a single...
4 min read
The factorial of a number N is the product of all positive descending integers (integers less than or equal to N). N! = N * (N - 1) ... * 3 * 2 * 1 In this section, we will create Java programs to find the factorial of...
3 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India