Download Lecture 7: Greedy Algorithms II

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Mathematics of radio engineering wikipedia , lookup

Factorization of polynomials over finite fields wikipedia , lookup

Location arithmetic wikipedia , lookup

Elementary mathematics wikipedia , lookup

Proofs of Fermat's little theorem wikipedia , lookup

Addition wikipedia , lookup

Arithmetic wikipedia , lookup

Weber problem wikipedia , lookup

Longest common subsequence problem wikipedia , lookup

Transcript
Lecture 8:
Dynamic Programming
Shang-Hua Teng
First Example: n choose k
• Many combinatorial problems require the calculation of
the binomial coefficient.
• This is equivalent to given n objects how many different
ways can you choose k of them.
n
n!



• Mathematically we have  k  k!n  k !
 
• or,
 n  1  n  1 0  k  n
 n      
    k  1  k 
k  
1
k 0
k n

Consider Divide and Conquer
int choose(n,k)
if (n = = k) return 1;
if (n < k) or (k < 0) return 0;
return choose(n-1, k-1) + choose(n - k, k);
How long does this take?
T(n,k) = T(n-1, k-1) + T(n-k, k)
(n )
k
Remember Pascal’s Triangle
function choose (n,k)
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
By adding two numbers from previous
row we can calculate the triangle quickly.
Consider triangle shape
0
1
2
3
4
5
0
1
1
1
1
1
1
1 2 3 4 5
1
2
3
4
5
1
3 1
6 4 1
10 10 5 1
• Looks like a two dimensional array.
i
• Create the triangle in this array and output A(i,j) for  
 j
Do it algorithmically
Create an 2-d array A of size (n+1) by (k +1 ).
Set A[i, 0]  1 for i = 0,1,2,.., n
Set A[i, i ]  1 for i = 0,1,2,...., n
for i = 1 to n
for j = 1 to k
A[ i, j ] = A[ i-1, j ] + A[ i –1, j-1 ];
Output A[n,k]
Runtime is O(nk)
Matrix Chain Multiplication - Review
• Recall how to multiply matrices.
• Given two matrices A ( d e ) B ( e f )
e
d
X B
A
f
e
=
d
C
f
e 1
• The product is C[i, j ]   A[i, k ]  B[k , j ]
k 0
• Time is O (def )
Matrix Chaining contd.
• What about multiplying multiple matrices?
A  A1  A 2  A3  A 4  A5
• We know matrix multiplication is associative. Can we use
this to help minimize calculations?
• Sure, consider
• If we multiply
A ( 53) B ( 3100) C (1005)
A  B C
• But instead if we try
we do 4000 operations
A  (B  C)
we do 1575 operations
How do we parenthesize
• Brute force - try all ways to parenthesize
•
•
•
•
A  A1  A 2  A3  A n
Find out which has smallest number of operations by doing
multiplication, then pick the best one.
But how many ways can we parenthesize these?
Equivalent to the number of ways to make a binary tree
with n nodes. This is (2 n ) see Catalan numbers.
See homework Problem 12-4, ex15.2-3 in the book.
Be greedy
• We just learned that a greedy algorithm can sometimes
work, let’s try.
• Why not try starting with the product with the most
operations.
• Consider
A (105) B ( 510)C(105) D( 510)
• If we do our greedy method we would compute
A  B C  D
• This is 2000 operations.
• Try AB  C D instead, which takes 1000
operations.
Another Greedy way
• Select the product with the fewest operations.
• Consider
A (10111) B (119)C(9100) D(10099)

 D
• Here the new greedy method would compute A  B  C
• This is 109989+9900+108900=228789 operations



• Try A  B  C  D
• Here we only need 9999+89991+89100=189090
Optimality of Subproblems
• Consider general question again, we want to parenthesize
A1  A 2  A3  A n
• Now suppose somehow we new that the last multiplication
we need to do was at the ith position.
(A1  A 2  Ai )  (Ai 1  A n )
• Then the problem is the same as knowing the best way to
parenthesize ( A1  A 2  Ai ) and ( A i 1  A n )
Overlapping Subproblems
• We know how to break problem into smaller pieces, why
doesn’t divide and conquer work?
• We need to optimize each piece why doesn’t greedy work.
• Again suppose we know where the final multiply is and we
want to generalize the cost with a recurrence, consider
Ni , j  min Ni ,k  N k 1, j  di d k 1d j 1
i k  j
• But notice these subproblems overlap.
Dynamic Programming
• Since subproblems overlap we can not use divide and
conquer method.
• Instead work from the “bottom up.”
• We know how to parenthesize one matrix, two matrices,
we can build from there…
Matrix Chain Order
n  length p   1
for i  1 to n
do mi, i   0
for l  2 to n
do for i  1 to n  l  1
do j  i  l  1
m[i, j ]  
for k  i to j  1
do q  m[i, k ]  m[ k  1, j ]  pi 1 pk p j
if q  m[i, j ]
then m[i. j ]  q
s[i, j ]  k
return m and s
What have we noticed
• Optimal solution depends on optimal subproblems
• Subproblems overlap.
• So a “top down” divide and conquer doesn’t seem to work.
• Somehow we need to build up from bottom by
remembering solution from subproblem.
Another Example
• Biologists need to measure how similar strands of DNA
are to determine how closely related an organism is to
another.
• They do this by considering DNA as strings of letters
A,C,G,T and then comparing similarities in the strings.
• Formally they look at common subsequences in the strings.
• Example X = AGTCAACGTT, Y=GTTCGACTGTG
• Both S = AGTG and S’=GTCACGT are subsequences
• How to do find these efficiently?
Brute Force
• if |X| = m, |Y| = n, then there are 2m subsequences of x; we
must compare each with Y (n comparisons)
• So the running time of the brute-force algorithm is O(n 2m)
• Notice that the LCS problem has optimal substructure:
solutions of subproblems are parts of the final solution.
• Subproblems: “find LCS of pairs of prefixes of X and Y”
Some observations
• Let X  x1 , x2 , , xm  and Y  y1 , y2 , , yn  be
sequences and let Z  z1 , z 2 , , z k  be any LCS of
X and Y .
• If x  y , then z  x  y and Z is an LCS of X and Y
m
n
k
m
n
k 1
m1
n 1
• If xm  yn , then z k  xm
implies that Z is an LCS of X m 1 and Y
• If xm  yn , then z k  yn
implies that Z is an LCS of X and Yn 1
Setup
• First we’ll find the length of LCS, along the way we will
leave “clues” on finding subsequence.
• Define Xi, Yj to be the prefixes of X and Y of length i and j
respectively.
• Define c[i,j] to be the length of LCS of Xi and Yj
• Then the length of LCS of X and Y will be c[m,n]
The recurrence
• The subproblems overlap, to find LCS we need to find
LCS of c[i, j-1] and of c[i-1, j]

0
if i  0 or j  0

c[i, j ]   c[i  1, j  1]  1
if i, j  0 and xi  y j ,
max( c[i, j  1], c[i  1, j ]) if i, j  0 and x  y
i
j

LCS-Length(X, Y)
m = length(X), n = length(Y)
for i = 1 to m
do c[i, 0] = 0
for j = 0 to n
do c[0, j] = 0
for i = 1 to m
do for j = 1 to n
do if ( xi = = yj )
then c[i, j] = c[i - 1, j - 1] + 1
bi, j   " "
else if c[i - 1, j]>=c[i, j - 1]
then c[i, j] = c[i - 1, j]
bi, j   " "
else c[i, j] = c[i, j - 1]
bi, j   " "
return c and b