Open In App

Time and Space Complexity of Bellman–Ford Algorithm

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

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 vertices in the graph. This space complexity is mainly due to storing the distances from the source vertex to all other vertices in the graph.

Operation Time Complexity Space Complexity
Initialization O(V) O(V)
Relaxation O(V*E) O(1)
Overall Complexity O(V*E) O(V)

Let’s explore the detailed time and space complexity of the Bellman–Ford Algorithm:

Time Complexity of Bellman-Ford Algorithm:

Best Case: O(E)

  • The best-case time complexity of O(E) occurs when no relaxation is required during Bellman-Ford algorithm execution.
  • Algorithm terminates after a single pass through all edges without updating any distances.
  • Initial distances assigned to vertices are already the shortest paths from the source vertex.
  • With no relaxations needed, algorithm efficiently traverses each edge once to verify validity.
  • Time complexity directly proportional to number of edges, as algorithm performs only one pass through all edges.
  • Efficiency in edge traversal results in reduced time complexity, particularly in sparse graphs with fewer edges.

Average Case: O(V*E)

  • The average-case time complexity of Bellman-Ford algorithm remains O(V*E).
  • This complexity is maintained across various graph structures and densities, as the algorithm’s performance primarily depends on the number of vertices and edges.
  • In practical scenarios, the average case is similar to the worst case, especially in dense graphs with many edges.

Worst Case: O(V*E)

  • The worst-case time complexity of Bellman-Ford algorithm is also O(V*E).
  • This scenario occurs when the algorithm needs to iterate through all vertices and edges for |V| – 1 passes, followed by one more pass for detecting negative weight cycles.
  • It’s important to note that the worst case includes the presence of negative weight cycles, which can cause the algorithm to run indefinitely if not handled properly.

Auxiliary Space Complexity of Bellman–Ford Algorithm:

The auxiliary space complexity of the Bellman-Ford algorithm is O(V), where V is the number of vertices in the graph, primarily due to the need to store distances from the source vertex to all other vertices.

  • A distance array, storing distances from the source vertex to every other vertex, requiring O(V) space.
  • Additional data structures, such as for tracking predecessors or relaxation updates, contributing linearly to space complexity.
  • Optional use of queues or stacks for vertex relaxation, which typically require minimal space compared to primary data structures.

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads