The Wayback Machine - https://web.archive.org/web/20240705052310/https://www.geeksforgeeks.org/time-and-space-complexity-of-dijkstras-algorithm/
Open In App

Time and Space Complexity of Dijkstra’s Algorithm

Last Updated : 09 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The time complexity of Dijkstra’s Algorithm is typically O(V2) when using a simple array implementation or O((V + E) log V) with a priority queue, where V represents the number of vertices and E represents the number of edges in the graph. The space complexity of the algorithm is O(V) for storing the distances and predecessors for each node, along with additional space for data structures like priority queues or arrays.

Aspect Complexity
Time Complexity O((V + E) log V)
Space Complexity O(V)

Let’s explore the detailed time and space complexity of the Dijkstra’s Algorithm:

Time Complexity of Dijkstra’s Algorithm:

Best Case Time Complexity: O((V + E) log V)

  • This best-case scenario occurs when using an optimized data structure like a Fibonacci heap for implementing the priority queue.
  • The time complexity is determined by the graph’s number of vertices (V) and edges (E).
  • In this scenario, the algorithm efficiently finds the shortest paths, with the priority queue operations optimized, leading to the overall time complexity of O((V + E) log V).
  • This scenario is typically encountered when the graph is sparse, meaning it has relatively few edges compared to vertices.

Average Case Time Complexity: O((V + E) log V)

  • The average-case time complexity of Dijkstra’s algorithm is typically the same as the best-case scenario, O((V + E) log V).
  • This is because Dijkstra’s algorithm performs well on most real-world graphs, which are often neither extremely sparse nor fully connected.
  • The algorithm efficiently finds shortest paths in graphs with varying densities, finding a balance between the quantity of edges and vertices.
  • In practice, this average complexity is encountered in a wide range of scenarios, making Dijkstra’s algorithm a reliable choice for many shortest path problems.

Worst Case Time Complexity: O((V2) log V)

  • In the worst-case scenario, Dijkstra’s algorithm operates less efficiently, typically when using a simple priority queue or an array-based implementation.
  • This occurs when the graph is dense, with many edges, and the priority queue operations become less efficient due to the lack of optimization.
  • The time complexity in this case is determined by the number of vertices squared (V2) and logarithmic factors related to priority queue operations, resulting in O((V2) log V).
  • The worst-case scenario often arises in fully connected graphs or graphs with many edges between each pair of vertices.

Auxiliary Space Complexity of Dijkstra’s Algorithm:

The auxiliary space complexity of Dijkstra’s algorithm is typically O(V) to O(E + V), where V is the number of vertices and E is the number of edges in the graph, depending on the implementation and data structures used.

The auxiliary space complexity of Dijkstra’s algorithm primarily depends on the data structures used for implementation, particularly the priority queue for managing vertices with their associated distances.



Similar Reads

Time Complexity and Space Complexity
Generally, there is always more than one way to solve a problem in computer science with different algorithms. Therefore, it is highly required to use a method to compare the solutions in order to judge which one is more optimal. The method must be: Independent of the machine and its configuration, on which the algorithm is running on.Shows a direc
14 min read
Time and Space Complexity of DFS and BFS Algorithm
The time complexity of both Depth-First Search (DFS) and Breadth-First Search (BFS) algorithms is O(V + E), where V is the number of vertices and E is the number of edges in the graph. The space complexity of DFS is O(V), where V represents the number of vertices in the graph, and for BFS, it is O(V), where V represents the number of vertices in th
2 min read
Time and Space Complexity of Floyd Warshall Algorithm
The Floyd Warshall Algorithm has a time complexity of O(V3) and a space complexity of O(V2), where V represents the number of vertices in the graph. This algorithm computes the shortest paths between all pairs of vertices in a weighted graph. The time complexity arises from the triple nested loops used to update the shortest path matrix, while the
3 min read
Time and Space Complexity of Bellman–Ford Algorithm
The Bellman-Ford algorithm has a time complexity of O(V*E), where V is the number of vertices and E is the number of edges in the graph. In the worst-case scenario, the algorithm needs to iterate through all edges for each vertex, resulting in this time complexity. The space complexity of the Bellman-Ford algorithm is O(V), where V is the number of
2 min read
Time and Space complexity of Radix Sort Algorithm
The Radix Sort Algorithm has a time complexity of O(n*d), where n is the number of elements in the input array and d is the number of digits in the largest number. The space complexity of Radix Sort is O(n + k), where n is the number of elements in the input array and k is the range of the input. This algorithm is efficient for sorting integers, es
2 min read
Time and Space Complexity Analysis of Prim's Algorithm
The time complexity of Prim's algorithm is O(V2) using an adjacency matrix and O((V +E) log V) using an adjacency list, where V is the number of vertices and E is the number of edges in the graph. The space complexity is O(V+E) for the priority queue and O(V2) for the adjacency matrix representation. The algorithm's time complexity depends on the d
3 min read
Time and Space Complexity Analysis of Kruskal Algorithm
Kruskal's algorithm is a popular algorithm for finding the Minimum Spanning Tree (MST) of a connected, undirected graph. The time complexity of Kruskal's algorithm is O(E log E), where E is the number of edges in the graph. The space complexity of Kruskal's algorithm is O(V + E), where V is the number of vertices and E is the number of edges in the
2 min read
Time and Space Complexity of Huffman Coding Algorithm
Huffman coding is a popular algorithm used for the lossless data compression. It works by assigning variable-length codes to input characters with the shorter codes assigned to more frequent characters. This results in a prefix-free binary code meaning no code is a prefix of the another. The algorithm was developed by the David A. Huffman in 1952 a
2 min read
Time and Space Complexity Analysis of Binary Search Algorithm
Time complexity of Binary Search is O(log n), where n is the number of elements in the array. It divides the array in half at each step. Space complexity is O(1) as it uses a constant amount of extra space. AspectComplexityTime ComplexityO(log n)Space ComplexityO(1)The time and space complexities of the binary search algorithm are mentioned below.
3 min read
Time and Space Complexity of Linear Search Algorithm
The time complexity of the Linear Search algorithm is O(n), where n is the number of elements in the array. The space complexity is O(1) as it requires a constant amount of extra space regardless of the input size. AspectComplexityTime ComplexityO(n)Space ComplexityO(1)Let's explore the detailed time and space complexity of the Linear Search Algori
2 min read
three90RightbarBannerImg