All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.shapleyvalue.util.Permutations Maven / Gradle / Ivy

The newest version!
package org.shapleyvalue.util;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.tuple.MutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Permutations {
	
	private static final Logger logger = LoggerFactory.getLogger(Permutations.class);
	
	public static List> getAllPermutation(long size) {
		
		List> res = new ArrayList<>();
		List elements = new ArrayList<>();

		for(int i=1 ; i<=size; i++) {
			elements.add(i);		
		}
		
		List> emptyListOfList = new ArrayList>();
		emptyListOfList.add(new ArrayList());
		Pair>,List> pair = new MutablePair<>(emptyListOfList,elements);
		List>,List>> tempList = new ArrayList<>();
		tempList.add(pair);
		
		for(int i=1 ; i<=size; i++) {
			tempList = getAllPermutation(tempList);		
		}

		for(Pair>,List> tempPair : tempList) {
			res.addAll(tempPair.getLeft());
		}
		if(logger.isDebugEnabled()) logger.debug("getAllPermutations res={}",res);
		return res;
	}
	
	private static List>,List>> getAllPermutation(List>,List>> input) {
		
		
		List>,List>> res = new ArrayList<>();
		for(Pair>,List> pair : input) {
			for(Integer i : pair.getRight()) {
				for(List tempList : pair.getLeft()) {	
			 		List> left = new ArrayList<>();
					List tempList2 = new ArrayList<>();
					tempList2.addAll(tempList);
					tempList2.add(i);
					left.add(tempList2);
					List right = new ArrayList<>();
					right.addAll(pair.getRight());
					right.remove(i);
					Pair>,List> newPair = new MutablePair<>(left,right);
					res.add(newPair);
				}
			}
		}
		
		return res;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy