Javatpoint Logo
Javatpoint Logo

Dijkstra Algorithm Example

Pseudocode for Djikstra's algorithm

Every vertex's route distance must be preserved. That can be kept in a v-dimensional array, where v is the total number of vertices.

Not only do we want to know how long the shortest path is, but we also want to be able to find it. Each vertex is mapped to the vertex that most recently changed its route length for this purpose.

When the process is finished, we may move backwards to discover the path by going from the source vertex to the destination vertex.

The vertex with the shortest route distance can be efficiently received using a minimal priority queue.

Dijkstra's algorithm source code

Below is a C++ implementation of Dijkstra's algorithm. Although the abstractions make it easier to connect the code to the algorithm, the complexity of the code can still be increased.

Output

Distance from start: 4
f g d a
....................................
Process executed in 1.11 second
Press any key to continue.

Explanation:

  • The shortest distance is determined by the algorithm, but the information on the path is not determined. To display the shortest path from the source to several vertices, create a parent array, update the parent array whenever the distance is updated (similar to prim's approach), and then utilize it.
  • The Dijkstra function may be used for directed graphs as well; the code is for undirected graphs.
  • The program calculates the shortest paths between the source and each vertex. Break them for a loop when the chosen minimum distance vertex is equal to the target if we are only interested in the shortest distance between the source and a single target.
  • The implementation's time complexity is O (V2). A binary heap can be used to decrease the size of an adjacency list representation of the input graph to O (E * log V). For further information, see Dijkstra's Algorithm for Adjacency List Representation.
  • For graphs with adverse weight cycles, Dijkstra's method fails. For a graph with negative edges, it could provide accurate answers, but you must permit numerous visits to a vertex or the version will lose its quick time complexity. The Bellman-Ford approach may be utilized for graphs with negative weight edges and cycles; we will shortly go into more detail about this algorithm in a future post.

Dijkstra's Algorithm Complexity

Time Complexity: O(E Log V) Where, E is the number of edges and V is the number of vertices.

Space Complexity: O(V)

Examples of Dijkstra's Algorithms

  • To determine the quickest route
  • In applications for social networking
  • Within a phone network
  • To locate the places on the map

Heap-based Dijkstra's shortest route algorithm in O(E logV)

It is generally advised to use a heap (or priority queue) for Dijkstra's algorithm since the necessary operations (extract minimum and reduce key) correspond with the specialty of the heap (or priority queue). The decrease key is not supported by priority_queue, which is a concern. Instead of updating a key to fix this issue, insert an additional duplicate of it. Therefore, we let the priority queue to include several instances of the same vertex. This method contains the following crucial characteristics and doesn't call for diminishing vital procedures.

  • We increase the number of vertex instances in the priority_queue each time a vertex's distance is decreased. Even if there are several examples, we just take the one that is closest to us into account and disregard the others.
  • There will only be a maximum of O(E) vertices in the priority queue, and O(logE) is the same as O(logV), therefore the time complexity stays at O(E * LogV).

Output

Vertex Distance from Source
0          0
1          4
2          12
3          19
4          21
5          11
6          9
7          8
8          14
...............................
Process executed in 0.11 second
Press any key to continue.

Time Complexity: O(E * logV), Where E is the number of edges and V is the number of vertices.

Auxiliary Space: O(V)







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA