# Subset Dynamic Programming

### Presentations text content in Subset Dynamic Programming

Subset Dynamic Programming

Kevin Choi

Slide2In this presentation:

What is subset DP?A simple soccer example.Implementing bitmasks.Code the soccer example.Try it yourself.

2

Contents.

000000010111

-DP

Slide3What is subset DP?

3

In normal DP:

Problem has

numeric axis

:

e.g.

(including 0)

Problem has

boundary cases

:

e.g. and Problem has optimal substructure:e.g.

0

0

0

0

1

0

0

0

1

4

0

0

1

3

6

0

1

2

3

4

1

1

1

1

1

Slide4What is subset DP?

4

In subset DP:

One or more axis are not numbers but are subsets.e.g. Problem still has boundary cases.e.g. if is empty (and 0 otherwise)Problem still has an optimal substructure:e.g.

0

0

0

6

36

150

0

0

2

12

50

196

0

0

2

12

50

196

0

1

5

19

73

235

0

0

2

12

50

196

0

1

5

19

73

235

0

1

5

19

73

235

1

3

9

27

81

243

Slide5Suppose King Arthur the new coach of the Dutch soccer team…

After dramatic EC qualification, radical reforms necessary.Strategy: divide field in positions.

A simple soccer example.

5

1

2

3

4

5

Slide6For each of the available players, assign an integer between 0 and 1000 for each position, representing the quality.

A simple soccer example.

6

player

pos1

pos2

pos3

pos4

pos5

…

The King

1000

1000100010001000…Godfather800300200700500…Memphis00000……………………

How can King Arthur use his DP skills to maximize the quality on the field?

1

2

3

4

5

Slide7Given positions, players and quality of player at position .What are the two axis of the table?The number of available players .The set of available positions on the field .What is the optimal substructure? indicates no position (so ).What are the boundary cases? for all . for all

A simple soccer example.

7

Slide8Given positions, players and quality of player at position .What would the running time be if we used brute-force?Total number of possibilities: (all places occupied).Running time for each possibility: .Total: .What is the running time if we use subset DP?Table size: by .Running time for each cell: .Total: .

A simple soccer example.

8

Slide9Integers

Discrete values

Subsets

Index positions in arrays

Implementing bitmasks.

9

0

1

2

34

01100101

Able to

switch

between different uses of integers.

−2

−1

0

1

2

Slide10Let , the bitmask representation mask of :Associate each element in to a bit in mask.If element in also in , set corresponding bit to 1.Else, bit remains 0.Store bitmask in integer.Bits have an index:Use rightmost bits to represent sets. Start with rightmost bit.0-based.

Implementing bitmasks.

10

000000010111

0

Slide11Standard set mathematics:Some bitmask operations:Check if element in set:Add element :Remove element :Get first 1 from right:Get first 0 from right:Remove first 1 from right:Add 1 to first 0 from right:

Implementing bitmasks.

11

In java / C++:

mask1 & mask2

mask1

| mask2

mask1

& ~mask2

(mask & 1<<i) > 0

mask |= 1<<i

mask &= ~(1<<i)

mask & -mask

~mask & mask+1

mask -= mask & -mask

mask += ~mask & mask+1

Slide12Recall: 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

Slide13Recall problem:Divide field in positions.For all the players:For each position, assign a quality .Output:The maximized quality.Optional: where the players should stay (not explained)

Code the soccer example.

13

You have 20 lines to solve the problem.

1

2

3

4

5

player

pos1

pos2

pos3

pos4

pos5

…

The King

1000

1000

1000

1000

1000

…

Godfather

800

300

200

700

500

…

Memphis

0

0

0

0

0

…

…

…

…

…

…

…

…

Slide14Recall: problem has 2 axis:The available players ( in total)The subsets of unused positions ( in total)So creating the table should be easy, right?C++: int table [m][1<<n] = {{}};Java: int[][] table = new int[m][1<<n];But DOMjudge verdicts RUN-ERROR for some reason. Why?Table size should be at least m+1 by 1<<n.C++: int table [m+1][1<<n] = {{}};Java: int[][] table = new int[m+1][1<<n];

Code the soccer example.

14

Important: make your arrays big enough!

Slide15Recall: the boundary cases of the problem were: for all . for all One can do (both C++/Java):for (int i=0; i<=m; i++) table[i][0] = 0;for (int S=0; S<(1<<n); S++) table[0][S] = 0; But:you don’t have to code anything .C++: assigning {{}} will fill a 2D int array with 0’sJava: initial value of elements in int array is 0.

Code the soccer example.

15

Note:

in practice, this is ugly, horrific coding style.

Slide16Recall: the optimal substructure was:First initialize :C++:int k [m+1][n+1] = {{}};for (int i=1; i<=m; i++) for (int j=1; j<=n; j++)cin >> k[i][j];Java:int[][] k = new int[m+1][n+1];for (int i=1; i<=m; i++) for (int j=1; j<=n; j++)k[i][j] = sc.nextInt();

Code the soccer example.

16

Slide17Recall: the optimal substructure was:Now we can choose:

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.

Slide18Recall: the optimal substructure was:Inside the double loop:table[m][S] = table[m-1][S];for (int j=1; j<=n; j++) // If java, place ‘{‘ here.{if ((S & 1<<(j-1)) == 0) continue;int alt = k[m][j] + table[m-1][S-(1<<(j-1))];if (alt > table[m][S]) table[m][S] = alt;}

Code the soccer example.

18

Lines used so far:

15

Slide19Other stuff…#include, std (C++), import, Scanner (Java), etc.Input of and :C++: int n, m; cin >> n >> m;Java: int n = sc.nextInt(), m = sc.nextInt(); Output of maximum:C++: cout << table[m][(1<<n)-1];Java: System.out.println(table[m][(1<<n)-1]);If needed: the positions assigned the players.

Code the soccer example.

19

Total number of lines (after some compression):

20

Slide20Try yourself: Traveling Salesman Problem.Given: cities with connections with positive distances.

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?

Slide21Other problems you can try:BAPC 2013 B – BribeEAPC 2010 A – Evolution (hard)

Try it yourself.

21

Any Questions?

Slide22Slide23

Slide24

Slide25

## Subset Dynamic Programming

Download Presentation - The PPT/PDF document "Subset Dynamic Programming" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.