Johnson's algorithm: Difference between revisions

Content deleted Content added
ChuispastonBot (talk | contribs)
m r2.7.1) (robot Adding: pt:Algoritmo de Johnson
Suurballe's algorithm; {{math}}
Line 3:
 
'''Johnson's algorithm''' is a way to find the [[shortest path]]s between [[all-pairs shortest path problem|all pairs of vertices]] in a [[sparse graph|sparse]] [[directed graph]]. It allows some of the edge [[weighted graph|weights]] to be [[negative number]]s, but no negative-weight [[cycle (graph theory)|cycles]] may exist. It works by using the [[Bellman–Ford algorithm]] to compute a transformation of the input graph that removes all negative weights, allowing [[Dijkstra's algorithm]] to be used on the transformed graph. It is named after Donald B. Johnson, who first published the technique in 1977.
 
A similar reweighting technique is also used in [[Suurballe's algorithm]] (1974) for finding two disjoint paths of minimum total length between the same two vertices in a graph with non-negative edge weights.
 
==Algorithm description==
Johnson's algorithm consists of the following steps:
#First, a new [[Vertex (graph theory)|node]] {{math|''q''}} is added to the graph, connected by zero-weight [[Edge (graph theory)|edges]] to each other node.
#Second, the [[Bellman–Ford algorithm]] is used, starting from the new vertex {{math|''q''}}, to find for each vertex {{math|''v''}} the least weight {{math|''h''(''v'')}} of a path from {{math|''q''}} to {{math|''v''}}. If this step detects a negative cycle, the algorithm is terminated.
#Next the edges of the original graph are reweighted using the values computed by the Bellman–Ford algorithm: an edge from {{math|''u''}} to {{math|''v''}}, having length {{math|''w(u,v)''}}, is given the new length {{math|''w(u,v)''  +  ''h(u)''  − ''h(v)''}}.
#Finally, {{math|''q''}} is removed, and [[Dijkstra's algorithm]] is used to find the shortest paths from each node {{math|''s''}} to every other vertex in the reweighted graph.
 
In the reweighted graph, all paths between a pair {{math|''s''}} and {{math|''t''}} of nodes have the same quantity {{math|''h(s)'' &nbspminus; ''h(t)''}} added to them, so a path that is shortest in the original graph remains shortest in the modified graph and vice versa. However, due to the way the values {{math|''h(v)''}} were computed, all modified edge lengths are non-negative, ensuring the optimality of the paths found by Dijkstra's algorithm. The distances in the original graph may be calculated from the distances calculated by Dijkstra's algorithm in the reweighted graph by reversing the reweighting transformation.
 
==Analysis==
The [[time complexity]] of this algorithm, using [[Fibonacci heap]]s in the implementation of Dijkstra's algorithm, is {{math|O(''V''<sup>2</sup>log ''V'' + ''VE'')}}: the algorithm uses {{math|O(''VE'')}} time for the Bellman–Ford stage of the algorithm, and {{math|O(''V'' log ''V'' + ''E'')}} for each of {{math|''V''}} instantiations of Dijkstra's algorithm. Thus, when the graph is sparse, the total time can be faster than the [[Floyd–Warshall algorithm]], which solves the same problem in time {{math|O(''V''<sup>3</sup>)}}.
 
==Example==
Line 21 ⟶ 23:
[[File:Johnson's algorithm.svg|540px|center]]
 
The graph on the left of the illustration has two negative edges, but no negative cycles. At the center is shown the new vertex {{math|''q''}}, a shortest path tree as computed by the Bellman–Ford algorithm with {{math|''q''}} as starting vertex, and the values {{math|''h''(''v'')}} computed at each other node as the length of the shortest path from {{math|''q''}} to that node. Note that these values are all non-positive, because {{math|''q''}} has a length-zero edge to each vertex and the shortest path can be no longer than that edge. On the right is shown the reweighted graph, formed by replacing each edge weight {{math|''w(u,v)''}} by {{math|''w(u,v)''&nbsp; +&nbsp; ''h(u)''&nbsp; &minus; ''h(v)''}}. In this reweighted graph, all edge weights are non-negative, but the shortest path between any two nodes uses the same sequence of edges as the shortest path between the same two nodes in the original graph. The algorithm concludes by applying Dijkstra's algorithm to each of the four starting nodes in the reweighted graph.
 
==References==
 
*{{citation|first=Donald B.|last=Johnson|title=Efficient algorithms for shortest paths in sparse networks|journal=[[Journal of the ACM]]|volume=24|issue=1|pages=1–13|year=1977|doi=10.1145/321992.321993}}.
 
*{{citation | contribution=Johnson's Algorithm | first=Paul E.|last=Black|title=Dictionary of Algorithms and Data Structures|publisher=[[National Institute of Standards and Technology]]| url=http://www.nist.gov/dads/HTML/johnsonsAlgorithm.html | year=2004 }}.
 
*{{Citation | last1=Cormen | first1=Thomas H. | author1-link=Thomas H. Cormen | last2=Leiserson | first2=Charles E. | author2-link=Charles E. Leiserson | last3=Rivest | first3=Ronald L. | author3-link=Ronald L. Rivest | last4=Stein | first4=Clifford | author4-link=Clifford Stein | title=[[Introduction to Algorithms]] | publisher=MIT Press and McGraw-Hill | isbn=978-0-262-03293-3 | year=2001}}. Section 25.3, "Johnson's algorithm for sparse graphs", pp.&nbsp;636–640.
*{{citation|first=Donald B.|last=Johnson|title=Efficient algorithms for shortest paths in sparse networks|journal=[[Journal of the ACM]]|volume=24|issue=1|pages=1–13|year=1977|doi=10.1145/321992.321993}}.
*{{citation|first=J. W.|last=Suurballe|title=Disjoint paths in a network|journal=Networks|volume=14|pages=125–145|year=1974|doi=10.1002/net.3230040204}}.
 
== External links ==