I have a smarter version of Bellman-Ford here:
//Queue Q; source s; vertices u, v
Q ← s // Q holds vertices whose d(v) values have been updated recently.
While (Q !empty)
{
u ← Dequeue(Q)
for each neighbor v of u
{
Relax(u, v)
if d(v) was updated by Relax and v not in Q
Enqueue(v)
}
}
Can anyone think of a type of graph for which this algorithm is lower bounded by (V*E) time complexity where v = #vertices, E = #edges
I want to see where the pitfalls of this are.
It's been years since I last thought about short path algorithms, correct me if I forgot something.
First, we'll start by ruling out the negative-weighted cycle case, but it can be a source of worst-case performance.
Let's suppose you visit a complete graph. From the first node, you'll queue V-1 nodes, because all of the nodes are neighbours to s. Let's suppose you are unlucky, and that the last node you queue is part of the shortest path to all the nodes. As a consequence, you'll have to queue V-2 nodes (all of them, minus the source node and the one you are evaluating...). Now let's be extremely unlucky, and suppose that the last node you queue is again part of the path to all the remaining nodes... You'll have to queue V-3 nodes, etc.
So if everything goes wrong, you'll have evaluated V-1*V-2*...*3*2*1 nodes. You should easily find out how this sums to |E|.
For each node, what are you doing ? doing a check on each of it's neighbours. Each node has V-1 neighbours.
We're already at O(|V-1||E|) worst-case complexity for your algorithm. If I remember well, the Bellman-Ford algorithm checks each edge to make sure there is no negative-weighted cycle, and so should you ; and that adds 1 to V-1, aka (|V||E|) worst-case complexity.