In class we went over a solution for the subset sum problem (given a set S of positive numbers, does a subset of S exist that sums to a positive value T). Here is my python implementation of the algorithm with a simple test case:
def ss(s, i, t):
if t == 0:
return True
if i == (len(s)-1):
return s[i] == t
return ss(s, i+1, t-s[i]) or ss(s, i+1, t)
s = [1, 3, 5]
t = 8
print(ss(s, 0, t))
>> True
We are supposed to make and prove correctness for a modified algorithm that can handle negative values in S and for T. However, it seems like every set with negative values I have tried so far on the unmodified algorithm still works. I can't seem to find an example where this algorithm fails for negative values.
Could someone explain to me why this algorithm doesn't work for all negative values and possibly give a counter example to demonstrate this?