{"id":37030251,"url":"https://github.com/dpaukov/combinatoricslib","last_synced_at":"2026-01-14T03:41:16.888Z","repository":{"id":21589438,"uuid":"24909497","full_name":"dpaukov/combinatoricslib","owner":"dpaukov","description":"Combinatorial Objects Generators for Java 7+.","archived":false,"fork":false,"pushed_at":"2021-08-13T05:26:09.000Z","size":2249,"stargazers_count":91,"open_issues_count":5,"forks_count":15,"subscribers_count":7,"default_branch":"master","last_synced_at":"2025-09-12T23:17:05.716Z","etag":null,"topics":["cartesian-products","combinations","combinatorics","java","permutation","subsets"],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"lgpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dpaukov.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-10-07T20:12:22.000Z","updated_at":"2025-08-17T08:51:03.000Z","dependencies_parsed_at":"2022-08-21T20:21:00.704Z","dependency_job_id":null,"html_url":"https://github.com/dpaukov/combinatoricslib","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/dpaukov/combinatoricslib","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dpaukov%2Fcombinatoricslib","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dpaukov%2Fcombinatoricslib/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dpaukov%2Fcombinatoricslib/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dpaukov%2Fcombinatoricslib/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dpaukov","download_url":"https://codeload.github.com/dpaukov/combinatoricslib/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dpaukov%2Fcombinatoricslib/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28408854,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-14T01:52:23.358Z","status":"online","status_checked_at":"2026-01-14T02:00:06.678Z","response_time":107,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cartesian-products","combinations","combinatorics","java","permutation","subsets"],"created_at":"2026-01-14T03:41:14.115Z","updated_at":"2026-01-14T03:41:16.863Z","avatar_url":"https://github.com/dpaukov.png","language":"Java","readme":"[![Build Status](https://travis-ci.com/dpaukov/combinatoricslib.svg?branch=master)](https://travis-ci.com/github/dpaukov/combinatoricslib)\n[![Coverage Status](https://coveralls.io/repos/dpaukov/combinatoricslib/badge.svg?branch=master)](https://coveralls.io/r/dpaukov/combinatoricslib?branch=master) \n[![Maven Central](https://img.shields.io/maven-central/v/com.googlecode.combinatoricslib/combinatoricslib.svg)](http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22com.googlecode.combinatoricslib%22%20AND%20a%3A%22combinatoricslib%22)\n\ncombinatoricslib 2.4\n====================\n\nVery simple java library to generate permutations, combinations and other combinatorial sequences for Java 7+.\nNew version of the library for (Java 8) can be found [here](https://github.com/dpaukov/combinatoricslib3).\n\n1. [Simple combinations](#1-simple-combinations)\n2. [Combinations with repetitions (multicombination)](#2-combinations-with-repetitions)\n3. [Simple permutations](#3-simple-permutations)\n4. [Permutations with repetitions](#4-permutations-with-repetitions)\n5. [Subsets](#5-subsets)\n6. [Integer Partitions](#6-integer-partitions)\n7. [List Partitions](#7-list-partitions)\n8. [Integer Compositions](#8-integer-compositions)\n9. [Cartesian Product](#9-cartesian-product)\n10. [The latest release](#the-latest-release)\n\n\nYou can use the following table to select a generator:\n\n| Description                      | Is Order Important? | Is Repetition Allowed? | CombinatoricsFactory Method  |\n|----------------------------------|:-------------------:|:----------------------:|---------| \n| [Simple combinations](#1-simple-combinations) | No | No | createSimpleCombinationGenerator() |\n| [Combinations with repetitions](#2-combinations-with-repetitions) | No | Yes | createMultiCombinationGenerator()|\n| [Simple permutations](#3-simple-permutations) | Yes | No | createPermutationGenerator() |\n| [Permutations with repetitions](#4-permutations-with-repetitions) | Yes | Yes | createPermutationWithRepetitionGenerator() |\n\n### 1. Simple combinations\nA simple k-combination of a finite set S is a subset of k distinct elements of S.\nSpecifying a subset does not arrange them in a particular order. As an example, a poker hand can\nbe described as a 5-combination of cards from a 52-card deck: the 5 cards of the hand are all distinct,\nand the order of the cards in the hand does not matter.\n\nLet's generate all 3-combination of the set of 5 colors (red, black, white, green, blue).\n```java\n   ICombinatoricsVector\u003cString\u003e vector = createVector(\"red\", \"black\", \"white\", \"green\", \"blue\");\n   Generator\u003cString\u003e gen = createSimpleCombinationGenerator(vector, 3);\n   for (ICombinatoricsVector\u003cString\u003e combination : gen) {\n      System.out.println(combination);\n   }\n```\nAnd the result of 10 combinations\n```\n   CombinatoricsVector=([red, black, white], size=3)\n   CombinatoricsVector=([red, black, green], size=3)\n   CombinatoricsVector=([red, black, blue], size=3)\n   CombinatoricsVector=([red, white, green], size=3)\n   CombinatoricsVector=([red, white, blue], size=3)\n   CombinatoricsVector=([red, green, blue], size=3)\n   CombinatoricsVector=([black, white, green], size=3)\n   CombinatoricsVector=([black, white, blue], size=3)\n   CombinatoricsVector=([black, green, blue], size=3)\n   CombinatoricsVector=([white, green, blue], size=3)\n```\n\n### 2. Combinations with repetitions\nA k-multicombination or k-combination with repetition of a finite set S is given by a sequence of\nk not necessarily distinct elements of S, where order is not taken into account.\n\nAs an example. Suppose there are 2 types of fruits (apple and orange) at a grocery store,\nand you want to buy 3 pieces of fruit. You could select\n- (apple, apple, apple)\n- (apple, apple, orange)\n- (apple, orange, orange)\n- (orange, orange, orange)\n\nLet's generate all 3-combinations with repetitions of the set (apple, orange).\n```java\n   ICombinatoricsVector\u003cString\u003e vector = createVector(\"apple\", \"orange\");\n   Generator\u003cString\u003e gen = createMultiCombinationGenerator(vector, 3);\n   for (ICombinatoricsVector\u003cString\u003e combination : gen) {\n      System.out.println(combination);\n   }\n```\nAnd the result of 4 multi-combinations\n```\n   CombinatoricsVector=([apple, apple, apple], size=3)\n   CombinatoricsVector=([apple, apple, orange], size=3)\n   CombinatoricsVector=([apple, orange, orange], size=3)\n   CombinatoricsVector=([orange, orange, orange], size=3)\n```\n\n### 3. Simple permutations\nA permutation is an ordering of a set in the context of all possible orderings. \nFor example, the set containing the first three digits, 123, has six permutations: 123, 132, 213, 231, 312, and 321.\n\nThis is an example of the permutations of 3 (apple, orange, cherry):\n\n```java\n   ICombinatoricsVector\u003cString\u003e vector = CombinatoricsFactory.createVector(\"apple\", \"orange\", \"cherry\");\n   Generator\u003cString\u003e gen = createPermutationGenerator(vector);\n   for (ICombinatoricsVector\u003cString\u003e perm : gen) {\n      System.out.println(perm);\n   }\n```\n\nAll possible permutations:\n```\n   CombinatoricsVector=([apple, orange, cherry], size=3)\n   CombinatoricsVector=([apple, cherry, orange], size=3)\n   CombinatoricsVector=([cherry, apple, orange], size=3)\n   CombinatoricsVector=([cherry, orange, apple], size=3)\n   CombinatoricsVector=([orange, cherry, apple], size=3)\n   CombinatoricsVector=([orange, apple, cherry], size=3)\n```\nThe generator can produce the permutations even if the initial vector has duplicates. \nFor example, all permutations of (1,1,2,2) are:\n```java\n   ICombinatoricsVector\u003cInteger\u003e vector = createVector(1, 1, 2, 2);\n   Generator\u003cInteger\u003e generator = createPermutationGenerator(vector);\n   for (ICombinatoricsVector\u003cInteger\u003e perm : generator) {\n      System.out.println(perm);\n   }\n```\n\nThe result of all possible permutations\n```\n   CombinatoricsVector=([1, 1, 2, 2], size=4)\n   CombinatoricsVector=([1, 2, 1, 2], size=4)\n   CombinatoricsVector=([1, 2, 2, 1], size=4)\n   CombinatoricsVector=([2, 1, 1, 2], size=4)\n   CombinatoricsVector=([2, 1, 2, 1], size=4)\n   CombinatoricsVector=([2, 2, 1, 1], size=4)\n```\n\n### 4. Permutations with repetitions\nThe permutation may have more elements than slots. For example, all possible permutation of '12' \nin three slots are: 111, 211, 121, 221, 112, 212, 122, and 222.\n\nLet's generate all possible permutations with repetitions of 3 elements from the set of apple and orange.\n\n```java\n   // Create an initial vector of 2 elements (apple, orange)\n   ICombinatoricsVector\u003cString\u003e vector = createVector(\"apple\", \"orange\");\n   Generator\u003cString\u003e gen = createPermutationWithRepetitionGenerator(vector, 3);\n   for (ICombinatoricsVector\u003cString\u003e perm : gen) {\n      System.out.println( perm );\n   }\n```\nAnd the result of 8 permutations\n```\n   CombinatoricsVector=([apple, apple, apple], size=3)\n   CombinatoricsVector=([orange, apple, apple], size=3)\n   CombinatoricsVector=([apple, orange, apple], size=3)\n   CombinatoricsVector=([orange, orange, apple], size=3)\n   CombinatoricsVector=([apple, apple, orange], size=3)\n   CombinatoricsVector=([orange, apple, orange], size=3)\n   CombinatoricsVector=([apple, orange, orange], size=3)\n   CombinatoricsVector=([orange, orange, orange], size=3)\n```\n\n### 5. Subsets\nA set A is a subset of a set B if A is \"contained\" inside B. A and B may coincide. \nThe relationship of one set being a subset of another is called inclusion or sometimes containment.\n\nExamples:\n\nThe set (1, 2) is a proper subset of (1, 2, 3).\nAny set is a subset of itself, but not a proper subset.\nThe empty set, denoted by ∅, is also a subset of any given set X.\nAll subsets of (1, 2, 3) are:\n\n- ()\n- (1)\n- (2)\n- (1, 2)\n- (3)\n- (1, 3)\n- (2, 3)\n- (1, 2, 3)\n\nAnd code which generates all subsets of (one, two, three)\n\n```java\n   ICombinatoricsVector\u003cString\u003e set = createVector( \"one\", \"two\", \"three\");\n   Generator\u003cString\u003e gen = createSubSetGenerator(set);\n   for (ICombinatoricsVector\u003cString\u003e subSet : gen) {\n      System.out.println(subSet);\n   }\n```\nAnd the result of all possible 8 subsets\n```\n   CombinatoricsVector=([], size=0)\n   CombinatoricsVector=([one], size=1)\n   CombinatoricsVector=([two], size=1)\n   CombinatoricsVector=([one, two], size=2)\n   CombinatoricsVector=([three], size=1)\n   CombinatoricsVector=([one, three], size=2)\n   CombinatoricsVector=([two, three], size=2)\n   CombinatoricsVector=([one, two, three], size=3)\n```\n\n### 6. Integer Partitions\nIn number theory, a partition of a positive integer n is a way of writing n as a sum of positive integers. \nTwo sums that differ only in the order of their summands are considered to be the same partition; \nif order matters then the sum becomes a composition. A summand in a partition is also called a part.\n\nThe partitions of 5 are listed below:\n\n- 1 + 1 + 1 + 1 + 1\n- 2 + 1 + 1 + 1\n- 2 + 2 + 1\n- 3 + 1 + 1\n- 3 + 2\n- 4 + 1\n- 5\n\nThe number of partitions of n is given by the partition function p(n). In number theory, the partition \nfunction p(n) represents the number of possible partitions of a natural number n, \nwhich is to say the number of distinct (and order independent) ways of representing n as a sum of natural numbers.\n\nLet's generate all possible partitions of 5:\n```java\n   Generator\u003cInteger\u003e partitions = createPartitionGenerator(5);\n   for (ICombinatoricsVector\u003cInteger\u003e p : partitions) {\n      System.out.println(p);\n   }\n```\nAnd the result of all 7 integer possible partitions\n```\n   CombinatoricsVector=([1, 1, 1, 1, 1], size=5)\n   CombinatoricsVector=([2, 1, 1, 1], size=4)\n   CombinatoricsVector=([2, 2, 1], size=3)\n   CombinatoricsVector=([3, 1, 1], size=3)\n   CombinatoricsVector=([3, 2], size=2)\n   CombinatoricsVector=([4, 1], size=2)\n   CombinatoricsVector=([5], size=1)\n```\n\n### 7. List Partitions\nIt is possible to generate non-overlapping sublists of length n of a given list\n\nFor example, if a list is (A, B, B, C), then the non-overlapping sublists of length 2 will be:\n\n- ( (A), (B, B, C) )\n- ( (B, B, C), (A) )\n- ( (B), (A, B, C) )\n- ( (A, B, C), (B) )\n- ( (A, B), (B, C) )\n- ( (B, C), (A, B) )\n- ( (B, B), (A, C) )\n- ( (A, C), (B, B) )\n- ( (A, B, B), (C) )\n- ( (C), (A, B, B) )\n\nTo do that you should use an instance of the complex combination generator\n```java\n   ICombinatoricsVector\u003cString\u003e vector = createVector \"A\", \"B\", \"B\", \"C\");\n   Generator\u003cICombinatoricsVector\u003cString\u003e\u003e gen = new ComplexCombinationGenerator\u003cString\u003e(vector, 2);\n   for (ICombinatoricsVector\u003cICombinatoricsVector\u003cString\u003e\u003e comb : gen) {\n      System.out.println(convert2String(comb) + \" - \" + comb);\n   }\n```\nAnd the result\n```\n   ([A],[B, B, C]) - CombinatoricsVector=([CombinatoricsVector=([A], size=1), CombinatoricsVector=([B, B, C], size=3)], size=2)\n   ([B, B, C],[A]) - CombinatoricsVector=([CombinatoricsVector=([B, B, C], size=3), CombinatoricsVector=([A], size=1)], size=2)\n   ([B],[A, B, C]) - CombinatoricsVector=([CombinatoricsVector=([B], size=1), CombinatoricsVector=([A, B, C], size=3)], size=2)\n   ([A, B, C],[B]) - CombinatoricsVector=([CombinatoricsVector=([A, B, C], size=3), CombinatoricsVector=([B], size=1)], size=2)\n   ([A, B],[B, C]) - CombinatoricsVector=([CombinatoricsVector=([A, B], size=2), CombinatoricsVector=([B, C], size=2)], size=2)\n   ([B, C],[A, B]) - CombinatoricsVector=([CombinatoricsVector=([B, C], size=2), CombinatoricsVector=([A, B], size=2)], size=2)\n   ([B, B],[A, C]) - CombinatoricsVector=([CombinatoricsVector=([B, B], size=2), CombinatoricsVector=([A, C], size=2)], size=2)\n   ([A, C],[B, B]) - CombinatoricsVector=([CombinatoricsVector=([A, C], size=2), CombinatoricsVector=([B, B], size=2)], size=2)\n   ([A, B, B],[C]) - CombinatoricsVector=([CombinatoricsVector=([A, B, B], size=3), CombinatoricsVector=([C], size=1)], size=2)\n   ([C],[A, B, B]) - CombinatoricsVector=([CombinatoricsVector=([C], size=1), CombinatoricsVector=([A, B, B], size=3)], size=2)\n```\n### 8. Integer Compositions\nA composition of an integer n is a way of writing n as the sum of a sequence of (strictly) positive integers. Two sequences that differ in the order of their terms define different compositions of their sum, while they are considered to define the same partition of that number (see. Integer Partitions above).\n\nThe 16 compositions of 5 are:\n\n- 5\n- 4+1\n- 3+2\n- 3+1+1\n- 2+3\n- 2+2+1\n- 2+1+2\n- 2+1+1+1\n- 1+4\n- 1+3+1\n- 1+2+2\n- 1+2+1+1\n- 1+1+3\n- 1+1+2+1\n- 1+1+1+2\n- 1+1+1+1+1.\n\nCompare this with the seven partitions of 5 (see Integer Partitions above):\n\n- 5\n- 4+1\n- 3+2\n- 3+1+1\n- 2+2+1\n- 2+1+1+1\n- 1+1+1+1+1.\n\nExample. Generate all possible integer compositions of 5.\n```java\n   // Create an instance of the integer composition generator to generate all possible compositions of 5\n   Generator\u003cInteger\u003e gen = createCompositionGenerator(5);\n   for (ICombinatoricsVector\u003cInteger\u003e p : gen) {\n      System.out.println(p);\n   }\n```\nAnd the result\n```\n   CombinatoricsVector=([5], size=1)\n   CombinatoricsVector=([1, 4], size=2)\n   CombinatoricsVector=([2, 3], size=2)\n   CombinatoricsVector=([1, 1, 3], size=3)\n   CombinatoricsVector=([3, 2], size=2)\n   CombinatoricsVector=([1, 2, 2], size=3)\n   CombinatoricsVector=([2, 1, 2], size=3)\n   CombinatoricsVector=([1, 1, 1, 2], size=4)\n   CombinatoricsVector=([4, 1], size=2)\n   CombinatoricsVector=([1, 3, 1], size=3)\n   CombinatoricsVector=([2, 2, 1], size=3)\n   CombinatoricsVector=([1, 1, 2, 1], size=4)\n   CombinatoricsVector=([3, 1, 1], size=3)\n   CombinatoricsVector=([1, 2, 1, 1], size=4)\n   CombinatoricsVector=([2, 1, 1, 1], size=4)\n   CombinatoricsVector=([1, 1, 1, 1, 1], size=5)\n```\n\n### 9. Cartesian Product\n\nThis generator generates Cartesian product from a number of lists.\nSet of lists is specified in the constructor of generator to generate k-element Cartesian product,\nwhere k is the size of the set of lists.\n\nA simple k-element Cartesian product of a finite sets S(1), S(2)...S(k) is a set\nof all ordered pairs (x(1), x(2)...x(k), where x(1) ∈ S(1), x(2) ∈ S(2) ... x(k) ∈ S(k)\n\nExample. Generate the cartesian product for (1, 2), (4), (5, 6).\n\n```java\n   ICombinatoricsVector\u003cInteger\u003e set01 = createVector(1, 2);\n   ICombinatoricsVector\u003cInteger\u003e set02 = createVector(4);\n   ICombinatoricsVector\u003cInteger\u003e set03 = createVector(5, 6);\n\n   Generator\u003cInteger\u003e generator = createCartesianProductGenerator(set01, set02, set03);\n   for (ICombinatoricsVector\u003cInteger\u003e catresianProduct : generator) {\n      System.out.println(catresianProduct);\n   }\n```\n\nThe cartesian product will be:\n```\n   CombinatoricsVector=([1, 4, 5], size=3)\n   CombinatoricsVector=([1, 4, 6], size=3)\n   CombinatoricsVector=([2, 4, 5], size=3)\n   CombinatoricsVector=([2, 4, 6], size=3)\n```\n\n### The latest stable release - version 2.3\n\nThe latest release of the library is available through The Maven Central Repository [here](http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22com.googlecode.combinatoricslib%22%20AND%20a%3A%22combinatoricslib%22).\nInclude the following section into your `pom.xml` file.\n\n```xml\n\u003cdependency\u003e\n  \u003cgroupId\u003ecom.googlecode.combinatoricslib\u003c/groupId\u003e\n  \u003cartifactId\u003ecombinatoricslib\u003c/artifactId\u003e\n  \u003cversion\u003e2.3\u003c/version\u003e\n  \u003cscope\u003ecompile\u003c/scope\u003e\n\u003c/dependency\u003e\n```","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdpaukov%2Fcombinatoricslib","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdpaukov%2Fcombinatoricslib","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdpaukov%2Fcombinatoricslib/lists"}