Giter Site home page Giter Site logo

node-subset-sum's Introduction

node-subset-sum

Polynomial approximation of the subset-sum problem.

For the given t integer and arr set of integers, the algorithm computes the closest possible sum to t from a subset of arr (the result is equal or less that t). Example: arr=[2, 3, 10], t=14, the closest sum is 13, since 10+3=13.

The subset-sum problem is NP-hard, so computing the exact solution takes exponential time.

This module uses a polynomial approximation algorithm, that allows you to fine-tune the error/time-complexity ratio.

Install

$ npm install subset-sum

Usage

var approx = require('subset-sum');

approx(arr, t[, eps])

Return the closest sum to t (lesser or equal) that can be computed from a subset of arr.

With eps = 0 the algorithm has exponential time, but it is accurate. With eps > 0 the computation has polynomial complexity, but the result may have error up to eps*t (so a closer sum to t may exist).

eps can be arbitrary small, but as eps goes to 0, the complexity becomes exponential.

Example

console.log(approx([2,3,4,5,10,11,13,16,22,28,30], 97)); // eps = 0, the solution is accurate (97)
console.log(approx([2,3,4,5,10,11,13,16,22,28,30], 97, 0.5)); // eps = 0.5, the solution has error (94)

Benchmark

See benchmark.js

License

MIT

node-subset-sum's People

Contributors

madbence avatar

Stargazers

 avatar

Watchers

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