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

Daa Mod - 4

The document discusses dynamic programming and provides examples of algorithms that use dynamic programming techniques. It introduces the Warshall algorithm for finding the transitive closure of a graph, Floyd's algorithm for solving the all-pairs shortest path problem, Bellman-Ford algorithm for single-source shortest paths, and the 0-1 knapsack problem and its dynamic programming solution. It also discusses the traveling salesman problem and provides an example of using row and column reduction to solve a TSP instance.

Uploaded by

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

Daa Mod - 4

The document discusses dynamic programming and provides examples of algorithms that use dynamic programming techniques. It introduces the Warshall algorithm for finding the transitive closure of a graph, Floyd's algorithm for solving the all-pairs shortest path problem, Bellman-Ford algorithm for single-source shortest paths, and the 0-1 knapsack problem and its dynamic programming solution. It also discusses the traveling salesman problem and provides an example of using row and column reduction to solve a TSP instance.

Uploaded by

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

Module 4: DYNAMIC PROGRAMMING

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:

Bellman Ford Algorithm:


Solution:
The knapsack problem:
Knapsack Algorithm Using Backtracking Principle:
Knapsack Problem:
EXAMPLE 1: Let us consider the instance given by the following data:
Item Weight Value
1 2 12
2 1 10
3 3 18
4 2 14 Capacity(M) = 5.
Solu on:
Item Weight(w) Value(v) v/w
1 2 12 6
2 1 10 10
3 3 18 6
4 2 14 7 M=5

Formula: Ub = v + (M-w) * (vi+1 / wi+1)

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

KNAPSACK FEASIBLE SOLUTION = {1, 2, 4}


THE TOTAL VALUE = 42 + 44 + 40 = 126
Travelling Salesman Problem:
We have seen how to apply dynamic programming to a subset selec on problem (0/1knapsack).Now
we turn our a en on to a permuta on problem. Note that permuta on problems usually are much
harder to solve than subset problems as there are n! different permuta ons of n objects where as
there is only 2n different subsets of n objects(n! > 2n). Let G = (V,E) be a directed graph with edge
costs qj. The variable qj is defined such that Cij > 0 for all i and j and Cy = ∞. if (i,j)
Type equation here.E. Let |V| = n and assume n > 1.A tour of G is a directed simple cycle that
includes every vertex in V. The cost of a tour is the sum of the cost of the edges on the tour. The
traveling sales person problem is to find a tour of minimum cost.

You are given,


 A set of some cities
 Distance between every pair of cities
Travelling Salesman Problem states-
 A salesman has to visit every city exactly once.
 He has to come back to the city from where he starts his journey.
 What is the shortest possible route that the salesman must follow to complete his
tour?

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

Travelling Salesman Problem states-


A salesman has to visit every city exactly once.
 He has to come back to the city from where he starts his journey.
 What is the shortest possible route that the salesman must follow to complete his tour?

Example-
The following graph shows a set of cities and distance between every pair of cities-

If salesman starting city is A, then a TSP tour in the graph is-


A→B→D→C→A

Cost of the tour


= 10 + 25 + 30 + 15
= 80
Actual Problem example:
In this article, we will discuss how to solve travelling salesman problem using branch and
bound approach with example.

Row Reduction-

Consider the rows of above matrix one by one.


If the row already contains an entry ‘0’, then-
 There is no need to reduce that row.
If the row does not contain an entry ‘0’, then-
 Reduce that particular row.
 Select the least value element from that row.
 Subtract that element from each element of that row.
 This will create an entry ‘0’ in that row, thus reducing that row.

Following this, we have-


 Reduce the elements of row-1 by 4.
 Reduce the elements of row-2 by 5.
 Reduce the elements of row-3 by 6.
 Reduce the elements of row-4 by 2.

Performing this, we obtain the following row-reduced matrix-

Column Reduction-

Consider the columns of above row-reduced matrix one by one.


If the column already contains an entry ‘0’, then-
 There is no need to reduce that column.

If the column does not contain an entry ‘0’, then-


 Reduce that particular column.
 Select the least value element from that column.
 Subtract that element from each element of that column.
 This will create an entry ‘0’ in that column, thus reducing that column.
Following this, we have-
 There is no need to reduce column-1.
 There is no need to reduce column-2.
 Reduce the elements of column-3 by 1.
 There is no need to reduce column-4.
Performing this, we obtain the following column-reduced matrix-

Finally, the initial distance matrix is completely reduced.


Now, we calculate the cost of node-1 by adding all the reduction elements.
Cost(1) = Sum of all reduction elements // we do not repeat the same reduction elements
=4+5+6+2+1
= 18

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] = ∞

Now, resulting cost matrix is-


Now,
 We reduce this matrix.
 Then, we find out the cost of node-02.

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-

 Reduce the elements of column-1 by 5.


 We cannot reduce column-2 as all its elements are ∞.
 There is no need to reduce column-3.
 There is no need to reduce column-4.

Performing this, we obtain the following column-reduced matrix-


Finally, the matrix is completely reduced.
Now, we calculate the cost of node-2.

Cost(2)
= Cost(1) + Sum of reduction elements + M[A,B]
= 18 + (13 + 5) + 0
= 36

Choosing To Go To Vertex-C: Node-3 (Path A → C)

 From the reduced matrix of step-01, M[A,C] = 7


 Set row-A and column-C to ∞
 Set M[C,A] = ∞

