# Subset Dynamic Programming  2017-01-14 60K 60 0 0

## Subset Dynamic Programming - Description

Kevin Choi. In this presentation:. What is subset DP. ?. A simple soccer example.. Implementing bitmasks.. Code the soccer example.. Try it yourself.. 2. Contents.. 000000010111. -DP. What is subset DP?. ID: 509684 Download Presentation

Embed code:

## 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.

### Presentations text content in Subset Dynamic Programming

Slide1

Subset Dynamic Programming

Kevin Choi

Slide2

In this presentation:

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

2

Contents.

000000010111

-DP

Slide3

What 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

Slide4

What 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

Slide5

Suppose 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

Slide6

For 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

Slide7

Given 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

Slide8

Given 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

Slide9

Integers

Discrete values

Subsets

Index positions in arrays

9

0

1

2

34

01100101

Able to

switch

between different uses of integers.

−2

−1

0

1

2

Slide10

Let , 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.

10

000000010111

0

Slide11

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

11

In java / C++:

Slide12

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

Slide13

Recall 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

Slide14

Recall: 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!

Slide15

Recall: 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;for (int S=0; S<(1<<n); S++) table[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.

Slide16

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

Slide17

Recall: 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.

Slide18

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

Slide19

Other 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

Slide20

Try 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?

Slide21

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

Try it yourself.

21

Any Questions?

Slide22

Slide23

Slide24

Slide25