# To calculate k e n let us define d e n the number of

• No School
• AA 1
• 24

This preview shows page 21 - 23 out of 24 pages.

To calculate K ( e, n ) , let us define D ( e, n ) , the number of different binary subtrees that can be generated from e empty elements, with n internal nodes to generate. We have D (0 , n ) = 0 D ( e, 0) = 1 D ( e, n ) = D ( e - 1 , n ) + D ( e + 1 , n - 1) The first equation states that no tree can be generated with zero empty node and n > 0 operators. The second equation says that if no operator is to be allocated, empty nodes must all be leaves and there is only one possible tree. The last equation states that if we have e > 0 empty nodes, the first one is either a leaf (and there are D ( e - 1 , n ) such trees) or an internal node ( D ( e + 1 , n - 1) trees). This allows us to compute D ( e, n ) for all e and n . To calculate distribution K ( e, n ) , observe that among the D ( e, n ) trees with e empty nodes and n operators, D ( e + 1 , n - 1) have a binary node in their first position. Therefore P ( K ( e, n ) = 0) = D ( e + 1 , n - 1) D ( e, n ) Of the remaining D ( e - 1 , n ) trees, D ( e, n - 1) have a binary node in their first position (same argument for e - 1 ), that is P ( K ( e, n ) = 1) = D ( e, n - 1) D ( e, n ) By induction over k , we have the general formula P ( K ( e, n ) = k ) = D ( e - k + 1 , n - 1) D ( e, n ) C.2 U NARY - BINARY TREES In the general case, internal nodes can be of two types: unary or binary. We adapt the previous algorithm by considering the two-dimensional probability distribution L ( e, n ) of position ( 0 -indexed) and arity of the next internal node (i.e. P ( L ( e, n ) = ( k, a ) is the probability that the next internal node is in position k and has arity a ). Start with an empty node, set e = 1 ; while n > 0 do Sample a position k and arity a from L ( e, n ) (if a = 1 the next internal node is unary); Sample the k next empty nodes as leaves; if a = 1 then Sample a unary operator; Create one empty child; Set e = e - k ; end else Sample a binary operator; Create two empty children; Set e = e - k + 1 ; end Set n = n - 1 ; end Algorithm 2: Generate a random unary-binary tree 21 To compute L ( e, n ) , we derive D ( e, n ) , the number of subtrees with n internal nodes that can be generated from e empty nodes. We have, for all n > 0 and e : D (0 , n ) = 0 D ( e, 0) = 1 D ( e, n ) = D ( e - 1 , n ) + D ( e, n - 1) + D ( e + 1 , n - 1) The first equation states that no tree can be generated with zero empty node and n > 0 operators. The second says that if no operator is to be allocated, empty nodes must all be leaves and there is only one possible tree. The third equation states that with e > 0 empty nodes, the first one will either be a leaf ( D ( e - 1 , n ) possible trees), a unary operator ( D ( e, n - 1) trees), or a binary operator ( D ( e + 1 , n - 1) trees). To derive L ( e, n ) , we observe that among the D ( e, n ) subtrees with e empty nodes and n internal nodes to be generated, D ( e, n - 1) have a unary operator in position zero, and D ( e + 1 , n - 1) have a binary operator in position zero. As a result, we have P ( L ( e, n ) = (0 , 1) ) = D ( e, n - 1) D ( e, n ) and P ( L ( e, n ) = (0 , 2) ) = D ( e + 1 , n - 1) D ( e, n ) As in the binary case, we can generalize these probabilities to all positions k in { 0 . . . e - 1 } P ( L ( e, n ) = ( k, 1) ) = D ( e - k, n - 1) D ( e, n ) and P ( L ( e, n ) = ( k, 2) ) = D ( e - k + 1 , n - 1) D ( e, n ) C.3 S AMPLING EXPRESSIONS To generate expressions, we sample random trees (binary, or unary binary), that we “decorate” by randomly selecting their internal nodes and leaves from a list of possible operators or mathematical entities (integers, variables, constants).  #### You've reached the end of your free preview.

Want to read all 24 pages?

• Fall '19

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern  