Dynamic Programming - PowerPoint PPT Presentation

About This Presentation
Title:

Dynamic Programming

Description:

If a mailbox can withstand x fire-crackers, it can also withstand x - 1 fire-crackers ... How many fire-crackers do you need to guarantee you will find out the smallest ... – PowerPoint PPT presentation

Number of Views:142
Avg rating:3.0/5.0
Slides: 19
Provided by: Owne414
Category:

less

Transcript and Presenter's Notes

Title: Dynamic Programming


1
Dynamic Programming
  • Day 3 Examples

2
Longest Common Subsequence
  • Given two sequences of characters A, B
  • Find the longest sequence C such that C is a
    subsequence of A as well as of B
  • Example
  • ABCDGH
  • ZBAEDH
  • Length of LCS 3

3
Longest Common Subsequence Iterative DP
-- A B C D G H
-- 0 0 0 0 0 0 0
Z 0
B 0
A 0
E 0
D 0
A 0
0
0
1
1
1
1
4
Longest Common SubsequenceIterative DP
-- A B C D G H
-- 0 0 0 0 0 0 0
Z 0 0 0 0 0 0 0
B 0 0 1 1 1 1 1
A 0 1 1 1 1 1 1
E 0 1 1 1 1 1 1
D 0 1 1 1 2 2 2
H 0 1 1 1 2 2 3
5
Longest Common Subsequence Length (UVA 10405)
Iterative DP
  • int a10101010
  • int main()
  • ...
  • memset(a, 0, sizeof(a))
  • for(int i 0 i lt str1.size() i)
  • for(int j 0 j lt str2.size() j)
  • if (str1i str2j)
  • ai1j1 gt? aij 1
  • ai1j1 gt? ai1j
  • ai1j1 gt? aij1
  • cout ltlt astr1.size()str2.size() ltlt
    endl

6
Longest Common Subsequence Length (UVA 10405) -
Memoization
  • int lcs(int a, int b)
  • if (cacheab gt 0) return cacheab
  • if (a str1.size() b str2.size())
  • return 0
  • int ret 0
  • if (str1a str2b)
  • ret 1 lcs(a1, b1)
  • return cacheab max(ret,
  • lcs(a, b 1),
  • lcs(a 1, b))

7
Longest Common SubsequencePath Extraction (UVA
531)
  • It is wonderful that we know how to compute the
    length of the longest common subsequence, BUT
  • How do we find out what the subsequence really
    is?
  • In a more general case, we often need to find out
    not only the quality of the optimal solution,
    but also what it is
  • How to do this? Lets look at our DP table again.

8
Longest Common SubsequencePath Extraction (UVA
531)
-- A B C D G H
-- 0 0 0 0 0 0 0
Z 0 0 0 0 0 0 0
B 0 0 1 1 1 1 1
A 0 1 1 1 1 1 1
E 0 1 1 1 1 1 1
D 0 1 1 1 2 2 2
H 0 1 1 1 2 2 3
9
Longest Common SubsequencePath Extraction (UVA
531)
  • We can trace our way back through the DP table
  • vectorltstringgt path
  • int i str1.size() - 1, j str2.size() - 1
  • while (i gt 0 j gt 0)
  • if (ai1j1 aij1) i--
  • else if (ai1j1 ai1j) j--
  • else
  • path.push_back(str1i)
  • i-- j--
  • reverse(path.begin(), path.end())

10
Critical Mass(UVA 580)
  • 3 types of boxes lead and uranium
  • Stacking boxes on top of each other
  • Stack is dangerous if 3 or more uranium boxes on
    top of each other
  • How many dangerous stacks of height H are there?
  • For example, 3 dangerous stacks if H 4
  • LUUU
  • UUUL
  • UUUU

11
Step 1 Identify the sub-problem
  • Modify the definition of a dangerous stack
  • Stack is dangerous if
  • It contains 3 adjacent uranium boxes
  • OR
  • It contains K adjacent uranium boxes at the
    bottom (K lt 3)

12
Step 2 Form a Recursive Solution
  • int doit(int height, int bottom)
  • if (bottom 0) return 1 ltlt height
  • if (height 0) return 0
  • return doit(height - 1, 3)
  • doit(height - 1, bottom - 1)

13
Step 3 Recursion -gt DP
  • int doit(int height, int bottom)
  • if (bottom 0) return 1 ltlt height
  • if (height 0) return 0
  • if (cacheheightbottom gt 0)
  • return cacheheightbottom
  • return cacheheightbottom
  • doit(height - 1, 3)
  • doit(height - 1, bottom - 1)

14
Headmasters Headache(UVA 10817)

15
Headmasters Headache(UVA 10817)
  • S subjects, 1 lt S lt 8
  • M teachers you have, each teaches some of the
    subjects, 1 lt M lt 20
  • N applicants from whom you can hire, each teaches
    some of the subjects 1 lt N lt 100
  • Each teacher and each applicant has a salary they
    require
  • What is the minimum budget the headmaster needs
    so that each subject is taught by at least 2
    teachers?

16
Headmasters Headache(UVA 10817)
Name Must Hire? Salary Math? Phys? Chem?
Anna YES 55,000 YES
Bob YES 50,000 YES
Carl YES 60,000 YES
Donald 75,000 YES YES
Emil 90,000 YES YES
Fred 50,000 YES
17
Questions to Think About
  • What are different possible representation of the
    sub-problem? What are the advantages and
    disadvantages of each?
  • Would you prefer to solve this problem using
    memoization or iterative DP? What are the
    advantages and disadvantages of each?

18
Mailbox Manufacturers Problem (UVA 882)
  • You have 1 lt k lt 10 mailboxes
  • At most 100 crackers fit into a mailbox
  • If a mailbox can withstand x fire-crackers, it
    can also withstand x - 1 fire-crackers
  • After explosion, a mailbox is either totally
    destroyed or unharmed
  • How many fire-crackers do you need to guarantee
    you will find out the smallest number of crackers
    that blow up a mailbox?
Write a Comment
User Comments (0)
About PowerShow.com