Docsity

Introduction to Computer science - Programming Abstractions, Lecture 25 - Lexicon case study, Lecture notes of Programming Abstractions

Toggle sidebar
bg1
1
Lexicon case study
•Special case of Set/Map
!Keys always strings, in fact, English words
!No associated value
•Core operations
!add
!containsWord
!containsPrefix
2
private:
Vector<string> words; abacus …
abate zygote
containsWord?
!Binary search using ==, O(lgN) time
containsPrefix?
!Binary search using substr ==, O(lgN)
add?
!Binary search to find position, shuffle to insert, O(N)
space usage?
! Per-entry = sizeof(string) " length*sizeof(char)
! " 8 bytes per word (assuming words average len 8 chars)
Lexicon as sorted vector
3
private:
struct node {
string word;
node *left, *right;
};
node *root;
containsWord?
!Tree search using ==, O(lgN) time
containsPrefix?
!Tree search using substr ==, O(lgN)
add?
!Tree search to find position, insert new leaf, O(lgN)
space usage?
! Per-entry = string + left/right ptrs + balance factor
! " 18 bytes per word
media
fjord surf
Lexicon as binary search tree
4
private:
struct cell{
string word;
cell *next;
};
cell *buckets[NBuckets];
containsWord?
!Hash to bucket, search bucket, O(N/B)
containsPrefix?
!Search all buckets, O(N)
add?
!Hash to bucket, search/add to bucket, O(N/B)
space usage?
! Per-entry = string + 4-byte pointer + 4-byte bucket ptr
! " 16 bytes per word
wacky
mediateflan
azure
Lexicon as hash table
pf3
pf4

Partial preview of the text

Download Introduction to Computer science - Programming Abstractions, Lecture 25 - Lexicon case study and more Lecture notes Programming Abstractions in PDF only on Docsity!

1

Lexicon case study

• Special case of Set/Map

! Keys always strings, in fact, English words

! No associated value

• Core operations

! add

! containsWord

! containsPrefix

2 private: Vector words; abacus^ abate^ … zygote containsWord? !Binary search using ==, O(lgN) time containsPrefix? !Binary search using substr ==, O(lgN) add? !Binary search to find position, shuffle to insert, O(N) space usage? ! Per-entry = sizeof(string) " length*sizeof(char) ! " 8 bytes per word (assuming words average len 8 chars)

Lexicon as sorted vector

3 **private: struct node { string word; node *left, right; }; node root; containsWord? !Tree search using ==, O(lgN) time containsPrefix? !Tree search using substr ==, O(lgN) add? !Tree search to find position, insert new leaf, O(lgN) space usage? ! Per-entry = string + left/right ptrs + balance factor ! " 18 bytes per word media fjord surf

Lexicon as binary search tree

4 **private: struct cell{ string word; cell next; }; cell buckets[NBuckets]; containsWord? !Hash to bucket, search bucket, O(N/B) containsPrefix? !Search all buckets, O(N) add? !Hash to bucket, search/add to bucket, O(N/B) space usage? ! Per-entry = string + 4-byte pointer + 4-byte bucket ptr ! " 16 bytes per word wacky flan mediate azure

Lexicon as hash table

5 bytes per word 8 18 16 add O(N) O(lgN) O(1) containsPrefix O(lgN) O(lgN) O(N) containsWord O(lgN) O(lgN) O(1) Sorted vector BST Hashtable

Summary so far

• OSPD2.txt file has 125,000 words

! Average length 8 characters, file is 1.1 MB

• At 8-18 bytes per word, total is 1MB to 2.3 MB

• Boggle lexicon actually only takes 350K ( .3 MB)….

! How??? 6

Notice any patterns?

straddle straddled straddler straddlers straddles straddling strafe strafed strafer strafers strafes strafing straggle straggled straggler stragglers straggles stragglier straggliest straggling straggly straight straightaway straightaways straighted straightedge straightedges straighten straightened straightener straighteners straightening straightens straighter straightest straightforward straightforwardly straightforwardness straighting straightjacket straightjackets straightlaced straightly straights straightway strain strained strainer strainers straining strains strait straiten straitened straitening straitens straiter straitest straitjacket straitjackets straitlaced straitly straitness straits strake straked strakes stramash stramashes stramonies stramony strand stranded strander stranders stranding strands strang strange strangely strangeness stranger strangered strangering strangers strangest strangle strangled stranglehold strangleholds strangler stranglers strangles strangling strangulate strangulated strangulates strangulating strangulation strap strapless strapped strapper strappers strapping strappings straps strass strasses strata stratagem stratagems stratal stratas strategic strategical strategically strategies strategist strategists strategy stratification stratifications stratified stratifies stratify stratifying stratosphere stratospheres stratospheric stratum stratums stratus stravage stravaged stravages stravaging stravaig stravaiged stravaiging stravaigs straw strawberries strawberry

• Trie from retrieval (but pronounced "try")

• 26-way branching tree

• Paths are prefixes

• Path that ends at thick circle is word

s t

Letter trie

t r a o u m o o l p w a y e^ t s d (^) r e c b **private: struct node { char letter; bool isWord; node children[26]; }; node root; containsWord? !Trace path, check isWord, O(length) containsPrefix? !Trace path O(length) add? !Trace path, add new nodes as needed O(length) O(length) not dependent on num words?! ! Longest word (according to OED) pneumonoultramicroscopicsilicovolcanocaniosis (45)

Lexicon as trie

space usage? ! Per-node = 106 bytes ! Trie for OSPD2 has ~250,000 nodes = 26.5 MB!!!

13 *private: struct node { char letter; bool isWord; int childIndex; int nChildren; }; node root; Unify suffixes as well as prefixes ! Trie had 250,000 nodes, dawg has just 80, ! 125,000 words — so many words have no unique nodes Space usage? ! Per-node = 10 bytes ! Dawg for OSPD2 has ~80,000 nodes = .8MB F (^126) 'A' T (^2712) 'B' F (^396) 'C' F (^4520) 'D' F (^558) 'S' T -1 0

Lexicon as dawg

14 *private: struct node { 5 bits letter; 1 bit isWord; 1 bit for lastChild 25 bits childIndex; }; node root; Bitpack node into smallest size ! Only 26 letters, don't need full ASCII, squeeze in 5 bits ! isWord/last each 1 bit ! Remaining 25 bits used for index ! Each node shrinks from 10 bytes down to 4 Space usage? ! Dawg for OSPD2 has ~80,000 nodes = .32 MB ! Just ~30% of the size of word file! F (^1) F 'A' T (^27) F 'B' F (^39) F 'C' F (^45) F 'D' F (^55) F 'S' T -1 T

The final result

Cool facts about the dawg

Easily written/read to disk

! Just take the entire array and write it out as is ! Indexes don't change, no pointers! ! Easy to restore ! That's what the binary "lexicon.dat" file is

Data structure facilitates other interesting operations

! Tweak width of "beam" as you traverse path to explore just near neighbors

  • Regex, spelling corrections, slight permutations ! Solving puzzles
  • Anagrams, hangman, scrabble, etc.

Original reference

! "The World's Fastest Scrabble Player", Appel & Jacobsen, CACM May 1988