Subset sum Problem

后端 未结 6 1127
Happy的楠姐
Happy的楠姐 2020-12-01 13:32

recently I became interested in the subset-sum problem which is finding a zero-sum subset in a superset. I found some solutions on SO, in addition, I came across a particula

6条回答
  •  失恋的感觉
    2020-12-01 14:00

    Just change the values in your set w and correspondingly make an array x as big as the len of w then pass the last value in the subsetsum function as the sum for which u want subsets and you wl bw done (if u want to check by giving your own values).

    def subsetsum(cs,k,r,x,w,d):
        x[k]=1
        if(cs+w[k]==d):
            for i in range(0,k+1):
    
                if x[i]==1:
                    print (w[i],end=" ")
            print()
    
        elif cs+w[k]+w[k+1]<=d :
            subsetsum(cs+w[k],k+1,r-w[k],x,w,d)
    
        if((cs +r-w[k]>=d) and (cs+w[k]<=d)) :
            x[k]=0
            subsetsum(cs,k+1,r-w[k],x,w,d)
    #driver for the above code
    w=[2,3,4,5,0]
    x=[0,0,0,0,0]
    
    subsetsum(0,0,sum(w),x,w,7)     
    

提交回复
热议问题