decision trees ranking and unranking
play

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


  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

  2. Permutations of [ 3 ] in lexicographic order List the permutations of [ 3 ] in one-line Rank Permutation form in lexicographic order and number 0 123 them ( rank them) 0 , 1 , 2 , . . . . 1 132 2 213 rank ( 312 ) = 4 and unrank ( 4 ) = 312 3 231 4 312 5 321 Sample questions For n = 6 , what is the 300 th 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 W n ; 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

  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

  4. Decision tree for permutations of [ n ] in lex order a 1 2 3 1 a 2 3 1 3 1 2 2 a 3 2 3 1 2 1 3 x 123 132 213 231 312 321 rank(x) 0 1 2 3 4 5 We will form permutations x = a 1 a 2 . . . a n in lexicographic order. First decision: which number goes first? Call it a 1 . All permutations with a 1 = 1 come before all those with a 1 = 2 , which come before all those with a 1 = 3 , . . . Second decision: a 2 ; Third decision: a 3 ; . . . After making all decisions, we are at a leaf, x = a 1 a 2 . . . a n . Prof. Tesler Decision Trees; Ranking and Unranking Math 184A / Winter 2017 4 / 33

  5. Decision tree for permutations of [ n ] in lex order a 1 2 3 1 a 2 3 1 3 1 2 2 a 3 2 3 1 2 1 3 x 123 132 213 231 312 321 rank(x) 0 1 2 3 4 5 The decision tree forms permutations x = a 1 a 2 . . . a n 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

  6. Residual trees We will demonstrate computing rank ( x ) for x = 321 . Form the path e 1 , . . . , e k (shown dotted) from the root down to x . Edge e i goes from vertex v i − 1 down to vertex v i . The residual tree R ( e i ) is the subtree rooted at v i − 1 , consisting of v i − 1 and all branches descending from it strictly to the left of e i . R ( e 1 ) R ( e 2 ) R ( e 3 ) x ∆ ( e i ) = # leaves of e decision tree in R ( e i ) . a 1 2 3 1 1 Don’t count root, v i − 1 ; it may have degree � 1 in a 2 3 1 3 1 2 e 2 2 R ( e i ) , but it wasn’t a leaf in the original tree. a 3 2 3 1 2 1 e 3 3 rank ( x ) = 123 132 213 231 312 321 ∆ ( e 1 ) + ∆ ( e 2 ) + · · · + ∆ ( e k ) ∆( )=1 e ∆( )=0 e ∆( )=4 e 2 3 1 Prof. Tesler Decision Trees; Ranking and Unranking Math 184A / Winter 2017 6 / 33

  7. Residual trees for 231 a 1 2 e 3 1 1 a 2 3 1 3 e 1 2 2 2 a 3 2 3 1 e 2 1 3 3 123 132 213 231 312 321 ∆( )=1 e ∆( )=0 e ∆( )=2 e 2 3 1 rank ( 231 ) = ∆ ( e 1 ) + ∆ ( e 2 ) + ∆ ( e 3 ) = 2 + 1 + 0 = 3 Prof. Tesler Decision Trees; Ranking and Unranking Math 184A / Winter 2017 7 / 33

  8. Rank of 625143 Draw a skeleton of the decision tree: Show the path for 625143 . e 1 Indicate the branches to the left 1 5 6 ............... and right of each vertex on the 1 3 path, but don’t need to fill them 4 5 2 e 2 ... ... ... ... out to the bottom. e 3 Compute how many leaves are on 1 3 4 5 ......... each branch left of the path. 1 e 3 4 4 ... ... e 3 4 5 ... e 6 3 Prof. Tesler Decision Trees; Ranking and Unranking Math 184A / Winter 2017 8 / 33

  9. Rank of 625143 Draw a skeleton of the decision tree: Show the path for 625143 . e 1 Indicate the branches to the left 1 5 6 ............... and right of each vertex on the 1 5! 5! 5! 5! 5! 3 path, but don’t need to fill them 4 5 2 e ∆( )=5(5!)=600 e 2 ... ... ... ... 1 out to the bottom. ∆( )=1(4!)=24 e e 2 3 Compute how many leaves are on 1 3 4 5 ......... each branch left of the path. ∆( )=3(3!)=18 e 3 1 e ∆( )=0(2!)=0 e 3 4 rank ( 625143 ) 4 4 ... ... = ∆ ( e 1 ) + ∆ ( e 2 ) + · · · + ∆ ( e 6 ) e 3 4 5 = 600 + 24 + 18 + 0 + 1 + 0 ... ∆( )=1(1!)=1 e e 5 = 643 6 ∆( )=0(0!)=0 e 3 6 unrank ( 643 ) = 625143 Prof. Tesler Decision Trees; Ranking and Unranking Math 184A / Winter 2017 9 / 33

  10. Computing decision numbers, d i , with example 625143 Number the branches of each vertex 0 , 1 , 2 , . . . from left to right. e 1 1 5 Let d i = branch # at the i th level. 6 ............... 1 5! 5! 5! 5! 5! 3 ( d 1 , . . . , d 6 ) = ( 5 , 1 , 3 , 0 , 1 , 0 ) 4 5 2 e ∆( )=5(5!)=600 e 2 ... ... ... ... 1 Branch numbers d i are not the ∆( )=1(4!)=24 e e 2 3 same as the edge labels a i : 1 3 4 5 ......... ( a 1 , . . . , a 6 ) = ( 6 , 2 , 5 , 1 , 4 , 3 ) . ∆( )=3(3!)=18 e 3 1 e ∆( )=0(2!)=0 e 3 4 4 4 ... ... e 3 5 4 ... ∆( )=1(1!)=1 e e 5 6 ∆( )=0(0!)=0 e 3 6 Prof. Tesler Decision Trees; Ranking and Unranking Math 184A / Winter 2017 10 / 33

  11. Computing decision numbers, d i , with example 625143 Let x = a 1 a 2 . . . a n . e 1 1 Sequentially compute d 1 , . . . , d n . 5 6 ............... 1 For x = 625143 , compute d 3 : 5! 5! 5! 5! 5! 3 4 5 2 e ∆( )=5(5!)=600 e 2 ... ... ... ... 1 a 3 = 5 . ∆( )=1(4!)=24 e e 2 3 The options < 5 are 1 , 2 , 3 , 4 . 1 3 4 5 ......... We already used 6 and 2 . ∆( )=3(3!)=18 e 3 Strike these out, leaving 1 { 1 , 2 , 3 , 4 } \ { 6 , 2 } = { 1 , 3 , 4 } . e ∆( )=0(2!)=0 e 3 4 4 4 ... ... d 3 = | { 1 , 3 , 4 } | = 3 . e 3 4 5 Each branch left of e 3 has ( n − 3 ) ! ... leaves, so ∆( )=1(1!)=1 e e 5 6 ∆ ( e 3 ) = d 3 · ( n − 3 ) ! = 3 · 3 ! = 18 . ∆( )=0(0!)=0 e 3 6 Similarly, d i = | { 1 , 2 , . . . , a i − 1 } \ { a 1 , a 2 , . . . , a i − 1 } | ∆ ( e i ) = d i · ( n − i ) ! Prof. Tesler Decision Trees; Ranking and Unranking Math 184A / Winter 2017 11 / 33

  12. General rule for rank ( x ) for permutations in lex order Let x = a 1 a 2 . . . a n . We’ll use n = 6 and x = 625143 . Let d i = | { 1 , 2 , . . . , a i − 1 } \ { a 1 , a 2 , . . . , a i − 1 } | d 1 = | { 1 , 2 , 3 , 4 , 5 } \ ∅ | = 5 ∆ ( e 1 ) = 5 · 5 ! = 600 a 1 = 6 ∆ ( e 2 ) = 1 · 4 ! = 24 a 2 = 2 d 2 = | { 1 } \ { 6 } | = | { 1 } | = 1 ∆ ( e 3 ) = 3 · 3 ! = 18 a 3 = 5 d 3 = | { 1 , 2 , 3 , 4 } \ { 6 , 2 } | = | { 1 , 3 , 4 } | = 3 d 4 = | ∅ \ { 6 , 2 , 5 } | = | ∅ | = 0 ∆ ( e 4 ) = 0 · 2 ! = a 4 = 1 0 a 5 = 4 d 5 = | { 1 , 2 , 3 } \ { 6 , 2 , 5 , 1 } | = | { 3 } | = 1 ∆ ( e 5 ) = 1 · 1 ! = 1 d 6 = | { 1 , 2 } \ { 6 , 2 , 5 , 1 , 4 } | = | ∅ | = 0 ∆ ( e 6 ) = 0 · 0 ! = a 6 = 3 0 rank ( x ) = 643 Each branch left of the one we chose has ( n − i ) ! leaves (ignoring the root), so ∆ ( e i ) = d i · ( n − i ) !. rank ( x ) = � n i = 1 ∆ ( e i ) = � n i = 1 d i · ( n − i ) ! Prof. Tesler Decision Trees; Ranking and Unranking Math 184A / Winter 2017 12 / 33

  13. Inverse relation between d i and a i . Example: 625143. Inverting d i ’s to compute a i ’s: After computing a 1 = 6 and a 2 = 2 , use d 3 = 3 to compute a 3 . e 1 1 5 6 ............... The branches are a subset of 1 5! 5! 5! 5! 5! 3 [ 6 ] = { 1 , . . . , 6 } . 4 5 2 e ∆( )=5(5!)=600 e 2 ... ... ... ... 1 We already used 6 and 2 , so ∆( )=1(4!)=24 e e 2 3 remove them, leaving 1 3 4 5 ......... [ 6 ] \ { 6 , 2 } = { 1 , 3 , 4 , 5 } . ∆( )=3(3!)=18 e 3 1 Index these 0,1,2,3 e ∆( )=0(2!)=0 e 3 4 4 4 ( 0-based indexing instead of ... ... 1-based indexing 1,2,3,4). e 3 5 4 Since d 3 = 3 , the 3 rd is a 3 = 5 . ... In general: a i is the d i th (starting at ∆( )=1(1!)=1 e e 5 6 0 th , not 1 st ) smallest number in ∆( )=0(0!)=0 e 3 6 [ n ] \ { a 1 , . . . , a i − 1 } . Prof. Tesler Decision Trees; Ranking and Unranking Math 184A / Winter 2017 13 / 33

Recommend


More recommend