Dynamic Programming Part One - PowerPoint PPT Presentation

1 / 52
About This Presentation
Title:

Dynamic Programming Part One

Description:

The number of 'usual' multiplications needed to multiply a n m matrix and a m r matrix is nmr ... many different orders of multiplications are there? This can ... – PowerPoint PPT presentation

Number of Views:87
Avg rating:3.0/5.0
Slides: 53
Provided by: hkoi
Category:

less

Transcript and Presenter's Notes

Title: Dynamic Programming Part One


1
Dynamic ProgrammingPart One
  • HKOI Training Team 2004

2
Recurrences
  • Function defined in terms of itself
  • M(X) X M(X-1)
  • Fn2 Fn1 Fn
  • Kn1 maxKn-1100, Kn
  • One or more base cases are needed
  • M(0) 1
  • F0 1, F1 1
  • K10 100, K11 177

3
Evaluating recurrences
  • Combination
  • C(n, r) C(n-1, r) C(n-1, r-1)
  • C(n, 0) C(n, n) 1
  • Algorithm
  • function C(n, r)
  • if (r0) or (rn)
  • return 1
  • else
  • return C(n-1, r) C(n-1, r-1)

4
Slow...
5
A table...
C(5, 2)
C(4, 2) C(4, 1)
C(3, 2) C(3, 1) C(3, 0)
C(2, 2) C(2, 1) C(2, 0)
C(1, 1) C(1, 0)
6
Redundant calculations
7
Fast...
8
Dynamic programming
  • Abbreviation DP / DyP
  • Not particularly related to programming
  • As in Linear Programming!
  • An approach (paradigm) for solving certain kinds
    of problems
  • Not a particular algorithm
  • Usually applied on recurrences

9
Principles of DP
  • Evaluating recurrences may sometimes be slow
    (exponential time)
  • Accelerate by avoiding redundant calculations!
  • Memorize previously calculated values
  • Usually applied to problems with
  • a recurrence relation
  • overlapping subproblems
  • solutions exhibiting optimal substructure

10
Why DP?
  • Reduce runtime
  • In the previous computation of C(n, k), how many
    times is the function invoked?
  • Slow algorithm 2C(n,k) 1 exponential in n
  • Fast algorithm O(nk) polynomial in n
  • Tradeoff memory
  • In the C(n, k) problem, the memory requirement is
    O(n) using pure recursion O(nk) is required for
    memorization
  • In fact, the O(nk) bound can be improved

11
Classification of problems
  • Induction
  • Evaluation of a certain function is the main
    concern
  • Examples
  • The N-th Fibonacci number
  • Number of different binary trees with N nodes
  • Optimization
  • Maximization or minimization of certain function
    is the objective
  • Examples
  • Shortest paths
  • Activity scheduling (maximize no. of activities)

12
Triangle (IOI 94)
  • Given a triangle with N levels like the one on
    the left, find a path with maximum sum from the
    top to the bottom
  • Only the sum is required

13
Triangle (analysis)
  • Exhaustion?
  • How many paths are there in total?
  • Greedy?
  • It doesnt work. Why?
  • Graph problem?
  • Possible, but not simple enough

14
Triangle (formulation)
  • Let Aij denote the number in the i-th row and
    j-th column, (i, j)
  • Let Fij denote the sum of the numbers on a
    path with max sum from (1, 1) to (i, j)
  • Answer maximum of FN1, FN2, , FNN

15
Triangle (formulation)
  • Base case F11 A11
  • Progress (i gt 1, 1 lt j lt i)
  • Fi1 Fi-11Ai1
  • Fii Fi-1i-1Aii
  • Fij maxFi-1j-1,Fi-1jAij

16
Triangle (order of calc.)
  • Fi depends on Fi-1
  • Compute F row by row, from top to bottom

17
Triangle (algorithm)
  • Algorithm
  • F11 ? A11
  • for i ? 2 to N do
  • Fi1 Fi-11Ai1
  • Fii Fi-1i-1Aii
  • for j ? 2 to i-1 do
  • Fij ? maxFij,Fij-1
  • Aij
  • answer ? maxFN1, , FNN

18
Triangle (complexity)
  • Number of array entries to be computed about
    N(N-1)/2
  • Time for computing one entry O(1)
  • Thus the total time complexity is O(N2)
  • Memory complexity O(N2)
  • This can be reduced to O(N) since Fi only
    depends on Fi-1
  • We can discard some previous entries after
    starting on a new row

