Chapter 3 Adf
Chapter 3 Adf
Chapter 3
Dynamic Programming
2
• For values of n and k that are not small, we cannot compute the
binomial coefficient directly from this definition because n ! is
very large.
• In the exercises we establish that
n 1 n 1
n 0 k n
k 1 k
k 1 k 0 or k n
Divide-and-Conquer
Recursive
Very inefficient – like recursive Fibonocci
To compute C(n,k), the algorithm computes
2 C(n, k) -1 terms (EXERCISE 2)
2 C(50, 10) -1 = 10,272,278,169
Algorithm 3.1 8
Algorithm 3.1
terms
10
• At each iteration, the values needed for that iteration have already been
computed
12
j=0
0 1 2 3 4
0 1
1 1 1
B [i][j] =B [i-1][j-1]+B[i-1][j]
2 1 2 1 B [i] [j]=1 j=0 or j=i
3 1 3 3 1
13
Algorithm 3.2
Binomial Coefficient Using Dynamic Programming
Problem: Compute the binomial coefficient.
Inputs: nonnegative integers n and k, where k ≤ n.
Outputs: bin2, the binomial coefficient
int bin2 (int n, int k)
{
index i, j;
int B[0..n][0..k];
i 0 1 2 3 . . .k-1 k k+1.. n
improvement
once a row is computed, we no longer need the values in the
row that precedes it. Therefore, the algorithm can be written
using only a one-dimensional array indexed from 0 to k.
bin (n , k ) = bin (n , n-k)
16
3
2 v3
v4 4
Optimization Problem
The Shortest Paths problem is an optimization problem
Multiple candidate solutions
Solution to the instance is a candidate solution with an
optimal value
Because there can be more than one shortest path from
one vertex to another, our problem is to find any one of
the shortest paths.
21
Adjacency Matrix M
Figure 3.3 1
v1 v2
1 2 3 4 5 1 2 3 4 5 9
1 0 1 1 5 1 0 1 3 1 4 3
5
2 9 0 3 2 2 8 0 3 2 5 v5 2
1 3
3 0 4 3 10 11 0 4 7
4 2 0 3 4 6 7 2 0 3
5 3 0 5 3 4 6 4 0 3
2 v3
v4
4
W D
The array D contains the lengths of the shortest
paths in the graph.
24
G: n vertices
Create Dk where 0 <= k <=n
Dk [i, j] = length of a shortest path from vi to vj using only
vertices in the set {v1 , v2 , . . . vk } as intermediate
vertices
Dn [i, j]= length of the shortest path from vi to vj that is
allowed to pass through any of the other vertices.
Because D(0)[i, j] is the length of a shortest path that is
not allowed to pass through any other vertices,
• D0 = W
• Dn = D
1 v2
v1
9 25
Example 3.2 3
5
v5 1 2 3
3
2 v3
v4
4
1 v2
v1
9 26
3
5
v5 1 2 3
3 2
v4 v3
4
27
D 0, D 1 , D 2, … D n .
( 3.2 )
W D
28
Case 1
At least one shortest path from vi to vj using only
vertices in {v1 , v2 , . . . vk } as intermediate vertex
does not use vk
Dk [i, j] = D(k-1) [i, j]
Case 2 30
vi … vk … vj
{
index i, j , k ;
D=W ;
for (k = 1; k <= n; k++)
for ( i = 1; i <= n; i++)
for ( j = 1; j <= n; j++)
D [ i ] [ j ] = minimum ( D [ i ] [ j ] , D [ i ] [ k ] + D [ k ] [ j ] )
No
The principle of optimality is said to apply in a problem
if an optimal solution to an instance of a problem
always contains optimal solutions to all substances.
The principle of optimality must apply in the shortest
path problem.
Shortest Paths Problem
If vk is a node on an optimal path from vi to vj then
the sub-paths vi to vk and vk to vj are also optimal
paths
35
Algorithm 3.4
Floyd’s Algorithm for Shortest Paths 2
Problem: Same as in Algorithm 3.3, except shortest paths
are also created.
36
}
37
38
To print Path(5, 3)
path(5, P[5][3]) vertex P[5][3] path(P[5][3] ,3)
1 v2
v1
9
3 1 2 3 4 5
5 1 0 0 4 0 4
v5 1 2 3 2 5 0 0 0 4
3 5 5 0 0 4
3 4 5 3 0 0 0
2 v3
v4 5 0 1 4 1 0
4
39
Algorithm 3.5
Print Shortest Path
Problem: Print the intermediate vertices on a shortest path
from one vertex to another vertex in a
weighted graph.
Inputs: the array P produced by Algorithm 3.4, and two indices,
q and r, of vertices in the graph that
Outputs: the intermediate vertices on a shortest path from vq to
vr.
40
Ralph
Level 0 Devia
Devia Ursula
Carmen Ralph
Tom Wally
The depth of a node in a tree is the number of edges in the unique path
from the root to the node.
The depth of a tree is the maximum depth of all nodes in the tree.
44
Chained-Matrix Multiplication
Optimal order for chained-matrix multiplication
dependent on array dimensions
Consider all possible orders and take the
minimum: tn > 2n-2
Principle of Optimality applies
Develop Dynamic Programming Solution
M[i,j] = minimum number of multiplications needed
to multiply Ai through Aj
47
2
The following are the 3 tours and v1 v2
lengths for the graph : 1
If we consider all possible tours, the 2nd vertex on the tour can be
any of n − 1 vertices, the 3rd vertex on the tour can be any of n − 2
vertices, … , the nth vertex on the tour can be only one vertex.
Therefore, the total number of tours is
(n – 1) (n – 2) . . . 1 =(n – 1) !
optimal tour
v1 vk …
subpath of from vk to v1
must be a shortest
Let A V
D [vi] [A] length of shortest path from vi to v1 passing through each
vertex of A exactly once.
D [v1] [V - {v1}] length of shortest path from v1 to v1 passing through
each vertex of V - {v1} exactly once.
Example 3.10
If A = { v3 }, then D [v2] [A] = length [v2 , v3 , v1 ]=
If A = { v3 , v4 }, then D [v2] [A] = min (length [v2, v3 , v4, v1 ],
length [v2, v4 , v3 , v1 ])
= min ( 20, ) = 20
51
Example 3.11
Determine an optimal tour. First consider the empty set:
D [v2] [] = 1
D [v3] [] =
1 2 3 4
D [v4] [] = 6
1 0 2 9
2 1 0 6 4
3 7 0 8
4 6 3 0
53
Algorithm 3.11
• The Dynamic Programming Algorithm for the TSP Problem
Thoerem 3.1
For all n 1
n
n
k
k
k 1
n 2 n 1
58
n2
n 1
T ( n ) ( n 1 k ) k (3 .8 )
k 1 k
It is not hard to see that
n 1 n 2
( n 1 k ) ( n 1)
k k
Substituti ng this equality into Equality 3.8, we have
n2
n 2
T ( n ) ( n 1) k
k 1 k
Applying theorem 3.1,
T ( n ) ( n 1)( n 2 ) 2 n 3 ( n 2 2 n ).
60
Θ(n2n )
Inefficient solution using dynamic
programming
Problem NP-Complete: no one has ever
developed a polynomial-time algorithm, but no one
has ever shown that such an algorithm is not
possible.
61
Example 3.12
20-city.
Assuming that the time to process the basic instruction in Brute-
Force algorithm is 1 microsecond, and that the time to compute
the length of each tour in Dynamic Prog. algorithm is 1
microsecond.
Brute-Force alg 19! s= 3857 years
Dynamic Prog. Algorithm (20-1) (20-2) 220-3 s = 45 seconds