Giter Site home page Giter Site logo

mgabilo / knapsack-algorithms Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 1.0 5 KB

Different algorithms to solve 0-1 knapsack and an algorithm to solve continuous knapsack

License: MIT License

Python 100.00%
computer-science-algorithms dynamic-programming toy-problems

knapsack-algorithms's Introduction

knapsack-algorithms

This is a simple Python script that demonstrates the following algorithms:

The script also counts the number of recursive calls for the recursive solutions to 0-1 knapsack and the number of iterations for the dynamic programming solution.

In the 0-1 knapsack problem, you're given a set of items, each of which has a value and a weight, and a knapsack with a maximum weight capacity. The object is to select a subset of the items to put into the knapsack such that the sum of the values of items in the knapsack is maximized (you can't pick another subset more valuable) but the sum of the weights of items in the knapsack does not exceed the maximum weight capacity. For each item, you can either select it once, or not select it at all; so, you can't select an item more than once, or select a fraction of an item, for example.

In the fractional knapsack problem, the object is the same, but you're allowed to select fractions of each item. For example, if an item has value 20 and weight 40, you can select 0.5 (half) of the item, yielding a value of 10 and a weight of 20. You can only select at most one of each item.

Example Usage

To run the script simply execute it on the command line.

./knapsack.py

It will produce an output that should look like the file knapsack-output.txt.

To modify the set of items from which you can select to put into the knapsack, modify the top of the script knapsack.py.

items = [ [20, 50], [15, 45], [10, 35], [20, 35], [25, 30], [30, 30], [15, 20], [10, 15], [5, 10], [20, 10] ]
weight_capacity = 166

Each pair represents an item, the first value is the item's value and the second is its weight. The variable weight_capacity is the total weight that can fit into the knapsack.

License

This project is licensed under the MIT License (see the LICENSE file for details).

Authors

knapsack-algorithms's People

Contributors

mgabilo avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

mody3062

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.