Subset sum overlapping dilemma recursively

2024/7/8 6:35:53

I'm studying recursive logic that one of the problems is subset sum. AFAI read, there are overlaps when it is run by recursion. But, I cannot figure out how there are. Also, I read that to overcome the issue DP can be used but I want to comprehend how the recursion overcome the problem. Could you picturize an example with overlapping?

Pseudo-code,

def hasSum( array, start, sum):if sum == 0:return trueif start > array.length - 1:return false;return hasSum(array, start + 1, sum - array[start])or hasSum(array, start + 1, sum)

I cannot associate the logic with the following picture, I'm certainly overlook a point / points.

enter image description here

Answer

There is overlap caused by this line here:

return hasSum(array, start + 1, sum - array[start])or hasSum(array, start + 1, sum)

If the algorithm doesn't find a valid condition with the first hasSum (which because of recursion has already gone through the entire array by the time is returns to this point), it will try again with the second hasSum which ignores the value of this current index. This means that the algorithm checks the same indexes multiple times.

https://en.xdnf.cn/q/120451.html

Related Q&A

Python - download video from indirect url

I have a link like thishttps://r9---sn-4g57knle.googlevideo.com/videoplayback?id=10bc30daeba89d81&itag=22&source=picasa&begin=0&requiressl=yes&mm=30&mn=sn-4g57knle&ms=nxu&a…

Python trading logic

Heres a simple code for downloading daily stock data and computing Bollinger band indicator, but what I am not able to do is set up a logic for generating a buy and sell signal. Can someone help me wit…

Convert PDF to Excel [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Return the furthermost outlier in kmeans clustering? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Highest number of consecutively repeating values in a list

Lets say I have this list:List= [1,1,1,0,0,1,1,1,1,1]How do I display the highest number of repeating 1s in a row? I want to return 5.

Python Maze Generation

I am trying to make a python maze generator but I keep getting an IndexError: list index out of range. Any ideas? Im kinda new to this stuff so I was using the code from rosetta code on maze generatio…

Why does Pythons `any` function not return True or False? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Write a list of lists into csv in Python [duplicate]

This question already has answers here:Writing a Python list of lists to a csv file(12 answers)Closed 5 years ago.I have a list of lists of pure data like thisa=[[1,2,3],[4,5,6],[7,8,9]]How can I write…

Upper limit of points pyplot

Just being a curious George here. But Im handling 279 million data points (x,y) and am wondering if pyplot can scatter such a number?Thanks.

how to delete the u and before the of database table display by python [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…