Page 1 of 1

Johnson’s algorithm is a way to find the shortest paths between all pairs of vertices in a sparse, edge-weighted, direct

Posted: Fri Apr 29, 2022 6:50 am
by answerhappygod
Johnson’s algorithm is a way to find the shortest paths between
all pairs of vertices in a sparse,
edge-weighted, directed graph. It allows some of the edge weights
to be negative numbers, but no
negative-weight 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.
In this project, you have to v that finds shortest paths between
every pair of vertices in a given
weighted directed Graph where weights may be negative as well using
by implementing Johnson’s
Algorithm. You should not use Brute Force as it may increase the
complexity and runtime of the
program.
Input Format:
In the first line you will be given an integer N denoting the
number of nodes and another integer E
denoting the number of edges. In the following E lines you will be
given information about the
directed edges. Each such line will contain three integers u, v and
w which indicates that there is a
directed from u to v and the cost of traversing that edge is w. The
graph may contain negative weights
for edges.
Output Format:
If the graph contains negative cycle(s) the program will print a
single line “Negative cycle
exists”. Otherwise the graph will print all the shortest path
distances in the form of u, v, w
denoting the minimum cost to reach v from u is w. The printing will
maintain lexicographic
order of the nodes, e.g, first distances from node 1, then node 2,
then node 3 and so on.
Use C++ to solve this problem and discuss the time
complexity.