Reputation: 61
Assuming that there are no negative edges.
Floyd-Warshall has a constant runtime of O(V^3). Bellman Ford has a worst case runtime of O(VE), but a best case of O(E).
So running BF for each individual node would have a worst case runtime of O(EV^2) but a best case of O(VE), is this correct?
Upvotes: 3
Views: 1103
Reputation: 4192
Bellman Ford will be slower than Floyd-Warshall in almost all cases. If the graph is a tree, then E = V
, and both will be the same V^3
. However, its very easy for E
to be much larger. E
can be up to V^2
in the case of a complete graph, where BF on just one node will take just as long as FW on the entire graph.
There's rarely a reason to use BF when Dijkstra's can solve the same problem in E+VlogV
, which will be faster than VE
in again all cases other than a simple tree.
Upvotes: 2