DAA-Module 4
DAA-Module 4
MODULE 4:
DYNAMIC PROGRAMMING
Dynamic Programming:
2
Dynamic Programming:
Transitive Closure: Warshall’s Algorithm,
All Pairs Shortest Paths: Floyd's Algorithm,
Optimal Binary Search Trees,
Knapsack problem,
Bellman-Ford Algorithm,
Travelling Sales Person problem
Dynamic Programming:
3
• Applications:
– Communications, transportation networks, and operations
research.
– pre-computing distances for motion planning in computer games.
All Pairs Shortest Paths
Floyd’s algorithm
6
Floyds Algorithm
7
• The element in the ith row and jth column of matrix R(k) is equal
to 1 if and only if
– there exists a directed path of a positive length from the
ith vertex to the jth vertex with each intermediate vertex, if
any, numbered not higher than k.
This means that there exists a path from the ith vertex vi to
the jth vertex vj with each intermediate vertex numbered
not higher than k:
vi, a list of intermediate vertices each numbered not higher
than k, vj
16
17
18
Algorithm
19
20
Problem Statement
• The travelling salesman problem consists of a salesman
and a set of cities.
• The salesman has to visit each one of the cities exactly
once starting from a certain one (e.g. the hometown) and
returning to the same city.
• The challenge of the problem is that the traveling salesman
wants to minimize the total length of the trip
• It is assumed that all cities are connected to all other cities
Travelling Salesman Problem
26
Naive Solution:
1. Consider city 1 as the starting and ending point.
2. Generate all (n-1)! Permutations of cities.
3. Calculate cost of every permutation and keep track of
minimum cost permutation.
4. Return the permutation with minimum cost.
Time Complexity: ?
n!
TSP using DP
27
k = 2, c12 + g(2,{3,4,5})
k = 3, c13 + g(3,{2,4,5})
g(1,{2,3,4,5}) = min k = 4, c14 + g(4,{2,3,5})
k = 5, c15 + g(5,{2,3,4})
30
31
32
Problem definition
• Single source shortest path - Given a graph and a source
vertex s in graph, find shortest paths from s to all vertices
in the given graph.
• The graph may contain negative weight edges.
dist1(1) = 0
dist1(2) = 6
dist1(3) = 5
dist1(4) = 5
dist1(5) = ∞
dist1(6) = ∞
dist1(7) = ∞
k=1
For every vertex (except source) look for the
incoming edge (except from source)
49
dist2(1) = 0
dist1(2), 6
dist2(2) = min = min =3
dist1(3) + cost(3,2) 5−2
dist1(3) 5
dist2(3) = min = min =3
dist1(4) + cost(4,3) 5−2
dist2(4) = 5
dist1(5), ∞
dist2(5) = min dist1(2) + cost(2,5) = min 6−1 = 5
dist1(3) + cost(3,5) 5+1
dist1(6) ∞
dist2(6) = min = min =4
dist1(4) + cost(4,6) 5−1
dist1(7) ∞
dist2(7) = min dist1(5) + cost(5,7) = min ∞ + 3 = ∞
dist1(6) + cost(6,7) ∞+3
k=2
For every vertex (except source) look for the
incoming edge (except from source)
50
dist3(1) = 0 dist2(2) 3
dist3(2) = min = min =1
dist2(3) + cost(3,2) 3−2
dist2(3) 3
dist3(3) = min = min =3
dist2(4) + cost(4,3) 5−2
dist3(4) = 5
dist2(5) 5
dist3(5) = min dist2(2) + cost(2,5) = min 3−1 = 2
dist2(3) + cost(3,5) 3+1
3 dist2(6) 4
dist (6) = min = min =4
dist2(4) + cost(4,6) 5−1
dist2(7) ∞
dist3(7) = min dist2(5) + cost(5,7) = min 5 + 3 = 7
dist2(6) + cost(6,7) 4+3
k=3
For every vertex (except source) look for the
incoming edge (except from source)
dist4(1) = 0
51
dist3(2) 1
dist4(2) = min = min =1
dist3(3) + cost(3,2) 3−2
dist3(3) 3
dist4(3) = min = min =3
dist3(4) + cost(4,3) 5−2
dist4(4) = 5
dist3(5) 2
dist4(5) = min dist3(2) + cost(2,5) = min 1−1 = 0
dist3(3) + cost(3,5) 3+1
4 dist3(6) 4
dist (6) = min = min =4
dist3(4) + cost(4,6) 5−1
dist3(7) 7
dist4(7) = min dist3(5) + cost(5,7) = min 2 + 3 = 5
4+3
dist3(6) + cost(6,7)
k=4
For every vertex (except source) look for the
incoming edge (except from source)
dist5(1) = 0
52
dist4(2) 1
dist5(2) = min = min =1
dist4(3) + cost(3,2) 3−2
dist4(3) 3
dist5(3) = min = min =3
dist4(4) + cost(4,3) 5−2
dist5(4) = 5
dist4(5) 0
dist5(5) = min dist4(2) + cost(2,5) = min 1−1 = 0
dist4(3) + cost(3,5) 3+1
5
dist4(6) 4
dist (6) = min = min =4
dist4(4) + cost(4,6) 5−1
dist4(7) 5
dist5(7) = min dist4(5) + cost(5,7) = min 0 + 3 = 3
dist4(6) + cost(6,7) 4+3
k=5
For every vertex (except source) look for the
incoming edge (except from source)
dist6(1) = 0
53
dist5(2) 1
dist6(2) = min = min =1
dist5(3) + cost(3,2) 3−2
dist5(3) 3
dist6(3) = min = min =3
dist5(4) + cost(4,3) 5−2
dist6(4) = 5
dist5(5) 0
dist6(5) = min dist5(2) + cost(2,5) = min 1−1 = 0
dist5(3) + cost(3,5) 3+1
6 dist5(6) 4
dist (6) = min = min =4
dist5(4) + cost(4,6) 5−1
dist5(7) 5
dist6(7) = min dist5(5) + cost(5,7) = min 0 + 3 = 3
dist5(6) + cost(6,7) 4+3
k=6
54
Algorithm
55
Analysis
56
1
Optimal Binary Search Tree
58
• The binary search tree for which the average number of comparisons in a
search is the smallest as possible is called as optimal binary search tree.
• If probabilities of searching for elements of a set are known an optimal
binary search tree can be constructed.
Optimal Binary Search Tree
59 Example
• Consider four keys A, B, C, and D to be searched for with probabilities
0.1, 0.2, 0.4, and 0.3, respectively.
• 14 different binary search trees are possible
• Two are shown here
• For our tiny example, we could find the optimal tree by generating all 14 binary
search trees with these keys.
• As a general algorithm, this exhaustive-search approach is unrealistic:
– the total number of binary search trees with n keys is equal to the
nth Catalan number,
Problem definition:
• Given a sorted array a1, . . . , an of search keys and an
array p1, . . . , pn of probabilities of searching,
where pi is the probability of searches to ai.
• Construct a binary search tree of all keys such that,
smallest average number of comparisons made in
a successful search.
Optimal Binary Search Tree
62
Example
68
index 1 2 3 4
Example
69
index 1 2 3 4
Example
70
index 1 2 3 4
Example
71