Code Monkey home page Code Monkey logo

knapsack-dynamic's Introduction

Introduction:

A solution to the well-known problem of packing a number of items in a backpack, with total weight less or equal to given weight W, maximizing the total value V they add to the backpack, given a number of items described with their weight and value accordingly. https://en.wikipedia.org/wiki/Knapsack_problem

In this solution, the indexes of the items that make the final cut are provided at the end.

Solution:

Basic premise: We try to fit as many items, starting from one, to as many as possible, always looking to maximize the value. At every step we see:

  • will we get bigger value if we include this item to the items which allow for it's weight or
  • will we get bigger value if don't include this item, and go with the items packed so-far for this weight

Also:

  • preferring the smallest weight, in case of multiple combinations resulting in same value, meant that we will need to sort the items by weight first (so that we always prefer adding the lighter item)
  • The overhead of backtracking for the indexes, was overcome by using a small OOP design for the intermediary result item (the Backpack class)
To run it:
mvn clean install
java -jar ./target/packer.jar $(pwd)/src/main/resources/test-cases.txt
Test cases

Test cases should be provided in the form of:

"<weight-limit> : (<index>, <weight>, €<value>) (<index>, <weight>, €<value>) ... (<index>, <weight>, €<value>)"

knapsack-dynamic's People

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.