Coder Social home page Coder Social logo

knapsack-problem's Introduction

Knapsack-Problem

Solution of knapsack problem using dynamic programming

Purpose

To get as much value into the knapsack as possible given the weight constraint of the knapsack.

Input

  • Maximum weight (capacity of knapsack)
  • Item Count
  • Item Value
  • Item Weight

Output

  • Maximum Value
  • Chosen Items

Solution Approach

  • Evaluate the values of the items iteratively.
  • For example, put the first item and select second item. Then evaluate first and second item. Make the most appropriate choice according to the value and weight of the items. And then evaluate the all item.
  • In order to get rid of the recalculation, the calculation for the items are kept on a table at each step.
  • After the items have been evaluated, the value of V[ItemCount,MaximumWeight] shows the maximum value we can get to the knapsack.

Example

  • Item Count = 4
  • Max Weight = 5
Item 1 2 3 4
Value 100 20 60 40
Weight 3 2 4 1

Value Matrix

V[i,w] w=0 1 2 3 4 5
i=0 0 0 0 0 0 0
1 0 0 0 100 100 100
2 0 0 20 100 100 120
3 0 0 20 100 100 120
4 0 40 40 100 140 140

Maximum value we can put the knapsack is V[4,5] = 140

Chosen items are 1 and 4

How the items are selected can be seen in the code

knapsack-problem's People

Contributors

ayzahmt avatar

Stargazers

 avatar

Watchers

 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.