org.voltdb.utils.PermutationGenerator Maven / Gradle / Ivy
/* This file is part of VoltDB.
* Copyright (C) 2008-2018 VoltDB Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with VoltDB. If not, see .
*/
package org.voltdb.utils;
import java.util.ArrayList;
import java.util.List;
/**
* Utility class to generate permutations of an arbitrary sequence of objects.
*
*/
public class PermutationGenerator {
public static List> generatePurmutations(List sequence) {
List> purmutations = new ArrayList>();
generatePurmutations(sequence, 0, purmutations);
return purmutations;
}
private static void generatePurmutations(List sequence, int idx, List> permutations) {
if (idx == sequence.size()) {
// if we are at the end of the array, we have one permutation
List purmutation = new ArrayList();
purmutation.addAll(sequence);
permutations.add(purmutation);
} else {
// recursively explore the permutations starting
// at index idx going through the last element in the sequence
generatePurmutations(sequence, idx+1, permutations);
for (int i = idx + 1; i < sequence.size(); i++) {
// swap i and idx elements
swapElements(sequence, i, idx);
generatePurmutations(sequence, idx+1, permutations);
// swap them back
swapElements(sequence, i, idx);
}
}
}
private static void swapElements(List sequence, int i, int j) {
assert (i < sequence.size() && j < sequence.size());
T temp = sequence.get(i);
sequence.set(i, sequence.get(j));
sequence.set(j, temp);
}
}