Johnson's algorithm - allows you to find the shortest paths between all pairs of vertices of a weighted directed graph . This algorithm works if the graph contains edges with positive or negative weight, but there are no cycles with negative weight. Named after , who published the algorithm in 1977.
Content
Algorithm
Dan Earl with weight function . If the weights of all the edges in the graph non-negative, you can find the shortest paths between all pairs of vertices by running the Dijkstra algorithm once for each vertex. If the graph contains edges with negative weight, but there are no cycles with negative weight, you can calculate a new set of edges with non-negative weights, allowing you to use the previous method. New set consisting of ribs weights must satisfy the following properties.
- For all the edges new weight .
- For all pairs of vertices way is the shortest path from the top to the top using weight function if and only if - also the shortest path from the top to the top with weight function .
Saving Shortest Paths
Lemma (Changing weights preserves the shortest paths). Let a weighted directed graph be given with weight function , let it go Is an arbitrary function that maps vertices to real numbers. For each rib define
Let be - arbitrary path from the top to the top . is the shortest path with a weight function if and only if it is the shortest path with a weight function , i.e. equality tantamount to equality . Also count contains a negative weight cycle using a weight function if and only if it contains a negative weight cycle using the weight function .
Weight Change
- For this graph, create a new graph where , for some new top , but .
- We expand the weight function so that for all vertices equality was fulfilled .
- Next we define for all vertices value and new weights for all ribs .
Basic Procedure
Johnson's algorithm uses the Bellman-Ford algorithm and Dijkstra's algorithm , implemented as subroutines. The edges are stored as lists of adjacent vertices. The algorithm returns the usual matrix the size where , or displays a message that the input graph contains a cycle with a negative weight.
Johnson Algorithm Count is being builtif Bellman_Ford G ′ {\ displaystyle G '} = FALSEthen print “The input graph contains a negative weight loop”elsefor for each ( G ′ , ω , s ) {\ displaystyle (G ', \; \ omega, \; s)} do assign to v ∈ V ′ {\ displaystyle v \ in V '} value h ( v ) {\ displaystyle h (v)} , calculated by the Bellman-Ford algorithmfor for each edge δ ( s , v ) {\ displaystyle \ delta (s, \; v)} do ( u , v ) ∈ E ′ {\ displaystyle (u, \; v) \ in E '} for for each vertex ω ^ ( u , v ) ← ω ( u , v ) + h ( u ) - h ( v ) {\ displaystyle {\ hat {\ omega}} (u, \; v) \ leftarrow \ omega (u, \; v) + h (u) -h (v)} do calculation using Dijkstra's algorithm u ∈ V {\ displaystyle u \ in V} quantities ( G , ω ^ , u ) {\ displaystyle (G, \; {\ hat {\ omega}}, \; u)} for all the peaks δ ^ ( u , v ) {\ displaystyle {\ hat {\ delta}} (u, \; v)} for for each vertex v ∈ V {\ displaystyle v \ in V} do v ∈ V {\ displaystyle v \ in V} return D d u v ← δ ^ ( u , v ) + h ( v ) - h ( u ) {\ displaystyle d_ {uv} \ leftarrow {\ hat {\ delta}} (u, \; v) + h (v) -h (u)}
Difficulty
If the Dijkstra algorithm has a non-decreasing priority queue implemented in the form of aFibonacci heap , then the Johnson algorithm time . With a simpler implementation of a non-decreasing priority queue, the run time becomes equal , but for sparse graphs this quantity in the asymptotic limit behaves better than the operating time of the Floyd - Warshell algorithm .
See also
- Dijkstra's Algorithm
- Bellman - Ford Algorithm
- Floyd-Warshell Algorithm
Links
Literature
- Thomas H. Cormen et al. Algorithms: construction and analysis. - 2nd ed. - M .: Williams Publishing House , 2007. - S. 726. - ISBN 5-8459-0857-4 .
- Thomas H. Cormen et al. Algorithms: construction and analysis. - 1st ed. - M .: MCLMO , 2004 .-- S. 523. - ISBN 5-900916-37-5 .