Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var count = 0;
- var people = ["A","B","C","D","E","F"];
- var combinations = [];
- var partitions = [];
- example();
- generate(people.splice(0), []);
- for (let i = 0; i < combinations.length; ++i)
- pretty_print(partitions[i]);
- function pretty_print(v) {
- var message = "combination no " + (++count) + ": [ ";
- for (let i = 0; i < v.length; ++i) { message += v[i] + " "; }
- message += "]";
- alert(message);
- }
- function go(combination, offset, k) {
- if (k == 0) {
- combinations.push(combination.slice(0));
- return;
- }
- for (let i = offset; i <= people.length - k; ++i) {
- combination.push(people[i]);
- go(combination, i+1, k-1);
- combination.pop();
- }
- }
- function example() {
- var k = 3;
- go([], 0, k);
- }
- function generate(people, partition)
- {
- if (people.length == 0)
- {
- partitions.push(partition.splice(0));
- return;
- }
- for (let i = 0; i < combinations.length; ++i)
- {
- var combination = combinations[i];
- partition.push(combination);
- var peopleCopy = people.splice(0);
- for (let j = 0; j < combination.length; ++j)
- {
- peopleCopy.splice(peopleCopy.indexOf(combination[j]), 1);
- }
- generate(peopleCopy, partition);
- for (let j = 0; j < combination.length; ++j)
- {
- partition.splice(partition.indexOf(combination[j]), 1);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement