Sie sind auf Seite 1von 20

Analysis of algorithms

Dynamic Programming Algorithms




Dr. Abeer Hamdy
What Is Dynamic Programming?
Dynamic programming is an algorithm design technique that was
invented in 1957 by a prominent U.S. mathematician Richard
Bellman as a general method for optimizing multistage decision
processes.
The word programming in the name of this technique stands
for planning or a series of choices and does not refer to
computer programming. The word dynamic conveys the idea
that the choices may depend on the current state, rather than
being decided ahead of time.

Originally a tool of applied mathematics designed for optimization
problems, in computer science it is considered as a general
algorithm design technique which is not limited to optimization
problems
What Is Dynamic Programming?
Dynamic programming is a technique for solving problems
using solutions to smaller instances of the problem.
These sub-problems are overlapping.
A recurrence relating the solution to the given
problem with solutions to its smaller sub-problems of
the same type should exist.
Rather than solving overlapping sub-problems again
and again, dynamic programming solves each of the
smaller sub-problems only once and stores the results
in a table from which the solution to the original
problem can be obtained (bottom-up dynamic
programming)
Dynamic programming is similar to divide and conquer in
the sense that it is based on a recursive division of a
problem instance into smaller or simpler problem
instances.

Divide and conquer algorithms often use a top-down
approach (working from the larger problem down to the
smaller problem).

Dynamic programming algorithms invariably proceed in a
bottom-up fashion, by solving all of the simplest problem
instances before combining them into more complicated
problem instances.
What Is Dynamic Programming?

It is an example of applying dynamic
programming to a non- optimization problem.

Fibonacci numbers can be defined by the
following recursive function:



The Fibonacci numbers are: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34,
55, 89,


Fibonacci Sequence

