Processing math: 100%
This is ericpony's blog

Monday, November 2, 2015

A note on the Bellman–Ford's algorithm

Bellman-Ford Shortest Path Algorithm is an elegant solution to the single-source shortest path problem, stated as follows: given a weighted digraph G=(V,E), let w(e)\in \mathbb R denote the weight of edge e. Fix s \in V to be the source vertex, and let dist(v) be the distance from s to v. That is,
dist(v) = \min\{\ \sum_{e\in P} w(e) : P \subseteq E is a path from s to v\ \}.
The goal of the problem then is to compute dist(v) for each v \in V.
Bellman-Ford algorithm maintains a map d: V \rightarrow \mathbb R to store the tentative distance values. We use d_k(v) to denote the value of d(s, u) computed in the kth iteration. Let d_0(v)=\infty for v \neq s and d_0(s)=0. In the nth iteration, d_n(v) is computed based on d_{n-1}(v) as follows:
d_n(v)=\min\{\ d_{n-1}(v),\ \min\{\ (u, v)\in E: d_{n-1}(u) + w(u,v)\ \}\ \}.
Here are some elementary facts about Bellman-Ford algorithm:
  • The original version of the algorithm runs for a fixed number of |V| iterations. In each iteration, the distance values are updated by looping through the edges. More precisely, for each (u, v) \in E, we relax it by setting d_n(v)=\min\{d_{n-1}(v), d_{n-1}(u) + w(u, v)\}. The time complexity of the algorithm is thus O(|V||E|).
  • Loop invariant: The following constraint holds for n\ge 0 and each v\in V:
    dist(v) \le d_{n+1}(v) \le d_n(v).
  • It turns out that d_n(v) is the length of the shortest path from s to v with at most n edges. Since a cycle-free path contains at most |V|-1 edges, d_{|V|-1}(v) = dist(v) if a negative cycle cannot be reached from the source vertex s.
  • In fact, G doesn't contain a reachable negative cycle if and only if d_n converges before n=|V|. This fact leads us to a O(|V||E|) algorithm for negative cycle detection.

Optimizations

1. One may immediately observe that if d_n(v)=d_{n-1}(v), then we don't have to check edge (v, u) in the nth iteration, leading to a constant-factor savings in time. Also, it is possible that d_n stabilize at some n less than |V|-1, allowing us to terminate the main loop earlier. However, there are graphs where |V|-1 iterations are needed in the worst case.
2. In the above definition of algorithm, we update d_n according to the values of d_{n-1}. Hence, the values of d_n don't depend on the order in which edges are relaxed. In practice, d_n and d_{n-1} can refer to the same map, in which case the order of relaxation impacts the efficiency of the algorithm. For example, when there is no negative cycle reachable from the source, the number of iterations can be reduced to \lceil |V|/2 \rceil if all vertices are assigned with some arbitrary linear order and the edges are relaxed by topological sort (Yen 1970). Furthermore, if the arbitrary linear order of the vertices are replaced by a random permutation, then the expected number of iterations needed in the main loop is at most |V|/3 (Bannister & Eppstein 2012).
Unfortunately, optimizations based on the order of relaxation cannot be applied as easily in the distributed settings.

Distributed adaptation

Bellman-Ford algorithm is particularly suitable (compared to Dijkstra’s algorithm) to be carried out on fully distributed platform. First, Bellman-Ford algorithm allows the distance value of each vertex to be updated asynchronously. Thus we don't need to use shared memory. Also, the correctness of the algorithm doesn't rely on the orders in which the edges are relaxed. Thus we don't need a master node to coordinate other nodes. The following pseudo-code describes a simple adaptation of Bellman-Ford algorithm to a message-passing system. Note that each d(v) is stored in the local memory at vertex v and cannot be accessed by other vertices.
Initialization:
    Set d(v)=\infty at each vertex v\neq s and set d(s)=0 at the source vertex s.
    Set cnt(v)=0 at each vertex v.
    Send \mathtt{update}(w(s, v)) to vertex v for each edge (s, v).

When vertex v receives message \mathtt{update}(t):
    If t<d(v) then
        Set cnt(v) = cnt(v) + 1
        If cnt(v) \ge |V| then
            Report that v is on a negative cycle.
        else
            Set d(v)=t and send \mathtt{update}(t+w(v, u)) to u for each edge (v, u).

Note that this algorithm is fully distributed---it doesn't need any synchronization mechanism, and initialization can be done by broadcasting a special reset message. On the other hand, since the number of messages a vertex would receive is indefinite, some of the vertices may never know whether it has already computed the shortest distance.

Applications

Optimal tokenization. Given a sequence of characters and a map from words to scores, insert delimiters to the sequence so that the resulted set of words has the maximum score. Hint: Model the positions of the sequence as vertices. A sub-string forms a weighted edge if it is a word with a nonzero score. Consider a shortest path from the first vertex to the last one.
(More to come)

References

1. Bellman–Ford algorithm on Wikipedia.
2. Sedgewick's web exercises for Algorithms, 4th ed.
Related Posts Plugin for WordPress, Blogger...