function knapsackSolveByDP(numsOfObject, capacityOfKnapsack, weight, value): result = [] # 用于存储最优解的物品索引 dp = new2D array of size (numsOfObject + 1) x (capacityOfKnapsack + 1) initialized to 0
for i from 1 to numsOfObject: for j from 1 to capacityOfKnapsack: if weight[i - 1] > j: dp[i][j] = dp[i - 1][j] else: dp[i][j] = max(value[i - 1] + dp[i - 1][j - weight[i - 1]], dp[i - 1][j])
i = numsOfObject j = capacityOfKnapsack
while i > 0and j > 0: if dp[i][j] == dp[i - 1][j]: i = i - 1 else: result.push(i - 1) # 将当前物品的索引加入结果 j = j - weight[i - 1] i = i - 1
function knapsackSolveByGreedy(numsOfObject, capacityOfKnapsack, weight, value): result = [] # 用于存储最优解的物品索引 valuePerWeight = [] # 存储每个物品的单位重量价值和对应的物品索引
for i from 0 to numsOfObject - 1: valuePerWeight.push((value[i] / weight[i], i))
# 根据单位重量价值从高到低对物品进行排序 sort(valuePerWeight in descending order by the first element)
for i from 0 to numsOfObject - 1: index = valuePerWeight[i].second # 取出单位重量价值最高的物品索引 if capacityOfKnapsack >= weight[index]: result.push(index) # 将当前物品的索引加入结果 capacityOfKnapsack -= weight[index]
return result # 返回最优解的物品索引
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
vector<int> knapsackSolveByGreedy(int numsOfObject, int capacityOfKnapsack, vector<int> weight, vector<int> value) { vector<int> result; vector<pair<double, int>> valuePerWeight; for (int i = 0; i < numsOfObject; i++) { valuePerWeight.push_back(make_pair(value[i] / weight[i], i)); } sort(valuePerWeight.begin(), valuePerWeight.end(), [](pair<double, int> a, pair<double, int> b) {return a.first > b.first; }); for (int i = 0; i < numsOfObject; i++) { if (capacityOfKnapsack >= weight[valuePerWeight[i].second]) { result.push_back(valuePerWeight[i].second); capacityOfKnapsack -= weight[valuePerWeight[i].second]; } } return result; }
Function knapsackSolveByBranchAndBound(numsOfObject, capacityOfKnapsack, weight, value): // Create a priority queue to manage nodes pq = Priority Queue // Sort items based on value/weight ratio Sort items by(value / weight) in descending order // Initialize variables to track the best solution maxValue = 0 selectedItems = Array of size numsOfObject // Create and initialize the root node root = Node(level: 0, weight: 0, value: 0, bound: calculateBound(root, items, capacityOfKnapsack)) root.selectedItems = Array of size numsOfObject // Push the root node to the priority queue Push root to pq // Start processing nodes while pq is not empty: node = Pop the highest priority node from pq // Check if the bound of the node is greater than the current best value if node.bound > maxValue: // Create left and right child nodes left = Copy node right = Copy node // Process the left child node (include the item) left.level = node.level + 1 left.weight += items[left.level - 1].weight left.value += items[left.level - 1].value left.selectedItems[left.level - 1] = true left.bound = calculateBound(left, items, capacityOfKnapsack) if left.weight <= capacityOfKnapsack and left.bound > maxValue: maxValue = left.value Copy left.selectedItems to selectedItems if left.bound > maxValue and left.level - 1 < numsOfObject: Push left to pq // Process the right child node (exclude the item) right.level = node.level + 1 right.bound = calculateBound(right, items, capacityOfKnapsack) if right.bound > maxValue and right.level - 1 < numsOfObject: Push right to pq result = Empty Array for i from 0 to numsOfObject: if selectedItems[i] is true: Add items[i] to result Return result End Function