> +
= =
=
1 ) 2 ( ) 1 (
1 0
) (
n if n Fibonacci n Fibonacci
n or n if n
n Fibonacci

Algorithm F(n)

if n = 0 || n=1
return n

return F(n-1) + F(n-2)

Complexity:
T(n) ~ 2T(n-1)+1

T(n) = O(2
n
)
Fibonacci Sequence Recursive algorithm
F4 F3
F3 F2 F2
F2 F1 F1 F0
F1 F0
F5
F1
F2
F1 F0
If we could store the list of all pre-computed values !!
Recursive Tree
Linear Time Iterative:
Algorithm Fibonacci(n)
if n=0||n=1 return n

F[0] 0 // F is a list of n+1 elements.
F[1] 1
for i 2 to n do
F[i] F[i-1] + F[i-2]
return F[n]
Complexity
T(n)=O(n)
Fibonacci Sequence Efficient Algorithm
Linear Time Iterative:

Algorithm Fibonacci(n)
if n=0||n=1 return n

F2 1
F1 0
for i 2 to n do
F F1 + F2
F1 F2
F2 F
return F
Fibonacci Sequence Efficient Algorithm 2
Complexity
T(n)=O(n)
Computing A Binomial Coefficient
Computing a binomial coefficient is a another
example of applying dynamic programming to a non-
optimization problem.
Recall that the binomial coefficient, is the number
of combinations (subsets) of k elements from an n-
element set (0 s k s n) and is denoted as:
C(n,k) or
The name binomial coefficient comes from the
participation of these numbers in the so-called
binomial formula:

( ) ( ) ( ) ( )
n i i n n
n
b n , n C b a i , n C a 0 , n C b a + + + + = +


|
|
.
|

\
|
k
n
Computing A Binomial Coefficient
One of the properties of binomial coefficient, we need to
concentrate on, is:





Lets consider the case of computing C(5,3) using this
recurrence.
C(5,3) = C(4,2) + C(4,3) , i.e.

< <
|
.
|

\
|

+
|
.
|

\
|

= =
=
|
.
|

\
|
n k 0 if
k
1 n
1 k
1 n
n k or 0 k if 1
k
n
|
|
.
|

\
|
+
|
|
.
|

\
|
=
|
|
.
|

\
|
3
4
2
4
3
5
Computing A Binomial Coefficient
|
.
|

\
|
+
|
.
|

\
|
=
|
.
|

\
|
3
4
2
4
3
5
|
.
|

\
|
+
|
.
|

\
|
=
|
.
|

\
|
2
3
1
3
2
4
|
.
|

\
|
+
|
.
|

\
|
=
|
.
|

\
|
3
3
2
3
3
4
|
.
|

\
|
+
|
.
|

\
|
=
|
.
|

\
|
1
2
0
2
1
3
|
.
|

\
|
+
|
.
|

\
|
=
|
.
|

\
|
2
2
1
2
2
3
|
.
|

\
|
+
|
.
|

\
|
=
|
.
|

\
|
2
2
1
2
2
3
1
|
.
|

\
|
+
|
.
|

\
|
=
|
.
|

\
|
1
1
0
1
1
2
1
1 1
|
.
|

\
|
+
|
.
|

\
|
=
|
.
|

\
|
1
1
0
1
1
2
1
1 1
|
.
|

\
|
+
|
.
|

\
|
=
|
.
|

\
|
1
1
0
1
1
2
1
1 1
Computing A Binomial Coefficient
The nature of the recurrence , two slides back,
which expresses the problem of computing C(n, k) in
terms of the smaller and overlapping problems of
computing C(n-1, k-1) and C(n-1, k), lends itself to
solving using the dynamic programming approach.
To do this, well record the values of the binomial
coefficients in a matrix of n+1 rows and k+1
columns, numbered from 0 to n and 0 to k,
respectively.
The dynamic programming algorithm to solve the
binomial coefficient problem is given on the next
slide, followed by an example computing C(5,3).

Computing A Binomial Coefficient
Algorithm Binomial
//Computes C(n,k) using dynamic programming
//Input: non-negative integers n k 0
//Output: C(n,k)
for i 0 to n do
for j 0 to min(i, k) do
if j = 0 or j = k
c[i,j] 1
else C[i,j] C[i-1,j-1] + C[i-1,j]
Return C[n,k]

To compute C(n,k), the
matrix is filled row by row,
starting with row 0 and
ending with row n. Each
row i (0 s i s n) is filled
left to right, starting with 1
because C(n,0) = 1.
Rows 0 through k also
end with 1 on the matrix
diagonal: C(i, i) = 1 for 0 s
i s k. The other values in
the matrix are computed
by adding the contents of
the cells in the preceding
row and the previous
column and in the
preceding row and the
same column.
Computing A Binomial Coefficient
|
|
.
|

\
|

1 k
1 n
|
|
.
|

\
|
k
1 n
|
|
.
|

\
|
k
n
For general C(n,k)
case
Computing A Binomial Coefficient
|
|
.
|

\
|
3
5
|
|
.
|

\
|
2
4
|
|
.
|

\
|
3
4
|
|
.
|

\
|
1
3
|
|
.
|

\
|
2
3
|
|
.
|

\
|
1
2
+
+ +
+
+
+
|
|
.
|

\
|
1
4
|
|
.
|

\
|
2
5
|
|
.
|

\
|
1
5
+
+ +
Computing A Binomial Coefficient
+
+ +
+
+
+
Pascals
Triangle ??
+
+ +
Computing A Binomial Coefficient
The previous dynamic program algorithm for
computing Binomial coefficients is the most straight
forward one, an entire two dimensional array is
created to store the coefficients.

However, 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.

Another improvement to the algorithm would be to
take advantage of the fact that:
C(n,k) = C(n, n-k)
Dynamic Programming summary
Dynamic programming is a bottom-up approach for
solving optimization and non optimization problems.

The steps in the development of a dynamic
programming algorithm is as follows:
1. Establish a recursive property that gives the
solution to an instance of the problem in terms of
smaller instances.
2. Solve an instance of the problem in a bottom up
fashion by solving smaller instances first, store them
, reuse to get solution of the original instance.
3. Revise the algorithm and do it better ,if possible,
by removing the allocated space that is not needed.
Dynamic programming - summary
It provides an iterative solution to the recursive
problem.
Recursion is efficient in case of disjoint sub-problems,
in case of overlapping sub-problems dynamic
programming is recommended.
It generally reduces the complexity of exponential
problem to polynomial problem.
The challenge typically lies in figuring out what smaller
sub-instances need to be considered and in deriving an
equation relates the solution to any instance with
solutions to its smaller sub-instances.

Das könnte Ihnen auch gefallen