I'm implementing my own version of the Bellman-Ford algorithm to find shortest paths with negative weights. However I'm not sure if this is correct, since in my opinion the complexity in my solution differs from the widely accepted Bellman-Ford complexity of O(|V||E| + |E|) = O(|V||E|).
For this I'm using an array A of adjacency lists for each vertex k in the graph. Each element in the adjacency list for vertex k, elem has two data members elem->vertex, corresponding to the edge (k , elem->vertex) and the weight elem->weight of the same edge. iS is the starting node and iT the end node.
I simplified the algorithm to just return the shortest distance from iS to iT
Bellman_Ford(A,iS,iT):
d <- new Array(A.size(),∞) // Distance array to vertex iS
d[iS] <- 0
for i <- 1 to A.size() - 1:
for k <- 0 to A.size() - 1:
for elem in A[k]:
if d[k] + elem->weight < d[elem->vertex]:
d[elem->vertex] <- d[k] + elem->weight
for k <- 0 to A.size() - 1:
for elem in A[k]:
if d[k] + elem->weight < d[elem->vertex]:
throw "Negative cycle found"
return d[iT] // if iT not reachable by iS will return ∞
Because of the Adjacency list it seems to me that the complexity is then: O(|V|(|V|+|E|) for the first group of nested loops and O(|E|) for the negative cycle search, for a total complexity of:
O(|V|(|V|+|E|) + |E|) = O(|V|² + |V||E| + |E|) = O(|V|² + |V||E|)