Daa Mod - 4
Daa Mod - 4
Warshall Algorithm:
Recall that the adjacency matrix A = {aij} of a directed graph is the Boolean matrix that has 1
in its ith row and jth column if and only if there is a directed edge from the ith vertex to the jth
vertex.
We may also be interested in a matrix containing the information about the existence of
directed paths of arbitrary lengths between vertices of a given graph.
Such a matrix, called the transitive closure of the digraph, would allow us to determine in
constant time whether the jth vertex is reachable from the ith vertex. Here are a few
application examples.
When a value in a spreadsheet cell is changed, the spreadsheet software must know all the
other cells affected by the change.
If the spreadsheet is modelled by a digraph whose vertices represent the spreadsheet cells and
edges indicate cell dependencies, the transitive closure will provide such information.
In software engineering, transitive closure can be used for investigating data flow and control
flow dependencies as well as for inheritance testing of object-oriented software.
In electronic engineering, it is used for redundancy identification and test generation for
digital circuits.
Definition:
The transitive closure of a directed graph with n vertices can be defined as the n × n Boolean matrix T
= {tij}, in which the element in the ith row and the jth column is 1 if there exists a nontrivial path (i.e.,
directed path of a positive length) from the ith vertex to the jth vertex; otherwise, tij is 0.
Algorithm Warshall((A[1...n, 1...n])
// Implements Warshall’s algorithm for computing the transitive closure
// Input: The adjacency matrix A of a digraph with n vertices
// Output: The transitive closure of the digraph
R(0) ← A
for k ← 1 to n do
for i ← 1 to n do
for j ← 1 to n do
R(k)[i, j ] ← R(k−1) [i, j ] or (R(k−1) [i, k] and R(k−1) [k, j ])
return R(n)
Example:
Floyd’s Algorithm:
Given a weighted connected graph (undirected or directed), the all-pairs shortest paths problem asks
to find the distances—i.e., the lengths of the shortest paths— from each vertex to all other vertices.
This is one of several variations of the problem involving shortest paths in graphs. Because of its
important applications to communications, transportation networks, and operations research, it has
been thoroughly studied over the years. Among recent applications of the all-pairs shortest-path
problem is precomputing distances for motion planning in computer games.
It is convenient to record the lengths of shortest paths in an n × n matrix D called the distance
matrix: the element dij in the ith row and the jth column of this matrix indicates the length of the
shortest path from the ith vertex to the jth vertex.
Example:
When i = 0:
v = 0 ; w = 0;
(M-w) = 5 – 0 = 5
(vi+1 / wi+1) = 6 // (vi+1 / wi+1) => we always consider the next item’s value
Ub = 0 + 5 * 6 = 30
When i = 1:
with w1 and v1
w = 0 + 2 = 2 //the eqn is feasible, w < M, the equa on is known to be feasible
v = 0 + 12 = 12
(M-w) = 5 – 2 = 3 (v1+1 / w1+1) = 10 // (vi+1 / wi+1) => we always consider the next item’s value
Ub = 12 + (3 * 10) = 42
Without w1 and v1
w = w0 = 0
v = v0 = 0
(M-w) = 5 – 0 = 5
(v1+1 / w1+1) = 10 // (vi+1 / wi+1) => remains the same in with or without v1,w1 values
Ub = 0 + (5 * 10) = 50
When i = 2:
With w2 and v2
w = 2 + 1 = 3 //the eqn is feasible(w < M),we need to add the previous feasible eqn.
v = 10 + 12 = 22
(M-w) = 5 – 3 = 2
(v2+1 / w2+1) =(v3 / w3) = 6 // (vi+1 / wi+1) => we always consider the next item’s value
Ub = 22 + (2 * 6) = 44
Without w2 and v2
w = 0 + 2= 2//the eqn is feasible, w < M, the equa on is known to be feasible
v = 0 +12 =12
(M-w) = 5 – 2 = 3
(v2+1 / w2+1) = (v3 / w3) = 6 // (vi+1 / wi+1) => remains the same in with or without vi+1 / wi+1
values
Ub = 12 + (3 * 6) = 30
When i = 3:
With w3 and v3
w = 3 + 3 = 6 //the eqn is not feasible. As (w > M),we need not to con nue to solve eqn.
Without w3 and v3
w = 0 + 3= 3//the eqn is feasible, w < M, the equa on is known to be feasible
v = 0 +22 =22
(M-w) = 5 – 3 = 2
(v3+1 / w3+1) = (v4 / w4) = 7
Ub = 22 + (2 * 7) = 36
When i =4:
with w4 and v4
w = w4 + w2 => 2 + 3 = 5 // feasible
v = v4 + v2 => 18 + 22 = 40
(v4+1 / w4+1) = 0 // As it is not available
M–w=5–5=0
Ub = 40 + 0 * 0 = 40
Without w4 and v4
w = w2 => 3 // feasible
v = v2 => 22
(v4+1 / w4+1) = 0 // As it is not available
M–w=5–3=2
Ub = 22 + 2 * 0 = 22
Example-
The following graph shows a set of cities and distance between every pair of cities-
A set of some cities
Distance between every pair of cities
Example-
The following graph shows a set of cities and distance between every pair of cities-
Row Reduction-
Column Reduction-
Step-02:
We consider all other vertices one by one.
We select the best vertex where we can land upon to minimize the tour cost.
Choosing To Go To Vertex-B: Node-2 (Path A → B)
From the reduced matrix of step-01, M[A,B] = 0
Set row-A and column-B to ∞
Set M[B,A] = ∞
Row Reduction-
We cannot reduce row-1 as all its elements are ∞.
Reduce all the elements of row-2 by 13.
There is no need to reduce row-3.
There is no need to reduce row-4.
Performing this, we obtain the following row-reduced matrix-
Column Reduction-
Cost(2)
= Cost(1) + Sum of reduction elements + M[A,B]
= 18 + (13 + 5) + 0
= 36
Now,
We reduce this matrix.
Then, we find out the cost of node-03.
Row Reduction-
Column Reduction-
Cost(3)
= Cost(1) + Sum of reduction elements + M[A,C]
= 18 + 0 + 7
= 25
Now,
We reduce this matrix.
Then, we find out the cost of node-04.
Row Reduction-
Column Reduction-
Cost(4)
= Cost(1) + Sum of reduction elements + M[A,D]
= 18 + 5 + 3
= 26
Thus, we have-
Cost(2) = 36 (for Path A → B)
Cost(3) = 25 (for Path A → C)
Cost(4) = 26 (for Path A → D)
We choose the node with the lowest cost.
Since cost for node-3 is lowest, so we prefer to visit node-3.
Thus, we choose node-3 i.e. path A → C.
Step-03:
We explore the vertices B and D from node-3.
We now start from the cost matrix at node-3 which is-
Cost(3) = 25
Now,
We reduce this matrix.
Then, we find out the cost of node-5.
Row Reduction-
Column Reduction-
Cost(5)
= cost(3) + Sum of reduction elements + M[C,B]
= 25 + (13 + 8) + ∞
=∞
Row Reduction-
Column Reduction-
Step-04:
Cost(6) = 25
Row Reduction-
Column Reduction-