SLIDE 1 Fun With Number Systems
Or: How Knowing Number Systems Can Help You Interview Better
SLIDE 2
Finding the Odd Ball
SLIDE 3
SLIDE 6 Goal: Find the odd ball and whether it's heavier or lighter in three weighings.
SLIDE 7 The Solution
5 6 7 11 8 9 10 12 2 3 4 11 5 6 7 12 1 4 7 8 2 5 10 11
SLIDE 8 Balanced Ternary
- Number system for encoding three-way
comparisons.
- Each digit corresponds to a power of three.
- Digits are -1, 0, +1.
- For notational simplicity, will use -, 0, +.
- Example: +0-0
- 1 x 33 + 0 x 32 – 1 x 31 + 0 x 30 = 24
- Example: --++
- -1 x 33 – 1 x 32 + 1 x 31 + 1 x 30 = -32
SLIDE 9
- 12 to +12 in Balanced Ternary
1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12
00- 0-+ 0-0 0--
- ++
- +0
- +-
- 0+
- 00
- 0-
- -+
- -0
- 12
000
SLIDE 10 1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12
SLIDE 11 1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12
SLIDE 12 1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12
SLIDE 13 1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12
1 4 7 10
SLIDE 14 1 2 3 4 5 6 7 8 9 10 11 12
1 4 7 10
00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0
SLIDE 15 1 2 3 4 5 6 7 8 9 10 11 12
1 4 7 10
00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0
2 5 8 11
SLIDE 16 1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12
1 4 7 10 2 5 8 11
SLIDE 17 1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12
1 4 7 10 2 5 8 11
SLIDE 18 1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12
1 4 7 10 2 5 8 11
??+
SLIDE 19 1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12
1 4 7 10 2 5 8 11
SLIDE 20 1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12
1 4 7 10 2 5 8 11
??-
SLIDE 21 1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12
1 4 7 10 2 5 8 11
SLIDE 22 1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12
1 4 7 10 2 5 8 11
??0
SLIDE 23 1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12
1 4 7 10 2 5 8 11
00- 0-+ 0-0 0--
- ++
- +0
- +-
- 0+
- 00
- 0-
- -+
- -0
- 12
SLIDE 24 1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12
1 4 7 10 2 5 8 11
00- 0-+ 0-0 0--
- ++
- +0
- +-
- 0+
- 00
- 0-
- -+
- -0
- 12
SLIDE 25 1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12
1 4 7 10 2 5 8 11
00- 0-+ 0-0 0--
- ++
- +0
- +-
- 0+
- 00
- 0-
- -+
- -0
- 12
??-
SLIDE 26 1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12
1 4 7 10 2 5 8 11
00- 0-+ 0-0 0--
- ++
- +0
- +-
- 0+
- 00
- 0-
- -+
- -0
- 12
SLIDE 27 1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12
1 4 7 10 2 5 8 11
00- 0-+ 0-0 0--
- ++
- +0
- +-
- 0+
- 00
- 0-
- -+
- -0
- 12
??+
SLIDE 28 1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12
1 4 7 10 2 5 8 11
00- 0-+ 0-0 0--
- ++
- +0
- +-
- 0+
- 00
- 0-
- -+
- -0
- 12
SLIDE 29 1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12
1 4 7 10 2 5 8 11
00- 0-+ 0-0 0--
- ++
- +0
- +-
- 0+
- 00
- 0-
- -+
- -0
- 12
??0
SLIDE 30 If the left side is heavier, record a +. If the right side is heavier, record a -. If the scale balances, record a 0.
SLIDE 31 1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12
SLIDE 32 1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12
2 3 4 11 12
SLIDE 33 1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12
2 3 4 11 12
SLIDE 34 1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12
2 3 4 11 5 6 7 12
SLIDE 35 1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +0- +00 +0+ ++- ++0 12
2 3 4 11 5 6 7 12
+-- +-0 +-+
SLIDE 36 1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +0- +00 +0+ ++- ++0 12
2 3 4 11 5 6 7 12
+-- +-0 +-+
SLIDE 37 Our Encoding Scheme
1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12
00- 0-+ 0-0 0--
- ++
- +0
- +-
- 0+
- 00
- 0-
- -+
- -0
- 12
Lighter Heavier
SLIDE 38 Our Encoding Scheme
1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12
00- 0-+ 0-0 0--
- ++
- +0
- +-
- 0+
- 00
- 0-
- -+
- -0
- 12
Lighter Heavier
SLIDE 39 Our Encoding Scheme
1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12
00- 0-+ 0-0 0--
Lighter Heavier
SLIDE 40 Our Encoding Scheme
1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12
00- 0-+ 0-0 0--
Lighter Heavier
SLIDE 41 Our Encoding Scheme
1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12
00- 0-+ 0-0 0--
Lighter Heavier
SLIDE 42 Our Encoding Scheme
1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12
00- 0-+ 0-0 0--
Lighter Heavier
SLIDE 43 Our Encoding Scheme
1 2 3 4 5 6 7 11 00+ 0+- 0+0 0++ +-- +-0 +-+ ++- ++0 12
00- 0-+ 0-0 0--
Lighter Heavier
8 9 10 +0- +00 +0+
SLIDE 44 The Solution
5 6 7 11 8 9 10 12 2 3 4 11 5 6 7 12 1 4 7 8 2 5 10 11
1 2 3 4 5 6 7 11 00+ 0+- 0+0 0++ +-- +-0 +-+ ++-
SLIDE 45 Generalizing the Result
- Why twelve balls?
- With three trits, 27 possible combinations. 13 are positive, nine
start with +.
- Must discard one starting with + to ensure number of + and - in
each column is the same, leaving 12 positive numbers.
- More generally:
- With n trits, 3n possible combinations. (3n – 1) / 2 are positive.
- 3n-1 numbers start with +. To balance + and -, we need to drop
- ne starting with +, leaving (3n – 3) / 2 positive numbers.
- Can do 3, 12, 39, 120, 363, 1092, ...
SLIDE 46 1 2 3 0+ +- +0 4 ++
Example: Two Weighings
0-
Lighter Heavier
SLIDE 47 1 2 3 0+ +- +0 4 ++
Example: Two Weighings
0-
Lighter Heavier
SLIDE 48 1 2 3 0+ +- +0
Example: Two Weighings
0-
Lighter Heavier
SLIDE 49 1 2 3 0+ +- +0
Example: Two Weighings
0-
Lighter Heavier
SLIDE 50 1 2 3 0+ +- +0
Example: Two Weighings
0-
Lighter Heavier
SLIDE 51 1 2 3 0+ +- +0
Example: Two Weighings
0-
Lighter Heavier
SLIDE 52 Example: Three Weighings
1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12
00- 0-+ 0-0 0--
- ++
- +0
- +-
- 0+
- 00
- 0-
- -+
- -0
- 12
Lighter Heavier +++ 13
SLIDE 53 Example: Three Weighings
1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12
00- 0-+ 0-0 0--
- ++
- +0
- +-
- 0+
- 00
- 0-
- -+
- -0
- 12
Lighter Heavier +++ 13
SLIDE 54 Example: Three Weighings
1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12
00- 0-+ 0-0 0--
- ++
- +0
- +-
- 0+
- 00
- 0-
- -+
- -0
- 12
Lighter Heavier
SLIDE 55 Example: Three Weighings
1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++-
- -0
- 12
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
00- 0-+ 0-0 0--
Lighter Heavier ++0 12
SLIDE 56 Example: Three Weighings
1 2 3 4 5 6 7 8 9 10 11 00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0 12
00- 0-+ 0-0 0--
Lighter Heavier
SLIDE 57 Example: Three Weighings
1 2 3 4 5 6 7 11 00+ 0+- 0+0 0++ +-- +-0 +-+ ++- ++0 12
00- 0-+ 0-0 0--
Lighter Heavier
8 9 10 +0- +00 +0+
SLIDE 58 Example: Four Weighings
1 2 3 4 5 6 7 8 9 10 11 000+ 00+- 00+0 00++ 0+-- 0+-0 0+-+ 0+0- 0+00 0+0+ 0++- 0++0 12 0+++ 13 14 15 16 17 18 19 20 21 22 23 24 +--- +--0 +--+ +-0- +-00 +-0+ +-+- +-+0 +-++ +0-- +0-0 25 +0-+ 26 27 28 29 30 31 32 33 34 35 36 37 +000 +00+ +0+- +0+0 +0++ ++-- ++-0 ++-+ ++0- ++00 ++0+ +++- 38 +++0 39 +00- ++++ 40
SLIDE 59 Example: Four Weighings
1 2 3 4 5 6 7 8 9 10 11 000+ 00+- 00+0 00++ 0+-- 0+-0 0+-+ 0+0- 0+00 0+0+ 0++- 0++0 12 0+++ 13 14 15 16 17 18 19 20 21 22 23 24 +--- +--0 +--+ +-0- +-00 +-0+ +-+- +-+0 +-++ +0-- +0-0 25 +0-+ 26 27 28 29 30 31 32 33 34 35 36 37 +000 +00+ +0+- +0+0 +0++ ++-- ++-0 ++-+ ++0- ++00 ++0+ +++- 38 +++0 39 +00- ++++ 40
SLIDE 60 Example: Four Weighings
1 2 3 4 5 6 7 8 9 10 11 000+ 00+- 00+0 00++ 0+-- 0+-0 0+-+ 0+0- 0+00 0+0+ 0++- 0++0 12 0+++ 13 14 15 16 17 18 19 20 21 22 23 24 +--- +--0 +--+ +-0- +-00 +-0+ +-+- +-+0 +-++ +0-- +0-0 25 +0-+ 26 27 28 29 30 31 32 33 34 35 36 37 +000 +00+ +0+- +0+0 +0++ ++-- ++-0 ++-+ ++0- ++00 ++0+ +++- 38 +++0 39 +00-
SLIDE 61 Example: Four Weighings
1 2 3 4 5 6 7 8 9 10 11 000+ 00+- 00+0 00++ 0+-- 0+-0 0+-+ 0+0- 0+00 0+0+ 0++- 0++0 12 0+++ 13 14 15 16 17 18 19 20 21 22 23 24 +--- +--0 +--+ +-0- +-00 +-0+ +-+- +-+0 +-++ +0-- +0-0 25 +0-+ 26 27 28 29 30 31 32 33 34 35 36 37 +000 +00+ +0+- +0+0 +0++ ++-- ++-0 ++-+ ++0- ++00 ++0+ +++- 38 +++0 39 +00-
SLIDE 62 Example: Four Weighings
1 2 3 4 5 6 7 8 9 10 11 000+ 00+- 00+0 00++ 0+-- 0+-0 0+-+ 0+0- 0+00 0+0+ 0++- 0++0 12 0+++ 13 14 15 16 17 18 19 20 21 22
+--- +--0 +--+ +-0- +-00 +-0+ +-+- +-+0 +-++
- 0++
- 0+0
- 25
- 0+-
- 26
- 27
- 28
- 29
- 30
- 31
32 33 34
- 35
- 36
- 37
- 000
- 00-
- 0-+
- 0-0
- 0--
++-- ++-0 ++-+
+++- 38
SLIDE 63 Example: Four Weighings
1 2 3 4 5 6 7 8 9 10 11 000+ 00+- 00+0 00++ 0+-- 0+-0 0+-+ 0+0- 0+00 0+0+ 0++- 0++0 12 0+++ 13 14 15 16 17 18 19 20 21 22
+--- +--0 +--+ +-0- +-00 +-0+ +-+- +-+0 +-++
- 0++
- 0+0
- 25
- 0+-
- 26
- 27
- 28
- 29
- 30
- 31
32 33 34
- 35
- 36
- 37
- 000
- 00-
- 0-+
- 0-0
- 0--
++-- ++-0 ++-+
+++- 38
SLIDE 64 Some Insights
- What number did we drop?
- With 2 trits, dropped ++.
- With 3 trits, dropped +++.
- With 4 trits, dropped ++++.
- Always drop ++…++
- What numbers did we invert?
- With two trits: +0
- With three trits: ++0, +0-, +00, +0-
- With four trits: +++0, ++0-, ++00, ++0+, +0--, +0-0, +0-+, +00-,
+000, +00+, +0+-, +0+0, +0++
- Always invert numbers starting with ++…++0.
- This always works!
SLIDE 65 How Many Extra +s Per Column?
- Answer: The 3j column has (3j – 1) / 2 extra +'s.
0+ +- +0
SLIDE 66 How Many Extra +s Per Column?
- Answer: The 3j column has (3j – 1) / 2 extra +'s.
00+ 0+- 0+0
SLIDE 67 How Many Extra +s Per Column?
- Answer: The 3j column has (3j – 1) / 2 extra +'s.
00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0
SLIDE 68 How Many Extra +s Per Column?
- Answer: The 3j column has (3j – 1) / 2 extra +'s.
00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0
SLIDE 69 How Many +'s Get Flipped?
- Answer: The 3j column has (3j-1) / 2 flipped +'s.
SLIDE 70 How Many +'s Get Flipped?
- Answer: The 3j column has (3j-1) / 2 flipped +'s.
0+ +- +0
SLIDE 71 How Many +'s Get Flipped?
- Answer: The 3j column has (3j-1) / 2 flipped +'s.
0+ +- +0
SLIDE 72 How Many +'s Get Flipped?
- Answer: The 3j column has (3j-1) / 2 flipped +'s.
00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0
SLIDE 73 How Many +'s Get Flipped?
- Answer: The 3j column has (3j-1) / 2 flipped +'s.
00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0
SLIDE 74 How Many +'s Get Flipped?
- Answer: The 3j column has (3j-1) / 2 flipped +'s.
00+ 0+- 0+0 0++ +-- +-0 +-+ +0- +00 +0+ ++- ++0
∑
i=0 j−1
3
i=3 j−1
2
SLIDE 75 Summary
- A three-way scale lends itself naturally to a
balanced ternary encoding for each of the balls.
- Given an encoding with the same number of +'s
and -'s in each column, we can use the scale to read off one trit of the answer at a time.
- Flipping numbers starting with +0, ++0, etc.
guarantees an encoding with this property.
SLIDE 76 Generating Permutations
SLIDE 77 “You are given a sorted string S of unique
- characters. Write a Java-style iterator that
traverses all the permutations of S in lexicographical order.”
SLIDE 78
Example
SLIDE 80 Example
abc acb bac bca cab cba
SLIDE 81 Example
abc acb bac bca cab cba 1 2 3 4 5
SLIDE 82
Lehmer Codes
B A E D C
SLIDE 83
Lehmer Codes
B A E D C
SLIDE 84
Lehmer Codes
B A E D C
SLIDE 85
1
Lehmer Codes
B A E D C
SLIDE 86
1
Lehmer Codes
B A E D C
SLIDE 87
1
Lehmer Codes
B A E D C
SLIDE 88
1
Lehmer Codes
B A E D C
SLIDE 89
1
Lehmer Codes
B A E D C
SLIDE 90
1
Lehmer Codes
B A E D C
SLIDE 91
1
Lehmer Codes
B A E D C
SLIDE 92
2 1
Lehmer Codes
B A E D C
SLIDE 93
2 1
Lehmer Codes
B A E D C
SLIDE 94
2 1
Lehmer Codes
B A E D C
SLIDE 95
2 1
Lehmer Codes
B A E D C
SLIDE 96
1 2 1
Lehmer Codes
B A E D C
SLIDE 97
1 2 1
Lehmer Codes
B A E D C
SLIDE 98
1 2 1
Lehmer Codes
B A E D C
SLIDE 99
1 2 1
Lehmer Codes
B A E D C
SLIDE 100
1 2 1
Lehmer Codes
B A E D C
SLIDE 101
1 2 1
Lehmer Codes
B A E D C C D A B E
SLIDE 102
1 2 1
Lehmer Codes
B A E D C 2 C D A B E
SLIDE 103
2 1 2 1
Lehmer Codes
B A E D C 2 C D A B E
SLIDE 104
2 1 2 1
Lehmer Codes
B A E D C 2 C D A B E
SLIDE 105
2 1 2 1
Lehmer Codes
B A E D C 2 C D A B E
SLIDE 106
2 1 2 1
Lehmer Codes
B A E D C 2 C D A B E
SLIDE 107
4 1 1
Lehmer Codes
A B C D E
SLIDE 108
4 1 1
Lehmer Codes
A B C D E
SLIDE 109
4 1 1
Lehmer Codes
A B C D E
SLIDE 110
4 1 1
Lehmer Codes
A B C D E
SLIDE 111
4 1 1
Lehmer Codes
A B C D E
SLIDE 112
4 1 1
Lehmer Codes
A B C D E
SLIDE 113
4 1 1
Lehmer Codes
A B C D E
SLIDE 114
4 1 1
Lehmer Codes
A B C D E
SLIDE 115
4 1 1
Lehmer Codes
A B C D E
SLIDE 116
4 1 1
Lehmer Codes
A B C D E
SLIDE 117
4 1 1
Lehmer Codes
A B C D E
SLIDE 118
4 1 1
Lehmer Codes
A B C D E
SLIDE 119
4 1 1
Lehmer Codes
A B C D E
SLIDE 120
4 1 1
Lehmer Codes
A B C D E
SLIDE 121
4 1 1
Lehmer Codes
A B C D E
SLIDE 122
4 1 1
Lehmer Codes
A B C D E
SLIDE 123
4 1 1
Lehmer Codes
A B C D E
SLIDE 124 Listing Lehmer Codes
1 2 3 4 5 6 7 8 9 10 11 2000 2010 2100 2110 2200 2210 3000 3010 3100 3110 3200 3210 abcd abdc acbd acdb adbc adcb bacd badc bcad bcda bdac bdac cabd cadb cbad cbda cdab cdba dabc dacb dbac dbca dcab dcba 12 13 14 15 16 17 18 19 20 21 22 23 0000 0010 0100 0110 0200 0210 1000 1010 1100 1110 1200 1210
SLIDE 125 Factoradic Numbers
- Mixed-radix number system.
- Nth digit in base n!.
- Nth digit can be 0, 1, 2, …, n
- Example: 3110!
- 3 x 3! + 1 x 2! + 1 x 1! + 0 x 0! = 21
- Example: 1210!
- 1 x 3! + 2 x 2! + 1 x 1! + 0 x 0! = 11
SLIDE 126 Listing Lehmer Codes
1 2 3 4 5 6 7 8 9 10 11 2000 2010 2100 2110 2200 2210 3000 3010 3100 3110 3200 3210 abcd abdc acbd acdb adbc adcb bacd badc bcad bcda bdac bdac cabd cadb cbad cbda cdab cdba dabc dacb dbac dbca dcab dcba 12 13 14 15 16 17 18 19 20 21 22 23 0000 0010 0100 0110 0200 0210 1000 1010 1100 1110 1200 1210
SLIDE 127 Listing Lehmer Codes
1 2 3 4 5 6 7 8 9 10 11 2000 2010 2100 2110 2200 2210 3000 3010 3100 3110 3200 3210 abcd abdc acbd acdb adbc adcb bacd badc bcad bcda bdac bdac cabd cadb cbad cbda cdab cdba dabc dacb dbac dbca dcab dcba 12 13 14 15 16 17 18 19 20 21 22 23 0000 0010 0100 0110 0200 0210 1000 1010 1100 1110 1200 1210
SLIDE 128 Writing n in factoradic gives the nth Lehmer code.
SLIDE 129 Converting to Factoradic
- Goal: convert k to factoradic.
- Assume we know n, the number of elements to
permute.
- To get the (n – 1)! place, divide k by (n – 1)!
- Quotient is the (n – 1)! place.
- Repeat for the remaining digits using the
remainder.
- Identical to converting to any other base, just
using factorials instead of powers.
SLIDE 130
Example: Convert 13 to Factoradic
SLIDE 131
Example: Convert 13 to Factoradic
13 = 2 x 3! + 1
SLIDE 132
Example: Convert 13 to Factoradic
13 = 2 x 3! + 1 1 = 0 x 2! + 1
SLIDE 133
Example: Convert 13 to Factoradic
13 = 2 x 3! + 1 1 = 0 x 2! + 1 1 = 1 x 1! + 0
SLIDE 134
Example: Convert 13 to Factoradic
13 = 2 x 3! + 1 1 = 0 x 2! + 1 1 = 1 x 1! + 0 0 = 0 x 0! + 0
SLIDE 135
Example: Convert 13 to Factoradic
13 = 2 x 3! + 1 1 = 0 x 2! + 1 1 = 1 x 1! + 0 0 = 0 x 0! + 0
SLIDE 136
Example: Convert 13 to Factoradic
13 = 2 x 3! + 1 1 = 0 x 2! + 1 1 = 1 x 1! + 0 0 = 0 x 0! + 0 Answer: 2010!
SLIDE 137 Generating Permutations
public static String kthPermutation(String chars, int k) { String result = ""; for (int n = chars.length() - 1; n >= 0; --n) { int quotient = k / factorial(n); int remainder = k % factorial(n); result += chars.charAt(quotient); chars = chars.substring(0, quotient) + chars.substring(quotient + 1); k = remainder; } return result; }
SLIDE 138 Analysis of Our Algorithm
- To get the kth permutation of n elements:
- Converting k to factoradic takes O(n)
- Building up the permutations takes O(n2 )
– Elements stored in a list; O(n) to remove each.
- Can reduce to O(n lg n) using order statistic tree.
- Easy to build an iterator from this.
SLIDE 139
Incrementing Binary Numbers
4
SLIDE 140
Incrementing Binary Numbers
4 1
SLIDE 141
Incrementing Binary Numbers
4 1
SLIDE 142
Incrementing Binary Numbers
4 1
SLIDE 143
Incrementing Binary Numbers
4 1 1
SLIDE 144
Incrementing Binary Numbers
4 1 1
SLIDE 145
Incrementing Binary Numbers
4 1
SLIDE 146
Incrementing Factoradic Numbers
SLIDE 147
Incrementing Factoradic Numbers
4
SLIDE 148
Incrementing Factoradic Numbers
4
SLIDE 149
Incrementing Factoradic Numbers
4 1
SLIDE 150
Incrementing Factoradic Numbers
4 1
SLIDE 151
Incrementing Factoradic Numbers
4 1
SLIDE 152
Incrementing Factoradic Numbers
4 1
SLIDE 153
Incrementing Factoradic Numbers
4 1 1
SLIDE 154
Incrementing Factoradic Numbers
4 1 1
SLIDE 155
Incrementing Factoradic Numbers
4 2
SLIDE 156
Incrementing Factoradic Numbers
4 2
SLIDE 157
Incrementing Factoradic Numbers
4 2 1
SLIDE 158
Incrementing Factoradic Numbers
4 2 1
SLIDE 159
Incrementing Factoradic Numbers
4 1
SLIDE 160 Incrementing Factoradic Numbers
- Find the digit to increment.
- Scan backwards from the end to find the first
number not at its maximum.
- Increment that digit.
- Set the digits after that to zero.
SLIDE 161
Incrementing Permutations
SLIDE 162
4 B C D A
Incrementing Permutations
4
SLIDE 163
4 B C D A
Incrementing Permutations
4
SLIDE 164
4 B C D A
Incrementing Permutations
4 1
SLIDE 165
4 B D C A
Incrementing Permutations
4 1
SLIDE 166
4 B D C A
Incrementing Permutations
4 1
SLIDE 167
4 B D C A
Incrementing Permutations
4 1
SLIDE 168
4 B D C A
Incrementing Permutations
4 1 1
SLIDE 169
4 C D B A
Incrementing Permutations
4 1 1
SLIDE 170
4 C D B A
Incrementing Permutations
4 1
SLIDE 171
4 C B D A
Incrementing Permutations
4 1
SLIDE 172
4 C B D A
Incrementing Permutations
4 1
SLIDE 173
4 C B D A
Incrementing Permutations
4 1
SLIDE 174
4 C B D A
Incrementing Permutations
4 1 1
SLIDE 175
4 C D B A
Incrementing Permutations
4 1 1
SLIDE 176
4 C D B A
Incrementing Permutations
4 1 1
SLIDE 177
4 C D B A
Incrementing Permutations
4 1 1
SLIDE 178
4 C D B A
Incrementing Permutations
4 2 1
SLIDE 179
4 D C B A
Incrementing Permutations
4 2 1
SLIDE 180
4 D C B A
Incrementing Permutations
4 2
SLIDE 181
4 D B C A
Incrementing Permutations
4 2
SLIDE 182
4 D B C A
Incrementing Permutations
4 2
SLIDE 183
4 D B C A
Incrementing Permutations
4 2
SLIDE 184
4 D B C A
Incrementing Permutations
4 2 1
SLIDE 185
4 D C B A
Incrementing Permutations
4 2 1
SLIDE 186
4 D C B A
Incrementing Permutations
4 2 1
SLIDE 187
4 D C B A
Incrementing Permutations
4 2 1
SLIDE 188
4 D C B A
Incrementing Permutations
4 2 1 1
SLIDE 189
4 D C A B
Incrementing Permutations
4 2 1 1
SLIDE 190
4 D C A B
Incrementing Permutations
4 1
SLIDE 191
4 A C D B
Incrementing Permutations
4 1
SLIDE 192
4 A C D B
Incrementing Permutations
4 1
SLIDE 193 Incrementing Permutations
- Find the digit to be incremented.
- Scan backwards from the end of the sequence to find
the longest increasing sequence.
- Increment that digit.
- Find the smallest element bigger than the element right
before that sequence.
- Swap those two elements.
- Set the digits after that to zero.
- Reverse the increasing sequence
- Runtime: O(n) per permutation.
SLIDE 194 Summary
- Lehmer codes describe a permutation as a
series of elements to choose in order.
- The factoradic number system maps directly
- nto Lehmer codes, and thus onto
permutations.
- By simulating what would happen if we wrote
- ut the Lehmer code, we can derive a fast
algorithm for generating ordered permutations.
SLIDE 195 Concluding Thoughts
SLIDE 196 Number systems are useful in number recovery by discovering one component of the number at a time.
SLIDE 197 Number systems are useful in enumeration by revealing structure hidden in the indices.
SLIDE 198
Fun With Number Systems
SLIDE 199
htiek@cs.stanford.edu
My Email Address