Algorithm for finding multiset permutation given lexicographic index

后端 未结 1 1733
太阳男子
太阳男子 2021-01-02 17:01

I am trying to find an efficient algorithm to find permutation of a multiset, given an index.

Ex: given {1, 3, 3}. All permutations in an ascending lexi

相关标签:
1条回答
  • 2021-01-02 17:17
    # Python 2
    from collections import Counter
    from math import factorial
    
    
    def count_permutations(counter):
        values = counter.values()
        return (
            factorial(sum(values))/reduce(lambda a, v: a * factorial(v), values, 1)
        )
    
    
    def permutation(l, index):
        l = sorted(l)
    
        if not index:
            return l
    
        counter = Counter(l)
        total_count = count_permutations(counter)
        acc = 0
        for i, v in enumerate(l):
    
            if i > 0 and v == l[i-1]:
                continue
    
            count = total_count * counter[v] / len(l)
    
            if acc + count > index:
                return [v] + permutation(l[:i] + l[i + 1:], index - acc)
    
            acc += count
    
        raise ValueError("Not enough permutations")
    

    Seems to work as expected

    In [17]: for x in range(50): print x, permutation([1, 1, 2, 2, 2], x)
    0 [1, 1, 2, 2, 2]
    1 [1, 2, 1, 2, 2]
    2 [1, 2, 2, 1, 2]
    3 [1, 2, 2, 2, 1]
    4 [2, 1, 1, 2, 2]
    5 [2, 1, 2, 1, 2]
    6 [2, 1, 2, 2, 1]
    7 [2, 2, 1, 1, 2]
    8 [2, 2, 1, 2, 1]
    9 [2, 2, 2, 1, 1]
    10---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    [...]
    ValueError: Not enough permutations
    

    Time complexity: O(n^2).

    0 讨论(0)
提交回复
热议问题