Download Generating Function Example - Ordered Binary Trees

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Location arithmetic wikipedia , lookup

Transcript
Generating Function Example - Ordered Binary
Trees - J. T. Butler
Count the ordered binary trees
Example
• An important topic in
computer science is graphs
and trees. These are useful,
for example, in data structures
r=1
r=2
r=3
r=4
G. Func. Examples - J. T. Butler
1
G. Func. Examples - J. T. Butler
Note:
2
“binary” --- at most two edges occur at each
node
“tree” --- a connected graph with no circuits
q
“ordered” — order is counted
≠
G. Func. Examples - J. T. Butler
3
G. Func. Examples - J. T. Butler
r is the number of leaves
These count as
2 not 1.
4
Let T (x) be the ordinary generating
function for tr, the number of
ordered binary trees with r leaves
T (x) = t0 + t1 x + t2 x2 + t3 x3 + …
leaves
G. Func. Examples - J. T. Butler
5
G. Func. Examples - J. T. Butler
1
6
Generating Function Example - Ordered Binary
Trees - J. T. Butler
We assume t0 = 0. That is, there are
no trees with 0 nodes. We form an
ordered binary tree as follows:
L
To form a tree, choose one tree for L and
one for R . Each choice is expressed as
T(x), giving T2(x). +x occurs because the
single tree with one leaf is not counted in
T2(x).
R
Subtrees with one
or more nodes
T (x) = T2(x) + x
G. Func. Examples - J. T. Butler
Rewrite (1) and solve for T(x).
(1)
7
G. Func. Examples - J. T. Butler
8
Apply the binomial theorem
0 = T 2 ( x) − T ( x) + x
(1 + x ) n = 1 + ∑
1± 1− 4x 1 1
= ± (1 − 4 x )1/ 2
T ( x) =
2
2 2
r =1
FG nIJ x
H rK
to get
(1 − 4 x ) 1/ 2 = 1 + ∑
r =1
G. Func. Examples - J. T. Butler
9
G. Func. Examples - J. T. Butler
(1 / 2)( −1 / 2)( −3 / 2)( −5 / 2)K
( −4) r x r
r
!(
1
/
2
−
r
)(
−
1
/
2
−
r
)
K
r =1
= 1− ∑
(1 / 2)( −1 / 2)( −3 / 2)K (1 / 2 − r + 1)
( −4 ) r x r
r
!
r =1
= 1− ∑
∞
= 1+ ∑
∞
r =1
∞
∞
= 1+ ∑
r =1
1 1 ⋅ 1 ⋅ 3 ⋅K⋅ ( 2r − 3) r r
4 x
r
r!
r =1 2
∞
∞
(1 − 4 x )1/ 2 = 1 − ∑
= 1− ∑
r =1
11
r
2 ⋅ 4 ⋅K⋅ ( 2 r − 2 ) 1 ⋅ 3 ⋅ 5 ⋅K⋅ ( 2r − 3) r r
4 x
2 r −1 ⋅ 1 ⋅ 2 ⋅K⋅ ( r − 1)
2r r !
1 ⋅ 2 ⋅ 3 ⋅ 4 ⋅ 5 ⋅K⋅ ( 2r − 3)(2 r − 2 ) r
2x
( r − 1)!( r − 1)! r
FG
H
IJ
K
2 2r − 2 r
x
r r −1
G. Func. Examples - J. T. Butler
2
FG1 / 2IJ (−4 x)
HrK
10
Note: The numerator is 1 when r = 1, not -1.
G. Func. Examples - J. T. Butler
r
12
Generating Function Example - Ordered Binary
Trees - J. T. Butler
Thus, the number of ordered binary trees with r
leaf nodes is
Of the two solutions for T(x), choose the
negative one, so that
T ( x) =
tr =
1 1
− (1 − 4 x )1/ 2
2 2
FG
H
IJ
K
1 2r − 2
r r −1
=0
G. Func. Examples - J. T. Butler
13
G. Func. Examples - J. T. Butler
1 2
3
4
tr
1 1
2
5
G. Func. Examples - J. T. Butler
5
6
7
14 42 132
14
How many ways are there to
place parentheses around r
letters so that inside each pair
there are two terms?
15
G. Func. Examples - J. T. Butler
16
Catalan numbers also count ways
to divide polygons into triangles
and ways a rook can go from the
lower left corner of a half
chessboard to the upper right
corner.
Example:
Ways to place parantheses
Number of ways
( ab)
1
((ab)c) ( a (bc))
2
((ab)(cd )), (((ab) c)d ),(a (b(cd ))),(a ((bc)d )), ((a (bc))d ) 5
G. Func. Examples - J. T. Butler
r=0
There are called “Catalan” numbers
after Eugene Charles Catalan, a
mathematician from Belgium.
In
1838, he solved the following problem:
We have
r
r ≥1
17
G. Func. Examples - J. T. Butler
3
18
Generating Function Example - Ordered Binary
Trees - J. T. Butler
14
Number of ways to divide a polygon with r
sides into triangles
1
2
5
G. Func. Examples - J. T. Butler
19
G. Func. Examples - J. T. Butler
20
5
6444444447444444448
Number of ways to go from the lower
left corner of a half chessboard to the
upper right corner
1
2
64748
64444744448
G. Func. Examples - J. T. Butler
21
G. Func. Examples - J. T. Butler
23
G. Func. Examples - J. T. Butler
4
22