Code Monkey home page Code Monkey logo

js-combinatorics's Introduction

build status

combinatorics.js

Simple combinatorics like power set, combination, and permutation in JavaScript

SYNOPSIS

In Browser

<script src="combinatorics.js"></script>

node.js

var Combinatorics = require('./combinatorics.js').Combinatorics;

power set

var cmb, a;
cmb = Combinatorics.power(['a','b','c']);
cmb.each(function(a){ console.log(a) });
//  []
//  ["a"]
//  ["b"]
//  ["a", "b"]
//  ["c"]
//  ["a", "c"]
//  ["b", "c"]
//  ["a", "b", "c"]

combination

cmb = Combinatorics.combination(['a','b','c','d'], 2);
while(a = cmb.next()) console.log(a);
//  ["a", "b"]
//  ["a", "c"]
//  ["a", "d"]
//  ["b", "c"]
//  ["b", "d"]
//  ["c", "d"]

permutation

cmb = Combinatorics.permutation(['a','b','c','d']); // assumes 4
console.log(cmb.toArray());
//  [
  ["a","b","c","d"],["a","b","d","c"],["a","c","b","d"],["a","c","d","b"],
  ["a","d","b","c"],["a","d","c","b"],["b","a","c","d"],["b","a","d","c"],
  ["b","c","a","d"],["b","c","d","a"],["b","d","a","c"],["b","d","c","a"],
  ["c","a","b","d"],["c","a","d","b"],["c","b","a","d"],["c","b","d","a"],
  ["c","d","a","b"],["c","d","b","a"],["d","a","b","c"],["d","a","c","b"],
  ["d","b","a","c"],["d","b","c","a"],["d","c","a","b"],["d","c","b","a"]
]

cartesian product

cp = Combinatorics.cartesianProduct([0, 1, 2], [0, 10, 20], [0, 100, 200]);
console.log(cp.toArray());
//  [
  [0, 0, 0],   [1, 0, 0],   [2, 0, 0],
  [0, 10, 0],  [1, 10, 0],  [2, 10, 0],
  [0, 20, 0],  [1, 20, 0],  [2, 20, 0],
  [0, 0, 100], [1, 0, 100], [2, 0, 100],
  [0, 10, 100],[1, 10, 100],[2, 10, 100],
  [0, 20, 100],[1, 20, 100],[2, 20, 100],
  [0, 0, 200], [1, 0, 200], [2, 0, 200],
  [0, 10, 200],[1, 10, 200],[2, 10, 200],
  [0, 20, 200],[1, 20, 200],[2, 20, 200]
]

Arithmetic Functions

DESCRIPTION

All methods create generators. Instead of creating all elements at once, each element is created on demand. So it is memory efficient even when you need to iterate through millions of elements.

Combinatorics.power( ary )

Creates a generator which generates the power set of ary

Combinatorics.combination( ary , nelem )

Creates a generator which generates the combination of ary with nelem elements. When nelem is ommited, ary.length is used.

Combinatorics.permutation( ary, nelem )

Creates a generator which generates the permutation of ary with nelem elements. When nelem is ommited, ary.length is used.

Combinatorics.cartesianProduct( ary0, ...)

Creates a generator which generates the cartesian product of the arrays. All arguments must be arrays with more than one element.

Generator Methods

All generators have following methods:

.next()

Returns the element or undefined if no more element is available.

.forEach(function(a){ ... });

Applies the callback function for each element.

.toArray()

All elements at once.

.map(function(a){ ... })

All elements at once with function f applied to each element.

.filter(function(a){ ... })

Returns an array with elements that passes the filter function. For example, you can redefine combination as follows:

myCombination = function(ary, n) {
  return Combinatorics.power(ary).filter(function (a) {
    return a.length === n;
  });
};

.length

Returns the number of elements to be generated Which equals to generator.toArray().length but it is precalculated without actually generating elements. Handy when you prepare for large iteraiton.

0 + generator

Same as generator.length

.nth(n)

Available for power and cartesianProduct generator which returns the nth element.

.get(x0, ...)

Available for cartesianProduct generator. Arguments are coordinates in integer. Arguments can be out of bounds but it returns undefined in such cases.

js-combinatorics's People

Contributors

dankogai avatar vzvu3k6k avatar

Watchers

James Cloos avatar Jordan Hill 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.