Presentation is loading. Please wait.

Presentation is loading. Please wait.

Subset Dynamic Programming Kevin Choi. In this presentation: –What is subset DP? –A simple soccer example. –Implementing bitmasks. –Code the soccer example.

Similar presentations


Presentation on theme: "Subset Dynamic Programming Kevin Choi. In this presentation: –What is subset DP? –A simple soccer example. –Implementing bitmasks. –Code the soccer example."— Presentation transcript:

1 Subset Dynamic Programming Kevin Choi

2 In this presentation: –What is subset DP? –A simple soccer example. –Implementing bitmasks. –Code the soccer example. –Try it yourself. 2Contents. 000000010111 -DP

3 What is subset DP? 3 00001 00014 00136 01234 11111

4 4 000636150 0021250196 0021250196 0151973235 0021250196 0151973235 0151973235 1392781243

5 A simple soccer example. 5 1 2 3 4 5

6 6 playerpos1pos2pos3pos4pos5… The King1000 … Godfather800300200700500… Memphis00000… ………………… How can King Arthur use his DP skills to maximize the quality on the field? 1 2 3 4 5

7 A simple soccer example. 7

8 8

9 Integers Discrete values Subsets Index positions in arrays Implementing bitmasks. 9 01234 01100101 Able to switch between different uses of integers. −2 −1 0 1 2

10 Implementing bitmasks. 10 000000010111 0

11 Implementing bitmasks. 11 In java / C++: – mask1 & mask2 – mask1 | mask2 – mask1 & ~mask2 – (mask & 1 0 – mask |= 1<<i – mask &= ~(1<<i) – mask & -mask – ~mask & mask+1 – mask -= mask & -mask – mask += ~mask & mask+1

12 Recall: King Arthur the new coach of the Dutch soccer team… –To keep a long story short: difference in opinion regarding positions and qualities. –But (angry) King Arthur persists and will implement it anyway! Code the soccer example. 12 1 2 3 4 5

13 Code the soccer example. 13 You have 20 lines to solve the problem. 1 2 3 4 5 playerpos1pos2pos3pos4pos5… The King1000 … Godfather800300200700500… Memphis00000… …………………

14 Code the soccer example. 14 Important: make your arrays big enough!

15 Code the soccer example. 15 Note: in practice, this is ugly, horrific coding style.

16 Code the soccer example. 16

17 Code the soccer example. 17 bool memory [m+1][1<<n] = {{}}; int maxQ(int m, int S) { if (memory[m][S]) return maxQ[m][S]; /* enter here your code */ memory[m][S] = true; return maxQ[m][S]; } for (int i=1; i<=m; i++) for (int S=1; S<(1<<n); S++) { /* enter here your code */ } Memoization:Bottom-top approach: Bottom-top preferred, because: Many subproblems are visited. Order of subproblems doesn’t matter. Less lines needed to implement.

18 Code the soccer example. 18 Lines used so far: 15

19 Code the soccer example. 19 Total number of lines (after some compression): 20

20 Try it yourself. 20 A B C D E F G 2 6 2 7 8 5 5 4 5 3 7 What is the shortest tour and its distance?

21 Other problems you can try: –BAPC 2013 B – Bribe –EAPC 2010 A – Evolution (hard) Try it yourself. 21 Any Questions?


Download ppt "Subset Dynamic Programming Kevin Choi. In this presentation: –What is subset DP? –A simple soccer example. –Implementing bitmasks. –Code the soccer example."

Similar presentations


Ads by Google