Lab Experiment-6.docx
Document Details
Uploaded by Deleted User
Tags
Full Transcript
**1. 0/1 Knapsack Problem** **Theory:** The 0/1 Knapsack problem is a classic optimization problem where you are given a set of items, each with a weight and a profit (or value). The goal is to determine the maximum profit you can obtain by selecting items to include in a knapsack, such that the t...
**1. 0/1 Knapsack Problem** **Theory:** The 0/1 Knapsack problem is a classic optimization problem where you are given a set of items, each with a weight and a profit (or value). The goal is to determine the maximum profit you can obtain by selecting items to include in a knapsack, such that the total weight does not exceed a given capacity. **Algorithm**: **Programming Language:** **Code:** def knapsack(capacity, weights, profits): n = len(weights) ratio = \[(profits\[i\] / weights\[i\], weights\[i\], profits\[i\]) for i in range(n)\] ratio.sort(reverse=True, key=lambda x: x\[0\]) profit\_value = 0 for r, weight, profit in ratio: if weight \