├── README.md ├── dist ├── knapsack_algorithm-1.0.0-py3-none-any.whl └── knapsack_algorithm-1.0.0.tar.gz ├── knapsack ├── init.py ├── knapsack_algorithm.py └── test.py ├── knapsack_algorithm.egg-info ├── PKG-INFO ├── SOURCES.txt ├── dependency_links.txt └── top_level.txt └── setup.py /README.md: -------------------------------------------------------------------------------- 1 | ## Knapsack Algorithm 2 | 3 | knapsack_algorithm is a Python package that provides a simple and efficient solution for the 0/1 knapsack problem. 4 | 5 | ### Features 6 | 7 | - **Dynamic Programming Solution**: Utilizes dynamic programming to solve the 0/1 knapsack problem efficiently. 8 | - **Error Handling**: Provides comprehensive error handling for input validation. 9 | - **Easy to Use**: Offers a user-friendly interface for solving knapsack problems with given values, weights, and capacity. 10 | 11 | ### Installation: 12 | 13 | You can install KnapsackAlgorithm using pip: 14 | 15 | pip install knapsack_algorithm 16 | 17 | #### Usage: 18 | 19 | Here's an example of how you can use knapsack_algorithm: 20 | 21 | from knapsack_algorithm import knapsack 22 | 23 | #### Documentation 24 | For detailed documentation and additional options, refer to the official documentation. 25 | 26 | #### License 27 | This project is licensed under the MIT License - see the LICENSE file for details. 28 | 29 | #### Contributing 30 | If you would like to contribute or report issues, please check our contribution guidelines. 31 | #### Example usage: 32 | 33 | ```bash 34 | values = [60, 100, 120] # The values of the items 35 | 36 | weights = [10, 20, 30] # The weights of the items 37 | 38 | capacity = 50 # The maximum capacity 39 | 40 | result = knapsack(values, weights, capacity) 41 | 42 | if result is not None: 43 | 44 | print("Maximum value in the knapsack:", result) 45 | 46 | 47 | -------------------------------------------------------------------------------- /dist/knapsack_algorithm-1.0.0-py3-none-any.whl: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tamilselvanarjun/knapsack_algorithm/4f6977f50fa022ff1d124e36547719756089fb91/dist/knapsack_algorithm-1.0.0-py3-none-any.whl -------------------------------------------------------------------------------- /dist/knapsack_algorithm-1.0.0.tar.gz: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tamilselvanarjun/knapsack_algorithm/4f6977f50fa022ff1d124e36547719756089fb91/dist/knapsack_algorithm-1.0.0.tar.gz -------------------------------------------------------------------------------- /knapsack/init.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tamilselvanarjun/knapsack_algorithm/4f6977f50fa022ff1d124e36547719756089fb91/knapsack/init.py -------------------------------------------------------------------------------- /knapsack/knapsack_algorithm.py: -------------------------------------------------------------------------------- 1 | def knapsack(values, weights, capacity): 2 | try: 3 | # Check for input errors 4 | if len(values) != len(weights): 5 | raise ValueError("Length of values and weights must be the same.") 6 | if capacity < 0: 7 | raise ValueError("Capacity must be a non-negative integer.") 8 | 9 | n = len(values) # Number of items 10 | # Create a 2D DP array to store the maximum value that can be obtained using the first i items and capacity 11 | dp = [[0 for _ in range(capacity + 1)] for _ in range(n + 1)] 12 | 13 | # Build the DP table in bottom-up manner 14 | for i in range(1, n + 1): 15 | for w in range(1, capacity + 1): 16 | if weights[i-1] <= w: 17 | # If the current item can fit in the knapsack, choose the maximum between taking the item and not taking it 18 | dp[i][w] = max(dp[i-1][w], dp[i-1][w-weights[i-1]] + values[i-1]) 19 | else: 20 | # If the current item cannot fit, do not take it 21 | dp[i][w] = dp[i-1][w] 22 | 23 | # The answer to the problem is in dp[n][capacity], which represents considering all items with the full capacity. 24 | return dp[n][capacity] 25 | 26 | except ValueError as ve: 27 | print(f"Input error: {ve}") 28 | return None 29 | except Exception as e: 30 | print(f"An unexpected error occurred: {e}") 31 | return None 32 | 33 | # Example usage: 34 | #values = [60, 100, 120] # The values of the items 35 | #weights = [10, 20, 30] # The weights of the items 36 | #capacity = 50 # The maximum capacity of the knapsack 37 | #result = knapsack(values, weights, capacity) 38 | #if result is not None: 39 | # print(result) 40 | -------------------------------------------------------------------------------- /knapsack/test.py: -------------------------------------------------------------------------------- 1 | import unittest 2 | from knapsack_algorithm import knapsack 3 | 4 | class TestKnapsack(unittest.TestCase): 5 | def test_valid_input(self): 6 | values = [60, 100, 120] 7 | weights = [10, 20, 30] 8 | capacity = 50 9 | result = knapsack(values, weights, capacity) 10 | self.assertEqual(result, 220) 11 | 12 | def test_invalid_input_negative_capacity(self): 13 | values = [60, 100, 120] 14 | weights = [10, 20, 30] 15 | capacity = -50 16 | result = knapsack(values, weights, capacity) 17 | self.assertIsNone(result) 18 | 19 | def test_invalid_input_empty_lists(self): 20 | values = [] 21 | weights = [] 22 | capacity = 50 23 | result = knapsack(values, weights, capacity) 24 | self.assertIsNone(result) 25 | 26 | def test_invalid_input_zero_capacity(self): 27 | values = [60, 100, 120] 28 | weights = [10, 20, 30] 29 | capacity = 0 30 | result = knapsack(values, weights, capacity) 31 | self.assertEqual(result, 0) # Knapsack capacity is zero, so result should be zero. 32 | 33 | def test_invalid_input_zero_weight(self): 34 | values = [60, 100, 120] 35 | weights = [0, 0, 0] 36 | capacity = 50 37 | result = knapsack(values, weights, capacity) 38 | self.assertEqual(result, 0) # All items have zero weight, so result should be zero. 39 | 40 | def test_valid_input_duplicate_items(self): 41 | values = [60, 100, 120, 60] # Duplicate item value 42 | weights = [10, 20, 30, 10] # Duplicate item weight 43 | capacity = 50 44 | result = knapsack(values, weights, capacity) 45 | self.assertEqual(result, 220) # Duplicate items should not affect the result 46 | 47 | if __name__ == '__main__': 48 | unittest.main() 49 | -------------------------------------------------------------------------------- /knapsack_algorithm.egg-info/PKG-INFO: -------------------------------------------------------------------------------- 1 | Metadata-Version: 2.1 2 | Name: knapsack_algorithm 3 | Version: 1.0.0 4 | Summary: A python implementation of the Knapsack problem using dynamic programming. 5 | Home-page: https://github.com/arjunlimat/knapsack_algorithm 6 | Author: Tamilselvan_Arjunan 7 | Author-email: nishantamil@gmail.com 8 | Classifier: Programming Language :: Python :: 3 9 | Classifier: License :: OSI Approved :: MIT License 10 | Classifier: Operating System :: OS Independent 11 | Requires-Python: >=3.6 12 | Description-Content-Type: text/markdown 13 | 14 | ## Knapsack Algorithm 15 | 16 | knapsack_algorithm is a Python package that provides a simple and efficient solution for the 0/1 knapsack problem. 17 | 18 | ### Features 19 | 20 | - **Dynamic Programming Solution**: Utilizes dynamic programming to solve the 0/1 knapsack problem efficiently. 21 | - **Error Handling**: Provides comprehensive error handling for input validation. 22 | - **Easy to Use**: Offers a user-friendly interface for solving knapsack problems with given values, weights, and capacity. 23 | 24 | ### Installation: 25 | 26 | You can install KnapsackAlgorithm using pip: 27 | 28 | pip install knapsack_algorithm 29 | 30 | #### Usage: 31 | 32 | Here's an example of how you can use knapsack_algorithm: 33 | 34 | from knapsack_algorithm import knapsack 35 | 36 | #### Documentation 37 | For detailed documentation and additional options, refer to the official documentation. 38 | 39 | #### License 40 | This project is licensed under the MIT License - see the LICENSE file for details. 41 | 42 | #### Contributing 43 | If you would like to contribute or report issues, please check our contribution guidelines. 44 | #### Example usage: 45 | 46 | ```bash 47 | values = [60, 100, 120] # The values of the items 48 | 49 | weights = [10, 20, 30] # The weights of the items 50 | 51 | capacity = 50 # The maximum capacity 52 | 53 | result = knapsack(values, weights, capacity) 54 | 55 | if result is not None: 56 | 57 | print("Maximum value in the knapsack:", result) 58 | 59 | 60 | -------------------------------------------------------------------------------- /knapsack_algorithm.egg-info/SOURCES.txt: -------------------------------------------------------------------------------- 1 | README.md 2 | setup.py 3 | knapsack_algorithm.egg-info/PKG-INFO 4 | knapsack_algorithm.egg-info/SOURCES.txt 5 | knapsack_algorithm.egg-info/dependency_links.txt 6 | knapsack_algorithm.egg-info/top_level.txt -------------------------------------------------------------------------------- /knapsack_algorithm.egg-info/dependency_links.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /knapsack_algorithm.egg-info/top_level.txt: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | # setup.py 2 | 3 | from setuptools import setup, find_packages 4 | 5 | setup( 6 | name='knapsack_algorithm', 7 | version='1.0.0', 8 | author='Tamilselvan_Arjunan', 9 | author_email='nishantamil@gmail.com', 10 | description='A python implementation of the Knapsack problem using dynamic programming.', 11 | long_description=open('README.md').read(), 12 | long_description_content_type='text/markdown', 13 | url='https://github.com/arjunlimat/knapsack_algorithm', 14 | packages=find_packages(), 15 | install_requires=[ 16 | # List your package dependencies here, e.g., 17 | # 'numpy', 18 | # 'pandas', 19 | ], 20 | classifiers=[ 21 | 'Programming Language :: Python :: 3', 22 | 'License :: OSI Approved :: MIT License', 23 | 'Operating System :: OS Independent', 24 | ], 25 | python_requires='>=3.6', 26 | ) --------------------------------------------------------------------------------