Coder Social home page Coder Social logo

knapsack-example's Introduction

knapsack-example

Knapsack algorithm implementation based on https://stackoverflow.com/a/49353839/4448410

How to select N products that their price sums to S randomly

This repo provides an example of an algorithm to :

  • select N products from a mysql database table
  • their prices sum to S
  • do this randomly(or appear random)

Knapsack problem

This is a version of the https://en.wikipedia.org/wiki/Knapsack_problem

Algorithm

The algorithm is based on the following:

In a collection of n positive numbers that sum up to S, at least one of them will be less than S divided by n (S/n)

Steps

  1. Select a product randomly where price < S/N. Get its price, lets say X.
  2. Select a product randomly where price < (S - X)/(N-1). Get its price, assume Y.
  3. Select a product randomly where price < (S - X - Y)/(N-2).
  4. Repeat this and get till you get N-1 elements and the remainingPrive P.
  5. Select a product that price equals to P. If not found, repeat.

Variation

Usage

This is intended to be used for providing random collections of products that their price sum to a fixed value.

knapsack-example's People

Contributors

iobotis 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.