- #1
Nano-Passion
- 1,291
- 0
They give me this problem in my last exam, I did not get it right - its a very convoluted and I think poorly worded problem. Rants aside, how am I supposed to be able to answer a problem like this on the next exam? What sort of mindset will help me deal with recursion problems of this form? How does this problem work mathematically?
Naturally I would solve this problem using for loops and if statements. I'm not sure what sort of mathematical method is being used here.
Suppose you had a vector v of numbers and you want to know if some subset of these numbers
adds up to a target number, t. E.g. if v is [3 2 4] and t is 7, the answer is true because the sum
of 3 and 4 is 7. If t is 8 the answer is false because there is no way to add up any subset of 3, 2,
and 4 to get 8. The function subsetsum answers this question using the following recursive
reasoning. Finish subsetsum. Your definition must be recursive. You may assume that t and
every element of v are positive.
◦ If v has length 0, there are no numbers to add so the result is 0 (i.e. false).
◦ If t is equal to the first number in v, the result is 1 (i.e. true).
◦ Let v1 be v without its first number. E.g. if v is [3 2 4] v1 is [2 4]. If you can find a
subset of v1 that sums to t the result is of subset(v, t) is 1.
◦ Also, if you can find a subset of v1 that sums to t-v(1) the result is of subset(v, t) is 1.
◦ Otherwise the result of subset(v, t) is 0.
So I know the answer to this which is:
[1] function res = subsetsum(v, t)
[2] if length(v) = = 0
[3] res=0;
[4] elseif t = = v(1)
[5] res=1;
[6] elseif subsetsum(v(2:length(v)), t)
[7] res = 1;
[8] elseif subsetsum(v(2:length(v)), t-v(1))
[9] res=1;
[10]else
[11]res = 0;
[12]end
So here is my best guess of what is going on for a vector of [3 2 4] with t = 7
subsetsum([3 2 4], 7)
length (v) == 0? No. go on
t == v(1). 7==3? no. go on.
subsetsum([2,4], 7) recursive call to top
length(v) == 0? No. go on
t ==v(1)? 7 == 2? No. go on
Line [6]-[7] not true, jump to line [8]
subsetsum([2 4], t-v(1) = 7- 3 = 4) recursive call to top subsetsum([2 4], 4)
length(v)==0? No. go on.
t = v(1)? 4 = 2? No. go on
back to recursive call line [6]
subsetsum([2 4], 4) recursive call to top
length(v)==0? No.
t == 2? no.
Line [6]-[7] not true, jump to line [8]
subsetsum([2 4], t - v(1) = 4 - 2 = 2) recursive call to top subsetsum([2 4], 2)
length(v)==0? No.
t = v(1)? 2 = 2? Yes. end if.
res = 1.
Naturally I would solve this problem using for loops and if statements. I'm not sure what sort of mathematical method is being used here.
Homework Statement
Suppose you had a vector v of numbers and you want to know if some subset of these numbers
adds up to a target number, t. E.g. if v is [3 2 4] and t is 7, the answer is true because the sum
of 3 and 4 is 7. If t is 8 the answer is false because there is no way to add up any subset of 3, 2,
and 4 to get 8. The function subsetsum answers this question using the following recursive
reasoning. Finish subsetsum. Your definition must be recursive. You may assume that t and
every element of v are positive.
◦ If v has length 0, there are no numbers to add so the result is 0 (i.e. false).
◦ If t is equal to the first number in v, the result is 1 (i.e. true).
◦ Let v1 be v without its first number. E.g. if v is [3 2 4] v1 is [2 4]. If you can find a
subset of v1 that sums to t the result is of subset(v, t) is 1.
◦ Also, if you can find a subset of v1 that sums to t-v(1) the result is of subset(v, t) is 1.
◦ Otherwise the result of subset(v, t) is 0.
So I know the answer to this which is:
[1] function res = subsetsum(v, t)
[2] if length(v) = = 0
[3] res=0;
[4] elseif t = = v(1)
[5] res=1;
[6] elseif subsetsum(v(2:length(v)), t)
[7] res = 1;
[8] elseif subsetsum(v(2:length(v)), t-v(1))
[9] res=1;
[10]else
[11]res = 0;
[12]end
So here is my best guess of what is going on for a vector of [3 2 4] with t = 7
subsetsum([3 2 4], 7)
length (v) == 0? No. go on
t == v(1). 7==3? no. go on.
subsetsum([2,4], 7) recursive call to top
length(v) == 0? No. go on
t ==v(1)? 7 == 2? No. go on
Line [6]-[7] not true, jump to line [8]
subsetsum([2 4], t-v(1) = 7- 3 = 4) recursive call to top subsetsum([2 4], 4)
length(v)==0? No. go on.
t = v(1)? 4 = 2? No. go on
back to recursive call line [6]
subsetsum([2 4], 4) recursive call to top
length(v)==0? No.
t == 2? no.
Line [6]-[7] not true, jump to line [8]
subsetsum([2 4], t - v(1) = 4 - 2 = 2) recursive call to top subsetsum([2 4], 2)
length(v)==0? No.
t = v(1)? 2 = 2? Yes. end if.
res = 1.