bellman-ford algorithm

is used to find distances and [[blk:1683775638][negative cycle]]s in directed graphs.
the difference between dijkstra's algorithm and bellman-ford is that bellman works on all real-valued weights and can handle negative weights while dijkstra cant.
negative cycles prevent some challenges when dealing with weighted graphs, as they can form cycles with a weight of .
consider the following graph whose weights function is taken from the real number line
at first glance the distance from to seems to equal 10, but if we were to go back and forth from the node to we'd find that on each step the distance decreases by -2, which would mean that the distance equals , this is why when dealing with negative weights we only consider directed graphs
the bellman ford algorithm works by choosing a vertex and assigning infinity values to all the other vertices, then visit each edge and relax the path distances if they are inaccurate, this needs to be done times because in the worst case, a vertex's path length might need to be readjusted times
let be a graph, the vertex is reachable from if in there is a path from to .
let be a directed graph with no negative cycles, let be a vertex in . for each , if in there is a path from to , then in there is a minimal simple path from to .
if is a simple path, the lemma follows trivially, if not, in there is atleast one cycle whose length isnt negative. let be the path received by cutting . in there is one cycle less than in , and its obvious that . we can continue with this proces until we get a simple path from to , , such that .
in a simple path no vertex repeats itself and so the number of simple paths from to is finite. since the number of simple paths in from to is finite, one of them must be the one with the minimum length.
let be a directed real-number-weighted graph with no negative cycles, and let be some arbitrary vertex. for each vertex , we denote by the number of edges in the minimal path from to with the minimum number of edges.
let be a directed real-weighted graph with no negative cycles and let be some arbitrary vertex. if the bellman-ford algorithm is applied to starting at , then for each , after iterations in the first loop the demands of would have been met.
by "the demands of a vertex" we refer to a correct configuration of values for
a proof by induction on (loop index) that for all if the demands of are met:
base (): the only vertex for which there is an empty path (a path with 0 edges) from to it, is the origin vertex . in we have , and since in there are no negative cycles then . the second demand of the vertex is emptily correct.
induction hypothesis (): we assume that for all , if , the lemma holds true.
we have to show that for all such that the demands of the vertex are met.
let be an arbitrary vertex in such that and let be a minimal simple path from to . let be the subpath that is received by cutting the last edge from . is a subpath of a simple minimal path and so it is minimal and simple too, it also has edges. according to the induction hypothesis, after iterations in the loop the demands of are met.
one of the relaxing steps that are done in the -th iteration in the first loop is a relaxing step over . after this relaxing step the demands of would be met.
let be a directed real-weighted graph with no negative cycles and let , if the bellman-ford algorithm was applied to , starting from , after iterations, for each vertex the demands of would be met.
since for all , , the claim follows trivially from lem-bellf-2
let be a directed real-weighted graph, let be an arbitrary vertex, assume in there is a negative cycle reachable from . if bellman-ford is applied to starting at , then during the checking process we find a non-empty relaxing step
assume in contradiction that in there is a negative cycle that is reachable from , but during the validation process in the second loop we dont find a non-empty relaxing step.
since is reachable from , after at most iterations in the first loop,
given that along there arent any relaxing steps, for each edge :
we write these inequalities one after another and get:
if we were to summarize those inequalities along the negative cycle and recall that we get:
the first two sums are equal because we're dealing with the same vertices and so:
which contradicts with the assumption that is a negative cycle
the bellman-ford algorithm recognizes the existence of a negative cycle and incase there does not exist such a cycle it correctly computes the distances of the vertices from the origin vertex and computes a distance tree.
the correctness of the algorithm in case the input graph contains no negative cycles was proved in lem-bellf-3, in lem-bellf-4 it was proved that if in there exists a negative cycle , then during the validation process, atleast one non-empty relaxing step is found.
the time complexity of the algorithm is .
for dense graphs, the time complexity of the algorithm goes up to .