0

i've been trying to turn the recurrence formula underneath into a pseudocode that uses memoization, however currently all i know is my below attempt is incorrect, is anyone able to point me in the right direction?

My recurrence formula:

N(C,i) =
1 if C = 0
0 if i=0 or C<0}
N(C-p_i, i-1) + N(C, i-1) otherwise

My current attempt:

MEM-N(C, i, r)
    if r[i] >= 0 then
        return r[i]
    if i = 0 and r[i] >= 0 or C < 0 and r[i] >= 0 then
        return 0
    else if C = 0 and r[i] >= 0 then
        return 1
    else
        q = -$\infty$
        q = MEM-N(C - $p_i$, i-1) + MEM-N(C,i - x, r)
        r[i] = q
        return q
9
  • if r[i] <= 0 then -- shouldn't this be > 0, since all return values are bound to be positive? Commented Jun 2, 2018 at 17:07
  • yes it should be >=, i made a spelling error Commented Jun 2, 2018 at 17:10
  • And perhaps return 0 instead of q = 0, assuming the indentation is correct. Commented Jun 2, 2018 at 17:12
  • While that's also correct, for now all i know is there's something wrong with the recursion in the last else statement, but im not sure of what Commented Jun 2, 2018 at 18:05
  • How then do you know that it is wrong? Also you should pass r to the first recursive call. Commented Jun 2, 2018 at 18:44

1 Answer 1

0

Following on from the comments:

MEM-N(C, i, r)
    if C = 0 then
        return 1
    else if i = 0 or C < 0 then
        return 0
    else if r[i] >= 0 then
        return r[i]                                        # move here
    else
        q = MEM-N(C - p_i, i - 1, r) + MEM-N(C, i - 1, r)  # fix
        r[i] = q
        return q
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.