lecture10_P6_BB.py
1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
def kp(i, profit, weight):
global bestset
global maxprofit
if( weight <= W and profit >maxp):
maxp = profit
bestset = include[:]
# best = include는 best를 include의 reference로 만든다.
# 한 번 동일한 값을 가진 후 그 이후는 계속 동일함.
if(promising(i,weight,profit)):
include[i+1]=1
kp(i+1,profit+p[i+1], weight+w[i+1])
include[i+1]=0
kp(i+1,profit, weight)
def promising(i,weight,profit):
global maxprofit
if(weight >=W):
return False
else:
j=i+1
bound = profit
totweight = weight
# item이 있고 그 무게 안에서 최대의 bound를 찾고
while((j <= n-1) and (totweight + w[j] <= W)):
totweight += w[j]
bound += p[j]
j+=1
# k = j
if(j <= n-1):
bound += (W-totweight)*p[k]/w[k] # 최종 bound 검색
return bound > maxprofit