700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > java蛮力法背包问题_蛮力法 动态规划法 求解01背包问题

java蛮力法背包问题_蛮力法 动态规划法 求解01背包问题

时间:2024-03-23 03:37:05

相关推荐

java蛮力法背包问题_蛮力法 动态规划法 求解01背包问题

V(i,j)= (2)

max{V(i-1,j),V(i-1,j-wi)+vi} j>=w

2)复杂度分析:2n

三、程序伪码:

(1)Brute Force:

Algorithm MFKnapsack(capacity, values, weights, n)

// Input: A nonnegative integer n indicating the number of the frist items being

//considered and a nonnegative integer capacity indicating the knapsack’s capacity, A

array values[] indicating the values of items, A array weights[] indicating the weights

of items.

//Output: The value of an optimal feasible subset of the first i items

if d== c.size()

for (int i = 0; i != d; ++i)

if c[i] == 1

cur_weight += weights[i];

cur_value += values[i];

if cur_weight <= capacity && cur_value > max_value

max_value cur_value;

return;

(2)Dynamic Programming:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。