算法分析与设计课后练习20

用启发式的方法求0/1背包问题
n=5
(w1,w2,w3,w4,w5)=(2,2,6,5,4)
(p1,p2,p3,p4,p5)=(6,3,5,4,6)
M=10

令L=15
Pleft(0)=24
Pleft(1)=18
Pleft(2)=15
Pleft(3)=10
Pleft(4)=6
Pleft(5)=0
S0={(0,0)}
S11={(6,2)}
0+ Pleft(1)=18=18>15
S1={(0,0), (6,2)}
S12={(3,2),(9,4)}
0+ Pleft(2)=15
6+ Pleft(2)=21
S2={(0,0),(3,2),(6,2),(9,4)}
S13={(5,6),(8,8),(11,8),(14,10)}
0+ Pleft(3)=10
3+ Pleft(3)=13
6+ Pleft(3)=16
9+ Pleft(3)=19
S3={(5,6),(6,2),(8,8),(9,4),(11,8),(14,10)}
S14={(9,11),(10,7),(12,13),(13,9),(15,13),(18,15)}
5+ Pleft(4)=9
6+ Pleft(4)=12
8+ Pleft(4)=14
9+ Pleft(4)=15
11+ Pleft(4)=17
14+ Pleft(4)=20
S4={(9,4),(10,7),(11,8),(13,9),(14,10)}
S14={(15,8),(16,11),(17,12),(19,13),(20,14)}
9+ Pleft(5)=9
10+ Pleft(5)=10
11+ Pleft(5)=11
13+ Pleft(5)=13
14+ Pleft(5)=14
S5={(15,8),}
最大代价为15,背包重量8
S5中(15,8)∈S14,(15-6,8-4)= (9,4)∈S4则x5=1
S4 中(9,4)∈S3则x4=0
S3中(9,4)∈S2则x3=0
S2中(9,4)∈S12,(9-3,4-2)= (6,2)∈S1则x2=1
S1中(6,2)∈S11,(6-6,2-2)= (0,0)∈S0则x1=1
则Xn={1,1,0,0,1}

你可能感兴趣的:(算法课作业,算法,java,前端)