Code Monkey home page Code Monkey logo

knapsack-problem's Introduction

Knapsack-Problem

Iterative algorithm solution for the knapsack problem

From Wikipedia, the knapsack problem or rucksack problem is a problem in combinatorial optimization: Given a set of items, each with a weight and a value, determine the number of each item to include in a collection so that the total weight is less than or equal to a given limit and the total value is as large as possible.

In this specific algorithm, it is assumed the total value we are looking to maximize is found by multiplying the value of each item in the knapsack. It can be easily modified for a simpler version which involves adding the values instead.

Usage

C = int
S = []
V = []

maxProductWrapper(C,S,V)

C is an int that represents the size (or weight) of the knapsack. S is an array containing each respective items' size (or weight). V is an array containing each respective items' value.

Each element in S directly correponds to an element in V respectively.

Examples

C = 12
S = [3,7,4,6,5]
V = [5,12,6,9,7]

maxProductWrapper(C,S,V)

returns [648, 6, 7, 4].

C indicates the knapsack can hold a weight of up to 12. Each element in S corresponds to the respective item's size (weight), while each element in V corresponds to the respective item's value. Thus item 1 is of size "3" and value "5". Item 2 is of size "7" and value "12" and so on.

The first returned value, 648, represents the maximum value found. The remaining values represent the size of the items chosen. Thus for the largest value for the knapsack, items of size 6, 7 and 4 will yield the highest value. Their respective values (9,12,6) multiplied equal 648.

Built with

Python

knapsack-problem's People

Contributors

andrewzaw avatar

Watchers

James Cloos avatar

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.