Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 6

Dijkstra’s algorithm is an efficient algorithm for graphs with non-negative weights.

However, Dijkstra’s
algorithm can yield results that are incorrect when working with graphs with negative edge weights.

(1) At initialization, Q = {1, 2, 3, 4} and d(1, 1) = 0 while d(1, v) = ∞ for v ∈ {2, 3, 4}. More importantly,
p(1) = p(2) = p(3) = p(4) = undefined. (Recall p(·) is the parent function used the build the Dijkstra Tree.

(2) At the first step, 1 is removed from Q and we examine its neighbors. During this step, we define: d(1,
1) = 0, d(1, 2) = −2, d(1, 3) = 3, d(1, 4) = ∞ and p(1) = undefined, p(2) = p(3) = 1 and p(4) = undefined.
Now Q = {2, 3, 4}.

(3) At the second stage, 2 is the vertex closest to 1 and so it is removed and we compute on its
neighbors. We see that d(1, 1) = −4, d(1, 2) = −2, d(1, 3) = −3, d(1, 4) = 0 and p(1) = 2, p(2) = 1, p(3) = 2
and p(4) = 2. Clearly we have a problem already since p(1) = 2 and p(2) = 1 means to get to vertex 1 we
go through vertex 2 and vice versa. At this stage Q = {3, 4}.

(4) We continue by removing Vertex 3 from Q and computing on its neighbors. We now have d(1, 1) =
−4, d(1, 2) = −4, d(1, 3) = −3, d(1, 4) = −5 and p(1) = 2, p(2) = 3, p(3) = 2 and p(4) = 3.

(5) Completing the algorithm and computing on the neighbors of 4 yields: d(1, 1) = −4, d(1, 2) = −4, d(1,
3) = −7, d(1, 4) = −5 and p(1) = 2, p(2) = 3, p(3) = 4, p(4) = 3. The resulting parent function cannot define a
proper tree structure and the algorithm fails

A negative cycle in a (directed) graph implies there is no shortest path between any two vertices as
repeatedly going around the cycle will make the path smaller and smaller.
Initially, the distance function is defined only for edges in the graph and to zero for the distance of the
vertex to itself. Thus we know: (1) d(vk, vk) = 0 for k = 1, 2, 3, 4. (2) d(v1, v2) = −2, d(v1, v3) = 3, d(v1, v4)
= ∞ (3) d(v2, v1) = ∞, d(v2, v3) = −1, d(v2, v4) = 2 (4) d(v3, v1) = ∞, d(v3, v2) = ∞, d(v3, v4) = −2 (5) d(v4,
v1) = ∞, d(v4, v2) = ∞, d(v4, v3) = ∞ There are four vertices in this example, so the outer-loop will be
executed four times. There will be a total of 64 comparisons at Line (4) and we cannot summarize them
all. Instead, we will discuss when the distance function changes. Outer-Loop with v1: : During the outer-
loop with v1, we are interested in paths that use v1. Since v1 has no in-edges, there are no paths that
can be made shorter by passing through v1. Thus, no change to the distance function is made. Outer-
Loop with v2:: During this loop, it is clear that two things happen: (1) When u1 = v1 and u2 = v3, the
distance d(v1, v3) is updated to −3 since there is a path of length −3 from v1 through v2 to v3. (2) When
u1 = v1 and u2 = v4, the distance d(v1, v4) is updated to 0 since there is a path of length 0 from v1
through v2 to v4. (Before this the distance from v1 to v4 was infinite.)
Outer-Loop with v3: : During this loop, it is clear that two things happen: (1) When u1 = v1 and u2 = v4,
the distance from v1 to v4 is updated to −5, since there is a path of length −5 going through v3
connecting v1 to v4. (2) When u1 = v2 and u2 = v4, the distance from v2 to v4 is updated to −3. Outer-
Loop with v4: : During this loop, no further distance improvements can be made. The complete distance
function has the form: (1) d(vk, vk) = 0 for k = 1, 2, 3, 4.

(2) d(v1, v2) = −2, d(v1, v3) = −3, d(v1, v4) = −5 (3) d(v2, v1) = ∞, d(v2, v3) = −1, d(v2, v4) = −3 (4) d(v3,
v1) = ∞, d(v3, v2) = ∞, d(v3, v4) = −2 (5) d(v4, v1) = ∞, d(v4, v2) = ∞, d(v4, v3) = ∞

Time Complexity-
 

 Floyd Warshall Algorithm consists of three loops over all the nodes.
 The inner most loop consists of only constant complexity operations.
 Hence, the asymptotic complexity of Floyd Warshall algorithm is O(n3).
 Here, n is the number of nodes in the given graph.
 When Floyd Warshall Algorithm Is Used?
 

 Floyd Warshall Algorithm is best suited for dense graphs.


 This is because its complexity depends only on the number of vertices in the given graph.
 For sparse graphs, Johnson’s Algorithm is more suitable.
Consider the following directed weighted graph-

Using Floyd Warshall Algorithm, find the shortest path distance between every pair of vertices.

Solution-
 

Step-01:
 

 Remove all the self loops and parallel edges (keeping the lowest weight edge) from the graph.
 In the given graph, there are neither self edges nor parallel edges.
 

Step-02:
 

 Write the initial distance matrix.


 It represents the distance between every pair of vertices in the form of given weights.
 For diagonal elements (representing self-loops), distance value = 0.
 For vertices having a direct edge between them, distance value = weight of that edge.
 For vertices having no direct edge between them, distance value = ∞.
 

Initial distance matrix for the given graph is-

 
 

Step-03:
 

Using Floyd Warshall Algorithm, write the following 4 matrices-


 

You might also like