Subset Sum Problem Dynamic Programming

preview_player
Показать описание
Given a set of non negative numbers and a total, find if there exists a subset in this set whose sum is same as total.
Рекомендации по теме
Комментарии
Автор

Turn on closed captions and see what youtube translates Tushar's name into at 0:02

bhavuks
Автор

I think your explanations are not very intuitive. You completely skip the idea of breaking problem in to subproblem and constructing solution from optimal solution of its subproblems. Instead of going through the tedious table construction exercise maybe you should talk more about how to figure out subproblems, the core logic.

kalpstream
Автор

"How do you solve this problem? Yes, we'll use Dynamic Programming."

ShahidNihal
Автор

New name for the channel: "Yes, we'll use Dynamic Programming"

GuyMorita
Автор

The way you tediously run through the grunt work of the algorithms is really effective for me. Thank you for doing this for free.

joecoke
Автор

You explained how this algorithm works, not why this algorithm works. You taught how to do the problem, not how to build logic for such problems.

himanshukaushik
Автор

For easier backtracking i'd add one more row in the beginning (element 0), that pretty much guarantees you always end up in the upper left corner without worrying about index out of bound issues and checks.

niintyhma
Автор

Sir
If you start by explaining the recursive solution to any problem, then it would be quite easy for beginners to understand the concept of DP more effectively . Some questions that were left unanswered in the videos like, why are we copying the value from the above cell or why are we moving T[i] steps back can be best explained by the recursive solution.

pratimaupadhyay
Автор

If you are a software engineer...





you are awesome

BackToBackSWE
Автор

This approach is great but has a high space complexity (O(w*n) where w is the number of bits you use to represent the required sum and n is the number of items in the list). I tried using a adjacency list instead where the key is the value of the required sum at every step (basically the columns in this matrix) and the value is the list of items which satisfy the sum. I think that it has a lower space complexity. Also, algorithms like these usually have pseudo-polynomial time complexity. Meaning, that the overall time complexity depends on how you represent the total sum required. For example, if the required sum is 100, we are gonna have 0-100 indexes in columns or keys (in my approach). Anyway, thanks for taking your time out and explaining this stuff Tooshar :D

ansrhl
Автор

There was direct solution provided for the tabulation solution but I couldn't able to grasp how actually it's working.
Now everything is clear to me after your explanation. Thanks a lot.

deepdive
Автор

You're the only one that understands what you're talking about. You start filling up a table base on an algorithm you didn't explain

iliassti
Автор

I am making a minesweeper solver, so I made an algorithm for subsetSum. However, I needed every possible subset, not just whether there existed one, and also needed to have multiple instances of the same value in the set. What I did was I started at the first value in the set, added any solutions to my solutionset, then I added the next number, and added solutions where the rightmost value was present. I saved some time by calculating max/min sum of the set so I can immediately rule out values if they are too high/low. I'm not sure how better this algorithm is that brute force, however.

Borthralla
Автор

Hi Tushar,
Thank you for your video.
Some thoughts:
1.You don't need (in code) the matrix - one row is quite enough
2.Since all info is copied from above the values in current row can be obtained from this same current row
3.Instead of denoting the corresponding cells with T(rue) it's much better and reasonable to do this with numerous of corresponding term-value.
For example, the matrix of the video would be:
[0, f, 2, f, f, f, f, f, f, f, f, f]
[0, f, 2, 3, f, 3, f, f, f, f, f, f]
[0, f, 2, 3, f, 3, f, 7, f, 7, 7, f]
[0, f, 2, 3, f, 3, f, 7, 8, 7, 7, 8]
[0, f, 2, 3, f, 3, f, 7, 8, 7, 7, 8]
4.You don't need to do calculations to all the cells - you only need add the value of current row to the values of columns (copied from the above non-negative values)
For example: 
to get the second row from the first one we need only 2 operations:
to add 0+3 -> we get 3 in the column 3=0+3
to add 2+3 -> we get 3 in the column 5=2+3
to get the third row from the second one we need only 3 operations:
to add 0+7 -> we get 7 in the column 7=0+7
to add 2+7 -> we get 7 in the column 9=2+7
to add 3+7 -> we get 7 in the column 10=3+7
to add 5+7 -> we get 7 in the column 12=5+7>11 - in fact we don't need this operation
5.So to get the result we check the column 11.
Since there we have the value 8 - we go to the left by 8 and obtain 3, then go to the left by 3 and obtain 0.
So 11=8+3
Here is the code in python:
#
def subset_with_given_sum(li, m):
a=[0]
b=[0]+[-1]*m
for x in li:
c=[]
for i in a:
if i+x<=m and b[i+x]<0:
b[i+x]=x
c.append(I+x)
a.extend(c)
print(b)
if b[m]>0:
print(b)
result=[b[m]]
j=m-b[m]
while j>0:
result.append(b[j])
j-=b[j]
print('{} -> {}'.format(result, m))
return result
print('no such sum -> {}'.format(m))
return

def test():
li=[2, 3, 7, 8, 10]
m=11
subset_with_given_sum(li, m)
m=14
subset_with_given_sum(li, m)
m=16
subset_with_given_sum(li, m)
m=19
subset_with_given_sum(li, m)

serhiypidkuyko
Автор

Directly jumping on the board without telling anything how to approach😂😂😂

rohit-ldfc
Автор

Thanks alot, this really helped me. By you showing how to calculate the values T[i][j] I could finally understand what the idea was.

ramo
Автор

Thanks for jumping straight into useful explanation instead of spending 10 min talking about obvious stuff like many videos

Vavaxman
Автор

Wow, this made my life much easier. Thank you! I was noticing the pattern in the 2d matrix, but was not understanding why this pattern made sense.

MatthewPostrel
Автор

If this code doesn't works you then, you should know that the first row should be initialized all False (the row which is even above the (0, 0) here because we need to look upwards each iteration so there should be something to look for in first iteration.

rahgurung
Автор

Thanks man! U helped me a lot... Please keep making such useful videos for us

jamespottex