Now, resulting cost matrix is-

Now,
 We reduce this matrix.
 Then, we find out the cost of node-03.

Row Reduction-

 We cannot reduce row-1 as all its elements are ∞.


 There is no need to reduce row-2.
 There is no need to reduce row-3.
 There is no need to reduce row-4.
Thus, the matrix is already row-reduced.

Column Reduction-

 There is no need to reduce column-1.


 There is no need to reduce column-2.
 We cannot reduce column-3 as all its elements are ∞.
 There is no need to reduce column-4.

Thus, the matrix is already column reduced.


Finally, the matrix is completely reduced.
Now, we calculate the cost of node-3.

Cost(3)
= Cost(1) + Sum of reduction elements + M[A,C]
= 18 + 0 + 7
= 25

Choosing To Go To Vertex-D: Node-4 (Path A → D)


 From the reduced matrix of step-01, M[A,D] = 3
 Set row-A and column-D to ∞
 Set M[D,A] = ∞
Now, resulting cost matrix is-

Now,
 We reduce this matrix.
 Then, we find out the cost of node-04.
Row Reduction-

 We cannot reduce row-1 as all its elements are ∞.


 There is no need to reduce row-2.
 Reduce all the elements of row-3 by 5.
 There is no need to reduce row-4.

Performing this, we obtain the following row-reduced matrix-

Column Reduction-

 There is no need to reduce column-1.


 There is no need to reduce column-2.
 There is no need to reduce column-3.
 We can not reduce column-4 as all its elements are ∞.

Thus, the matrix is already column-reduced.


Finally, the matrix is completely reduced.
Now, we calculate the cost of node-4.

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

Choosing To Go To Vertex-B: Node-5 (Path A → C → B)

 From the reduced matrix of step-02, M[C,B] = ∞


 Set row-C and column-B to ∞
 Set M[B,A] = ∞

Now, resulting cost matrix is-

Now,
 We reduce this matrix.
 Then, we find out the cost of node-5.

Row Reduction-

 We cannot reduce row-1 as all its elements are ∞.


 Reduce all the elements of row-2 by 13.
 We cannot reduce row-3 as all its elements are ∞.
 Reduce all the elements of row-4 by 8.
Performing this, we obtain the following row-reduced matrix-

Column Reduction-

 There is no need to reduce column-1.


 We cannot reduce column-2 as all its elements are ∞.
 We cannot reduce column-3 as all its elements are ∞.
 There is no need to reduce column-4.

Thus, the matrix is already column reduced.


Finally, the matrix is completely reduced.
Now, we calculate the cost of node-5.

Cost(5)
= cost(3) + Sum of reduction elements + M[C,B]
= 25 + (13 + 8) + ∞
=∞

Choosing To Go To Vertex-D: Node-6 (Path A → C → D)

 From the reduced matrix of step-02, M[C,D] = ∞


 Set row-C and column-D to ∞
 Set M[D,A] = ∞

Now, resulting cost matrix is-


Now,
 We reduce this matrix.
 Then, we find out the cost of node-6.

Row Reduction-

 We can not reduce row-1 as all its elements are ∞.


 There is no need to reduce row-2.
 We can not reduce row-3 as all its elements are ∞.
 We can not reduce row-4 as all its elements are ∞.

Thus, the matrix is already row reduced.

Column Reduction-

 There is no need to reduce column-1.


 We can not reduce column-2 as all its elements are ∞.
 We can not reduce column-3 as all its elements are ∞.
 We can not reduce column-4 as all its elements are ∞.

Thus, the matrix is already column reduced.


Finally, the matrix is completely reduced.
Now, we calculate the cost of node-6.
Cost(6)
= cost(3) + Sum of reduction elements + M[C,D]
= 25 + 0 + 0 = 25
Thus, we have-
 Cost(5) = ∞ (for Path A → C → B)
 Cost(6) = 25 (for Path A → C → D)

We choose the node with the lowest cost.


Since cost for node-6 is lowest, so we prefer to visit node-6.
Thus, we choose node-6 i.e., path C → D.

Step-04:

We explore vertex B from node-6.


We start with the cost matrix at node-6 which is-

Cost(6) = 25

Choosing To Go to Vertex-B: Node-7 (Path A → C → D → B)

 From the reduced matrix of step-03, M[D,B] = 0


 Set row-D and column-B to ∞
 Set M[B,A] = ∞

Now, resulting cost matrix is-


Now,
 We reduce this matrix.
 Then, we find out the cost of node-7.

Row Reduction-

 We can not reduce row-1 as all its elements are ∞.


 We can not reduce row-2 as all its elements are ∞.
 We can not reduce row-3 as all its elements are ∞.
 We can not reduce row-4 as all its elements are ∞.

Column Reduction-

 We can not reduce column-1 as all its elements are ∞.


 We can not reduce column-2 as all its elements are ∞.
 We can not reduce column-3 as all its elements are ∞.
 We can not reduce column-4 as all its elements are ∞.
Thus, the matrix is already column reduced.
Finally, the matrix is completely reduced.
All the entries have become ∞.
Now, we calculate the cost of node-7.
Cost(7) = cost(6) + Sum of reduction elements + M[D,B]
= 25 + 0 + 0 = 25
Thus,
 Optimal path is: A → C → D → B → A
 Cost of Optimal path = 25 units

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