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

U3 - Dynamic Programming

Dynamic Programming

Uploaded by

BhaskarHosur
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

U3 - Dynamic Programming

Dynamic Programming

Uploaded by

BhaskarHosur
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

DYNAMIC

PROGRAMMING
-BHASKAR H S
Dynamic Programming
Dynamic Programming is a general algorithm design
technique for solving problems defined by recurrences with
overlapping subproblems

• “Programming” here means “planning”

• Main idea:
- set up a recurrence relating a solution to a larger
instance to solutions of some smaller instances
- solve smaller instances once
- record solutions in a table
- extract solution to the initial instance from that table
Examples of DP algorithms

• Computing a binomial coefficient


• Some difficult discrete optimization problems:
- knapsack
- traveling salesman

• Constructing an optimal binary search tree


• Warshall’s algorithm for transitive closure
• Floyd’s algorithm for all-pairs shortest paths
Warshall’s Algorithm: Transitive Closure

• Computes the transitive closure of a relation


• Alternatively: existence of all nontrivial paths in a digraph
• Example of transitive closure:

3 3
1 1

0 0 1 0 0 0 1 0
1 0 0 1 1 1 1 1
2 4 0 0 0 0 4 0 0 0 0
0 1 0 0
2 1 1 1 1
Warshall’s Algorithm
Constructs transitive closure T as the last matrix in the sequence
of n-by-n matrices R(0), … , R(k), … , R(n) where
R(k)[i,j] = 1 iff there is nontrivial path from i to j with only first k
vertices allowed as intermediate
Note that R(0) = A (adjacency matrix), R(n) = T (transitive closure)
3 3 3 3 3
1 1 1 1 1

4 4 4 2 4 4
2 2 2 2

R(0) R(1) R(2) R(3) R(4)


0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0
1 0 0 1 1 0 1 1 1 0 1 1 1 0 1 1 1 1 1 1
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 0 0 0 1 0 0 1 1 1 1 1 1 1 1 1 1 1 1
Warshall’s Algorithm
(recurrence)
On the k-th iteration, the algorithm determines for every pair of
vertices i, j if a path exists from i and j with just vertices 1,…,k

{
allowed as intermediate

R(k-1)[i,j] (path using just 1 ,…,k-1)


R(k)[i,j] = or
R(k-1)[i,k] and R(k-1)[k,j] (path from i to k
and from k to i
k using just 1 ,…,k-
i 1)

j
Warshall’s Algorithm (matrix
generation)
Recurrence relating elements R(k) to elements of
R(k-1) is:

R(k)[i,j] = R(k-1)[i,j] or (R(k-1)[i,k] and R(k-1)


[k,j])
It implies the following rules for generating R(k)
from R(k-1):

Rule 1 If an element in row i and column j is 1 in


R(k-1),
it remains 1 in R(k)

Rule 2 If an element in row i and column j is 0 in


R(k-1),
it has to be changed to 1 in R(k) if and
only if
Warshall’s Algorithm
(example)
3
1 0 0 1 0 0 0 1 0
1 0 0 1 1 0 1 1
R(0) = 0 0 0 0 R(1) = 0 0 0 0
2 4
0 1 0 0 0 1 0 0

0 0 1 0 0 0 1 0 0 0 1 0
1 0 1 1 1 0 1 1 1 1 1 1
R(2) = 0 0 0 0 R(3) = 0 0 0 0 R(4) = 0 0 0 0
1 1 1 1 1 1 1 1 1 1 1 1
Warshall’s Algorithm (pseudocode and analysis)

Time efficiency: Θ(n3)


Space efficiency: Matrices can be written over their
predecessors
STEP BY STEP SOLUTION FOR A
PROBLEM
Final Transitive Closure
Matrix
Floyd’s Algorithm: All pairs
shortest paths
Problem: In a weighted (di)graph, find shortest
paths between every pair of vertices

Same idea: construct solution through series of


matrices D(0), …,D (n) using increasing subsets of
the vertices allowed as intermediate
4 3
Example:
1
1
6
1 5

2 4
3
Floyd’s Algorithm (matrix
generation)
On the k-th iteration, the algorithm determines
shortest paths between every pair of vertices i, j
that use only vertices among 1,…,k as
intermediate
D(k)[i,j] = min {D(k-1)[i,j], D(k-1)[i,k] + D(k-1)[k,j]}
D(k-1)[i,k]
k
i
D(k-1)[k,j]
D(k-1)[i,j]

j
Floyd’s Algorithm (example)

1
2 2 0 ∞ 3 ∞ 0 ∞ 3 ∞
3 6 7 2 0 ∞ ∞ 2 0 5 ∞
D(0) = ∞ 7 0 1 D(1) = ∞ 7 0 1
3
1
4 6 ∞ ∞ 0 6 ∞ 9 0

0 ∞ 3 ∞ 0 10 3 4 0 10 3 4
2 0 5 ∞ 2 0 5 6 2 0 5 6
D(2) = 9 7 0 1 D(3) = 9 7 0 1 D(4) = 7 7 0 1
6 ∞ 9 0 6 16 9 0 6 16 9 0

D(3): 3 to 1 not allowing 4=9. D(4): 3 to 1 with allowing 4=7


Floyd’s Algorithm (pseudocode and analysis)

Time efficiency: Θ(n3)


Space efficiency: Matrices can be written over their predecessors
STEP BY STEP SOLUTION FOR A
PROBLEM
Knapsack Problem by DP
Given n items of
integer weights: w1 w2 … w n
values: v1 v2 … vn
a knapsack of integer capacity W
find most valuable subset of the items that fit into the
knapsack

Recursive solution?
What is smaller problem?
How to use solution to smaller in solution to larger
Table?
Order to solve?
Initial conditions?
Knapsack Problem by DP (example)
Example: Knapsack of capacity W = 5
item weight value
1 2 $12
2 1 $10
3 3 $20
4 2 $15 capacity j

0 1 2 3 4 5
0 0 0 0 0 0 0
w1 = 1
2, v1= 012
w2 = 2
1, v2= 010
3 0
w3 = 3, v3= 20
4 0
w4 = 2, v4= 15
Knapsack Problem by DP
Given n items of
integer weights: w1 w2 … w n
values: v1 v2 … vn
a knapsack of integer capacity W
find most valuable subset of the items that fit into the
knapsack

Consider instance defined by first i items and capacity j (j 


W).
Let V[i,j] be optimal value of such instance. Then
Knapsack Problem by DP (example)
Example: Knapsack of capacity W = 5
item weight value
1 2 $12
2 1 $10
3 3 $20 Capacity j
4 2 $15

w1 = 2, v1= 12
w2 = 1, v2= 10
w3 = 3, v3= 20
w4 = 2, v4= 15
Computing Binomial
Coefficient
 A K-combination of an n-Set or in Simple nCk
 We have learnt a method of computing this Binomial
Coefficient by the formula

=

 Dynamic programming gives us an other method of


computing the Binomial Coefficient using a recurrence
relation in form of a table.
Recurrence Relation For Binomial
Coefficient

Using the recurrence relation we construct a table for the


binomial coefficients.
Table for Binomial
Coefficient
Algorithm or Pseudocode
of Binomial Coefficient

Time efficiency: Θ(nk)


Space efficiency: Matrices can be written over their

You might also like