简体   繁体   English

斐波纳契调用图中值的分区(调用图是二叉树)

[英]Partitions of values in a fibonacci call graph (call graph is a binary tree)

I have an ongoing project investigating the Fibonacci sequence, this is just a personal project, I have created a binary tree class which makes a binary tree of the Fibonacci call graph, so for f(3) I generate the tree: 我有一个正在进行的调查Fibonacci序列的项目,这只是一个个人项目,我创建了一个二叉树tree class ,它创建了Fibonacci调用图的二叉树,所以对于f(3)我生成树:

二叉树

I want to create a method of my tree class get_partitions() that traverses the tree to generate partitions of the root value , I regard here summands that differ in order as different partions; 我想创建一个我的tree class get_partitions() ,遍历树以生成root value分区,我在这里看到的顺序不同的顺序作为不同的分区; so for the example here of f(3) , the get_partitions() method would traverse the tree and yield: 所以对于f(3)这里的例子, get_partitions()方法将遍历树并产生:

Partion 1: 2,1
Partion 2: 2,1,0
Partion 3: 1,1,1
Partion 4: 1,1,1,0
Partion 5: 1,0,1,1
Partion 6: 1,0,1,1,0

As ultimately I want to enumerate every permutation of Fibonacci numbers that Partition the root value , in this case 3 , so for Partition 1 enumerated would be (2,1),(1,2) , or Partion 2 would be enumerated (2,1,0),(2,0,1),(1,2,0),(1,0,2),(0,2,1),(0,1,2) , etc… 最后,我想列举Fibonacci数的每个排列,即对root value进行Partition 1 ,在本例中为3 ,因此对于枚举的Partition 1将是(2,1),(1,2) ,或者枚举Partion 2将被枚举(2,1,0),(2,0,1),(1,2,0),(1,0,2),(0,2,1),(0,1,2)等......

[Edit 1] My concern is with Partion 4 and Partion 5 in this examples as enumerating all combinations of these partions would yield duplicate partions. [编辑1]我关注的是本例中的第Partion 4和第Partion 5 ,因为列举这些部分的所有组合会产生重复的部分。

Would it be correct that the number of combinations for a given root value would yield a Catalan number? 给定root value的组合数量会产生加泰罗尼亚数字是否正确?

My Tree class is: 我的Tree class是:

class FibTree(object):
"""Class which builds binary tree from Fibonacci function call graph"""
def __init__(self, n, parent=None, level=None, i=None):
    if level is None:
        level = 0
    if i is None:
        i = 1
    self.n = n
    self.parent = parent
    self.level = level
    self.i = i # Node index value
    if n < 2:
        self.left = None
        self.right = None
        self.value = n
    else:
        self.left = FibTree(n - 1, self, level + 1, i*2)
        self.right = FibTree(n - 2, self, level + 1, (i*2)+1)
        self.value = self.left.value + self.right.value

I'd be grateful of any help for producing the tree class method and any enlightenment on the maths to my problem. 我要感谢任何有关生成树类方法的帮助以及对我的问题的数学启示。

[EDIT:] How I get my partions All partions must sum to Root value: [编辑:]我如何获得我的分区所有分区必须总和为Root值:
Partion 1: Taken from Level 1 (2,1) Partion 1:取自1级(2,1)
Partion 2: Use the left child node value of root , but now take the values of the children of the right child node of the root node (1,0) , to give a Partion of (2,1,0) Partion 2:使用left child node的值root ,但现在走的孩子的价值观right child node的的root结点(1,0)给出的Partion (2,1,0)
Partion 3: As traversal of right child node of the root node has been exhausted, traverse to next level of left child node value of root (level 2), and use the these child node values as first part of partion (1,1) then take the right child node value of the root node (1), to give a partion of (1,1,1) Partion 3:作为遍历right child node的的root节点已经耗尽,横动到的下一级left child node的值root (级别2),并使用这些子节点的值作为partion的第一部分(1,1)然后采取right child node的的值root节点(1),得到的partion (1,1,1)
Partion 4: Keeping the initial partion values from the previous partion (1,1) , but as with Partion 2 take the values of the children of the right child node of the root node (1,0) , to give a Partion of (1,1,1,0) Partion 4:从先前partion保持初始值partion (1,1)但与Partion 2取的孩子的值right child node的的root节点(1,0)以得到的Partion (1,1,1,0)
Partion 5: As the left child, of the left child of the root, has children, use these as the first part of the partion (1,0) then take the right child value of the left child of the root (1), giving a partion so far of (1,0,1) , then take the right child node of the root (1) , to give a final partion of (1,0,1,1) Partion 5:作为根的左孩子的左孩子有孩子,使用这些作为分区的第一部分(1,0)然后取root的左子的右子值(1),给出目前为止(1,0,1) ,然后取根(1)的右子节点,给出(1,0,1,1)的最终部分
Partion 6: As Partion 5, take the first part of Partion 5 (1,0,1) , then as Partion 2 and 4 take the value of the child nodes of the right node of the root. Partion 6:作为第5部分,取第5部分(1,0,1)的第一部分,然后作为第2部分和第4部分取根的右节点的子节点的值。

Here's an generator which produces the sort of values you want, but I haven't tried to find a fully optimised solution since your question is a bit confusing in places. 这是一个生成您想要的值的生成器,但我没有尝试找到完全优化的解决方案,因为您的问题在某些地方有点令人困惑。

  1. Are you sure about including 0? 你确定包含0吗? Its not completely arbitrary because the maximum number of zeros in a partition is the number of ones (eg [1, 0, 1, 0, 1, 0]), but they don't seem to add anything. 它不是完全随意的,因为分区中的最大零数是1的数量(例如[1,0,1,0,1,0]),但它们似乎没有添加任何东西。

  2. How exactly do you order the partitions? 你究竟如何订购分区? When n=3, and ignoring zeros, they appear to be ordered by length, but if n=8, for example, is [2, 2, 2, 2] before or after [1, 2, 2, 3]? 当n = 3,并且忽略零时,它们似乎按长度排序,但是如果n = 8,例如,在[1,2,2,3]之前或之后是[2,2,2,2]?

  3. Do you actually want a class to do this, or did you just use that as an example because it seemed the easiest way? 你真的想要一个班级做这个,或者你只是用它作为一个例子,因为它似乎是最简单的方法?

This code will yield all permutations of values in the fibonacci sequence which add to n , including n itself. 此代码将产生fibonacci序列中添加到n所有值的排列,包括n本身。 It will only work if n is actually in the sequence (eg fibs(4) will raise an exception). 只有当n实际上在序列中时才会起作用(例如, fibs(4)会引发异常)。

Here's the code: 这是代码:

def fibs(n, _pairs=None):
    'Return partitions in the fibonacci sequence'
    if _pairs is None:
        # Generate a dict of fib numbers, values are the previous two numbers
        # E.g. {..., 8: [3, 5], 13: [5, 8], ... n: [fib_n-2, fib_n-1]} 
        a, b, c = 0, 1, 1
        _pairs = {1: [0, 1]}
        while c < n:
            a, b = b, a + b
            c = a + b
            _pairs[c] = [a, b]

    # Yield every sum combination of pairs
    yield (n,)
    if n == 1:
        yield (1, 0)
    else:
        right, left = _pairs[n]
        for vl in fibs(left, _pairs):
            for vr in fibs(right, _pairs):
                yield vl + vr

You can easily filter out duplicates using set(tuple(sorted(i)) for i in fibs(n)) and create permutations using itertools.permutations . 您可以使用set(tuple(sorted(i)) for i in fibs(n))轻松过滤掉重复项,并使用itertools.permutations创建排列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM