# fast-knapsack

> Lightweight, flexible, and efficient solution for knapsack problems. This is definitely what you are looking for.

Latest version **0.1.1** (published 2018-02-09) · MIT license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install fast-knapsack
pnpm add fast-knapsack
yarn add fast-knapsack
bun add fast-knapsack
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 0.1.1 |
| Published | 2018-02-09 |
| First published | 2018-02-09 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Unpacked size | 5.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Aemielvin Loremia |
| Maintainers | erocrizs |
| Keywords | knapsack, algorithm |

## Links

- npm: https://www.npmjs.com/package/fast-knapsack
- Repository: https://github.com/erocrizs/fast-knapsack
- Homepage: https://github.com/erocrizs/fast-knapsack#readme
- Issues: https://github.com/erocrizs/fast-knapsack/issues
- npm.io page: https://npm.io/package/fast-knapsack

## Recent versions

- 0.1.1 (latest) — 2018-02-09
- 0.1.0 — 2018-02-09

## README

Fast Knapsack (WIP)
================

[![Build Status](https://travis-ci.org/erocrizs/fast-knapsack.svg?branch=master)](https://travis-ci.org/erocrizs/fast-knapsack) 
[![Coverage Status](https://coveralls.io/repos/github/erocrizs/fast-knapsack/badge.svg?branch=setup-coveralls)](https://coveralls.io/github/erocrizs/fast-knapsack?branch=setup-coveralls)

Lightweight, flexible, and efficient solution for knapsack problems. This is definitely what you are looking for.

This module provides an algorithm for solving the classic knapsack problem in polynomial time using dynamic programming.

## Installation
```
npm install fast-knapsack --save
```

### Usage
```js
const knapsack = require('fast-knapsack');
const items = {
    'bee': {w: 2, v: 3},
    'cat': {w: 3, v: 4},
    'dog': {w: 5, v: 6}
};

// run by promise
knapsack(items, 6, 'w', 'v')
    .promise()
    .then(res => {
        // success
    })
    .catch(err => {
        // fail
    });

// run by callbacks
knapsack(items, 6, 'w', 'v')
    .callback((err, success) => {
        if (err) {
            // fail
            return;
        }
        // success
    })
```

### API
#### `knapsack (items, limit[, weight, value, count])`
Constructs and returns a new object that contains different methods for solving the knapsack problem.
* `items`: collection of input items for knapsack
    - array (each element is one item)
    - plain JSON object (each key/value is one item)
* `limit`: maximum weight capacity of knapsack
    - number (representing maximum capacity)
* `weight` (optional): used for getting the weight of an item
    - number (weight of each item)
    - string (the attribute of the element containing the weight)
    - function (takes in item object and returns the item's weight)
    - default is `undefined` (assumes all are weighted as 1)
* `value` (optional): used for getting the value of an item
    - number (value of each item)
    - string (the attribute of the element containing the value)
    - function (takes in item object and returns the item's value)
    - default is `undefined`, assumes all item's value equals item weight
* `count` (optional): used for determing how many specific item are remaining.
    - number (count of each item)
    - string (the attribute of the element containing the count)
    - function (takes in item object and returns the item's count)
    - default is `undefined`, assumes there's only one of each item

#### `promise ()`
Returns a promise that gets resolved after completing the knapsack solution.

#### `run (callback)`
Input callback gets called once after completing the knapsack solution.

---
_Source: https://npm.io/package/fast-knapsack · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
