目前,我可以使用以下强力Prolog代码枚举rooted planar unlabeled二叉树。
e --> u | b | t.
u --> ['[op(u),['], e, [']]'].
b --> ['[op(b),['], e, [','], e, [']]'].
t --> ['number(n)'].
注意:请参阅下面的输出列表。
并使用
以递增的大小输出它们es(S) :-
length(Ls, _),
phrase(e, Ls), % or e(Ls,[]),
atomic_list_concat(Ls,S).
然而,这是一种低效的强力算法。
是否有更有效的算法来枚举带根的平面未标记二叉树?
注意:可以使用前两次迭代中的树生成树,考虑Fibonacci数,并添加一元分支或二元分支,但这会导致重复的树。我自己可以做那个版本,我正在寻找的是一种算法,它可以在没有重复的情况下以高效的方式生成树。
注意:二叉树也称为binary expression tree或K-ary tree,K <= 2。
我对M(15)的蛮力版需要1小时27分钟, 而M(15)的高效版本花了大约2秒钟。
显然,有效的算法就是这样,效率更高,为什么我问这个问题。
具有N
节点的树的数量为带根的平面未标记二进制树,由Motzkin数字给出。见:OEIS A001006
Nodes Trees
1 1
2 1
3 2
4 4
5 9
具有N个内部节点的树的数量为带有根的平面未标记二进制树,由加泰罗尼亚数字给出。有一种更有效的算法,用于使用加泰罗尼亚数生成有根平面二叉树。
注意:
基于加泰罗尼亚数字的树木数量不具有一元分支,仅计算内部节点。
而
基于Motzkin数字的树木数量执行具有一元分支并计算所有节点。
请参阅:
OEIS A000108
汤姆戴维斯Catalan Numbers
% M is Motzkin number, N is number of list elements passed to atomic_list_concat\2
m_to_n(1,1).
m_to_n(2,3).
m_to_n(M,N) :-
M > 2,
N is (M*2)-1.
es_m(M,S) :-
m_to_n(M,N),
length(Ls,N),
e(Ls,[]),
atomic_list_concat(Ls,S).
es_c(M,Count) :-
aggregate_all(count, es_m(M,_), Count).
?- time(es_c(1,Count)).
% 57 inferences, 0.000 CPU in 0.000 seconds (?% CPU, Infinite Lips)
Count = 1.
?- time(es_c(2,Count)).
% 141 inferences, 0.000 CPU in 0.000 seconds (?% CPU, Infinite Lips)
Count = 1.
?- time(es_c(3,Count)).
% 571 inferences, 0.000 CPU in 0.000 seconds (?% CPU, Infinite Lips)
Count = 2.
?- time(es_c(4,Count)).
% 2,740 inferences, 0.000 CPU in 0.000 seconds (?% CPU, Infinite Lips)
Count = 4.
?- time(es_c(5,Count)).
% 13,780 inferences, 0.000 CPU in 0.001 seconds (0% CPU, Infinite Lips)
Count = 9.
?- time(es_c(6,Count)).
% 70,072 inferences, 0.000 CPU in 0.002 seconds (0% CPU, Infinite Lips)
Count = 21.
?- time(es_c(7,Count)).
% 357,358 inferences, 0.016 CPU in 0.012 seconds (136% CPU, 22870912 Lips)
Count = 51.
?- time(es_c(8,Count)).
% 1,824,082 inferences, 0.063 CPU in 0.056 seconds (111% CPU, 29185312 Lips)
Count = 127.
?- time(es_c(9,Count)).
% 9,313,720 inferences, 0.297 CPU in 0.290 seconds (102% CPU, 31372531 Lips)
Count = 323.
?- time(es_c(10,Count)).
% 47,561,878 inferences, 1.469 CPU in 1.467 seconds (100% CPU, 32382555 Lips)
Count = 835.
?- time(es_c(11,Count)).
% 242,896,160 inferences, 7.672 CPU in 7.665 seconds (100% CPU, 31660599 Lips)
Count = 2188.
?- time(es_c(12,Count)).
% 1,240,493,974 inferences, 38.797 CPU in 38.841 seconds (100% CPU, 31974069 Lips)
Count = 5798.
?- time(es_c(13,Count)).
% 6,335,410,822 inferences, 206.047 CPU in 213.116 seconds (97% CPU, 30747425 Lips)
Count = 15511.
?- time(es_c(14,Count)).
% 32,356,235,848 inferences, 1016.156 CPU in 1018.955 seconds (100% CPU, 31841792 Lips)
Count = 41835.
?- time(es_c(15,Count)).
% 165,250,501,417 inferences, 5231.766 CPU in 5268.363 seconds (99% CPU, 31585991 Lips)
Count = 113634.
Philippe Flajolet和Robert Sedgewick撰写的"Analytic Combinatorics"可免费下载的PDF格式书籍
另请参阅Catalan代码中的引用。
<expression> ::=
<unary expression>
| <binary expression>
| <terminal>
<unary expression> ::=
"(u" <expression> ")"
<binary expression> ::=
"(b" <expression> " " <expression> ")"
<terminal> ::=
"t"
将这些视为笔记或面包屑,以防万一我忘记它之后需要在几个月内再次使用它。
为了测试答案,我使用安装了Python 3的WSL(适用于Linux的Windows子系统)
使用Windows 10我在目录
中创建了一个名为motzkin.py
的文件
C:\Users\Eric\Documents\Prolog
使用Python代码
def ubtrees(n):
if n == 1:
yield 't'
elif n > 1:
for t in ubtrees(n - 1):
yield '(u {})'.format(t)
for i in range(1, n - 1):
for t1 in ubtrees(i):
for t2 in ubtrees(n - 1 - i):
yield '(b {} {})'.format(t1, t2)
然后在WSL中我创建了一个指向Windows Prolog目录的符号链接
$ ln -s "/mnt/c/Users/Eric/Documents/Prolog" /home/eric/Prolog
并更改为WSL Prolog目录
$ cd Prolog
然后启动Python3
~/Prolog$ python3
并导入Python代码
>>> import motzkin
并使用ubtrees为Motzkin数字的参数运行以下内容
>>> for value in ubtrees(1):
... print(value)
...
t
>>> for value in ubtrees(2):
... print(value)
...
(u t)
>>> for value in ubtrees(3):
... print(value)
...
(u (u t))
(b t t)
>>> for value in ubtrees(4):
... print(value)
...
(u (u (u t)))
(u (b t t))
(b t (u t))
(b (u t) t)
>>> for value in ubtrees(5):
... print(value)
...
(u (u (u (u t))))
(u (u (b t t)))
(u (b t (u t)))
(u (b (u t) t))
(b t (u (u t)))
(b t (b t t))
(b (u t) (u t))
(b (u (u t)) t)
(b (b t t) t)
并检查Motzkin数字
def m_count(m):
count = sum(1 for x in ubtrees(m))
print("Count: ", count)
>>> m_count(1)
Count: 1
>>> m_count(2)
Count: 1
>>> m_count(3)
Count: 2
>>> m_count(4)
Count: 4
>>> m_count(5)
Count: 9
>>> m_count(6)
Count: 21
>>> m_count(7)
Count: 51
>>> m_count(8)
Count: 127
>>> m_count(9)
Count: 323
>>> m_count(10)
Count: 835
>>> m_count(11)
Count: 2188
>>> m_count(12)
Count: 5798
>>> m_count(13)
Count: 15511
>>> m_count(14)
Count: 41835
>>> m_count(15)
Count: 113634
退出交互式Python使用
quit()
我学习Motzkin数字的方法是手工用笔和纸计算树,并通过使用向前面的树M(N-1)添加一元分支和前面的树的二元分支的方法找到副本M(N-2)树。
从M(4)树
为M(5)生成两棵树(b (u t) (u t))
第一个通过添加一元分支
(b (u t) t)
,第二个是将一元分支添加到
(b t (u t))
执行此操作后,我得到了数字序列1,2,4,9,21,我使用了OEIS search,最高结果是A001006,用于Motzkin数字。一旦我有了更大的Motzkin数字列表,我使用Prolog代码生成更大输入值的计数,他们都同意了。现在,您可以使用有效示例将OEIS添加到编程工具箱中,以向其他人演示。
如果您已经阅读了这么远,那么您可能会发现这是一个更大问题的一部分,它首先在Prolog中构建一个系统,可以使用术语重写来解决基本微积分的数学表达式,但更重要的是显示所采取的步骤。因此,这将成为生成二进制表达式树的一部分,用作测试用例。下一步是能够单独设置一元和二元的节点数,而不是通过Motzkin数固定它们。我只使用Motzkin数字来验证我是否正确生成了组合的子集。现在我有了模式,我可以修改它以接受一个参数用于一元节点的数量,一个用于二进制节点。请参阅:How to enumerate combinations using DCGs with CLP(FD) and multiple constraints
只有当我遇到困难时,我才会问与此相关的问题,所以不要指望看到所有必要的面包屑。
?- length(Ls, N), phrase(e, Ls).
Ls = ['number(0)'],
N = 1 ;
Ls = ['[op(u),[', 'number(0)', ']]'],
N = 3 ;
Ls = ['[op(u),[', '[op(u),[', 'number(0)', ']]', ']]'],
N = 5 ;
Ls = ['[op(b),[', 'number(0)', ',', 'number(0)', ']]'],
N = 5 ;
Ls = ['[op(u),[', '[op(u),[', '[op(u),[', 'number(0)', ']]', ']]', ']]'],
N = 7 ;
Ls = ['[op(u),[', '[op(b),[', 'number(0)', ',', 'number(0)', ']]', ']]'],
N = 7 ;
Ls = ['[op(b),[', '[op(u),[', 'number(0)', ']]', ',', 'number(0)', ']]'],
N = 7 ;
Ls = ['[op(b),[', 'number(0)', ',', '[op(u),[', 'number(0)', ']]', ']]'],
N = 7 ;
?- es(S).
S = 'number(0)' ;
S = '[op(u),[number(0)]]' ;
S = '[op(u),[[op(u),[number(0)]]]]' ;
S = '[op(b),[number(0),number(0)]]' ;
S = '[op(u),[[op(u),[[op(u),[number(0)]]]]]]' ;
S = '[op(u),[[op(b),[number(0),number(0)]]]]' ;
S = '[op(b),[[op(u),[number(0)]],number(0)]]' ;
S = '[op(b),[number(0),[op(u),[number(0)]]]]' ;
S = '[op(u),[[op(u),[[op(u),[[op(u),[number(0)]]]]]]]]' ;
S = '[op(u),[[op(u),[[op(b),[number(0),number(0)]]]]]]' ;
S = '[op(u),[[op(b),[[op(u),[number(0)]],number(0)]]]]' ;
S = '[op(u),[[op(b),[number(0),[op(u),[number(0)]]]]]]' ;
S = '[op(b),[[op(u),[[op(u),[number(0)]]]],number(0)]]' ;
S = '[op(b),[[op(u),[number(0)]],[op(u),[number(0)]]]]' ;
S = '[op(b),[[op(b),[number(0),number(0)]],number(0)]]' ;
S = '[op(b),[number(0),[op(u),[[op(u),[number(0)]]]]]]' ;
S = '[op(b),[number(0),[op(b),[number(0),number(0)]]]]' ;
?- es_m(1,E).
E = 'number(n)' ;
false.
?- es_m(2,E).
E = '[op(u),[number(n)]]' ;
false.
?- es_m(3,E).
E = '[op(u),[[op(u),[number(n)]]]]' ;
E = '[op(b),[number(n),number(n)]]' ;
false.
?- es_m(4,E).
E = '[op(u),[[op(u),[[op(u),[number(n)]]]]]]' ;
E = '[op(u),[[op(b),[number(n),number(n)]]]]' ;
E = '[op(b),[[op(u),[number(n)]],number(n)]]' ;
E = '[op(b),[number(n),[op(u),[number(n)]]]]' ;
false.
?- es_m(5,E).
E = '[op(u),[[op(u),[[op(u),[[op(u),[number(n)]]]]]]]]' ;
E = '[op(u),[[op(u),[[op(b),[number(n),number(n)]]]]]]' ;
E = '[op(u),[[op(b),[[op(u),[number(n)]],number(n)]]]]' ;
E = '[op(u),[[op(b),[number(n),[op(u),[number(n)]]]]]]' ;
E = '[op(b),[[op(u),[[op(u),[number(n)]]]],number(n)]]' ;
E = '[op(b),[[op(u),[number(n)]],[op(u),[number(n)]]]]' ;
E = '[op(b),[[op(b),[number(n),number(n)]],number(n)]]' ;
E = '[op(b),[number(n),[op(u),[[op(u),[number(n)]]]]]]' ;
E = '[op(b),[number(n),[op(b),[number(n),number(n)]]]]' ;
false.
答案 0 :(得分:3)
除了已发布的解决方案之外,我还想为此任务提供以下 Prolog 解决方案。
首先,这是树的声明性描述:
e(number) --> []. e(u(Arg)) --> [_], e(Arg). e(b(Left,Right)) --> [_,_], e(Left), e(Right).
我也在使用dcg。但是,我使用它的目的不同于问题:在我的情况下,我只描述一个列表,以便限制解决方案的深度。将非终结符视为通过应用规则将明确消耗多少“令牌”。另请注意,我使用复合词来自然地描述这些树。
示例查询,使用迭代深化:
?- length(Ls, _), phrase(e(E), Ls). Ls = [], E = number ; Ls = [_5710], E = u(number) ; Ls = [_5710, _5716], E = u(u(number)) ; Ls = [_5710, _5716], E = b(number, number) ; Ls = [_5710, _5716, _5722], E = u(u(u(number))) .
我现在可以计算解决方案如下:
es_count(M, Count) :- length([_|Ls], M), findall(., phrase(e(_), Ls), Sols), length(Sols, Count).
以下是一些解决方案:
?- length(_, M), time(es_count(M, Count)), portray_clause(m_count(M,Count)), false. % 7 inferences, 0.000 CPU in 0.000 seconds (64% CPU, 636364 Lips) % 28 inferences, 0.000 CPU in 0.000 seconds (81% CPU, 1120000 Lips) m_count(1, 1). % 29 inferences, 0.000 CPU in 0.000 seconds (31% CPU, 1318182 Lips) m_count(2, 1). % 33 inferences, 0.000 CPU in 0.000 seconds (76% CPU, 1736842 Lips) m_count(3, 2). % 41 inferences, 0.000 CPU in 0.000 seconds (81% CPU, 1952381 Lips) m_count(4, 4). % 61 inferences, 0.000 CPU in 0.000 seconds (88% CPU, 2178571 Lips) m_count(5, 9). % 109 inferences, 0.000 CPU in 0.000 seconds (91% CPU, 2595238 Lips) m_count(6, 21). % 230 inferences, 0.000 CPU in 0.000 seconds (93% CPU, 2948718 Lips) m_count(7, 51). % 538 inferences, 0.000 CPU in 0.000 seconds (97% CPU, 3221557 Lips) m_count(8, 127). % 1,337 inferences, 0.000 CPU in 0.000 seconds (99% CPU, 3293103 Lips) m_count(9, 323). % 3,434 inferences, 0.001 CPU in 0.001 seconds (99% CPU, 3400000 Lips) m_count(10, 835). % 9,000 inferences, 0.003 CPU in 0.003 seconds (94% CPU, 3301541 Lips) m_count(11, 2188). % 23,908 inferences, 0.007 CPU in 0.024 seconds (31% CPU, 3300387 Lips) m_count(12, 5798). % 64,158 inferences, 0.019 CPU in 0.024 seconds (79% CPU, 3387792 Lips) m_count(13, 15511). % 173,579 inferences, 0.051 CPU in 0.062 seconds (83% CPU, 3397448 Lips) m_count(14, 41835). % 472,853 inferences, 0.139 CPU in 0.152 seconds (92% CPU, 3393690 Lips) m_count(15, 113634).
Prolog是一个很好的语言,可以基于声明性描述详尽地生成生成解决方案!
答案 1 :(得分:3)
按照@DavidEisenstat的建议,在recurrence relation的Prolog中编码:
.sapUiArea {
background-color: #fafafa;
color: #333333;
font-family: Arial, Helvetica, sans-serif;
font-size: 16px;
}
我们得到了
motzkin_numbers(0, 1).
motzkin_numbers(1, 1).
motzkin_numbers(N, M) :-
N>1,
N1 is N-1,
motzkin_numbers(N1, M1),
N2 is N-2,
aggregate_all(sum(MiP), (
between(0, N2, I),
motzkin_numbers(I, M_i),
I_n1i is N-2-I,
motzkin_numbers(I_n1i, M_n1i),
MiP is M_i * M_n1i), Ms),
M is M1 + Ms.
但SWI-Prolog最近添加了表格:只需添加此声明
?- length(_,N), time(motzkin_numbers(N,M)).
...
N = 14,
M = 113634 ;
% 4 inferences, 0.000 CPU in 0.000 seconds (89% CPU, 115724 Lips)
% 1,863,722 inferences, 1.107 CPU in 1.107 seconds (100% CPU, 1683529 Lips)
N = 15,
M = 310572 ;
% 4 inferences, 0.000 CPU in 0.000 seconds (88% CPU, 129232 Lips)
% 4,499,430 inferences, 2.645 CPU in 2.646 seconds (100% CPU, 1700821 Lips)
N = 16,
M = 853467
...
我们得到了
:- table motzkin_numbers/2.
答案 2 :(得分:1)
在Python 3中:
def ubtrees(n):
if n == 1:
yield 't'
elif n > 1:
for t in ubtrees(n - 1):
yield '(u {})'.format(t)
for i in range(1, n - 1):
for t1 in ubtrees(i):
for t2 in ubtrees(n - 1 - i):
yield '(b {} {})'.format(t1, t2)
此递归枚举过程对应于重复
M_1 = 1
M_n = M_{n-1} + sum_{i=1}^{n-2} M_i M_{n-1-i},
从Wikipedia中给出的重复次数转移一次。