Skip to content

Instantly share code, notes, and snippets.

@jonataswalker
Forked from binarymax/LICENSE.txt
Created June 8, 2016 19:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonataswalker/8ff14968ded25f55c52bf2fa8c9673a9 to your computer and use it in GitHub Desktop.
Save jonataswalker/8ff14968ded25f55c52bf2fa8c9673a9 to your computer and use it in GitHub Desktop.
Powerset from array
/*******************************************
* Powerset calculation from array 'o'.
*
* The cardinality of a set's Powerset is 2^n,
* where n is the cardinality of the set.
*
* This conveniently translates into an algorithm
* whereby all the binary representation of (0...2^n)
* are representitive of all possible combinations of
* the set, with 0.toString(2) as the empty set, and
* ((2^n)-1).toString() as the full set.
*
* For example, with set ['A','B','C']:
* 0 == 000 == [null]
* 1 == 001 == ['C']
* 2 == 010 == ['B']
* 3 == 011 == ['B','C']
* 4 == 100 == ['A']
* 5 == 101 == ['A','C']
* 6 == 110 == ['A','B']
* 7 == 111 == ['A','B','C']
*
*******************************************/
function P(
o, //array passed in as only parameter.
w, //binary representation of iterator 0...(2^n)-1 (see 's' below)
e, //represents an iterator that loops through each bit in w
r, //the powerset array that will be returned, an array of subset arrays
s, //iterator that steps -1 as ((2^n)-1)...0
E, //the length of the original set
t //accounts for lack of preceding 0's in w. For example (1).toString(2) == "1", not "001"
) {
for(r=[s=1<<(E=o.length)];s;) //initialize r=[(2^n)].
for(w=s.toString(2), //Store binary representaion
e=t=w.length, //Length of the binary (see parameter t)
r[--s]=[]; //new subset
e;) //e iterates over (t-1)...0
~-w[--e]|| //if bit in position e is 1
r[s].push(o[e+E-t]); // ... add respective set item to subset
// ... NOTE: the subset order is reversed
return r //return the powerset
}
<!doctype html>
<html>
<head>
<title>Powerset</title>
<meta charset="utf-8" />
<script src="index.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
function page_load() {
/*Powerset:*/
var Set = [1,2,3,4,5,6];
var Powerset = P(Set);
document.body.innerHTML+=JSON.stringify(Powerset);
document.body.innerHTML+='<br />';
document.body.innerHTML+=Powerset.length;
}
</script>
</head>
<body onload="page_load();">
</body>
</html>
function P(o,w,e,r,s,E,t){for(r=[s=1<<(E=o.length)];s;)for(w=s.toString(2),e=t=w.length,r[--s]=[];e;)~-w[--e]||r[s].push(o[e+E-t]);return r}
Copyright (c) 2011 Max Lovenheim Irwin, http://binarymax.com
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
{
"name": "Powerset",
"description": "P(a) returns the powerset for array a.",
"keywords": [
"Powerset",
"set",
"P"
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment