Вы находитесь на странице: 1из 55

Algorithms

Analysis, Design and Implementation


Bahman Arasteh
Algorithm Design: Arasteh 1
Bahman Arasteh
Department of Computer Engineering
Azad University of Tabriz
E-mail: b_arasteh@iaut.ac.ir
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.
It works in problems such as Mergesort, where the
Algorithm Design: Arasteh 2
It works in problems such as Mergesort, where the
smaller instances are unrelated.
They are unrelated because consists of an array of
keys that must be sorted independently.
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
we need to compute the fourth and third
Algorithm Design: Arasteh 3
we need to compute the fourth and third
Fibonacci terms.
However, the determinations of the fourth and
third Fibonacci terms are related in that they both
require the second Fibonacci term.
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: Arasteh 4
we solve small instances first,
store the results, and later,
whenever we need a result, look it up instead of
recomputing it.
Dynamic programming is therefore a bottom-up
approach.
Dynamic Programming
The steps in the development of a dynamic
programming algorithm are as follows:
1. Establish a recursive property that gives the solution
to an instance of the problem.
2. Solve an instance of the problem in a bottom-up
Algorithm Design: Arasteh 5
2. Solve an instance of the problem in a bottom-up
fashion by solving smaller instances first
Dynamic Programming
The Binomial Coefficient:
Definition:
n
k
n
k n k
for k n
|
\

|
.
| =

s s
!
!( )!
0
Algorithm Design: Arasteh 6
Recursive Definition:
n
k
n
k
n
k
k n
k or k n
|
\

|
.
| =

|
\

|
.
| +

|
\

|
.
| < <
= =

1
1
1
0
1 0
Dynamic Programming
The Recursive Algorithm for Binomial Coefficient:
Algorithm Design: Arasteh 7
Dynamic Programming
Analysis of The Recursive Algorithm :
Algorithm Design: Arasteh 8
Dynamic Programming
Analysis of The Recursive Algorithm :
Algorithm Design: Arasteh 9
Dynamic Programming
Binomial Coefficient Using Divide-and-Conquer:
Algorithm Design: Arasteh 10
Dynamic Programming
The steps for constructing a dynamic programming
algorithm for this problem are as follows:
1. Establish a 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
j or j i
[ ][ ]
[ ][ ] [ ][ ]
=
+ < <
= =

1 1 1 0
1 0
Algorithm Design: Arasteh 11
2. Solve an instance of the problem in a bottom-up fashion by computing
the rows in B in sequence starting with the first row
j or j i = =

