algorithms ch3
TRANSCRIPT
-
8/6/2019 Algorithms Ch3
1/55
AlgorithmsAnalysis, Design and Implementation
Bahman Arasteh
Algorithm Design: Arasteh1
Department of Computer Engineering
Azad University of Tabriz
E-mail: [email protected]
-
8/6/2019 Algorithms Ch3
2/55
Chapter 3: Dynamic Programming
The divide-and-conquer approach solves an
instance of a problem
by dividing it into smaller instances and then
blindly solving these smaller instances.
this is a top-town approach.
Algorithm Design: Arasteh2
wor s n pro ems suc as ergesor , w ere esmaller instances are unrelated.
They are unrelated because consists of an array ofkeys that must be sorted independently.
-
8/6/2019 Algorithms Ch3
3/55
Dynamic Programming
In problems where the smaller instances are
related, a divide-and-conquer algorithm often
ends up repeatedly solving common instances,
and the result is a very inefficient algorithm.
For example to compute the fifth Fibonacci term
Algorithm Design: Arasteh3
we nee o compu e e our an rFibonacci terms.
However, the determinations of the fourth and
third Fibonacci terms are related in that they both
require the second Fibonacci term.
-
8/6/2019 Algorithms Ch3
4/55
Dynamic Programming
Dynamic programming
Dynamic programming is similar to divide-and-
conquer in that an instance of a problem in
divided into smaller instances.
However, in this approach
Algorithm Design: Arasteh4
we solve small instances first,
store the results, and later,
whenever we need a result, look it up instead ofrecomputing it.
Dynamic programming is therefore a bottom-upapproach.
-
8/6/2019 Algorithms Ch3
5/55
Dynamic Programming
The steps in the development of a dynamic
programming algorithm are as follows:
1. Establisha recursive property that gives the solutionto an instance of the problem.
Algorithm Design: Arasteh5
. o ve an ns ance o e pro em n a -fashion by solving smaller instances first
-
8/6/2019 Algorithms Ch3
6/55
Dynamic Programming
The Binomial Coefficient:
Definition:
n
k
n
k n k for k n
!
!( )!0
Algorithm Design: Arasteh6
Recursive Definition:
n
k
n
k
n
kk n
k or k n
1
1
10
1 0
-
8/6/2019 Algorithms Ch3
7/55
Dynamic Programming
The Recursive Algorithm for Binomial Coefficient:
Algorithm Design: Arasteh7
-
8/6/2019 Algorithms Ch3
8/55
Dynamic Programming
Analysis of The Recursive Algorithm :
Algorithm Design: Arasteh8
-
8/6/2019 Algorithms Ch3
9/55
Dynamic Programming
Analysis of The Recursive Algorithm :
Algorithm Design: Arasteh9
-
8/6/2019 Algorithms Ch3
10/55
Dynamic Programming
Binomial Coefficient Using Divide-and-Conquer:
Algorithm Design: Arasteh10
-
8/6/2019 Algorithms Ch3
11/55
Dynamic Programming
The steps for constructing a dynamic programming
algorithm for this problem are as follows:1. Establisha recursive property. This has already been done in Equality
3.1. Written in terms of B, it is
B i j B i j B i j j i
[ ][ ][ ][ ] [ ][ ]
1 1 1 0
Algorithm Design: Arasteh11
2. Solve an instance of the problem in a bottom-upfashion by computingthe rows in B in sequence starting with the first row
-
8/6/2019 Algorithms Ch3
12/55
Dynamic Programming
The array Bused to compute the binomial coefficient:
Algorithm Design: Arasteh12
-
8/6/2019 Algorithms Ch3
13/55
Dynamic Programming
B[4][2] =?
B[0][0] = 1
B[1][0] = 1
B[1][1] = 1
Algorithm Design: Arasteh13
B[2][0] = 1
B[2][1] = B[1][0] + B[1][1] = 1 + 1 = 2
B[2][2] =1
B[3][0] = 1
B[3][1] = B[2][0] + B[2][1] = 1 + 2 = 3
B[3][2] = B[2][1] + B[2][2] = 2 + 1 = 3
B[4][0] = 1
B[4][1] = B[3][0] + B[3][1] = 1 + 3 = 4
B[4][2] = B[3][1] + B[3][2] = 3 + 3 = 6
-
8/6/2019 Algorithms Ch3
14/55
Dynamic Programming
Algorithm Design: Arasteh14
-
8/6/2019 Algorithms Ch3
15/55
Dynamic Programming
Algorithm Design: Arasteh15
-
8/6/2019 Algorithms Ch3
16/55
Dynamic Programming
Algorithm Design: Arasteh16
-
8/6/2019 Algorithms Ch3
17/55
Dynamic Programming
number of passes for each value of i:
i 0 1 2 3 k k+1 n
Number of
passes
1 2 3 4 k+1 k+1 k+1
Algorithm Design: Arasteh17
)(
2
)1)(22()1)(1(
2
)1(
)1()1()1(4321
1
nkkkn
kknkk
kkkk
timeskn
-
8/6/2019 Algorithms Ch3
18/55
Dynamic Programming
Floyd's Algorithm for Shortest Paths:
Suppose we have a labeled digraph that gives theflying time on certain routes connecting cities, and wewish to construct a table that gives the shortest timerequired to fly from any one city to any other.
We now have an instance of the allpairs shortest
Algorithm Design: Arasteh18
paths (APSP) problem.
To state the problem precisely, we are given a directedgraph G = (V, E) in which each arc v w has a
non-negative cost C[v, w].
-
8/6/2019 Algorithms Ch3
19/55
Dynamic Programming
Floyd's Algorithm for Shortest Paths:
Algorithm Design: Arasteh19
-
8/6/2019 Algorithms Ch3
20/55
Dynamic Programming
Floyd's Algorithm for Shortest Paths:
Algorithm Design: Arasteh20
-
8/6/2019 Algorithms Ch3
21/55
Dynamic Programming
Floyd's Algorithm for Shortest Paths:
Algorithm Design: Arasteh21
D(k)[i][j] = minimum(D(k-1)[i][j], D(k-1)[i][k] + D(k-1)[k][j])
-
8/6/2019 Algorithms Ch3
22/55
Dynamic Programming
Floyd's Algorithm for Shortest Paths:
Algorithm Design: Arasteh22
D(k)[i][j] = minimum(D(k-1)[i][j], D(k-1)[i][k] + D(k-1)[k][j])
-
8/6/2019 Algorithms Ch3
23/55
Dynamic Programming
Floyd's Algorithm for Shortest Paths:
Algorithm Design: Arasteh23
-
8/6/2019 Algorithms Ch3
24/55
Dynamic Programming
Floyd's Algorithm for Shortest Paths:
Algorithm Design: Arasteh24
-
8/6/2019 Algorithms Ch3
25/55
Dynamic Programming
Floyd's Algorithm for Shortest Paths:
D(2)[5][4]D(1)[2][4] = minimum(D(0)[2][4], D(0)[2][1] + D(0)[1][4] )
= minimum(2, 9 + 1) = 2
D(1)[5][2] = minimum(D(0)[5][2], D(0)[5][1] + D(0)[1][2] )
Algorithm Design: Arasteh25
,
D(1)[5][4] = minimum(D(0)[5][4], D(0)[5][1] + D(0)[1][4] )= minimum(, 3 + 1) = 4
D(2)[5][4] = minimum(D(1)[5][4], D(1)[5][2] + D(1)[2][4] )
= minimum(4, 4 + 2) = 4
-
8/6/2019 Algorithms Ch3
26/55
Dynamic Programming
Floyd's Algorithm for Shortest Paths:
Algorithm Design: Arasteh26
-
8/6/2019 Algorithms Ch3
27/55
Dynamic Programming
Analysis of Floyd's Algorithm for Shortest Paths:
T(n) = n n n = n3 (n3)
Algorithm Design: Arasteh27
-
8/6/2019 Algorithms Ch3
28/55
Dynamic Programming
Example of Previous Graph:
Algorithm Design: Arasteh28
-
8/6/2019 Algorithms Ch3
29/55
Dynamic Programming
Example :
Algorithm Design: Arasteh29
-
8/6/2019 Algorithms Ch3
30/55
Dynamic Programming
Example :
Algorithm Design: Arasteh30
-
8/6/2019 Algorithms Ch3
31/55
Dynamic Programming
Example :
Algorithm Design: Arasteh31
-
8/6/2019 Algorithms Ch3
32/55
Dynamic Programming
Print Shortest Path:
Algorithm Design: Arasteh32
-
8/6/2019 Algorithms Ch3
33/55
Dynamic Programming
Chained Matrix Multiplication :
Suppose we want to multiply a 2 2 matrix times a 3 4 matrix asfollows:
Algorithm Design: Arasteh33
In general, to multiply an i jmatrix times a j kmatrix using thestandard method, it is necessary to do
-
8/6/2019 Algorithms Ch3
34/55
Dynamic Programming
Chained Matrix Multiplication :
Consider the multiplication of the following four matrices:
Algorithm Design: Arasteh34
we have the following number of elementary for each order
-
8/6/2019 Algorithms Ch3
35/55
Dynamic Programming
Example: Suppose we have the following six matrices:
A1 A2 A3 A4 A5 A65 2 23 34 46 67 78
d0 d1 d1 d2 d2 d3 d3 d4 d4 d5 d5 d6M[4][6] = ?
To multiply A4, A5, and A6, we have the following two orders and
Algorithm Design: Arasteh35
numbers of elementary multiplications:
-
8/6/2019 Algorithms Ch3
36/55
Dynamic Programming
The optimal order for multiplying six matrices must have one of
these factorizations:1. A1 ( A2 A3 A4 A5 A6 )
2. ( A1 A2 ) ( A3 A4 A5 A6 )
3. ( A1 A2 A3 ) ( A4 A5 A6 )
4. ( A1 A2 A3 A4 ) ( A5 A6 )
5. A A A A A A
Algorithm Design: Arasteh36
The number of multiplications for the kth factorization is
M[1][k] + M[k+1][6] + d0 dkd6 We have established that:
)]6][1[]][1[(min]6][1[ 6051 dddkMkMimumM kk
-
8/6/2019 Algorithms Ch3
37/55
Dynamic Programming
0]][[
),]][1[]][[(min]][[ 11
iiM
jiifdddjkMkiMimumjiM jkijki
Algorithm Design: Arasteh37
-
8/6/2019 Algorithms Ch3
38/55
Dynamic Programming
M[1][2], M[2][3]=?
Algorithm Design: Arasteh38
-
8/6/2019 Algorithms Ch3
39/55
Dynamic Programming
M[1][2], M[2][3]=?
Algorithm Design: Arasteh39
-
8/6/2019 Algorithms Ch3
40/55
Dynamic Programming
Algorithm Design: Arasteh40
-
8/6/2019 Algorithms Ch3
41/55
Dynamic Programming
Analysis:
)()1)(1(
])[( 31
nnnn
diagonaldiagonalnn
Algorithm Design: Arasteh41
1diagonal
-
8/6/2019 Algorithms Ch3
42/55
Dynamic Programming
The Traveling Salesperson Problem (TSP):
Suppose a salesperson is planning a sales trip that
includes 20 cities.
Each city is connected to some of the other cities bya road.
Algorithm Design: Arasteh42
,shortest route that starts at the salesperson's home
city, visits each of the cities once, and ends up at thehome city.
This problem of determining a shortest route is
called the Traveling Salesperson problem.
-
8/6/2019 Algorithms Ch3
43/55
Dynamic Programming
The Traveling Salesperson Problem (TSP):
A tour(also called a Hamiltonian circuit) in adirected graph is a path from a vertex to itself thatpasses through each of the other vertices exactlyonce.
An o timal tourin a wei hted, directed ra h is
Algorithm Design: Arasteh43
such a path of minimum length.
The Traveling Salesperson problem is to find anoptimal tour in a weighted, directed graph when atleast one tour exists.
-
8/6/2019 Algorithms Ch3
44/55
Dynamic Programming
Example (TSP):
Algorithm Design: Arasteh44
-
8/6/2019 Algorithms Ch3
45/55
Dynamic Programming
TSP:
We solved this instance by simply considering allpossible tours.
In general, there can be an edge from every vertex toevery other vertex.
Algorithm Design: Arasteh45
,the tour can be any of n 1 vertices, the third vertex on
the tour can be any of n 2 vertices, , the nth vertexon the tour can be only one vertex.
Therefore, the total number of tours is
-
8/6/2019 Algorithms Ch3
46/55
Dynamic Programming
TSP:
Algorithm Design: Arasteh46
-
8/6/2019 Algorithms Ch3
47/55
Dynamic Programming
TSP:
Algorithm Design: Arasteh47
-
8/6/2019 Algorithms Ch3
48/55
Dynamic Programming
TSP:
Algorithm Design: Arasteh48
A={v3}
D[v2][A] = length[v2, v3, v1] =
A={v3, v4}
D[v2][A] = minimum( length[v2, v3, v4, v1], length[v2, v4, v3, v1]
= minimum(20, ) = 20
-
8/6/2019 Algorithms Ch3
49/55
Dynamic Programming
TSP:
Optimal Tour length:
}]),{][[]][1[(1
2min vvvimum jj
nj
VDjW
Algorithm Design: Arasteh49
in general for i 1 and vi not in A:
]1][[]][[
}]){][[]][[(]][[ min:
iWD
AifADjiW
v
AD
v
vvimumv
i
jjAj
i
j
-
8/6/2019 Algorithms Ch3
50/55
Dynamic Programming
TSP:
Algorithm Design: Arasteh50
-
8/6/2019 Algorithms Ch3
51/55
Dynamic Programming
TSP:
Algorithm Design: Arasteh51
-
8/6/2019 Algorithms Ch3
52/55
Dynamic Programming
TSP:
Algorithm Design: Arasteh52
-
8/6/2019 Algorithms Ch3
53/55
Dynamic Programming
Analysis of TSP:
21
1
n
n
k
nk
nk
Algorithm Design: Arasteh53
1k
nk
k
2
1
1
11
1
111
nn
k
n
k
n
k nk
n
nk
n
nk
n
k
-
8/6/2019 Algorithms Ch3
54/55
Dynamic Programming
Analysis of TSP:
Time Complexity:
T(n) = (n-1)(n-2)2n-3 (n22n)Memory Complexity:
M(n) = 2 n2n-1 = n2n (n2n)
Algorithm Design: Arasteh54
-
8/6/2019 Algorithms Ch3
55/55
End
Algorithm Design: Arasteh55