19
Longest Common Subsequence
  • Given two strings A and B of length n and m
    respectively, find their longest common
    subsequence (NOT substring)
  • Example
  • A aabcaabcaadyyyefg
  • B cdfehgjaefazadxex
  • LCS caaade
  • Explanation
  • A a a b c a a b c a a d y y y e f g
  • B c d f e h g j a e f a z a d x e x

20
LCS (analysis)
  • Exhaustion
  • Number of subsequences of A 2n
  • Number of subsequences of B 2m
  • Exponential time!
  • Observation
  • If a LCS of A and B is nonempty then it must have
    a last character (trivial)

A a a b c a a b c a a d y y y e f g B c d f e
h g j a e f a z a d x e x
21
LCS (analysis)
  • Observation
  • Suppose the last LCS character corresponds to
    Ai and Bj
  • Removing the last LCS character results in a LCS
    of A1..i-1 and B1..j-1

22
LCS (formulation)
  • Thus it is reasonable to define Fij as the
    length of any LCS of A1..i and B1..j
  • Base cases
  • Fi0 0 for all i
  • F0j 0 for all j
  • Progress (0 lt i lt n, 0 lt j lt m)
  • Fij Fi-1j-1 1 if AiBj
  • Fij maxFi-1j, Fij-1 else

23
LCS (order of calc.)
  • Fij depends on Fab, a i and b j
  • Order from top to bottom, from left to right
  • There are some other possible orders!

0 1 2 3 4
0
1
2
3
24
LCS (complexity)
  • Time complexity O(NM)
  • Memory complexity O(NM)
  • Again, this can be reduced to O(NM), but why and
    how?

25
Coins
  • Given an unlimited supply of 2, 3 and 5 coins,
    find the number of different ways to form a
    denomination of N
  • Example
  • N 10
  • 2, 2, 2, 2, 2, 2, 2, 3, 3, 2, 3, 5,
  • 5, 5
  • Solve this induction problem by yourself

26
Matrix Chain Multiplication
  • A matrix is a rectangle of numbers
  • The number of usual multiplications needed to
    multiply a nm matrix and a mr matrix is nmr
  • Matrix multiplication is associative
  • (AB)C A(BC)

A matrix of size 23
27
Matrix Chain Multiplication
  • Given a sequence of matrices A1, A2, , An, find
    an order of multiplications such that the total
    number of usual multiplications is minimized
  • Example
  • A 37, B 75, C 52
  • (AB)C takes 375352 135 muls
  • A(BC) takes 752372 112 muls

28
MCM (analysis)
  • How many different orders of multiplications are
    there?
  • This can be solved by DP!
  • Does greedy work?
  • Lets have a look at the brackets
  • (A((BC)((DE)F)))(G((HI)J))
  • There must be one last multiplication
  • (A((BC)((DE)F)))(G((HI)J))
  • Every subexpression with more than one matrix has
    one last multiplication

29
MCM (analysis)
  • Optimal substructure
  • If the last multiplication is (A1A2Ai)(Ai1AN)
    and the multiplication scheme is optimal, then
    the multiplication schemes for A1A2Ai and
    Ai1AN must also be optimal

30
MCM (formulation)
  • Let ri and ci be the number of rows and the
    number of columns of Ai respectively
  • Let Fij be the minimum number of usual
    multiplications required to compute AiAi1Aj
  • Base case Fii 0 for all i
  • Progress for all i lt j
  • Fij minFikFk1j
  • rickcj

i k lt j
31
MCM (order of calc.)
  • F with fewer matrices first

j, i 1 2 3 4 5 6
1
2
3
4
5
6
first
last
32
MCM (alternative)
  • Let Gij be the minimum number of usual
    multiplications required to compute AiAi1Aij-1
  • Base case Gi1 0 for all i
  • Progress for all 1 lt j lt N-i
  • Gij minGikGikj-k
  • ririkcij-1
  • Explain!!

1 k lt j
33
MCM (alternative)
  • Still, G with fewer matrices first

i, j 1 2 3 4 5 6
1
2
3
4
5
6
last
first
34
MCM (algorithm)
  • Bottom-up algorithm (algorithm 2)
  • for i ? 1 to N do
  • Gi1 ? 0
  • for j ? 2 to N do
  • for i ? 1 to N-j1
  • Gij ? minGik
  • Gikj-k
  • ririkcij-1
  • answer ? G1N

1 k lt j
35
MCM algorithm)
  • Top-down algorithm (algorithm 1)
  • function MCM_DP(i, j)
  • if Fij is not 8, return Fij
  • m ? 8
  • for k ? i to j-1 do
  • m ? minm,
  • MCM_DP(i, k)
  • MCM_DP(k1, j)
  • rickcj
  • Fij ? m
  • return Fij

36
MCM (algorithm)
  • Top-down algorithm (main)
  • initialize everything in F to 8
  • for i ? 1 to N do
  • Fii ? 0
  • answer ? MCM_DP(1, N)

37
MCM (complexity)
  • Number of array entries to be computed about
    N(N-1)/2
  • Number of references to other entries when
    computing one single entry
  • It varies
  • On the average, about N/2
  • Thus the total time complexity is O(N3)

38
Fishing
  • There are N fish ponds and you are going to spend
    M minutes on fishing
  • Given the time-reward relationship of each pond,
    determine the time you should spend at each pond
    in order to get the biggest reward

time/pond 1 2 3
1 minute 0 fish 2 fish 1 fish
2 minutes 3 fish 2 fish 2 fish
3 minutes 3 fish 4 fish 4 fish
39
Fishing (example)
  • For example, if N3, M3 and the relationships
    are given in the previous slide, then the optimal
    schedule is
  • Pond 1 2 minutes
  • Pond 2 1 minute
  • Pond 3 0 minute
  • Reward 5 fish

40
Fishing (analysis)
  • You can think of yourself visiting ponds 1, 2, 3,
    , N in order
  • Why?
  • Suppose in an optimal schedule you spend K
    minutes on fishing at pond 1
  • So you have M-K minutes to spend at the remaining
    N-1 ponds
  • The problem is reduced
  • But how can I know what is K?
  • You dont know, so try all possible values!

41
Fishing (formulation)
  • Let Fij be the maximum reward you can get by
    spending j minutes at the first i ponds
  • Base cases 0 i, j N
  • Fi0 0
  • F0j 0
  • Progress 1 i N, 1 j M
  • Fij maxFi-1kRij-k

0 k j
42
Brackets
  • A balanced-bracket expression (BBE) is defined as
    follows
  • The empty string is a BBE
  • If X and Y are BBEs then (X) and XY BBEs
  • Nothing else is a BBE
  • For example, (), (()), ()(()())(()) are BBEs
    while ((), )(, (()()))()() are not BBEs
  • Find the number of different BBEs with exactly N
    pairs of brackets

43
Brackets (analysis)
  • Obviously this is an induction problem
  • Listing out all BBEs with N pairs of brackets is
    not a good idea
  • What makes a bracket expression not a BBE?
  • the numbers of opening and closing brackets do
    not match
  • the number of closing brackets exceeds the number
    of opening brackets at some point during a scan
    from left to right

44
Brackets (analysis)
  • Clearly only the second rule matters in this
    problem
  • Intuitively it is easy to construct a BBE from
    left to right
  • We call a bracket expression a BBE-prefix (BP) if
    the number of closing brackets never exceeds the
    number of opening brackets during a scan from
    left to right

45
Brackets (formulation)
  • Let Fij be the number of BPs with i opening
    brackets and j closing brackets (thus of length
    ij)
  • Base cases
  • F00 1
  • Fij 0 for all 0 i lt j N
  • Progress (0 lt j i N)
  • Fij Fi-1jFij-1

46
Polygon Triangulation
  • Given an N-sided convex polygon A, find a
    triangulation scheme with minimum total cut length

47
Polygon (analysis)
  • Every edge of A belongs to exactly one triangle
    resulting from the triangulation
  • We get two (or one) smaller polygons after
    deleting a triangle

48
Polygon (analysis)
  • The order of cutting does not matter
  • Optimal substructure
  • If the cutting scheme for A is optimal, then the
    cutting schemes for B and C must also be optimal

49
Polygon (formulation)
  • Take this problem as an exercise
  • A small hint similar to Matrix Chain
    Multiplication
  • Nothing more

50
Summary
  • Remember, DP is just a technique, not a
    particular algorithm
  • The problems we have discussed are quite
    straightforward that you should be able to know
    they can be solved by DP with little inspection
  • The DP problems in NOI and IOI are much harder
  • They are well disguised
  • Looking at a wide variety of DP problems seems to
    be the only way to master DP

51
Looking forward
  • Hopefully the trainer responsible for Advanced DP
    II will talk about DP on non-rectangular
    structures, such as trees and graphs
  • The problems discussed will be more complicated
    than those you have just seen, so please be
    prepared

52
The end
The last thing
LETS HAVE LUNCH!!!
Write a Comment
User Comments (0)
About PowerShow.com