0% found this document useful (0 votes)
48 views27 pages

Module 4

The document discusses dynamic programming and its application to solving the 0/1 knapsack problem and the traveling salesman problem. It explains that dynamic programming solves problems with overlapping subproblems by solving each subproblem only once and storing the results in a table to obtain the solution. For the knapsack problem, it provides an example of finding the optimal solution using dynamic programming with items of different weights and profits. It also discusses that the time complexity of solving the traveling salesman problem using dynamic programming is exponential, with a formula provided.

Uploaded by

M.A raja
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)
48 views27 pages

Module 4

The document discusses dynamic programming and its application to solving the 0/1 knapsack problem and the traveling salesman problem. It explains that dynamic programming solves problems with overlapping subproblems by solving each subproblem only once and storing the results in a table to obtain the solution. For the knapsack problem, it provides an example of finding the optimal solution using dynamic programming with items of different weights and profits. It also discusses that the time complexity of solving the traveling salesman problem using dynamic programming is exponential, with a formula provided.

Uploaded by

M.A raja
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/ 27

MODULE 4- DYNAMIC PROGRAMMING

Solves problems with overlapping subproblems.


Rather than solving overlapping subproblems again and again,
solve each of the smaller subproblems only once and record
the results in a table from which a solution to the original
problem can then be obtained.
Dynamic knapsack- 0/1 Knapsack
Problem 1: Find the optimal solution for the following
instance of knapsack problem using dynamic
programming. Maximum capacity of knapsack W=5.
solve the following 0/1 knapsack instance: n=3, [ω1, ω2, ω3] = [1,
2, 2] and [P1, P2, P3] = [18, 16, 6] and M=4.
Item Weight Profit

1 1 18
2 2 16
3 2 6
Dynamic Knapsack: M=4
Item Weight Profit

1 1 18
2 2 16
3 2 6
0 1 2 3 4

0
1

3
W=5

0 1 2 3 4 5
0
1
2
3
4
Warshall’s algorithm (transitive closure) R(1)=
a b c d a b c d
A= a a
R(0) b b
c c
d d
R(2)= R(3)= R(4)=
a b c d a b c d a b c d
a a a
b b b
c c c
d d d
Warshall’s algorithm (transitive closure) R(1)=
a b c d a b c d
0100 a b A= a 0 1 0 0 a 0 1 0 0
0010 R(0) b 0 0 1 0 b 0 0 1 0
0001 d c 0 0 0 1 c 0 0 0 1
c
0000 d 0 0 0 0 d 0 0 0 0
R(2)= R(3)= R(4)=
a b c d a b c d a b c d
a 0 1 1 0 a 0 1 1 1 a 0 1 1 1
b 0 0 1 0 b 0 0 1 1 b 0 0 1 1
c 0 0 0 1 c 0 0 0 1 c 0 0 0 1
d 0 0 0 0 d 0 0 0 0 d 0 0 0 0
a b c d e a b c d e
A= a R(2)= a
10010
0 1 0 0 0 R(1)
b b
00011 c c
10000
d d
01 0 0 1
e e
R(3)=
a b c d e a b c d e
a b c d e
a a
a
b b
b
c c
c
d d
d
e e
e
Floyd’s Algorithm (All-Pairs Shortest-Paths
Problem)

Floyd’s is applicable to both


undirected and directed weighted
graphs provided that they do not
contain a cycle of a negative
length Time complexity of Floyd’s O(n3)
1 2 3 1 2 3
1 1
2 2
3 3

1 2 3
1
2
3
1 2 3 1 2 3 1 2 3
1 0 4 11 1 0 4 11 1 0 4 6
2 6 0 2 2 6 0 2 2 6 0 2
3 3 ∞ 0 3 3 7 0 3 3 ∞ 0

1 2 3
1 0 4 6
2 5 0 2
3 3 ∞ 0
0∞3∞
2 0 ∞∞ a b c d a b c d
∞7 0 1 a 0 ∞ 3 ∞ a
6 ∞∞ 0
b 2 0 ∞ ∞ b
c ∞ 7 0 1 c
d 6 ∞ ∞ 0 d

a b c d a b c d a b c d
a a a
b b b
c c c
d d d
0∞3∞
2 0 ∞∞ a b c d a b c d a b c d
∞ 7 0 1 D0= a 0 ∞ 3 ∞ a 0 ∞ 3 ∞ a 0 ∞ 3 ∞
6 ∞∞ 0
b 2 0 ∞ ∞ b 2 0 5 ∞ b 2 0 5 ∞
2 c ∞ 7 0 1 c ∞ 7 0 1 c 9 7 0 1
a b
d 6 ∞ ∞ 0 d 6 ∞ 9 0 d 6 ∞ 9 0
3
7 6

c d a b c d a b c d
1
a 0 10 3 4 a 0 10 3 4
b 2 0 5 6 b 2 0 5 6
c 9 7 0 1 c 7 7 0 1
d 6 16 9 0 d 6 16 9 0
Travelling Salesman Problem Using Dynamic
Programming
• Let G=(V, E) be a directed graph with edge costs cij. The variable cij is defined
such that cij>0 for all i and j and cij= ∞ if <i , j> 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
the tour is the sum of the cost of the edges on the tour. The travelling
salesperson problem is to find a tour of minimum cost.
• Time complexity of this problem using dynamic programming is exponential :
O(2n)
Formula:
g(i, S) = min {cik + g(k,S-{k})}
kƐS
1
1 2

2 3 4
4 3

3 4 2 4 2 3

4 3 4 2 3 2

1 1 1 1 1 1

You might also like