Clever Geek Handbook
📜 ⬆️ ⬇️

Johnson Algorithm

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 EarlG=(V,E) {\ displaystyle G = (V, E)}   with weight functionω:E→R {\ displaystyle \ omega \ colon E \ to R}   . If the weights of all the edgesω {\ displaystyle \ omega}   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ω^ {\ displaystyle {\ hat {\ omega}}}   must satisfy the following properties.

  • For all the edges(u,v) {\ displaystyle (u, \; v)}   new weightω^(u,v)>0 {\ displaystyle {\ hat {\ omega}} (u, \; v)> 0}   .
  • For all pairs of verticesu,v∈V {\ displaystyle u, \; v \ in V}   wayp {\ displaystyle p}   is the shortest path from the topu {\ displaystyle u}   to the topv {\ displaystyle v}   using weight functionω {\ displaystyle \ omega}   if and only ifp {\ displaystyle p}   - also the shortest path from the topu {\ displaystyle u}   to the topv {\ displaystyle v}   with weight functionω^ {\ displaystyle {\ hat {\ omega}}}   .

Saving Shortest Paths

Lemma (Changing weights preserves the shortest paths). Let a weighted directed graph be givenG=(V,E) {\ displaystyle G = (V, \; E)}   with weight functionω:E→R {\ displaystyle \ omega \ colon E \ to R}   , let it goh:V→R {\ displaystyle h \ colon V \ to R}   Is an arbitrary function that maps vertices to real numbers. For each rib(u,v)∈E {\ displaystyle (u, \; v) \ in E}   define

ω^(u,v)=ω(u,v)+h(u)-h(v).{\ displaystyle {\ hat {\ omega}} (u, \; v) = \ omega (u, \; v) + h (u) -h (v).}  

Let bep=⟨v0,vone,...,vk⟩ {\ displaystyle p = \ langle v_ {0}, \; v_ {1}, \; \ ldots, \; v_ {k} \ rangle}   - arbitrary path from the topv0 {\ displaystyle v_ {0}}   to the topvk {\ displaystyle v_ {k}}   .p {\ displaystyle p}   is the shortest path with a weight functionω {\ displaystyle \ omega}   if and only if it is the shortest path with a weight functionω^ {\ displaystyle {\ hat {\ omega}}}   , i.e. equalityω(p)=δ(v0,vk) {\ displaystyle \ omega (p) = \ delta (v_ {0}, \; v_ {k})}   tantamount to equalityω^(p)=δ^(v0,vk) {\ displaystyle {\ hat {\ omega}} (p) = {\ hat {\ delta}} (v_ {0}, \; v_ {k})}   . Also countG {\ displaystyle G}   contains a negative weight cycle using a weight functionω {\ displaystyle \ omega}   if and only if it contains a negative weight cycle using the weight functionω^ {\ displaystyle {\ hat {\ omega}}}   .

Weight Change

  1. For this graph, create a new graphG′=(V′,E′) {\ displaystyle G '= (V', \; E ')}   whereV′=V∪{s} {\ displaystyle V '= V \ cup \ {s \}}   , for some new tops∉V {\ displaystyle s \ not \ in V}   , butE′=E∪{(s,v):v∈V} {\ displaystyle E '= E \ cup \ {(s, \; v): v \ in V \}}   .
  2. We expand the weight functionω {\ displaystyle \ omega}   so that for all verticesv∈V {\ displaystyle v \ in V}   equality was fulfilledω(s,v)=0 {\ displaystyle \ omega (s, \; v) = 0}   .
  3. Next we define for all verticesv∈V′ {\ displaystyle v \ in V '}   valueh(v)=δ(s,v) {\ displaystyle h (v) = \ delta (s, \; v)}   and new weights for all ribsω^(u,v)=ω(u,v)+h(u)-h(v)⩾0 {\ displaystyle {\ hat {\ omega}} (u, \; v) = \ omega (u, \; v) + h (u) -h (v) \ geqslant 0}   .

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 matrixD=dij {\ displaystyle D = d_ {ij}}   the size|V|×|V| {\ displaystyle | V | \ times | V |}   wheredij=δ(i,j) {\ displaystyle d_ {ij} = \ delta (i, \; j)}   , or displays a message that the input graph contains a cycle with a negative weight.

  Johnson Algorithm
 Count is being built G′{\ displaystyle G '} if Bellman_Ford(G′,ω,s){\ displaystyle (G ', \; \ omega, \; s)}  = FALSEthen print “The input graph contains a negative weight loop”elsefor for eachv∈V′{\ displaystyle v \ in V '} do assign toh(v){\ displaystyle h (v)}  valueδ(s,v){\ displaystyle \ delta (s, \; v)}  ,
calculated by the Bellman-Ford algorithmfor for each edge(u,v)∈E′{\ displaystyle (u, \; v) \ in E '} doω^(u,v)←ω(u,v)+h(u)-h(v){\ displaystyle {\ hat {\ omega}} (u, \; v) \ leftarrow \ omega (u, \; v) + h (u) -h (v)} for for each vertexu∈V{\ displaystyle u \ in V} do calculation using Dijkstra's algorithm(G,ω^,u){\ displaystyle (G, \; {\ hat {\ omega}}, \; u)}  quantitiesδ^(u,v){\ displaystyle {\ hat {\ delta}} (u, \; v)} 
for all the peaksv∈V{\ displaystyle v \ in V} for for each vertexv∈V{\ displaystyle v \ in V} doduv←δ^(u,v)+h(v)-h(u){\ displaystyle d_ {uv} \ leftarrow {\ hat {\ delta}} (u, \; v) + h (v) -h (u)} return D

Difficulty

If the Dijkstra algorithm has a non-decreasing priority queue implemented in the form of aFibonacci heap , then the Johnson algorithm timeO(V2log⁡V+VE) {\ displaystyle O (V ^ {2} \ log V + VE)}   . With a simpler implementation of a non-decreasing priority queue, the run time becomes equalO(VElog⁡V) {\ displaystyle O (VE \ log V)}   , 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

  • Algorithm Visualizer

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 .
Source - https://ru.wikipedia.org/w/index.php?title= Johnson's algorithm&oldid = 79362221


More articles:

  • Sendik, Raul
  • Above Eternal Peace
  • Mukachevo and Uzhgorod diocese
  • Novobelitsky District
  • Leweg, Pierre
  • Record Label
  • Medal of Stolypin P. A.
  • Le Mans 24 Hour Winners List
  • Dubrovinsky, Joseph Fedorovich
  • Philaret the Merciful

All articles

Clever Geek | 2019