the method: public static Set<Function<T,T>> bijectionsFinder(Set<T> d)
say d= {1,2,3}. We are supposed to find all bijections from d -> d and return a set of those bijections.
Im not sure how to start.
Main method:
public static void main(String... args) {
Set<Integer> a_few = Stream.of(1, 2, 3).collect(Collectors.toSet());
Set<Function<T, T>> bijections = bijectionsOf(a_few);
bijections.forEach(aBijection -> {
a_few.forEach(n -> System.out.printf("%d --> %d; ", n, aBijection.apply(n)));
System.out.println();
});
}
Expected output if d= {1,2,3} :

Bijection of the dataset to itself results in a set of Permutations of the given data.
And each Bijection should meet the definition:
I.e. in a valid Bijection every element should be paired, a not none of the elements can be paired with more than one element.
To approach this a problem in a way that is easier to digest, we can describe Bijections as objects.
Let's define the following classes:
Bijection- representing a Bijection of the given dataset to itself. Since Bijection consists of pairs of elements, an instance ofBijectionholds a reference to a list ofPairs.Pair- represent a pair of elements in a Bijection.While initializing a new instance of
Bijectionbased on the given data (see the methodinit()) aListofPairs is being created. To meet the definition shown above, the number ofPairis equal to the number of elements in the given dataset, and initially eachPairis being assigned with an element from the given dataset as its first element (a).During the process of generating permutations, each pair is being provided with the second element (
b). To track the pair that needs to be changed,Bijectionhas a propertycursorBoth
BijectionandPairexpose methodcopy()to facilitate the process of generating permutations.Here's the logic for generating all possible Bijections (i.e. all Permutations of elements in the given dataset expressed as connected pairs) encapsulated into a utility class.
It's a basic recursive implementation. The Base case of recursion is when provide source of data is empty, i.e. no elements left to use and Bijection (Permutation) gets added to the resulting list.
As the source of data for generating Permutations, I'll use a
LinkedHashSet(as suggested by @Rogue in a comment) because it facilitates removal of elements in constant time and guaranties consistent order of iteration.main()Output: