Decision Trees Ranking and Unranking Prof. Tesler Math 184A - - PowerPoint PPT Presentation

decision trees ranking and unranking
SMART_READER_LITE
LIVE PREVIEW

Decision Trees Ranking and Unranking Prof. Tesler Math 184A - - PowerPoint PPT Presentation

Decision Trees Ranking and Unranking Prof. Tesler Math 184A Winter 2017 Prof. Tesler Decision Trees; Ranking and Unranking Math 184A / Winter 2017 1 / 33 Permutations of [ 3 ] in lexicographic order List the permutations of [ 3 ] in one-line


slide-1
SLIDE 1

Decision Trees Ranking and Unranking

  • Prof. Tesler

Math 184A Winter 2017

  • Prof. Tesler

Decision Trees; Ranking and Unranking Math 184A / Winter 2017 1 / 33

slide-2
SLIDE 2

Permutations of [3] in lexicographic order

List the permutations of [3] in one-line form in lexicographic order and number them (rank them) 0, 1, 2, . . . . rank(312) = 4 and unrank(4) = 312 Rank Permutation 123 1 132 2 213 3 231 4 312 5 321

Sample questions

For n = 6, what is the 300th permutation? What is the rank of permutation 625143? What is the permutation immeditately after (or before) 625143? Develop a systematic way to list all objects in some set (k-element subsets of [n]; partitions of [n] into k parts; Dyck words Wn; etc.) in a predictable order, and ask similar questions for those lists.

  • Prof. Tesler

Decision Trees; Ranking and Unranking Math 184A / Winter 2017 2 / 33

slide-3
SLIDE 3

Applications in Math and Computer Science

Let S be a finite set and N = |S|. Lists: Systematic way to list all elements of S. Iterators: Systematic way to loop through all elements of S. Storage space: It takes less space for a program to store one number 0, 1, 2, . . . , N − 1 than the whole object. E.g., represent the three element permutation 3,1,2 by its rank, 4. Random element: Picking an element of S at random (with uniform probability) becomes easy:

Choose a random number r = 0, 1, . . . , N − 1, each with probability 1

N

then compute x = unrank(r).

We illustrate with permutations. This also applies to other Combinatorial structures that can be constructed by a decision tree or recursion (e.g., integer partitions and compositions, set partitions, various types of trees and graphs, Dyck words, etc.).

  • Prof. Tesler

Decision Trees; Ranking and Unranking Math 184A / Winter 2017 3 / 33

slide-4
SLIDE 4

Decision tree for permutations of [n] in lex order

4 3 2 1 2 2 1 1 3 3 3 3 2 2 1 1 312 321 123 132 213 231

1

a

2

a

3

a rank(x) x 2 3 5 1

We will form permutations x = a1 a2 . . . an in lexicographic order. First decision: which number goes first? Call it a1. All permutations with a1 = 1 come before all those with a1 = 2, which come before all those with a1 = 3, . . . Second decision: a2; Third decision: a3; . . . After making all decisions, we are at a leaf, x = a1 a2 . . . an.

  • Prof. Tesler

Decision Trees; Ranking and Unranking Math 184A / Winter 2017 4 / 33

slide-5
SLIDE 5

Decision tree for permutations of [n] in lex order

4 3 2 1 2 2 1 1 3 3 3 3 2 2 1 1 312 321 123 132 213 231

1

a

2

a

3

a rank(x) x 2 3 5 1

The decision tree forms permutations x = a1 a2 . . . an in lexicographic order, as leaves of the decision tree. Number the leaves 0, 1, . . . , N − 1 from left-to-right. The rank of leaf x is the number of leaves to its left. Note: using lowercase n for size of permutation and uppercase N for number of permutations.

  • Prof. Tesler

Decision Trees; Ranking and Unranking Math 184A / Winter 2017 5 / 33

slide-6
SLIDE 6

Residual trees

We will demonstrate computing rank(x) for x = 321. Form the path e1, . . . , ek (shown dotted) from the root down to x. Edge ei goes from vertex vi−1 down to vertex vi. The residual tree R(ei) is the subtree rooted at vi−1, consisting of vi−1 and all branches descending from it strictly to the left of ei. R(e1) R(e2) R(e3) x

e ∆( )=0

3

e ∆( )=1

2

e

2

e

3

e

1

e 3 2 1 2 2 1 1 3 3 3 3 2 2 1 1 312 321 123 132 213 231

2

a

3 1

a a ∆( )=4

1

∆(ei) = # leaves of decision tree in R(ei). Don’t count root, vi−1; it may have degree 1 in R(ei), but it wasn’t a leaf in the original tree. rank(x) = ∆(e1) + ∆(e2) + · · · + ∆(ek)

  • Prof. Tesler

Decision Trees; Ranking and Unranking Math 184A / Winter 2017 6 / 33

slide-7
SLIDE 7

Residual trees for 231

3

e

2

e ∆( )=2

1

e ∆( )=0

3

e ∆( )=1

2

e 3 2 1 2 2 1 1 3 3 3 3 2 2 1 1 312 321 123 132 213 231

2

a

3 1

a a

1

e

rank(231) = ∆(e1) + ∆(e2) + ∆(e3) = 2 + 1 + 0 = 3

  • Prof. Tesler

Decision Trees; Ranking and Unranking Math 184A / Winter 2017 7 / 33

slide-8
SLIDE 8

Rank of 625143

...

e

2

e

3

e

5

e

6

e

4

e 1

............... ...

1 3 4 1 5

.........

2 5

... ...

4 3 3 4

...

1

... ...

3 4 6 3 5

1

Draw a skeleton of the decision tree: Show the path for 625143. Indicate the branches to the left and right of each vertex on the path, but don’t need to fill them

  • ut to the bottom.

Compute how many leaves are on each branch left of the path.

  • Prof. Tesler

Decision Trees; Ranking and Unranking Math 184A / Winter 2017 8 / 33

slide-9
SLIDE 9

Rank of 625143

∆( )=5(5!)=600

1

e 5! 5! 5! 5! 5! ∆( )=1(4!)=24

2

e

5

e

6

e ∆( )=0(2!)=0

4

e

4

e ∆( )=1(1!)=1

5

e ∆( )=3(3!)=18

3

e ∆( )=0(0!)=0

6

e 1

............... ...

1 3 4 1 5

.........

2 5

... ...

4 3 3 4

...

1

... ...

3 4 6 3 5

1

...

e

2

e

3

e

Draw a skeleton of the decision tree: Show the path for 625143. Indicate the branches to the left and right of each vertex on the path, but don’t need to fill them

  • ut to the bottom.

Compute how many leaves are on each branch left of the path. rank(625143) = ∆(e1) + ∆(e2) + · · · + ∆(e6) = 600 + 24 + 18 + 0 + 1 + 0 = 643 unrank(643) = 625143

  • Prof. Tesler

Decision Trees; Ranking and Unranking Math 184A / Winter 2017 9 / 33

slide-10
SLIDE 10

Computing decision numbers, di, with example 625143

∆( )=5(5!)=600

1

e 5! 5! 5! 5! 5! ∆( )=1(4!)=24

2

e

5

e

6

e ∆( )=0(2!)=0

4

e

4

e ∆( )=1(1!)=1

5

e ∆( )=3(3!)=18

3

e ∆( )=0(0!)=0

6

e 1

............... ...

1 3 4 1 5

.........

2 5

... ...

4 3 3 4

...

1

... ...

3 4 6 3 5

1

...

e

2

e

3

e

Number the branches of each vertex 0, 1, 2, . . . from left to right. Let di = branch # at the ith level. (d1, . . . , d6) = (5, 1, 3, 0, 1, 0) Branch numbers di are not the same as the edge labels ai: (a1, . . . , a6) = (6, 2, 5, 1, 4, 3).

  • Prof. Tesler

Decision Trees; Ranking and Unranking Math 184A / Winter 2017 10 / 33

slide-11
SLIDE 11

Computing decision numbers, di, with example 625143

∆( )=5(5!)=600

1

e 5! 5! 5! 5! 5! ∆( )=1(4!)=24

2

e

5

e

6

e ∆( )=0(2!)=0

4

e

4

e ∆( )=1(1!)=1

5

e ∆( )=3(3!)=18

3

e ∆( )=0(0!)=0

6

e 1

............... ...

1 3 4 1 5

.........

2 5

... ...

4 3 3 4

...

1

... ...

3 4 6 3 5

1

...

e

2

e

3

e

Let x = a1 a2 . . . an. Sequentially compute d1, . . . , dn. For x = 625143, compute d3:

a3 = 5. The options < 5 are 1, 2, 3, 4. We already used 6 and 2. Strike these out, leaving {1, 2, 3, 4} \ {6, 2} = {1, 3, 4}. d3 = | {1, 3, 4} | = 3. Each branch left of e3 has (n − 3)! leaves, so ∆(e3) = d3 · (n − 3)! = 3 · 3! = 18.

Similarly,

di = | {1, 2, . . . , ai − 1} \ {a1, a2, . . . , ai−1} | ∆(ei) = di · (n − i)!

  • Prof. Tesler

Decision Trees; Ranking and Unranking Math 184A / Winter 2017 11 / 33

slide-12
SLIDE 12

General rule for rank(x) for permutations in lex order

Let x = a1 a2 . . . an. We’ll use n = 6 and x = 625143. Let di = | {1, 2, . . . , ai − 1} \ {a1, a2, . . . , ai−1} | a1 = 6 d1 = | {1, 2, 3, 4, 5} \ ∅| = 5 ∆(e1) = 5 · 5! = 600 a2 = 2 d2 = | {1} \ {6} | = | {1} | = 1 ∆(e2) = 1 · 4! = 24 a3 = 5 d3 = | {1, 2, 3, 4} \ {6, 2} | = | {1, 3, 4} | = 3 ∆(e3) = 3 · 3! = 18 a4 = 1 d4 = |∅ \ {6, 2, 5} | = |∅| = 0 ∆(e4) = 0 · 2! = a5 = 4 d5 = | {1, 2, 3} \ {6, 2, 5, 1} | = | {3} | = 1 ∆(e5) = 1 · 1! = 1 a6 = 3 d6 = | {1, 2} \ {6, 2, 5, 1, 4} | = |∅| = 0 ∆(e6) = 0 · 0! = rank(x) = 643 Each branch left of the one we chose has (n − i)! leaves (ignoring the root), so ∆(ei) = di · (n − i)!. rank(x) = n

i=1 ∆(ei) = n i=1 di · (n − i)!

  • Prof. Tesler

Decision Trees; Ranking and Unranking Math 184A / Winter 2017 12 / 33

slide-13
SLIDE 13

Inverse relation between di and ai. Example: 625143.

∆( )=5(5!)=600

1

e 5! 5! 5! 5! 5! ∆( )=1(4!)=24

2

e

5

e

6

e ∆( )=0(2!)=0

4

e

4

e ∆( )=1(1!)=1

5

e ∆( )=3(3!)=18

3

e ∆( )=0(0!)=0

6

e 1

............... ...

1 3 4 1 5

.........

2 5

... ...

4 3 3 4

...

1

... ...

3 4 6 3 5

1

...

e

2

e

3

e

Inverting di’s to compute ai’s: After computing a1 = 6 and a2 = 2, use d3 = 3 to compute a3. The branches are a subset of [6] = {1, . . . , 6}. We already used 6 and 2, so remove them, leaving [6] \ {6, 2} = {1, 3, 4, 5}. Index these 0,1,2,3 (0-based indexing instead of 1-based indexing 1,2,3,4). Since d3 = 3, the 3rd is a3 = 5. In general: ai is the dith (starting at 0th, not 1st) smallest number in [n] \ {a1, . . . , ai−1}.

  • Prof. Tesler

Decision Trees; Ranking and Unranking Math 184A / Winter 2017 13 / 33

slide-14
SLIDE 14

Inverse relation between di and ai. Example: 625143.

Let di = | {1, 2, . . . , ai − 1} \ {a1, a2, . . . , ai−1} |. d1 = | {1, 2, 3, 4, 5} \ ∅| = 5 ∆(e1) = 5 · 5! = 600 d2 = | {1} \ {6} | = | {1} | = 1 ∆(e2) = 1 · 4! = 24 d3 = | {1, 2, 3, 4} \ {6, 2} | = | {1, 3, 4} | = 3 ∆(e3) = 3 · 3! = 18 d4 = |∅ \ {6, 2, 5} | = |∅| = 0 ∆(e4) = 0 · 2! = d5 = | {1, 2, 3} \ {6, 2, 5, 1} | = | {3} | = 1 ∆(e5) = 1 · 1! = 1 d6 = | {1, 2} \ {6, 2, 5, 1, 4} | = |∅| = 0 ∆(e6) = 0 · 0! = rank(x) = 643 Inverse: Given di’s, compute ai’s. After computing a1, . . . , ai−1, remove them from the list 1, . . . , n. The dith (0-based) remaining number is ai: Position d1 = 5 in 1, 2, 3, 4, 5, 6 is a1 = 6. Position d2 = 1 in 1, 2, 3, 4, 5 is a2 = 2. Position d3 = 3 in 1, 3, 4, 5 is a3 = 5. Position d4 = 0 in 1, 3, 4 is a4 = 1. Position d5 = 1 in 3, 4 is a5 = 4. Position d6 = 0 in 3 is a6 = 3.

  • Prof. Tesler

Decision Trees; Ranking and Unranking Math 184A / Winter 2017 14 / 33

slide-15
SLIDE 15

300th permutation of [6] (count starts at 0: unrank(0) = 123456)

Permutations Ranks Decision 1----- 0,...,119 2----- 120,...,239 3----- 240,...,359 a1 = 3 4----- 360,...,479 5----- 480,...,599 6----- 600,...,719 31---- 240,...,263 32---- 264,...,287 34---- 288,...,311 a2 = 4 . . . 341--- 288,...,293 342--- 294,...,299 345--- 300,...,305 a3 = 5 . . . 3451-- 300,...,301 a4 = 1 . . . 34512- 300,...,300 a5 = 2 . . . 345126 300 a6 = 6

Greedy algorithm

We’ll compute x=unrank(k) for k=300. Choose the decision edge e1 = (root, v1) with ∆(e1) as large as possible and ∆(e1) k. Choose the decision edge e2 = (v1, v2) with ∆(e2) as large as possible and ∆(e1) + ∆(e2) k. Choose the edge e3 = (v2, v3) with ∆(e3) as large as possible and ∆(e1) + ∆(e2) + ∆(e3) k. Continue in that fashion until a leaf is reached and output the value x at that leaf: unrank(k) = x.

  • Prof. Tesler

Decision Trees; Ranking and Unranking Math 184A / Winter 2017 15 / 33

slide-16
SLIDE 16

300th permutation of [6] (count starts at 0: unrank(0) = 123456)

e1

Choose edge e1 from the root with ∆(e1) maximal s.t. ∆(e1)300. ∆(e1) = 5! · d1 = 120 d1 300. d1 = 2 maximizes 120 d1 while satisfying 120 d1 300. Position d1 = 2 (0-based) in 1, 2, 3, 4, 5, 6 is a1 = 3.

e2

Choose e2 under e1 with ∆(e2) maximal s.t. ∆(e1)+∆(e2)300. ∆(e1) + ∆(e2) 300 simplifies to ∆(e2) 300 − 240 = 60. The rank deficit is 60. ∆(e2) = 4! · d2 = 24 d2 60 so d2 = 2. Position d2 = 2 (0-based) in 1, 2, 4, 5, 6 is a2 = 4.

e3, . . . , e6

Continue in this fashion to get d3, . . . , d6.

  • Prof. Tesler

Decision Trees; Ranking and Unranking Math 184A / Winter 2017 16 / 33

slide-17
SLIDE 17

300th permutation of [6] (count starts at 0: unrank(0) = 123456)

In order i = 1, . . . , 6, pick each di as an integer as large as possible with ∆(ei) 300 − (∆(e1) + · · · + ∆(ei−1)): ∆(e1) = 5! d1 = 120 d1 300 d1 = 2 ∆(e1) = 240 ∆(e2) = 4! d2 = 24 d2 300 − 240 = 60 d2 = 2 ∆(e2) = 48 ∆(e3) = 3! d3 = 6 d3 60 − 48 = 12 d3 = 2 ∆(e3) = 12 ∆(e4) = 2! d4 = 2 d4 12 − 12 = 0 d4 = 0 ∆(e4) = 0 ∆(e5) = 1! d5 = 1 d5 0 − 0 = 0 d5 = 0 ∆(e5) = 0 ∆(e6) = 0! d6 = 1 d6 0 − 0 = 0 d6 = 0 ∆(e6) = 0 Position d1 = 2 in 1, 2, 3, 4, 5, 6 is a1 = 3. (All positions are 0-based.) Position d2 = 2 in 1, 2, 4, 5, 6 is a2 = 4. Position d3 = 2 in 1, 2, 5, 6 is a3 = 5. Position d4 = 0 in 1, 2, 6 is a4 = 1. Position d5 = 0 in 2, 6 is a5 = 2. Position d6 = 0 in 6 is a6 = 6. unrank(300) = a1 · · · a6 = 345126.

  • Prof. Tesler

Decision Trees; Ranking and Unranking Math 184A / Winter 2017 17 / 33

slide-18
SLIDE 18

Successors and predecessors

The successor of x is the permutation that comes immediately after x in lexicographic order. The predecessor of x is the permutation that comes immediately before x in lexicographic order. These terms generalize to other sets and other orders. Permutations of [3] in lex order: 123, 132, 213, 231, 312, 321. succ(213) = 231 and pred(231) = 213. Application: This is useful to loop (iterate) over all elements of a

  • set. Method 2 may be faster than Method 1, since often, most

steps in computing unrank(i) and unrank(i + 1) are the same.

Method 1: Unrank all numbers 0, 1, 2, . . . , N − 1

for i from 0 to N-1 { x = unrank(i); ... do something with x ... }

Method 2: Directly go from each element to the next one

x = unrank(0); while (x exists) { ... do something with x ... x = succ(x); }

  • Prof. Tesler

Decision Trees; Ranking and Unranking Math 184A / Winter 2017 18 / 33

slide-19
SLIDE 19

Successor of permutation 5637421

5641237

... ... ... ... ... ... ... ...

3 7 4 2 1 1 2 3 7

... ...

4

... ...

5 6

......

5637421

Start at leaf x = 5637421. Keep moving up the tree until you can move right. 1, 2, 4, 7 are each the rightmost branch (largest choice) of their parent. 3 has siblings 4, 7 to the right. Move to the edge immediately right (4). Then go down, always choosing the leftmost branch (smallest choice), until terminating at a leaf. succ(5637421) = 5641237 pred(5641237) = 5637421

  • Prof. Tesler

Decision Trees; Ranking and Unranking Math 184A / Winter 2017 19 / 33

slide-20
SLIDE 20

Computing successor using ascents and descents

Ascents and descents

An ascent is consecutive entries a < b; a descent is consecutive entries a > b. x = 5637421 has 5 < 6 > 3 < 7 > 4 > 2 > 1 ascents 5<6, 3<7 descents 6>3, 7>4, 4>2, 2>1

Computing successor of x = 5637421

Find the rightmost ascent a < b in x: It’s 3<7, so a = 3. Find the smallest number c > a that’s to the right of a in x: c = 4. Take the entries left of a as-is: 56 then c: 4 then the other numbers in ascending order: 1237 Put them together in that order: y = succ(x) = 5641237

  • Prof. Tesler

Decision Trees; Ranking and Unranking Math 184A / Winter 2017 20 / 33

slide-21
SLIDE 21

Computing predecessor using ascents and descents

Computing predecessor of y = 5641237

5 < 6 > 4 > 1 < 2 < 3 < 7 ascents 5<6, 1<2, 2<3, 3<7 descents 6>4, 4>1 Find the rightmost descent a > b in y: It’s 4>1, so a = 4. Find the largest number c < a that’s to the right of a in y: c = 3. Take the entries left of a as-is: 56 then c: 3 then the other numbers in descending order: 7421 Put them together in that order: x = pred(y) = 5637421

  • Prof. Tesler

Decision Trees; Ranking and Unranking Math 184A / Winter 2017 21 / 33

slide-22
SLIDE 22

New example: List decreasing functions f : [k] → [n] in lex order

  • Prof. Tesler

Decision Trees; Ranking and Unranking Math 184A / Winter 2017 22 / 33

slide-23
SLIDE 23

List decreasing functions f : [k] → [n] in lex order

List the decreasing functions f : [3] → [5] in one-line form in lexicographic order and number them (rank them) 0, 1, 2, . . . . rank(532) = 6 and unrank(6) = 532. There are n

k

  • decreasing functions:

choose a k-element subset of [n] and put them in decreasing order. Rank Function 321 1 421 2 431 3 432 4 521 5 531 6 532 7 541 8 542 9 543

  • Prof. Tesler

Decision Trees; Ranking and Unranking Math 184A / Winter 2017 23 / 33

slide-24
SLIDE 24

List decreasing functions f : [k] → [n] in lex order

Decision tree for decreasing functions f : [3] → [5]

3 4 5 6 7 8 9

2

a

3

a rank(x) x

1

a 321 421 431 432 521 531 532 541 542 543 3 4 5 2 2 3 2 3 4 1 1 1 2 1 1 2 1 3 2 1 2

ai stands for f(i). Sequentially choose a1, . . . , ak. Choices for a1: k through n. Choices for ai: k − i + 1 through ai−1 − 1.

  • Prof. Tesler

Decision Trees; Ranking and Unranking Math 184A / Winter 2017 24 / 33

slide-25
SLIDE 25

Ranking decreasing functions

1

e

2

e

3

e

2

a

3

a x

1

a 321 421 431 432 521 531 532 541 542 543 3 4 5 2 2 3 2 3 4 1 1 1 2 1 1 2 1 3

2

For decreasing functions f : [3] → [5] listed in lex order, compute rank(542). e1 = (−−−, 5−−), e2 = (5−−, 54−), e3 = (54−, 542). R(e1) has all decreasing functions f : {1, 2, 3} → [5 − 1], so ∆(e1) = 4

3

  • = 4.

R(e2) has all decreasing functions f : {2, 3} → [4 − 1], so ∆(e2) = 3

2

  • = 3.

R(e3) has all decreasing functions f : {3} → [2 − 1], so ∆(e1) = 1

1

  • = 1.

rank(542) = 4 + 3 + 1 = 8 In general, ∆(ei) = ai−1

k+1−i

  • and

rank(a1 a2 . . . ak) = a1−1

k

  • +

a2−1

k−1

  • +

a3−1

k−2

  • + · · · +

ak−1

1

  • .

Note: It’s independent of n. We just need n k.

  • Prof. Tesler

Decision Trees; Ranking and Unranking Math 184A / Winter 2017 25 / 33

slide-26
SLIDE 26

Ranking decreasing functions

For decreasing functions f : [4] → [12] listed in lex order, compute rank(10, 7, 5, 2). 10 − 1 4

  • +

7 − 1 3

  • +

5 − 1 2

  • +

2 − 1 1

  • =

9 4

  • +

6 3

  • +

4 2

  • +

1 1

  • = 126 + 20 + 6 + 1 = 153
  • Prof. Tesler

Decision Trees; Ranking and Unranking Math 184A / Winter 2017 26 / 33

slide-27
SLIDE 27

Unranking decreasing functions

Find unrank(43), the 43rd decreasing function f : [3] → [9] in lex order. (Start counting from 0th function: unrank(0) = 321.) There are 9

3

  • = 84 such functions. 43 < 84, so unrank(43) exists.

rank(a1 a2 a3) = a1−1

3

  • +

a2−1

2

  • +

a3−1

1

  • and 9 a1 > a2 > a3 1.

a 1 2 3 4 5 6 7 8 9 a1−1

3

  • 1

4 10 20 35 56 a2−1

2

  • 1

3 6 10 15 21 28 a3−1

1

  • 1

2 3 4 5 6 7 8

We will select a1, a2, a3 by the greedy algorithm described previously.

  • Prof. Tesler

Decision Trees; Ranking and Unranking Math 184A / Winter 2017 27 / 33

slide-28
SLIDE 28

Unranking decreasing functions: find unrank(43)

rank(a1 a2 a3) = a1−1

3

  • +

a2−1

2

  • +

a3−1

1

  • and 9 a1 > a2 > a3 1.

a 1 2 3 4 5 6 7 8 9 a1−1

3

  • 1

4 10 20 35 56 a2−1

2

  • 1

3 6 10 15 21 28 a3−1

1

  • 1

2 3 4 5 6 7 8

Choose 3a1 9 with a1−1

3

  • as big as possible 43:

a1 = 8

Choose 2a2 7 with a1−1

3

  • +

a2−1

2

  • as big as possible 43:

Simplify: 35 + a2−1

2

  • 43 so

a2−1

2

  • 8.

a2 = 5

Choose 1a3 4 with a1−1

3

  • +

a2−1

2

  • +

a3−1

1

  • as big as possible 43:

Simplify: 35 + 6 + (a3 − 1) = 40 + a3 43 so a3 3. a3 = 3 rank(a1 a2 a3) = a1−1

3

  • +

a2−1

2

  • +

a3−1

1

  • =

7

3

  • +

4

2

  • +

2

1

  • = 35+6+2 = 43.

unrank(43) = 853

  • Prof. Tesler

Decision Trees; Ranking and Unranking Math 184A / Winter 2017 28 / 33

slide-29
SLIDE 29

Converting recursions into decision trees or code Example: Catalan numbers

  • Prof. Tesler

Decision Trees; Ranking and Unranking Math 184A / Winter 2017 29 / 33

slide-30
SLIDE 30

Converting recursions into decision trees or code

Consider the various recursions we’ve studied for binomial coefficients, Stirling numbers of the second kind, signless Stirling numbers of the first kind, Catalan numbers, etc. Any of these can be unravelled to form a decision tree or computer code to list out all elements of the set that they count. E.g., the recursion for Catalan numbers Cn = n−1

i=0 CiCn−1−i with C0 = 1,

gives rise to pseudocode to list all Dyck words on n 1’s and n 2’s:

function Dyck(n) { if (n == 0) { return (’’); } results = (); for i from 0 to n-1 { foreach x in Dyck(i) { foreach y in Dyck(n-1-i) { push results, (’1’ . x . ’2’ . y); } } } return(results); }

  • Prof. Tesler

Decision Trees; Ranking and Unranking Math 184A / Winter 2017 30 / 33

slide-31
SLIDE 31

Converting recursions into ranks

function Dyck(n) { if (n == 0) { return (’’); } results = (); for i from 0 to n-1 { foreach x in Dyck(i) { foreach y in Dyck(n-1-i) { push results, (’1’ . x . ’2’ . y); } } } return(results); }

The rank of 1 x 2 y ∈ Wn (with i ∈ {0, . . . , n − 1}, x ∈ Wi, y ∈ Wn−1−i) is rankn(1 x 2 y) =  

i−1

  • j=0

Cj Cn−1−j  

  • (a)

+ ranki(x) · Cn−1−i

  • (b)

+ rankn−1−i(y)

  • (c)

Before reaching 1 x 2 y: (a) Previous iterations of the i-loop list all 1 x′ 2 y′ with j < i, x′ ∈ Wj, and y′ ∈ Wn−1−j. (b) Within the current iteration of the i loop, previous iterations of the x loop list all 1 x′ 2 y′ with x′ ∈ Wi where ranki(x′) < ranki(x). There are ranki(x) such values of x′, each with Cn−1−i values of y.

  • Prof. Tesler

Decision Trees; Ranking and Unranking Math 184A / Winter 2017 31 / 33

slide-32
SLIDE 32

Converting recursions into ranks

function Dyck(n) { if (n == 0) { return (’’); } results = (); for i from 0 to n-1 { foreach x in Dyck(i) { foreach y in Dyck(n-1-i) { push results, (’1’ . x . ’2’ . y); } } } return(results); }

The rank of 1 x 2 y ∈ Wn (with i ∈ {0, . . . , n − 1}, x ∈ Wi, y ∈ Wn−1−i) is rankn(1 x 2 y) =  

i−1

  • j=0

Cj Cn−1−j  

  • (a)

+ ranki(x) · Cn−1−i

  • (b)

+ rankn−1−i(y)

  • (c)

Before reaching 1 x 2 y (continued): (c) Within the current iteration of the i, x loops, previous iterations of the y loop list 1 x 2 y′ for y′ ∈ Wn−1−i that come before y. (d) Finally, we arrive at 1 x 2 y.

  • Prof. Tesler

Decision Trees; Ranking and Unranking Math 184A / Winter 2017 32 / 33

slide-33
SLIDE 33

Converting recursions into ranks

Rank W0 null Rank W1 () Rank W2 ()() 1 (()) Rank W3 ()()() 1 ()(()) 2 (())() 3 (()()) 4 ((())) Rank W4 ()()()() 1 ()()(()) 2 ()(())() 3 ()(()()) 4 ()((())) 5 (())()() 6 (())(()) Rank W4 7 (()())() 8 ((()))() 9 (()()()) 10 (()(())) 11 ((())()) 12 ((()())) 13 (((()))) To compute rank of ((()))(()) in W5: Decompose

  • (())
  • (()) into i = 2, x = (()), y = (()).

i = 0, 1 contributed C0 C4 + C1 C3 = 1 · 14 + 1 · 5 = 19 elements. For i = 2, ()() came before x = (()), so ranki(x) = 1. It was paired with Cn−i−1 = C2 = 2 values of y, contributing 1 · 2 = 2 elements. For i = 2 and x = (()), one y, ()(), precedes y = ()(), so rankn−i−1(y) = 1. The rank is 19 + 2 + 1 + 0 = 22.

  • Prof. Tesler

Decision Trees; Ranking and Unranking Math 184A / Winter 2017 33 / 33