Bitwise State Representation - PowerPoint PPT Presentation

1 / 46
About This Presentation
Title:

Bitwise State Representation

Description:

insert start to q, add start to c. while q not empty. pop an ... Othello. 11101011 (where occupied) 10001001 (color) You are 1, where you can play? Solution ... – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 47
Provided by: alan81
Category:

less

Transcript and Presenter's Notes

Title: Bitwise State Representation


1
BitwiseStateRepresentation
  • Alan Tam Siu Lung
  • 99967891 Tam_at_SiuLung.com 96397999
  • 2005-02-19

2
Prerequisite
  • BFS
  • Complex data types in your language
  • Hash Table
  • Binary Search Tree

3
BFS
  • q queue of states
  • c set of states
  • insert start to q, add start to c
  • while q not empty
  • pop an element u from q
  • for all neighbor v of u
  • if v is goal, return v
  • if v not in c, push v to q and add v to c

4
What state operations needed?
  • Copy (possibly eliminated if done properly)
  • Comparison (by ordering or by hashing)
  • Check for goal state
  • Enumerating all neighbors
  • Mappable to 0..M for acceptable M (optional)
  • Time Concern All these operations are O(n) where
    n is size of the state
  • Memory Concern Need to store many states in
    memory

5
Lower Bound
  • Information Entropy (Claude, 1948)
  • H Si pi log pi
  • where pi is the probably of state i happening
  • Worst case occurs when all pi n1
  • H log n
  • where n is the number of probable states
  • So, if we want to represent n states, we need at
    least log n bits.

6
But
  • Lower Bound is better for
  • Copying
  • Comparison
  • Lower Bound may incur higher cost of
  • Checking if it is a goal state
  • Generating all neighbors of a state
  • Encoding and Decoding of a state
  • For input, debug and output
  • So, we would rather a balance in between

7
Missionaries and Cannibals
  • Initial 3 M, 3 C and a boat on the left.
  • Each time 1 or 2 can take the boat.
  • Everywhere there are more C than M (Mgt0), game
    over.
  • Goal 3M and 3C on the right.

8
Will we do it this way?
  • struct state
  • int m2, c2
  • bool b
  • record state
  • m, c array1..2 of 1..3
  • b boolean
  • end

9
Will we do it this way?
  • struct state
  • int m2, c2
  • bool b
  • record state
  • m, c array1..2 of 1..3
  • b boolean
  • end
  • How many bits used?
  • How many bits required?
  • Can we optimize by

10
Calculating Possible States
  • M ? C Boat
  • 3, 2, 1, 02 ? L, R 32
  • 3M3C, 3M2C, 3M1C, 3M, 2M2C, 1M1C, 3C, 2C, 1C,
    ? ? L, R 20
  • Why there are actually 16 only?

11
C/C Union
  • union state
  • bool b 1
  • unsigned int m 2
  • unsigned int c 2
  • C
  • struct state start (state)0x177
  • C
  • int start_int 0x177
  • state start reinterpret_castltstategt(start_int
    )
  • printf(d d d\n, start.b, start.m, start.c)

12
Can some operations be slow?
  • Yes, provided that its slowness doesnt affect
    our overall performance.
  • E.g. decrypting a goal state into the answer
  • If it is slow and the domain is small
  • Hardcode/pre-compute a table for it.
  • If states are not uniquely represented, checking

13
Example
  • char moves258
  • -1, -1, 11, 7, -1, -1, -1, -1,
  • -1, 10, 12, 8, -1, -1, -1, -1,
  • 5, 11, 13, 9, -1, -1, -1, -1,
  • 6, 12, 14, -1, -1, -1, -1, -1,
  • 7, 13, -1, -1, -1, -1, -1, -1,
  • -1, -1, 16, 12, 2, -1, -1, -1,
  • -1, 15, 17, 13, 3, -1, -1, -1,
  • 10, 16, 18, 14, 4, -1, -1, 0,
  • 11, 17, 19, -1, -1, -1, -1, 1,
  • 12, 18, -1, -1, -1, -1, -1, 2,
  • -1, -1, 21, 17, 7, 1, -1, -1,
  • -1, 20, 22, 18, 8, 2, 0, -1,
  • 15, 21, 23, 19, 9, 3, 1, 5,
  • 16, 22, 24, -1, -1, 4, 2, 6,
  • 17, 23, -1, -1, -1, -1, 3, 7,
  • -1, -1, -1, 22, 12, 6, -1, -1,
  • -1, -1, -1, 23, 13, 7, 5, -1,
  • 20, -1, -1, 24, 14, 8, 6, 10,
  • 21, -1, -1, -1, -1, 9, 7, 11,
  • 22, -1, -1, -1, -1, -1, 8, 12,
  • -1, -1, -1, -1, 17, 11, -1, -1,
  • -1, -1, -1, -1, 18, 12, 10, -1,
  • -1, -1, -1, -1, 19, 13, 11, 15,
  • -1, -1, -1, -1, -1, 14, 12, 16,
  • -1, -1, -1, -1, -1, -1, 13, 17

14
8-puzzle
4 5
6 1 8
7 3 2
? ? ?
1 2 3
4 5 6
7 8
15
8-puzzle
  • struct state
  • char num9
  • char space_pos
  • record state
  • num array1..9 of 0..8
  • space_pos 1..9
  • end

4 5
6 1 8
7 3 2
? ? ?
1 2 3
4 5 6
7 8
16
Calculations
  • Possible different states
  • 181440 (18 bit)
  • Space we used
  • 80 bits

17
Lexicographical Ordering
  • Store it verbatim 89 387420489
  • Hash table? How large?
  • Calculate a mapping from the 362880 possible
    permutations to 0..362879

18
Example
  • What are smaller than 530841672?
  • 53084160-6? 1 ? 1
  • 5308410-5?? 1 ? 2
  • 530840-0??? 0 ? 6
  • 53080-3???? 2 ? 24
  • 5300-7????? 5 ? 120
  • 53?????? 0 ? 720
  • 50-2??????? 3 ? 5040
  • 0-4???????? 5 ? 40320

19
Computer Organization
  • x86 stores whole numbers in
  • Binary Form
  • x86 stores negative integers in
  • 2s complement
  • x86 stores real numbers in
  • IEEE 754
  • x86 stores alphabets in
  • ASCII

20
Modern Computer Architecture
  • Simple Instruction Multiple Data (SIMD)
  • Segmentation/Paging
  • Caching
  • Pipelining (Instruction Prefetch)
  • Branch Prediction

21
Integer Representation
  • 46709394 (10)
  • 00000010 11001000 10111010 10010010 (2)
  • 02 C8 BA 92 (16) (0x02c8ba92 in C/C)
  • Little Endian

10010010 10111010 11001000 00000010
92 BA C8 02
22
State Representation
  • M C 4 B 16
  • M3, C3, B0
  • 00001111 (15)
  • M0, C0, B1
  • 00010000 (16)
  • M2, C2, B1
  • 00011010 (26)

23
If you discovered
  • M3, C3, B0
  • 00001111 (15)
  • M0, C0, B1
  • 00010000 (16)
  • M2, C2, B1
  • 00011010 (26)
  • This is Bitwise Representation

24
Why bitwise?
  • It is the native language of the computer
  • It works extremely fast
  • Integer Multiplication 4 cycles latency
  • Moving Bits 1 cycle latency
  • Packing and unpacking is easier to code and thus
    less error prone
  • Bit Parallelism

25
Bitwise Operations
  • Binary
  • ltlt shl
  • gtgt shr
  • and
  • or
  • xor
  • Unary
  • not
  • shift bits left
  • shift bits right
  • intersection
  • union
  • mutual exclusion
  • complement

26
Examples
4 ltlt 2 16
14 gtgt 2
26 14
24 9
48 97
56 (8-bit unsigned)
-14 gtgt 2 (8-bit)
27
Examples
4 ltlt 2 16
14 gtgt 2 3
26 14 10
24 9 25
48 97 81
56 (8-bit unsigned) 199
-14 gtgt 2 (8-bit) -4
28
Bitmap
  • Shift operators and bitwise operators can be used
    to manage a sequence of bits of ? 64 elements
  • Operations
  • Get(p 0..Size-1) Boolean
  • Set(p 0..Size-1)
  • Unset(p 0..Size-1)
  • Toggle(p 0..Size-1)

29
Using a 32-bit Integer
  • 00000010 11001000 10111010 10010010
  • 1 ltlt p a number with only bit p on
  • value (1 ltlt p) ! 0 ? Get(p)
  • value value (1 ltlt p) ? Set(p)
  • value value (1 ltlt p) ? Unset(p)
  • value value (1 ltlt p) ? Toggle(p)

Bit 31
Bit 0
30
Using Bitmap
  • Space-efficient
  • Runtime may be faster
  • Cache hit
  • Less copying
  • Or maybe slower
  • More calculations

31
Massive Calculations
  • Example Count number of bits on a 16-bit integer
    v
  • v (v 0x5555) ((v gtgt 1) 0x5555)
  • v (v 0x3333) ((v gtgt 2) 0x3333)
  • v (v 0x0f0f) ((v gtgt 4) 0x0f0f)
  • v (v 0x00ff) ((v gtgt 8) 0x00ff)

