<s>
Johnson	B-Algorithm
's	I-Algorithm
algorithm	I-Algorithm
is	O
a	O
way	O
to	O
find	O
the	O
shortest	O
paths	O
between	O
all	O
pairs	O
of	O
vertices	O
in	O
an	O
edge-weighted	O
directed	O
graph	B-Application
.	O
</s>
<s>
It	O
works	O
by	O
using	O
the	O
Bellman	B-Algorithm
–	I-Algorithm
Ford	I-Algorithm
algorithm	I-Algorithm
to	O
compute	O
a	O
transformation	O
of	O
the	O
input	O
graph	B-Application
that	O
removes	O
all	O
negative	O
weights	O
,	O
allowing	O
Dijkstra	B-Algorithm
's	I-Algorithm
algorithm	I-Algorithm
to	O
be	O
used	O
on	O
the	O
transformed	O
graph	B-Application
.	O
</s>
<s>
A	O
similar	O
reweighting	O
technique	O
is	O
also	O
used	O
in	O
Suurballe	B-Protocol
's	I-Protocol
algorithm	I-Protocol
for	O
finding	O
two	O
disjoint	O
paths	O
of	O
minimum	O
total	O
length	O
between	O
the	O
same	O
two	O
vertices	O
in	O
a	O
graph	B-Application
with	O
non-negative	O
edge	O
weights	O
.	O
</s>
<s>
Johnson	B-Algorithm
's	I-Algorithm
algorithm	I-Algorithm
consists	O
of	O
the	O
following	O
steps	O
:	O
</s>
<s>
First	O
,	O
a	O
new	O
node	O
is	O
added	O
to	O
the	O
graph	B-Application
,	O
connected	O
by	O
zero-weight	O
edges	O
to	O
each	O
of	O
the	O
other	O
nodes	O
.	O
</s>
<s>
Second	O
,	O
the	O
Bellman	B-Algorithm
–	I-Algorithm
Ford	I-Algorithm
algorithm	I-Algorithm
is	O
used	O
,	O
starting	O
from	O
the	O
new	O
vertex	O
,	O
to	O
find	O
for	O
each	O
vertex	O
the	O
minimum	O
weight	O
of	O
a	O
path	O
from	O
to	O
.	O
</s>
<s>
Next	O
the	O
edges	O
of	O
the	O
original	O
graph	B-Application
are	O
reweighted	O
using	O
the	O
values	O
computed	O
by	O
the	O
Bellman	B-Algorithm
–	I-Algorithm
Ford	I-Algorithm
algorithm	I-Algorithm
:	O
an	O
edge	O
from	O
to	O
,	O
having	O
length	O
,	O
is	O
given	O
the	O
new	O
length	O
.	O
</s>
<s>
Finally	O
,	O
is	O
removed	O
,	O
and	O
Dijkstra	B-Algorithm
's	I-Algorithm
algorithm	I-Algorithm
is	O
used	O
to	O
find	O
the	O
shortest	O
paths	O
from	O
each	O
node	O
to	O
every	O
other	O
vertex	O
in	O
the	O
reweighted	O
graph	B-Application
.	O
</s>
<s>
The	O
distance	O
in	O
the	O
original	O
graph	B-Application
is	O
then	O
computed	O
for	O
each	O
distance	O
(	O
,	O
)	O
,	O
by	O
adding	O
to	O
the	O
distance	O
returned	O
by	O
Dijkstra	B-Algorithm
's	I-Algorithm
algorithm	I-Algorithm
.	O
</s>
<s>
The	O
first	O
three	O
stages	O
of	O
Johnson	B-Algorithm
's	I-Algorithm
algorithm	I-Algorithm
are	O
depicted	O
in	O
the	O
illustration	O
below	O
.	O
</s>
<s>
The	O
graph	B-Application
on	O
the	O
left	O
of	O
the	O
illustration	O
has	O
two	O
negative	O
edges	O
,	O
but	O
no	O
negative	O
cycles	O
.	O
</s>
<s>
The	O
center	O
graph	B-Application
shows	O
the	O
new	O
vertex	O
,	O
a	O
shortest	O
path	O
tree	O
as	O
computed	O
by	O
the	O
Bellman	B-Algorithm
–	I-Algorithm
Ford	I-Algorithm
algorithm	I-Algorithm
with	O
as	O
starting	O
vertex	O
,	O
and	O
the	O
values	O
computed	O
at	O
each	O
other	O
node	O
as	O
the	O
length	O
of	O
the	O
shortest	O
path	O
from	O
to	O
that	O
node	O
.	O
</s>
<s>
On	O
the	O
right	O
is	O
shown	O
the	O
reweighted	O
graph	B-Application
,	O
formed	O
by	O
replacing	O
each	O
edge	O
weight	O
by	O
.	O
</s>
<s>
In	O
this	O
reweighted	O
graph	B-Application
,	O
all	O
edge	O
weights	O
are	O
non-negative	O
,	O
but	O
the	O
shortest	O
path	O
between	O
any	O
two	O
nodes	O
uses	O
the	O
same	O
sequence	O
of	O
edges	O
as	O
the	O
shortest	O
path	O
between	O
the	O
same	O
two	O
nodes	O
in	O
the	O
original	O
graph	B-Application
.	O
</s>
<s>
The	O
algorithm	O
concludes	O
by	O
applying	O
Dijkstra	B-Algorithm
's	I-Algorithm
algorithm	I-Algorithm
to	O
each	O
of	O
the	O
four	O
starting	O
nodes	O
in	O
the	O
reweighted	O
graph	B-Application
.	O
</s>
<s>
In	O
the	O
reweighted	O
graph	B-Application
,	O
all	O
paths	O
between	O
a	O
pair	O
and	O
of	O
nodes	O
have	O
the	O
same	O
quantity	O
added	O
to	O
them	O
.	O
</s>
<s>
Its	O
weight	O
W	O
in	O
the	O
reweighted	O
graph	B-Application
is	O
given	O
by	O
the	O
following	O
expression	O
:	O
</s>
<s>
The	O
weight	O
of	O
edges	O
that	O
belong	O
to	O
a	O
shortest	O
path	O
from	O
q	O
to	O
any	O
node	O
is	O
zero	O
,	O
and	O
therefore	O
the	O
lengths	O
of	O
the	O
shortest	O
paths	O
from	O
q	O
to	O
every	O
node	O
become	O
zero	O
in	O
the	O
reweighted	O
graph	B-Application
;	O
however	O
,	O
they	O
still	O
remain	O
shortest	O
paths	O
.	O
</s>
<s>
The	O
non-existence	O
of	O
negative	O
edges	O
ensures	O
the	O
optimality	O
of	O
the	O
paths	O
found	O
by	O
Dijkstra	B-Algorithm
's	I-Algorithm
algorithm	I-Algorithm
.	O
</s>
<s>
The	O
distances	O
in	O
the	O
original	O
graph	B-Application
may	O
be	O
calculated	O
from	O
the	O
distances	O
calculated	O
by	O
Dijkstra	B-Algorithm
's	I-Algorithm
algorithm	I-Algorithm
in	O
the	O
reweighted	O
graph	B-Application
by	O
reversing	O
the	O
reweighting	O
transformation	O
.	O
</s>
<s>
The	O
time	O
complexity	O
of	O
this	O
algorithm	O
,	O
using	O
Fibonacci	B-Application
heaps	I-Application
in	O
the	O
implementation	O
of	O
Dijkstra	B-Algorithm
's	I-Algorithm
algorithm	I-Algorithm
,	O
is	O
:	O
the	O
algorithm	O
uses	O
time	O
for	O
the	O
Bellman	B-Algorithm
–	I-Algorithm
Ford	I-Algorithm
stage	O
of	O
the	O
algorithm	O
,	O
and	O
for	O
each	O
of	O
the	O
instantiations	O
of	O
Dijkstra	B-Algorithm
's	I-Algorithm
algorithm	I-Algorithm
.	O
</s>
<s>
Thus	O
,	O
when	O
the	O
graph	B-Application
is	O
sparse	O
,	O
the	O
total	O
time	O
can	O
be	O
faster	O
than	O
the	O
Floyd	B-Algorithm
–	I-Algorithm
Warshall	I-Algorithm
algorithm	I-Algorithm
,	O
which	O
solves	O
the	O
same	O
problem	O
in	O
time	O
.	O
</s>
