Floyds algorithm displayed as algorithm 2 can be used to


Question: Show that Dijkstra's algorithm may not work if edges can have negative weights.

Floyd's algorithm, displayed as Algorithm 2, can be used to find the length of a shortest path between all pairs of vertices in a weighted connected simple graph. However, this algorithm cannot be used to construct shortest paths. (We assign an infinite weight to any pair of vertices not connected by an edge in the graph).

ALGORITHM 2 Floyd's Algorithm.

procedure Floyd(G: weighted simple graph)

{G has vertices v1, v2........... vn and weights w(vi, vj)

with w(vi, vj) = ∞ if {vi, vj} is not an edge}

for i := 1 to n

for j := 1 to n

d(vi, vj):= w(vi, vj)

for i := 1 to n

for j := 1 to n

for k := 1 to n

if d(vj, vi) + d(vi, vk) < d(vj, vk)

then d(vj, vk) := d(vj, vi) + d(vi, vk)

return [d(vi, vj)] {d(vi, vj) is the length of a shortest path between vi and vj for 1 ≤ i ≤ n, 1 ≤ j ≤ n}

Solution Preview :

Prepared by a verified Expert
Data Structure & Algorithms: Floyds algorithm displayed as algorithm 2 can be used to
Reference No:- TGS02370759

Now Priced at $20 (50% Discount)

Recommended (97%)

Rated (4.9/5)