32
Counting bits
  • 1 0 0 1 1 1 1 1 0 0 0 1 0 0 1 0
  • 01 01 10 10 00 01 00 01
  • 0010 0100 0001 0001
  • 00000110 00000010
  • 0000000000001000
  • Exercise Do it for 32-bit

33
Massive Calculations
  • Given a bit pattern, find a bit pattern which
  • only the leftmost bit of a bit group is on
  • E.g.
  • 1110101101111001 becomes1000101001000001

34
Answer
  • 1110101101111001 becomes1000101001000001
  • The leftmost of a bit group means
  • It is 1
  • Its left is 0
  • So
  • value value (value gtgt 1)

35
Bit Movements
  • Question
  • 1 1 0 1 0 1 0 0 becomes0101000100010000
  • Solution
  • value ((value 0x00f0) ltlt 4)) (value
    0x000f)
  • value ((value 0x0c0c) ltlt 2)) (value
    0x0303)
  • value ((value 0x2222) ltlt 1)) (value
    0x1111)
  • Exercise
  • 1 1 0 1 0 1 0 0 becomes1111001100110000
  • 0 1 0 1 becomes0000111100001111

36
Direct Addressing
0000 0000000000000000 1000 1111000000000000
0001 0000000000001111 1001 1111000000001111
0010 0000000011110000 1010 1111000011110000
0011 0000000011111111 1011 1111000011111111
0100 0000111100000000 1100 1111111100000000
0101 0000111100001111 1101 1111111100001111
0110 0000111111110000 1110 1111111111110000
0111 0000111111111111 1111 1111111111111111
37
Complex Bit Patterns
  • Othello
  • 11101011 (where occupied)
  • 10001001 (color)
  • You are 1, where you can play?
  • Solution
  • 11101011 10001001 01100010
  • 11101011 01100010 01001101
  • 01001101 11101011 00000100
  • How about the reverse direction?

38
Other Uses
  • Store multiple values, each one assigned a
    bit-range
  • (value 32) (value 31)
  • Storing sets of ? 64 elements
  • X union Y
  • X minus Y
  • Is X subset of Y?

39
Multiple Comparisons
  • switch (i)
  • case 1
  • case 3
  • case 4
  • case 7
  • case 8
  • case 10
  • f()
  • break
  • default
  • g()
  • if ((1ltlti) 0x039a)
  • f()
  • else
  • g()

40
Edit Distance
Y\X a b a b a a c
0 1 2 3 4 5 6 7
a 1 0 1 2 3 4 5 6
b 2 1 0 1 2 3 4 5
b 3 2 1 1 1 2 3 4
a 4 3 2 1 2 1 2 3
a 5 4 3 2 2 2 1 2
41
Difference Encoding
HDV a b a b a a c
0 1 2 3 4 5 6 7
a 1 -0- 0- 0- 0- 0- 0- 0-
b 2 -0 -0- 0- 0- 0- 0- 0-
b 3 -0 -0 00 00 0- 0- 0-
a 4 -0 -0 -00 00 -0- 0- 0-
a 5 -0 -0 -0 00 00 -0- 0-
42
Bit Arrays
  • VP Vertical Positive
  • VPi1,j 1 iff Di1,j Di,j 1
  • VN Vertical Negative
  • VPi1,j 1 iff Di1,j Di,j 1
  • HP Horizontal Positive
  • VPi,j1 1 iff Di,j1 Di,j 1
  • HN Horizontal Negative
  • VPi,j1 1 iff Di,j1 Di,j 1
  • Diagonal Zero
  • Di1,j1 1 iff Di1,j1 Di,j

43
Simple if chars are the same
HDV a b a b a a c
0 1 2 3 4 5 6 7
a 1 -0- 0- 0- 0-
b 2 -0 -0- 0-
b 3 -0 -0
a 4 -0 00 -0-
a 5 00 ?0?
44
Calculations
H V I
- - 0
- 0 00
- -0
0 - 00
0 0 N/A
0 0
- 0-
0 0
00
  • if chars equal
  • VP HN
  • VN VP
  • HP VN
  • HN VP
  • D 0
  • if chars not equal
  • VP VN HP VP
  • VN VP HN
  • HP HN VP HP
  • HN HP VN
  • D HP VP

45
Checking if chars are equal
  • For each alphabet
  • Generate a bitmap
  • Calculate the 5 arrays separately
  • Combine the 5 arrays based on the bitmap
  • Technicality
  • Note that D is unnecessary except for
    backtracking
  • Either V or H needs shifting

46
Performance
  • Using the RAM model
  • Old Algorithm
  • Runtime O(n2)
  • Memory O(n log n) bits
  • New Algorithm
  • Runtime O(S n2 / log n)
  • Memory O(n) bits
Write a Comment
User Comments (0)
About PowerShow.com