簡體   English   中英

堆的算法 - Python 中的非遞歸方法生成排列

[英]Heap's Algorithm - Non Recursive Method in Python to generate permutations

我是編程新手。 我正在研究堆算法,特別是非遞歸方法。 互聯網上沒有太多關於算法如何工作的解釋。 我從Bernardo Sulzbach那里找到了這篇作品,但他沒有解釋他的算法是如何工作的。 幾天來我一直堅持下去,嘗試了一切都無法弄清楚。 我在每一行之后添加了注釋以使自己理解 - 這里發生了什么? 但我仍然無法讓它工作。 難道我做錯了什么? 請幫忙。

# Heap's Algorithm (Non Recursive)

# function to swap values in python
def swap(elements, i, j):
    elements[i], elements[j] = elements[j], elements[i]

# function to generate permutation
def generate_permutations(elements, n): 
    # Passing two parameters of elements and n to function "generate_permutations".
    c = [0] * n 
    # c is a new list and its value set to an array literal with value 0, n times.
    # Example - [a] * 3 ==> ['a', 'a', 'a', 'a']
    yield elements 
    # "yield" statement is used to define generators, while "return" statement causes a function to exit.
    # "yield" replacing the return of a function to provide a result to its caller without destroying
    # local variables. Unlike a function, where on each call it starts with new sets of variables, a generator
    # will resume the execution where it left off.
    i = 0
    # i is a new variable and its value is set to 0. It can also be used to count the number of loop runs.
    while i < n:
    # while loop ==> while i is less than n, do following:
        if c[i] < i:
        # if statement ==> while i is less than n and if any element from 'c' list is less than i, 
        # then do following:
            if i % 2 == 0:
            # if statement ==> while i is less than n, and if any element in 'c' list is less than i, and
            # i is an even number, then do following:
                swap(elements, 0, i)
                # calling swap function and passing following arguments: elements, '0' and 'i'
            else:
            # else, if all three conditions above are not true then do following:
                swap(elements, c[i], i)
                # calling swap funtions and passing following arguments: elements, an item from 'c' list and 'i'
            yield elements
            # ??? yield elements
            c[i] += 1
            # after that, increment c[i] by 1.
            i = 0
            # set the value of i to 0
        else:
            # else, if c[i] < i is not true the do the following.
            c[i] = 0
            # set the value of c[i] to 0
            i += 1
            # and increment i by 1

def permutations(elements):
    return generate_permutations(elements, len(elements))

# Driver Code
# c = ?
# n = ?
# i = ?
print(permutations(['abc']))

只是清理你的代碼(太多評論):

# Heap's Algorithm (Non Recursive)
# https://en.wikipedia.org/wiki/Heap%27s_algorithm

def swap(seq, i, j):
  seq[i], seq[j] = seq[j], seq[i]

def generate_permutations(seq, seqLen, resLen): 
  c = [0] * seqLen
  yield seq[:resLen]
  
  i = 0
  while i < seqLen:
    if c[i] < i:
      if i % 2 == 0:
        swap(seq, 0, i)
      else:
        swap(seq, c[i], i)
      yield seq[:resLen]
      c[i] += 1
      i = 0
    else:
      c[i] = 0
      i += 1

def permutations(seq, resLen=None):
  if not resLen: resLen = len(seq)
  return generate_permutations(seq, len(seq), resLen)

for p in permutations([1,2,3]): print(p)
for p in permutations([1,2,3],2): print(p)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM