0% found this document useful (0 votes)
404 views

Johnsons Algorithm

The document discusses algorithms for finding shortest paths in graphs, including the single-source shortest path problem and the all-pairs shortest path problem. It describes Dijkstra's algorithm and Bellman-Ford algorithm for single-source shortest paths and introduces Johnson's algorithm for finding all-pairs shortest paths in graphs with negative edge weights. Johnson's algorithm works by transforming the graph using a potential function to remove negative edges, allowing the use of Dijkstra's algorithm on the transformed graph to solve the all-pairs shortest path problem.

Uploaded by

mahnoor nadeem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
404 views

Johnsons Algorithm

The document discusses algorithms for finding shortest paths in graphs, including the single-source shortest path problem and the all-pairs shortest path problem. It describes Dijkstra's algorithm and Bellman-Ford algorithm for single-source shortest paths and introduces Johnson's algorithm for finding all-pairs shortest paths in graphs with negative edge weights. Johnson's algorithm works by transforming the graph using a potential function to remove negative edges, allowing the use of Dijkstra's algorithm on the transformed graph to solve the all-pairs shortest path problem.

Uploaded by

mahnoor nadeem
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

The shortest path problems are

A New Algorithm about finding the path between


For Shortest Path two vertices such that the sum of
the weights of its constituent
Algorithms edges is minimized. The shortest
path problems can be solved by
various approaches. We use
single source shortest path
Abstract techniques if we are given a graph
and we are only to find the
shortest path from node A to node
The presented approach finds the B i.e. we have to traverse from S
shortest paths between all pairs of (source) to V (vertex) for all
vertices in a directed and edge- vertices, in other words; from one
weighted graph. Some of the edge point to everywhere.
weights can be negative numbers
but no negative weight cycles may
exist. The idea for the solution for
all pairs shortest path is to run the
single source algorithm V times,
once from each source. The
proposed approach was first
published in 1977 and was named
after Donald B. Johnson.

It works by using Bellman-Ford


algorithm and Dijkstra’s algorithm.
Bellman-Ford algorithm is used to
compute a transformation of the Dijkstra’s algorithm and Bellman-
input graph that removes negative Ford algorithm are the most
edges, allowing Dijkstra’s common examples of single
algorithm to be used on the source shortest path algorithms.
transformed graph. Dijkstra’s algorithm was proposed
by a computer scientist Edsger W.
Dijkstra in 1956. It is also known
Introduction: as SPF (shortest path first). This
algorithm is for finding the shortest
path between two nodes in a
directed graph. Although, the
drawbacks of Dijjkstra’s algorithm
includes that it does a blind search
so, wastes a lot of time while
processing, also that it cannot deal
with negative edge weights.
On the other hand, where
Bellman-Ford algorithm can work The Running times of such graphs
for the graphs when some of the are given below:
edge weights can be negative
numbers. Bellman-Ford algorithm
was first proposed by Alfonso For Algorith Time
Shimbel in 1955, but it was ms
published in 1958 and 1956 by Unweighted( BFS O(V+E)
Richard Bellman and Lester Ford w=1)
Jr. respectively. Sometimes, it is Non neg. Dijkstra O(VlgV
also called the Bellman-Ford- weights +E)
Moore algorithm for the same general Bellma O(V+E)
algorithm was also published by n-Ford
Edward F. Moore in 1957.
Bellman-Ford is slower than
Dijkstra’s algorithm for the same
problem, but it is more versatile as Another approach is all pairs
it can work for the graphs in which shortest paths. While the single
some of the edge weights are source shortest problems are
negative. about having to find the shortest
path between two nodes in a
BFS (breadth first search) graph or a network, the all pairs
algorithm is best suited algorithm shortest path problems are about
for unweighted (w=1) graphs. This finding the shortest path for all the
algorithm is generally known for pairs of vertices in a graph.
traversing trees or graphs.
This type of approach is used in
fields where the area of our
interest is to find the shortest path
in a graph where we already know
the shortest paths for all pairs of
vertices as we pre-compute them
for this purpose e.g. Google Maps,
Internet Routing.
In Google Maps, we want to be
Floyd-Warshall was published by able to very quickly support
Robert Floyd and Stephen shortest path queries between
Warshall individually in 1962. It is major cities, then we may want to
an algorithm for finding the first compute all-pair shortest
shortest path in a directed graph paths for all major cities, because
with positive or negative edge road networks do not change very
weights, but with no negative much, the large scale.
cycles. A single execution of the
algorithm will find the summed
Pre-compute this, and then given
weights (lengths) of shortest paths
a query of two vertices, probably
between all pairs of the vertices.
get a million queries, we could
very quickly know what the answer
Floyd-Warshall algorithm is very is. And this is the basis for real
similar to the Johnson’s algorithm. world shortest paths.
However, they vary in their
running times for the same
So, typically we do not compute
problem.
shortest path from A to B every
single time. You use waypoints
All pairs shortest path is along the way ad you have pre-
something we might want to do if compute all-pairs shortest paths
we are pre-processing. between waypoints.

Another situation where we need


Application: all-pairs shortest paths is Internet
Routing. In Internet Routing, we
may need to know the shortest
path to get to the fewest hop path
The solution for all pairs shortest
to get to an internet site. We know
path is to run the single source
the IP address. We need to know
algorithm V times. Once from each
where to go. We do not need to
source.
know the whole path. We need to
know the next step.
For Algorithms Time
Unweighted(W=1) |V|*BFS O(VE)
Problem: Non Neg. Weights |V|*Dijkstra O(V2lgV
+VE)
General |V|*Bellman- O(V2E)
Ford
The problem that occurs in finding
the shortest path for a graph is that
a directed graph G having vertices
V, edges E and edge weight
function w may happen to have
The problem was that, if you have
negative numbers for some of the
negative edge weights you cannot
edge weights.
use Dijkstra’s Algorithm but in
Johnsons’s Algorithm, even when
The most common algorithm for we have negative edge weights,
finding the shortest path is we can achieve the same bound as
Dijkstra’s algorithm but it has this running Dijkstra’s algorithm for the
drawback that it cannot deal with same problem. This actually is
the negative edge weights. Another possible because of the VE term in
algorithm is Bellman-Ford the running time of Dijkstra’s * |V| =
algorithm that can deal with the O(V² logV + VE), which lets us run
negative edge weights as long as Bellman-ford once.
the graph contains no negative
cycle is a better approach than
And letting Bellman-Ford once will
Dijkstra’s algorithm when it comes
let us run Dijkstra’s V time.
to the directed graphs having
negative edge weights, but
Bellman-Ford is a single source
JOHNSON’S
shortest path algorithm.
ALGORITHM:
Pseudo Code:
// The Algo is as follows;
We are going to change the
weights on the edges, and for that
we are going to assign weights to
the vertices.

We will stick to the three given


steps below:

1. Find function h : V -→ R
such that
wh(u,v) = w(u,v) + h(u) – h(v)
>= 0
∀ (u,v) belongs to V
(we do this, so we can use
Dijkstra’s algorithm instead of
Bellman-Ford)

2. Run |V|*Dijkstra on (V,E,wh) -


→Sh (u,v)
(this will give us shortest path
Sh)

3. Claim: δ(u,v) = δh(u,v) –


h(u)+h(v)
(where δ is the original This means shortest paths are
weighting function) reserved. Shortest paths are this
shortest, therefore, if we look at the
delta function, which is about the
shortest path weights, this claim
Proof of Claim: holds.

Consider path p from u to v. Now, how do we find h??


u = v0,v1,v2, ….. , vk =v
If we could find h then we know we
could run Dijsktra’s Algorithm and
wh(p) = wh (vi-1, vi) the other step.
And Dijkstra’s Algorithm is going
to cost the V2logV + VE.
= [w(vi-1+vi) + h(vi-1) –
h(vi)]
w(u, v) + h (u) – h(v) >= 0
= w(p) + h(v0) – h(vk)
h(v) – h(u) <= w(u, v) ∀ (u, v)
wh(p)=w(p) + h(u) – h(v)
belongs to V.
wh(p) – h(u) + h(v) = w(p)
This is called a system of
difference constraints.
So, this was talking about arbitrary
path. Every path gets lengthened
by this function( +h(u)-h(v) ),which
is purely the function of the end Theorm:
points.

If the graph, V, E, w has a


So, in particular, that means the
negative weight cycle, then that
shortest path in w land will still be
system has no solution to
the shortest path in wh land.
difference constraints.
Proof: So, we are saying that zero is
strictly less than zero and that’s not
true.
Consider negative weight cycle C:

So, that means there is no way to


v0 -→v1 -→ v2 -→ …… -→ vk -→ v0 get all of these constraint
simultaneously true.

Proof by Contradiction
The claim is that the sum of these
weights is negative.

h(v1)-h(v0) <= w(v0,v1) If there is no negative weight cycle,


then there is a solution (but we did
h(v2)-h(v1) <= w(v1,v2)
not do this, we did the converse).
: :
: :
Theorm: (converse)
: :
h(vk)-h(vk-1) <= w(vk-1,vk)
If (V, E, w) has no negative weight
h(v0)-h(vk) <= (vk,v0) cycle then there is a solution.
-------------------------------------- (This is kind of more practical
direction.)
0 <= w(C) <=0

Now, the cycle has negative


weight, so this is less than zero.
And if it makes sense then
Bellman-Ford will do it.

We add this source vertex S, so


that there is a clear sources to run
Bellman-Ford from, and then run
Bellman-Ford from there only. That
will give us a weight function for the
Proof: vertices, namely how long does it
take to get from S to those vertices.
Those weights will actually be all
Add S to v negative. But then, we are going to
modify all the edge weights to this
Add (S, v) to E ∀ v belongs to V
formula,
w (S, v) = 0 ∀ v belongs to V
h(v) = δ(S,v) finite ∀ v belongs to V

w(u,v)+h(u)-h(v) >= 0
When we are done all of the
w(u,v)+δ(S,u)-δ(S,v) >= 0 weights will be non-negative
because we had triangle inequality.
δ(S,v) <= δ(S,u)+w(u,v)
And now we can run Dijkstra’s
Algorithm from every vertex.
This is triangle inequality.
We run Bellman-Ford once,
because we know it handles
So, of course we want to compute
negative weights. It also tells us if
shortest paths, because shortest
there are any negative weight
path satisfy triangle inequality.
cycles. That’s why we want this
The whole name of the game in theorm.
shortest path is to find a place
where you do not satisfy triangle
inequality and fix it.
Otherwise, when there is no sma-5503-fall-2005/video-
negative weight cycles, them lectures/lecture-19-shortest-
Bellman-Ford finds valid h. paths-iii-all-pairs-shortest-
paths-matrix-multiplication-
floyd-warshall-johnson/
Then we plug that ‘’h’’ here.
4. https://en.wikipedia.org/wiki/J
ohnson%27s_algorithm
Then we have non negative 5. https://www.javatpoint.com/jo
weights. So in VE time, we have hnsons-algorithm
reduced to the non-negative all pair 6. http://citc.ui.ac.ir/zamani/clrs.
shortest paths. And then, we run pdf
Dijkstra’s Algorithm V times. 7. https://brilliant.org/wiki/johns
ons-algorithm/
8. Introduction to Algorithms 3rd
Then we got almost our answer but Edition by Clifford Stein,
we have to modify them to get back Thomas H. Cormen, Charles
the correct weights on our shortest E. Leiserson, Ronald L.
paths. Rivest
9. https://iq.opengenus.org/johns
on-algorithm/
REFERENCES: 10. https://www.youtube.co
m/watch?v=hLEgT-2t8Ag
11. https://www.youtube.co
1. https://ocw.mit.edu/courses/el m/watch?v=FtN3BYH2Zes
ectrical-engineering-and- 12. https://www.youtube.co
computer-science/6-006- m/watch?v=XB4MIexjvY0
introduction-to-algorithms-fall- 13. Cormen, Thomas H.;
2011/lecture-videos/lecture- Leiserson, Charles E.;
16-dijkstra/ Rivest, Ronald L.; Stein,
2. https://www.youtube.com/wat Clifford (2001), Introduction
ch?v=Sygq1e0xWnM to Algorithms, MIT Press and
McGraw-Hill, ISBN 978-0-
3. https://ocw.mit.edu/courses/el
262-03293-3 . Section 25.3,
ectrical-engineering-and-
"Johnson's algorithm.
computer-science/6-046j-
introduction-to-algorithms-

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy