Given a source node and a weighted graph, returns the shortest path from the source to all the other vertices in the given graph, where the graph can contain negative weights. It solves the single source shortest path with negative weights problem.

Restrictions

  • must be a weighted graph
  • must not contain a negative weight cycle
    • however, the Bellman-Ford algorithm can detect if it does contain one!
    • minimum distance does not make sense in graphs with negative weight cycles, as one could traverse the cycle an infinite amount of times, and there would be no lower bound on the weight

Abstract

Efficiency

  • Time complexity:

Implementation

TODO

Proof

Let represent the shortest distance from node to node that the algorithm has found so far. Let represent the actual shortest distance from node to node .

Let be the statement that the results that Bellman-Ford gives after amount of outer loops, resolves shortest paths consisting of or less edges, and thus resolves a graph of nodes

Prove

No edges, is true by default

Assume

After outer loops, for all reachable end nodes, the algorithm has selected the shortest path out of paths of or less edges.
A graph of nodes will have been fully resolved.

Prove

requires resolution of a graph with or less nodes.

The only nodes not resolved are end nodes where the shortest path consists of edges.

let represent any node with a shortest path of edges

As graph is connected, paths to these nodes can be represented as a family of the following path

Where is the node immediately before the target node in that path. As shortest path to nodes consists of edges, they would have already been resolved.

As per the function, all edges of graph are once again looped over. All nodes with shortest path of or less edges would have already been found, and thus will not accept another detour. Thus all edges are the only edges that could change shortest distances.

The shortest path from to through = the shortest path from to + edge.

Thus the th loop, for each node, finds all shortest distances to the target node going through each option, and only updates the distance with the shortest one found.

Longest possible acyclic path = = for a graph of edges, thus by now all possible acyclic shortest paths have been found

Thus algorithm finds the correct shortest path for all nodes in loops, resolving a graph with nodes.

Proof for ability to find negative cycle

By the time outer loop of algorithm has ran times. All shortest paths consisting of or less edges have been found.

Thus by looping again (the th time), we see if there is a shortest path consisting of edges, a path that touches nodes (including start and nodes). There are nodes, thus at least one node has been visited twice (Pigeon hole principle), as such a cycle must exist, as it is the only way to visit a node twice. This implies that going through the same path with the loop, has a lower total weight than going the same path without the loop. Thus it can be derived that the overall weight of the loop is negative, thus negative loop.