Search code examples
algorithmgraph-algorithmspace-complexitybellman-ford

Bellman-Ford Algorithm Space Complexity


I have been searching about Bellman-Ford Algorithm's space complexity but on wikipedia Bellman-Ford Algorithm and it says space complexity is O(V). on this link it says O(V^2) . My question is; what is the true space complexity and why?


Solution

  • It depends on the way we define it.

    1. If we assume that the graph is given, the extra space complexity is O(V) (for an array of distances).

    2. If we assume that the graph also counts, it can be O(V^2) for an adjacency matrix and O(V+E) for an adjacency list.

    They both are "true" in some sense. It's just about what we want to count in a specific problem.