1 0
Dynamic Programming
The array B used to compute the binomial coefficient:
Algorithm Design: Arasteh 12
Dynamic Programming
B[4][2] =?
B[0][0] = 1
B[1][0] = 1
B[1][1] = 1
Algorithm Design: Arasteh
13
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
Dynamic Programming
Algorithm Design: Arasteh 14
Dynamic Programming
Algorithm Design: Arasteh 15
Dynamic Programming
Algorithm Design: Arasteh 16
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: Arasteh 17
) (
2
) 1 )( 2 2 (
) 1 )( 1 (
2
) 1 (
) 1 ( ) 1 ( ) 1 ( 4 3 2 1
1
nk
k k n
k k n
k k
k k k k
times k n
O e
+ +
= + + +
+
= + + + + + + + + + + + +
+


Dynamic Programming
Floyd's Algorithm for Shortest Paths:
Suppose we have a labeled digraph that gives the
flying time on certain routes connecting cities, and we
wish to construct a table that gives the shortest time
required to fly from any one city to any other.
We now have an instance of the allpairs shortest
paths (APSP) problem.
Algorithm Design: Arasteh 18
paths (APSP) problem.
To state the problem precisely, we are given a directed
graph G = (V, E) in which each arc v w has a
non-negative cost C[v, w].
Dynamic Programming
Floyd's Algorithm for Shortest Paths:
Algorithm Design: Arasteh 19
Dynamic Programming
Floyd's Algorithm for Shortest Paths:
Algorithm Design: Arasteh 20
Dynamic Programming
Floyd's Algorithm for Shortest Paths:
Algorithm Design: Arasteh 21
D
(k)
[i][j] = minimum(D
(k-1)
[i][j], D
(k-1)
[i][k] + D
(k-1)
[k][j])
Dynamic Programming
Floyd's Algorithm for Shortest Paths:
Algorithm Design: Arasteh 22
D
(k)
[i][j] = minimum(D
(k-1)
[i][j], D
(k-1)
[i][k] + D
(k-1)
[k][j])
Dynamic Programming
Floyd's Algorithm for Shortest Paths:
Algorithm Design: Arasteh 23
Dynamic Programming
Floyd's Algorithm for Shortest Paths:
Algorithm Design: Arasteh 24
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] )
= minimum(, 3 + 1) = 4
Algorithm Design: Arasteh 25
= minimum(, 3 + 1) = 4
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
Dynamic Programming
Floyd's Algorithm for Shortest Paths:
Algorithm Design: Arasteh 26
Dynamic Programming
Analysis of Floyd's Algorithm for Shortest Paths:
T(n) = n n n = n
3
e O(n
3
)
Algorithm Design: Arasteh 27
Dynamic Programming
Example of Previous Graph:
Algorithm Design: Arasteh 28
Dynamic Programming
Example :
Algorithm Design: Arasteh 29
Dynamic Programming
Example :
Algorithm Design: Arasteh 30
Dynamic Programming
Example :
Algorithm Design: Arasteh 31
Dynamic Programming
Print Shortest Path:
Algorithm Design: Arasteh 32
Dynamic Programming
Chained Matrix Multiplication :
Suppose we want to multiply a 2 2 matrix times a 3 4 matrix as
follows:
Algorithm Design: Arasteh 33
In general, to multiply an i j matrix times a j k matrix using the
standard method, it is necessary to do
Dynamic Programming
Chained Matrix Multiplication :
Consider the multiplication of the following four matrices:
Algorithm Design: Arasteh 34
we have the following number of elementary for each order
Dynamic Programming
Example: Suppose we have the following six matrices:
A
1
A
2
A
3
A
4
A
5
A
6
5 2 23 34 46 67 78
d
0
d
1
d
1
d
2
d
2
d
3
d
3
d
4
d
4
d
5
d
5
d
6
M[4][6] = ?
To multiply A
4
, A
5
, and A
6
, we have the following two orders and
numbers of elementary multiplications:
Algorithm Design: Arasteh 35
numbers of elementary multiplications:
Dynamic Programming
The optimal order for multiplying six matrices must have one of
these factorizations:
1. A
1
( A
2
A
3
A
4
A
5
A
6
)
2. ( A
1
A
2
) ( A
3
A
4
A
5
A
6
)
3. ( A
1
A
2
A
3
) ( A
4
A
5
A
6
)
4. ( A
1
A
2
A
3
A
4
) ( A
5
A
6
)
5. ( A
1
A
2
A
3
A
4
A
5
) A
6
Algorithm Design: Arasteh 36
5. ( A
1
A
2
A
3
A
4
A
5
) A
6
The number of multiplications for the kth factorization is
M[1][k] + M[k+1][6] + d
0
d
k
d
6
We have established that:
) ] 6 ][ 1 [ ] ][ 1 [ ( min ] 6 ][ 1 [
6 0
5 1
d d d
k M k M imum M
k
k
+ + + =
s s
Dynamic Programming

=
< + + + =

s s
0 ] ][ [
), ] ][ 1 [ ] ][ [ ( min ] ][ [
1
1
i i M
j i if
d d d
j k M k i M imum j i M
j k i
j k i
Algorithm Design: Arasteh 37
Dynamic Programming
M[1][2], M[2][3]=?
Algorithm Design: Arasteh 38
Dynamic Programming
M[1][2], M[2][3]=?
Algorithm Design: Arasteh 39
Dynamic Programming
Algorithm Design: Arasteh 40
Dynamic Programming
Analysis:
) (
6
) 1 )( 1 (
] ) [(
3
1
n
n n n
diagonal diagonal n
n
O e
+
=

Algorithm Design: Arasteh 41


) (
6
] ) [(
1
n
diagonal diagonal n
diagonal
O e =

=
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 by
a road.
To minimize travel time, we want to determine a
Algorithm Design: Arasteh 42
To minimize travel time, we want to determine a
shortest route that starts at the salesperson's home
city, visits each of the cities once, and ends up at the
home city.
This problem of determining a shortest route is
called the Traveling Salesperson problem.
Dynamic Programming
The Traveling Salesperson Problem (TSP):
A tour (also called a Hamiltonian circuit) in a
directed graph is a path from a vertex to itself that
passes through each of the other vertices exactly
once.
An optimal tour in a weighted, directed graph is
Algorithm Design: Arasteh 43
An optimal tour in a weighted, directed graph is
such a path of minimum length.
The Traveling Salesperson problem is to find an
optimal tour in a weighted, directed graph when at
least one tour exists.
Dynamic Programming
Example (TSP):
Algorithm Design: Arasteh 44
Dynamic Programming
TSP:
We solved this instance by simply considering all
possible tours.
In general, there can be an edge from every vertex to
every other vertex.
If we consider all possible tours, the second vertex on
Algorithm Design: Arasteh 45
If we consider all possible tours, the second vertex on
the tour can be any of n 1 vertices, the third 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
Dynamic Programming
TSP:
Algorithm Design: Arasteh 46
Dynamic Programming
TSP:
Algorithm Design: Arasteh 47
Dynamic Programming
TSP:
A={v }
Algorithm Design: Arasteh 48
A={v
3
}
D[v
2
][A] = length[v
2
, v
3
, v
1
] =
A={v
3,
v
4
}
D[v
2
][A] = minimum( length[v
2
, v
3
, v
4,
v
1
], length[v
2
, v
4
, v
3,
v
1
]
= minimum(20, ) = 20
Dynamic Programming
TSP:
Optimal Tour length:
}]) , { ][ [ ] ][ 1 [ (
1
2
min v v v imum
j j
n j
V D j W +
s s
Algorithm Design: Arasteh 49
in general for i 1 and v
i
not in A:
] 1 ][ [ ] ][ [
}]) { ][ [ ] ][ [ ( ] ][ [
min
:
i W D
A if A D j i W
v
A D
v
v v imum v
i
j j
A j
i
j
=
= + =
e
o
o
Dynamic Programming
TSP:
Algorithm Design: Arasteh 50
Dynamic Programming
TSP:
Algorithm Design: Arasteh 51
Dynamic Programming
TSP:
Algorithm Design: Arasteh 52
Dynamic Programming
Analysis of TSP:
2
1
1

=
=
|
|
.
|

\
|

n
n
k
n
k
n
k
|
|

|

= |
|

|
1 n n
Algorithm Design: Arasteh 53
|
|
.
|

\
|

=
|
|
.
|

\
|
1
1
k
n
n
k
n
k
2
1
1
1
1
1
1 1 1

= = =
=
|
|
.
|

\
|

=
|
|
.
|

\
|

=
|
|
.
|

\
|

n
n
k
n
k
n
k
n
k
n
n
k
n
n
k
n
k
Dynamic Programming
Analysis of TSP:
Time Complexity:
T(n) = (n-1)(n-2)2
n-3
e O(n
2
2
n
)
Memory Complexity:
M(n) = 2 n2
n-1
= n2
n
e O(n2
n
)
Algorithm Design: Arasteh 54
End
Algorithm Design: Arasteh 55
End

Вам также может понравиться