This is truly a naive variety - it traverses the tree of all possible permutations until, fortunately, it finds a sorted one. It has complexity O (n!), I suppose:>
About the permutation function - it works "backward" - note that the definition takes the head out of the result . If you expand your point of view, you will notice that instead of deleting it, it inserts values, turning back. Since the algorithm works in the opposite direction, therefore, the selected H ead can be anything that allows you to create a result, therefore, any unused value from the list.
Basically, the permutation algorithm is transferred to the following procedural implementation:
- select an item from the list
- put it before sorting
This way you create permutations. All of them.
In short-perm, it generates the entire space of possible solutions, starting with an empty solution and checking how this solution is possible from a valid deletion.
?- perm( [ 1, 2, 3 ] , P ) P = [1, 2, 3]; P = [1, 3, 2]; P = [2, 1, 3]; P = [2, 3, 1]; P = [3, 1, 2]; P = [3, 2, 1]; no
Kornel Kisielewicz Feb 04 '10 at 0:50 2010-02-04 00:50
source share