+ All Categories
Home > Documents > AlgoPARC - hawaii.edunodari/teaching/f18/lectures/lecture06.pdf · ICS 491: Competitve Programming...

AlgoPARC - hawaii.edunodari/teaching/f18/lectures/lecture06.pdf · ICS 491: Competitve Programming...

Date post: 27-May-2020
Category:
Upload: others
View: 4 times
Download: 0 times
Share this document with a friend
40
ICS 491: Competitve Programming – Lecture 6: Dynamic Programming II ICS 491: Competitive Programming Prof. Nodari Sitchinava www.algoparc.ics.hawaii.edu AlgoPARC Lecture 6: Dynamic Programming II
Transcript
Page 1: AlgoPARC - hawaii.edunodari/teaching/f18/lectures/lecture06.pdf · ICS 491: Competitve Programming Lecture 6: Dynamic Programming II Example: Subset Sum Problem (Subset Sum) . Given

ICS 491: Competitve Programming – Lecture 6: Dynamic Programming II

ICS 491: Competitive ProgrammingProf. Nodari Sitchinava

www.algoparc.ics.hawaii.edu

AlgoPARC

Lecture 6: Dynamic Programming II

Page 2: AlgoPARC - hawaii.edunodari/teaching/f18/lectures/lecture06.pdf · ICS 491: Competitve Programming Lecture 6: Dynamic Programming II Example: Subset Sum Problem (Subset Sum) . Given

ICS 491: Competitve Programming – Lecture 6: Dynamic Programming II

URI OnlineJudge

https://www.urionlinejudge.com.br/

Problems organized by topicNo Cryptojacking

Page 3: AlgoPARC - hawaii.edunodari/teaching/f18/lectures/lecture06.pdf · ICS 491: Competitve Programming Lecture 6: Dynamic Programming II Example: Subset Sum Problem (Subset Sum) . Given

ICS 491: Competitve Programming – Lecture 6: Dynamic Programming II

Weekly Mini-Contest – 75 min

Dynamic Programming

Page 4: AlgoPARC - hawaii.edunodari/teaching/f18/lectures/lecture06.pdf · ICS 491: Competitve Programming Lecture 6: Dynamic Programming II Example: Subset Sum Problem (Subset Sum) . Given

ICS 491: Competitve Programming – Lecture 6: Dynamic Programming II

Solutions

Page 5: AlgoPARC - hawaii.edunodari/teaching/f18/lectures/lecture06.pdf · ICS 491: Competitve Programming Lecture 6: Dynamic Programming II Example: Subset Sum Problem (Subset Sum) . Given

ICS 491: Competitve Programming – Lecture 6: Dynamic Programming II

Dynamic Programming (DP)

Last time: Recursive Complete Search,Pruned with a Lookup (Memo) Table

Page 6: AlgoPARC - hawaii.edunodari/teaching/f18/lectures/lecture06.pdf · ICS 491: Competitve Programming Lecture 6: Dynamic Programming II Example: Subset Sum Problem (Subset Sum) . Given

ICS 491: Competitve Programming – Lecture 6: Dynamic Programming II

Dynamic Programming (DP)

Last time: Recursive Complete Search,Pruned with a Lookup (Memo) Table

Today: Bottom-up DP

Page 7: AlgoPARC - hawaii.edunodari/teaching/f18/lectures/lecture06.pdf · ICS 491: Competitve Programming Lecture 6: Dynamic Programming II Example: Subset Sum Problem (Subset Sum) . Given

ICS 491: Competitve Programming – Lecture 6: Dynamic Programming II

Example: Subset Sum

Problem (Subset Sum). Given a set S of 1 ≤ n ≤ 20 integersand a positive integer x, is there a subset of S that sums to x?

S = {17, 5, 7, 15, 3, 8} x = 16

Page 8: AlgoPARC - hawaii.edunodari/teaching/f18/lectures/lecture06.pdf · ICS 491: Competitve Programming Lecture 6: Dynamic Programming II Example: Subset Sum Problem (Subset Sum) . Given

ICS 491: Competitve Programming – Lecture 6: Dynamic Programming II

Example: Subset Sum

Problem (Subset Sum). Given a set S of 1 ≤ n ≤ 20 integersand a positive integer x, is there a subset of S that sums to x?

S = {17, 5, 7, 15, 3, 8} x = 161 1 10 0 0

Page 9: AlgoPARC - hawaii.edunodari/teaching/f18/lectures/lecture06.pdf · ICS 491: Competitve Programming Lecture 6: Dynamic Programming II Example: Subset Sum Problem (Subset Sum) . Given

ICS 491: Competitve Programming – Lecture 6: Dynamic Programming II

Example: Subset Sum

Problem (Subset Sum). Given a set S of 1 ≤ n ≤ 20 integersand a positive integer x, is there a subset of S that sums to x?

S = {17, 5, 7, 15, 3, 8} x = 161 1 10 0 0

int S[20] = {...}; int x = ...; // initialize S & x

int SS[20][MAX_X]; memset(SS, UNDEFINED, sizeof(SS));

// returns true iff exists subset within S[i:n]

// that adds up to x

SubsetSum(i, x):

if (x < 0 or i > n) return false;

else if (x == 0) return S[i][x] = true;

else if (SS[i][x] != UNDEFINED) return SS[i][x];

else

return SS[i][x] = SubsetSum(i+1, x) or SubsetSum(i+1, x-S[i]);

main() {

return SubsetSum(0, x);

}

Page 10: AlgoPARC - hawaii.edunodari/teaching/f18/lectures/lecture06.pdf · ICS 491: Competitve Programming Lecture 6: Dynamic Programming II Example: Subset Sum Problem (Subset Sum) . Given

ICS 491: Competitve Programming – Lecture 6: Dynamic Programming II

Example: Subset Sum

int S[20] = {...}; int x = ...; // initialize S & x

int SS[20][MAX_X]; memset(SS, UNDEFINED, sizeof(SS));

// returns true iff exists subset within S[i:n-1]

// that adds up to x

SubsetSum(i, x):

if (x < 0 or i > n) return false;

else if (x == 0) return S[i][x] = true;

else if (SS[i][x] != UNDEFINED) return SS[i][x];

else

return SS[i][x] = SubsetSum(i+1, x) or SubsetSum(i+1, x-S[i]);

main() {

return SubsetSum(0, x);

}

Page 11: AlgoPARC - hawaii.edunodari/teaching/f18/lectures/lecture06.pdf · ICS 491: Competitve Programming Lecture 6: Dynamic Programming II Example: Subset Sum Problem (Subset Sum) . Given

ICS 491: Competitve Programming – Lecture 6: Dynamic Programming II

Example: Subset Sum

int S[20] = {...}; int x = ...; // initialize S & x

int SS[20][MAX_X]; memset(SS, UNDEFINED, sizeof(SS));

// returns true iff exists subset within S[i:n-1]

// that adds up to x

SubsetSum(i, x):

if (x < 0 or i > n) return false;

else if (x == 0) return S[i][x] = true;

else if (SS[i][x] != UNDEFINED) return SS[i][x];

else

return SS[i][x] = SubsetSum(i+1, x) or SubsetSum(i+1, x-S[i]);

main() {

return SubsetSum(0, x);

}

i

xSS

Page 12: AlgoPARC - hawaii.edunodari/teaching/f18/lectures/lecture06.pdf · ICS 491: Competitve Programming Lecture 6: Dynamic Programming II Example: Subset Sum Problem (Subset Sum) . Given

ICS 491: Competitve Programming – Lecture 6: Dynamic Programming II

Example: Subset Sum

int S[20] = {...}; int x = ...; // initialize S & x

int SS[20][MAX_X]; memset(SS, UNDEFINED, sizeof(SS));

// returns true iff exists subset within S[i:n-1]

// that adds up to x

SubsetSum(i, x):

if (x < 0 or i > n) return false;

else if (x == 0) return S[i][x] = true;

else if (SS[i][x] != UNDEFINED) return SS[i][x];

else

return SS[i][x] = SubsetSum(i+1, x) or SubsetSum(i+1, x-S[i]);

main() {

return SubsetSum(0, x);

}

i

x

i+1

x-S[i]SS

Page 13: AlgoPARC - hawaii.edunodari/teaching/f18/lectures/lecture06.pdf · ICS 491: Competitve Programming Lecture 6: Dynamic Programming II Example: Subset Sum Problem (Subset Sum) . Given

ICS 491: Competitve Programming – Lecture 6: Dynamic Programming II

Example: Subset Sum

int S[20] = {...}; int x = ...; // initialize S & x

int SS[20][MAX_X]; memset(SS, UNDEFINED, sizeof(SS));

// returns true iff exists subset within S[i:n-1]

// that adds up to x

SubsetSum(i, x):

if (x < 0 or i > n) return false;

else if (x == 0) return S[i][x] = true;

else if (SS[i][x] != UNDEFINED) return SS[i][x];

else

return SS[i][x] = SubsetSum(i+1, x) or SubsetSum(i+1, x-S[i]);

main() {

return SubsetSum(0, x);

}

i

x

i+1

x-S[i]SS

Page 14: AlgoPARC - hawaii.edunodari/teaching/f18/lectures/lecture06.pdf · ICS 491: Competitve Programming Lecture 6: Dynamic Programming II Example: Subset Sum Problem (Subset Sum) . Given

ICS 491: Competitve Programming – Lecture 6: Dynamic Programming II

Example: Subset Sum

int S[20] = {...}; int x = ...; // initialize S & x

int SS[20][MAX_X]; memset(SS, UNDEFINED, sizeof(SS));

// returns true iff exists subset within S[i:n-1]

// that adds up to x

SubsetSum(i, x):

if (x < 0 or i > n) return false;

else if (x == 0) return S[i][x] = true;

else if (SS[i][x] != UNDEFINED) return SS[i][x];

else

return SS[i][x] = SubsetSum(i+1, x) or SubsetSum(i+1, x-S[i]);

main() {

return SubsetSum(0, x);

}

i

x

i+1

x-S[i]SS

Page 15: AlgoPARC - hawaii.edunodari/teaching/f18/lectures/lecture06.pdf · ICS 491: Competitve Programming Lecture 6: Dynamic Programming II Example: Subset Sum Problem (Subset Sum) . Given

ICS 491: Competitve Programming – Lecture 6: Dynamic Programming II

Example: Subset Sum

int S[20] = {...}; int x = ...; // initialize S & x

int SS[20][MAX_X]; memset(SS, UNDEFINED, sizeof(SS));

// returns true iff exists subset within S[i:n-1]

// that adds up to x

SubsetSum(i, x):

if (x < 0 or i > n) return false;

else if (x == 0) return S[i][x] = true;

else if (SS[i][x] != UNDEFINED) return SS[i][x];

else

return SS[i][x] = SubsetSum(i+1, x) or SubsetSum(i+1, x-S[i]);

main() {

return SubsetSum(0, x);

}

i

x

i+1

x-S[i]SS

int S[20] = {...}; int x = ...; // initialize S & x

int SS[20+1][MAX_X]; memset(SS, false, sizeof(SS));

// returns true iff exists subset within S[i:n]

// that adds up to x

main():

for (i = 19; i >=0; i++)

for (j = 0; j < MAX_X; j++)

SS[i,j] = SS[i+1][j] or SS[i+1][j-S[i]]

return SS[0][x];

Page 16: AlgoPARC - hawaii.edunodari/teaching/f18/lectures/lecture06.pdf · ICS 491: Competitve Programming Lecture 6: Dynamic Programming II Example: Subset Sum Problem (Subset Sum) . Given

ICS 491: Competitve Programming – Lecture 6: Dynamic Programming II

Example: Subset Sum

int S[20] = {...}; int x = ...; // initialize S & x

int SS[20][MAX_X]; memset(SS, UNDEFINED, sizeof(SS));

// returns true iff exists subset within S[i:n-1]

// that adds up to x

SubsetSum(i, x):

if (x < 0 or i > n) return false;

else if (x == 0) return S[i][x] = true;

else if (SS[i][x] != UNDEFINED) return SS[i][x];

else

return SS[i][x] = SubsetSum(i+1, x) or SubsetSum(i+1, x-S[i]);

main() {

return SubsetSum(0, x);

}

i

x

i+1

x-S[i]SS

int S[20] = {...}; int x = ...; // initialize S & x

int SS[20+1][MAX_X]; memset(SS, false, sizeof(SS));

// returns true iff exists subset within S[i:n]

// that adds up to x

main():

for (i = 19; i >=0; i++)

for (j = ; j < MAX_X; j++)

SS[i,j] = SS[i+1][j] or SS[i+1][j-S[i]]

return SS[0][x];

S[i]

Page 17: AlgoPARC - hawaii.edunodari/teaching/f18/lectures/lecture06.pdf · ICS 491: Competitve Programming Lecture 6: Dynamic Programming II Example: Subset Sum Problem (Subset Sum) . Given

ICS 491: Competitve Programming – Lecture 6: Dynamic Programming II

Example: Subset Sum

int S[20] = {...}; int x = ...; // initialize S & x

int SS[20][MAX_X]; memset(SS, UNDEFINED, sizeof(SS));

// returns true iff exists subset within S[i:n-1]

// that adds up to x

SubsetSum(i, x):

if (x < 0 or i > n) return false;

else if (x == 0) return S[i][x] = true;

else if (SS[i][x] != UNDEFINED) return SS[i][x];

else

return SS[i][x] = SubsetSum(i+1, x) or SubsetSum(i+1, x-S[i]);

main() {

return SubsetSum(0, x);

}

i

x

i+1

x-S[i]SS

int S[20] = {...}; int x = ...; // initialize S & x

int SS[20+1][MAX_X]; memset(SS, false, sizeof(SS));

// returns true iff exists subset within S[i:n]

// that adds up to x

main():

for (i = 19; i >=0; i++)

for (j = ; j < MAX_X; j++)

SS[i,j] = SS[i+1][j] or SS[i+1][j-S[i]]

return SS[0][x];

S[i]

Can also fill it out in assending order of i, by looking at subproblemsS[0:i], instead of S[i:n-1]

Page 18: AlgoPARC - hawaii.edunodari/teaching/f18/lectures/lecture06.pdf · ICS 491: Competitve Programming Lecture 6: Dynamic Programming II Example: Subset Sum Problem (Subset Sum) . Given

ICS 491: Competitve Programming – Lecture 6: Dynamic Programming II

0-1 Knapsack

Problem (0-1 Knapsack). Given a set S of n items, each with itsown value Vi and weight Wi for all 1 ≤ i ≤ n and a maximumknapsack capacity C, compute the maximum value of the itemsthat you can carry. You cannot take fractions of items.

Page 19: AlgoPARC - hawaii.edunodari/teaching/f18/lectures/lecture06.pdf · ICS 491: Competitve Programming Lecture 6: Dynamic Programming II Example: Subset Sum Problem (Subset Sum) . Given

ICS 491: Competitve Programming – Lecture 6: Dynamic Programming II

0-1 Knapsack

Problem (0-1 Knapsack). Given a set S of n items, each with itsown value Vi and weight Wi for all 1 ≤ i ≤ n and a maximumknapsack capacity C, compute the maximum value of the itemsthat you can carry. You cannot take fractions of items.

main() {

return maxV(1, C);

}

if (M[i][C] != UNDEFINED) return M[i, C];

maxV(i+1, C);

M[i][C]=

M[i][C]=

maxV(i,C) {

if (i > n || C <= 0) return 0;

if (W[i] > C)

return

return max(maxV(i+1, C),

V[i]+maxV(i+1, C-W[i]));

}memset(M, UNDEFINED, sizeof(M));

int M[maxN+1][maxC];

Page 20: AlgoPARC - hawaii.edunodari/teaching/f18/lectures/lecture06.pdf · ICS 491: Competitve Programming Lecture 6: Dynamic Programming II Example: Subset Sum Problem (Subset Sum) . Given

ICS 491: Competitve Programming – Lecture 6: Dynamic Programming II

0-1 Knapsack

Problem (0-1 Knapsack). Given a set S of n items, each with itsown value Vi and weight Wi for all 1 ≤ i ≤ n and a maximumknapsack capacity C, compute the maximum value of the itemsthat you can carry. You cannot take fractions of items.

main() {

return maxV(1, C);

}

if (M[i][C] != UNDEFINED) return M[i, C];

maxV(i+1, C);

M[i][C]=

M[i][C]=

maxV(i,C) {

if (i > n || C <= 0) return 0;

if (W[i] > C)

return

return max(maxV(i+1, C),

V[i]+maxV(i+1, C-W[i]));

}memset(M, UNDEFINED, sizeof(M));

int M[maxN+1][maxC];

CC-W[i]

i

i+1

M

Page 21: AlgoPARC - hawaii.edunodari/teaching/f18/lectures/lecture06.pdf · ICS 491: Competitve Programming Lecture 6: Dynamic Programming II Example: Subset Sum Problem (Subset Sum) . Given

ICS 491: Competitve Programming – Lecture 6: Dynamic Programming II

0-1 Knapsack

Problem (0-1 Knapsack). Given a set S of n items, each with itsown value Vi and weight Wi for all 1 ≤ i ≤ n and a maximumknapsack capacity C, compute the maximum value of the itemsthat you can carry. You cannot take fractions of items.

main() {

return maxV(1, C);

}

if (M[i][C] != UNDEFINED) return M[i, C];

maxV(i+1, C);

M[i][C]=

M[i][C]=

maxV(i,C) {

if (i > n || C <= 0) return 0;

if (W[i] > C)

return

return max(maxV(i+1, C),

V[i]+maxV(i+1, C-W[i]));

}memset(M, UNDEFINED, sizeof(M));

int M[maxN+1][maxC];

CC-W[i]

i

i+1

M

Page 22: AlgoPARC - hawaii.edunodari/teaching/f18/lectures/lecture06.pdf · ICS 491: Competitve Programming Lecture 6: Dynamic Programming II Example: Subset Sum Problem (Subset Sum) . Given

ICS 491: Competitve Programming – Lecture 6: Dynamic Programming II

Why Bottom-up DP?

Page 23: AlgoPARC - hawaii.edunodari/teaching/f18/lectures/lecture06.pdf · ICS 491: Competitve Programming Lecture 6: Dynamic Programming II Example: Subset Sum Problem (Subset Sum) . Given

ICS 491: Competitve Programming – Lecture 6: Dynamic Programming II

Why Bottom-up DP?

Cons:Extra work if recursion is already definedLess intuitiveMight use extra memory (all memo entries must be filled)

Page 24: AlgoPARC - hawaii.edunodari/teaching/f18/lectures/lecture06.pdf · ICS 491: Competitve Programming Lecture 6: Dynamic Programming II Example: Subset Sum Problem (Subset Sum) . Given

ICS 491: Competitve Programming – Lecture 6: Dynamic Programming II

Why Bottom-up DP?

A bit faster (no recursive overhead)

Cons:Extra work if recursion is already definedLess intuitiveMight use extra memory (all memo entries must be filled)

Pros:

Page 25: AlgoPARC - hawaii.edunodari/teaching/f18/lectures/lecture06.pdf · ICS 491: Competitve Programming Lecture 6: Dynamic Programming II Example: Subset Sum Problem (Subset Sum) . Given

ICS 491: Competitve Programming – Lecture 6: Dynamic Programming II

Why Bottom-up DP?

A bit faster (no recursive overhead)Sometimes top-down recursion is harder (or impossible) todefine

Cons:Extra work if recursion is already definedLess intuitiveMight use extra memory (all memo entries must be filled)

Pros:

Page 26: AlgoPARC - hawaii.edunodari/teaching/f18/lectures/lecture06.pdf · ICS 491: Competitve Programming Lecture 6: Dynamic Programming II Example: Subset Sum Problem (Subset Sum) . Given

ICS 491: Competitve Programming – Lecture 6: Dynamic Programming II

Example

Problem. In a football game, after every scoring play, thecheerleaders do as many jumps as the total number of points onthe scoreboard. For example, if the team first scored atouchdown (7 pts), then a field goal (3 pts), then a safety (2 pts),the cheerleaders did 7 + 10 + 12 = 29 total jumps.Given the number n of total jumps, compute the largest possiblenumber of points scored in the game?

Page 27: AlgoPARC - hawaii.edunodari/teaching/f18/lectures/lecture06.pdf · ICS 491: Competitve Programming Lecture 6: Dynamic Programming II Example: Subset Sum Problem (Subset Sum) . Given

ICS 491: Competitve Programming – Lecture 6: Dynamic Programming II

Example

Problem. In a football game, after every scoring play, thecheerleaders do as many jumps as the total number of points onthe scoreboard. For example, if the team first scored atouchdown (7 pts), then a field goal (3 pts), then a safety (2 pts),the cheerleaders did 7 + 10 + 12 = 29 total jumps.Given the number n of total jumps, compute the largest possiblenumber of points scored in the game?

You may assume the possible points are given as a set of S of mpositive integers, with the largest value at most 20. For example,in regular football rules, m = 5 and S = {2, 3, 6, 7, 8}.

Page 28: AlgoPARC - hawaii.edunodari/teaching/f18/lectures/lecture06.pdf · ICS 491: Competitve Programming Lecture 6: Dynamic Programming II Example: Subset Sum Problem (Subset Sum) . Given

ICS 491: Competitve Programming – Lecture 6: Dynamic Programming II

Recursive Solution

Page 29: AlgoPARC - hawaii.edunodari/teaching/f18/lectures/lecture06.pdf · ICS 491: Competitve Programming Lecture 6: Dynamic Programming II Example: Subset Sum Problem (Subset Sum) . Given

ICS 491: Competitve Programming – Lecture 6: Dynamic Programming II

Recursive Solution

Let P(n) define the maximum number of points for n total jumps.What is the recursive definition of P(n)?

Hint: Try all possible values for the last score and take themaximum

Page 30: AlgoPARC - hawaii.edunodari/teaching/f18/lectures/lecture06.pdf · ICS 491: Competitve Programming Lecture 6: Dynamic Programming II Example: Subset Sum Problem (Subset Sum) . Given

ICS 491: Competitve Programming – Lecture 6: Dynamic Programming II

Recursive Solution

Let P(n) define the maximum number of points for n total jumps.What is the recursive definition of P(n)?

P(n) = max

P(n − S[0]) + S[0]P(n − S[1]) + S[1]P(n − S[2]) + S[2]. . .P(n − S[m − 1]) + S[m − 1]

?

Hint: Try all possible values for the last score and take themaximum

Page 31: AlgoPARC - hawaii.edunodari/teaching/f18/lectures/lecture06.pdf · ICS 491: Competitve Programming Lecture 6: Dynamic Programming II Example: Subset Sum Problem (Subset Sum) . Given

ICS 491: Competitve Programming – Lecture 6: Dynamic Programming II

Recursive Solution

Let P(n) define the maximum number of points for n total jumps.What is the recursive definition of P(n)?

P(n) = max

P(n − S[0]) + S[0]P(n − S[1]) + S[1]P(n − S[2]) + S[2]. . .P(n − S[m − 1]) + S[m − 1]

?

In the previous example:

Points scored: 7, 3, 2Total jumps = 7 + 10 + 12 = 29P(29) = P(29− 2) + 2 ?

Hint: Try all possible values for the last score and take themaximum

Page 32: AlgoPARC - hawaii.edunodari/teaching/f18/lectures/lecture06.pdf · ICS 491: Competitve Programming Lecture 6: Dynamic Programming II Example: Subset Sum Problem (Subset Sum) . Given

ICS 491: Competitve Programming – Lecture 6: Dynamic Programming II

Recursive Solution

Let P(n) define the maximum number of points for n total jumps.What is the recursive definition of P(n)?

P(n) = max

P(n − S[0]) + S[0]P(n − S[1]) + S[1]P(n − S[2]) + S[2]. . .P(n − S[m − 1]) + S[m − 1]

?

In the previous example:

Points scored: 7, 3, 2Total jumps = 7 + 10 + 12 = 29P(29) = P(29− 2) + 2 ?

Hint: Try all possible values for the last score and take themaximum

( )

Page 33: AlgoPARC - hawaii.edunodari/teaching/f18/lectures/lecture06.pdf · ICS 491: Competitve Programming Lecture 6: Dynamic Programming II Example: Subset Sum Problem (Subset Sum) . Given

ICS 491: Competitve Programming – Lecture 6: Dynamic Programming II

Recursive Solution

Let P(n) define the maximum number of points for n total jumps.What is the recursive definition of P(n)?

P(n) = max

P(n − S[0]) + S[0]P(n − S[1]) + S[1]P(n − S[2]) + S[2]. . .P(n − S[m − 1]) + S[m − 1]

?

In the previous example:

Points scored: 7, 3, 2Total jumps = 7 + 10 + 12 = 29P(29) = P(29− 2) + 2 ?

Hint: Try all possible values for the last score and take themaximum

P(29) = P(17) + 2( )

Page 34: AlgoPARC - hawaii.edunodari/teaching/f18/lectures/lecture06.pdf · ICS 491: Competitve Programming Lecture 6: Dynamic Programming II Example: Subset Sum Problem (Subset Sum) . Given

ICS 491: Competitve Programming – Lecture 6: Dynamic Programming II

Recursive Solution

Let P(n) define the maximum number of points for n total jumps.What is the recursive definition of P(n)?

P(n) = max

P(n − S[0]) + S[0]P(n − S[1]) + S[1]P(n − S[2]) + S[2]. . .P(n − S[m − 1]) + S[m − 1]

?

In the previous example:

Points scored: 7, 3, 2Total jumps = 7 + 10 + 12 = 29P(29) = P(29− 2) + 2 ?

Hint: Try all possible values for the last score and take themaximum

P(29) = P(17) + 2( )

If given the current score x :

P(29) = P(29− x) + 2

Page 35: AlgoPARC - hawaii.edunodari/teaching/f18/lectures/lecture06.pdf · ICS 491: Competitve Programming Lecture 6: Dynamic Programming II Example: Subset Sum Problem (Subset Sum) . Given

ICS 491: Competitve Programming – Lecture 6: Dynamic Programming II

Recursive Solution

Let P(n) define the maximum number of points for n total jumps.What is the recursive definition of P(n)?

P(n) = max

P(n − S[0]) + S[0]P(n − S[1]) + S[1]P(n − S[2]) + S[2]. . .P(n − S[m − 1]) + S[m − 1]

?

In the previous example:

Points scored: 7, 3, 2Total jumps = 7 + 10 + 12 = 29P(29) = P(29− 2) + 2 ?

Hint: Try all possible values for the last score and take themaximum

P(29) = P(17) + 2( )

If given the current score x :

P(29) = P(29− x) + 2P(n) = P(n − x) + S[i ]

Page 36: AlgoPARC - hawaii.edunodari/teaching/f18/lectures/lecture06.pdf · ICS 491: Competitve Programming Lecture 6: Dynamic Programming II Example: Subset Sum Problem (Subset Sum) . Given

ICS 491: Competitve Programming – Lecture 6: Dynamic Programming II

Recursive Solution

Let P(n) define the maximum number of points for n total jumps.What is the recursive definition of P(n)?

In the previous example:

Points scored: 7, 3, 2Total jumps = 7 + 10 + 12 = 29P(29) = P(29− 2) + 2 ?

Hint: Try all possible values for the last score and take themaximum

P(29) = P(17) + 2( )

If given the current score x :

P(29) = P(29− x) + 2P(n) = P(n − x) + S[i ]

P(n, x) = max

P(n − x , x − S[0]) + S[0]P(n − x , x − S[1]) + S[1]P(n − x , x − S[2]) + S[2]. . .P(n − x , x − S[m − 1]) + S[m − 1]

Page 37: AlgoPARC - hawaii.edunodari/teaching/f18/lectures/lecture06.pdf · ICS 491: Competitve Programming Lecture 6: Dynamic Programming II Example: Subset Sum Problem (Subset Sum) . Given

ICS 491: Competitve Programming – Lecture 6: Dynamic Programming II

Recursive Solution

Let P(n) define the maximum number of points for n total jumps.What is the recursive definition of P(n)?

In the previous example:

Points scored: 7, 3, 2Total jumps = 7 + 10 + 12 = 29P(29) = P(29− 2) + 2 ?

Hint: Try all possible values for the last score and take themaximum

P(29) = P(17) + 2( )

If given the current score x :

P(29) = P(29− x) + 2P(n) = P(n − x) + S[i ]

P(n, x) = max

P(n − x , x − S[0]) + S[0]P(n − x , x − S[1]) + S[1]P(n − x , x − S[2]) + S[2]. . .P(n − x , x − S[m − 1]) + S[m − 1]

x =

Page 38: AlgoPARC - hawaii.edunodari/teaching/f18/lectures/lecture06.pdf · ICS 491: Competitve Programming Lecture 6: Dynamic Programming II Example: Subset Sum Problem (Subset Sum) . Given

ICS 491: Competitve Programming – Lecture 6: Dynamic Programming II

Bottom-up Solution

x = P(n, x) = max

P(n − x , x − S[0]) + S[0]P(n − x , x − S[1]) + S[1]P(n − x , x − S[2]) + S[2]. . .P(n − x , x − S[m − 1]) + S[m − 1]

points

jumps

x

j

Page 39: AlgoPARC - hawaii.edunodari/teaching/f18/lectures/lecture06.pdf · ICS 491: Competitve Programming Lecture 6: Dynamic Programming II Example: Subset Sum Problem (Subset Sum) . Given

ICS 491: Competitve Programming – Lecture 6: Dynamic Programming II

Bottom-up Solution

x = P(n, x) = max

P(n − x , x − S[0]) + S[0]P(n − x , x − S[1]) + S[1]P(n − x , x − S[2]) + S[2]. . .P(n − x , x − S[m − 1]) + S[m − 1]

points

jumps

x

j

Just need to mark M[j ][x ]and find the rightmostmarked M[n][] in the n-th row

M

Page 40: AlgoPARC - hawaii.edunodari/teaching/f18/lectures/lecture06.pdf · ICS 491: Competitve Programming Lecture 6: Dynamic Programming II Example: Subset Sum Problem (Subset Sum) . Given

ICS 491: Competitve Programming – Lecture 6: Dynamic Programming II

Questions?


Recommended