Docsity

String Sets and Symbol Tables: Implementations and Comparison, Slides of Data Representation and Algorithm Design

Toggle sidebar
bg1
6.2 String Sets
2
Symbol Table Review
Symbol table.
!Associate a value with a key.
!Search for value given key.
!Balanced trees use O(log N) key comparisons.
!Hashing uses O(1) probes, but probe proportional to key length.
Q. Are key comparisons necessary? No.
Q. Is time proportional to key length required? No.
Best possible. Examine O(log N) bits.
This lecture. Specialized symbol table/set for string keys.
!Faster than hashing.
!More flexible than BST.
3
Applications
Applications.
!Spell checkers.
!Auto-complete.
!Data compression. [stay tuned]
!Computational biology.
!Inverted index of Web.
!Routing tables for IP addresses.
!Storing and querying XML documents.
!Linux kernel API: memory management.
!T9 predictive text input for cell phones.
4
String Set: Operations
Operations.
!set.add(String s) insert string s into set
!set.contains(String s) is string s in the set?
removes duplicates from input stream
StringSET set = new StringSET();
while (!StdIn.isEmpty()) {
String key = StdIn.readString();
if (!set.contains(key)) {
set.add(key);
System.out.println(key);
}
}
goal: implement this efficiently
docsity.com
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download String Sets and Symbol Tables: Implementations and Comparison and more Slides Data Representation and Algorithm Design in PDF only on Docsity!

6.2 String Sets

2

Symbol Table Review

Symbol table.

! Associate a value with a key.

! Search for value given key.

! Balanced trees use O(log N) key comparisons.

! Hashing uses O(1) probes, but probe proportional to key length.

Q. Are key comparisons necessary? No.

Q. Is time proportional to key length required? No.

Best possible. Examine O(log N) bits.

This lecture. Specialized symbol table/set for string keys.

! Faster than hashing.

! More flexible than BST.

3

Applications

Applications.

! Spell checkers.

! Auto-complete.

! Data compression. [stay tuned]

! Computational biology.

! Inverted index of Web.

! Routing tables for IP addresses.

! Storing and querying XML documents.

! Linux kernel API: memory management.

! T9 predictive text input for cell phones.

4

String Set: Operations

Operations.

! set.add(String s) insert string s into set

! set.contains(String s) is string s in the set?

removes duplicates from input stream StringSET set = new StringSET(); while (!StdIn.isEmpty()) { String key = StdIn.readString(); if (!set.contains(key)) { set.add(key); System.out.println(key); } } goal: implement this efficiently

5

Keys

Key = sequence of "digits."

! DNA: sequence of a, c, g, t.

! IPv6 address: sequence of 128 bits.

! English words: sequence of lowercase letters.

! Protein: sequence of amino acids A, C, ..., Y.

! Credit card number: sequence of 16 decimal digits.

! International words: sequence of Unicode characters.

! Library call numbers: sequence of letters, numbers, periods.

This lecture. Key = string over ASCII alphabet.

6

String Set: Implementations Cost Summary

Challenge. As fast as hashing, as flexible as BST.

N = number of strings L = size of string C = number of characters in input R = radix

  • only reads in data Actor. 8 2MB, 11.4M words, 900K distinct. Moby. 1 .2MB, 210K words, 32K distinct. Red-black Implementation Hashing L + log N Search hit L log N Insert L Typical Case C Space C
  1. 40 Moby
  2. 76
  3. 4 Actors
  4. 6 Dedup Input * L L L 0. 26 15. 1

Tries

8

Tries. [from retrieval, but pronounced "try"]

! Store characters in internal nodes, not keys.

! Store records in external nodes.

! Use the characters of the key to guide the search.

Ex. sells sea shells by the sea

Tries

by sea sells shells the

13

R-Way Trie: Java Implementation

public void add(String s) { add(root, s, 0 ); } private Node add(Node x, String s, int i) { if (x == null) x = new Node(); if (i == s.length()) x.end = true; else { char c = s.charAt(i); x.next[c] = add(x.next[c], s, i+ 1 ); } return x; } 14

Existence Symbol Table: Implementations Cost Summary

R-way trie. Faster than hashing for small R;

slow, memory hog for large R.

Challenge. Use less space.

R = 256 Red-Black Implementation Hashing L + log N Search hit L log N Insert L Typical Case C Space C

  1. 40 Moby
  2. 76
  3. 4 Actors
  4. 6 R-Way Trie L L R N + C 1. 12 Memory Dedup Input L L L 0. 26 15. 1 R = 128 N = number of strings L = size of string C = number of characters in input R = radix

Ternary Search Trie

18

Ternary Search Trie

Ternary search trie. [Bentley-Sedgewick]

! Each node has 3 children:

! Left (smaller), middle (equal), right (larger).

Ex. sells sea shells by the sea shore

Observation. Few wasted links!

19

26 - Way Trie vs. TST

TST. Collapses empty links in 26-way trie.

26-way trie (1035 null links, not shown) TST (155 null links) now f toirp i dlikm t jaogt s noobb s hkuyt a bceet m eegng f jeawy o jwoly r gaipg w weaes c waabd c cauwe f teaep a tgaor j daumg and 20

TST Implementation

TST string set. A node.

Node. Five fields:

! Character c.

! Reference to left TST. [smaller]

! Reference to middle TST. [equal]

! Reference to right TST. [larger]

! Bit to indicate whether this

node is the last character in some word.

root h a i d s had is i p hip private class Node { char c; Node l, m, r; boolean end; } hi 21

TST: Java Implementation

public boolean contains(String s) { if (s.length() == 0 ) return false; return contains(root, s, 0 ); } private boolean contains(Node x, String s, int i) { if (x == null) return false; char c = s.charAt(i); if (c < x.c) return contains(x.l, s, i); else if (c > x.c) return contains(x.r, s, i); else if (i < s.length()- 1 ) return contains(x.m, s, i+ 1 ); else (i < s.length()- 1 ) return x.end; } 22

TST: Java Implementation

public void add(String s) { root = add(root, s, 0 ); } private Node add(Node x, String s, int i) { char c = s.charAt(i); if (x == null) x = new Node(c); if (c < x.c) x.l = add(x.l, s, i); else if (c > x.c) x.r = add(x.r, s, i); else if (i < s.length()- 1 ) x.m = add(x.m, s, i+ 1 ); else (i < s.length() - 1 x.end = true; return x; }

27

Tries: Advanced Operations

Insert. Insert a key.

Contains. Check if given key in the set.

Delete. Delete key from the symbol table.

Sort. Iterate through the keys in ascending order.

Select. Find the kth^ largest key.

Range search. Find all elements between k 1 and k 2.

Longest prefix match. Find longest prefix match.

Wildcard match. Allow wildcard characters.

Near neighbor search. Find strings that differ in! P chars.

BST ops spell checking and OCR Trie ops ST ops 28

Longest Prefix Match

Longest prefix match. Find string in set with longest prefix match.

Ex. Search IP database for longest prefix matching destination IP,

and route packets accordingly.

" 128 " " 128. 112 " " 128. 112. 136 " " 128. 112. 055 " " 128. 112. 055. 15 " " 128. 112. 155. 11 " " 128. 112. 155. 13 " " 128. 222 " " 128. 222. 136 " prefix(" 128. 112. 136. 11 ") = " 128. 112. 136 " prefix(" 128. 166. 123. 45 ") = " 128 " 29

R-way Trie: Longest Prefix Match

Longest prefix match. Find string in set with longest prefix match.

public String prefix(String s) { int len = prefix(root, s, 0 ); return s.substring( 0 , len); } private int prefix(Node x, String s, int i) { if (x == null) return 0 ; int len = 0 ; if (x.end) len = i; if (i == s.length()) return len; char c = s.charAt(i); return Math.max(len, prefix(x.next[c], s, i+ 1 )); } 30

Wildcard Match

Wildcard match. Use wildcard. to match any character.

coalizer coberger codifier cofaster cofather cognizer cohelper colander coleader ... compiler ... composer computer cowkeper acresce acroach acuracy octarch science scranch scratch scrauch screich scrinch scritch scrunch scudick scutock co....er .c...c.

31

TST: Wildcard Match

Wildcard match. Use wildcard. to match any character.

! Search as usual if query character is not a period.

! Go down all three branches if query character is a period.

public void wildcard(String s) { wildcard(root, s, 0 , ""); } private void wildcard(Node x, String s, int i, String prefix) { if (x == null) return; char c = s.charAt(i); if (c == '.' || c < x.c) wildcard(x.left, s, i, prefix); if (c == '.' || c == x.c) { if (i < s.length() - 1 ) wildcard(x.mid, s, i+ 1 , prefix + x.c); else if (x.end) System.out.println(prefix + x.c); } if (c == '.' || c > x.c) wildcard(x.right, s, i, prefix); } for printing out matches (use StringBuilder for efficiency) 32

T 9 Texting

Goal. Type text messages on a phone keypad.

Multi-tap input. Enter a letter by repeatedly pressing a key until the

desired letter appears.

T9 text input. ["A much faster and more fun way to enter text."]

! Find all words that correspond to given sequence of numbers.

! Press 0 to see all completion options.

Ex: hello

! Multi-tap: 4 4 3 3 5 5 5 5 5 5 6 6 6

! T9: 4 3 5 5 6

www.t 9 .com display matching words in order of "importance" 35

TST: Collapsing 1 - Way Branches

Collapsing 1-way branches.

! Internal node stores char; external node stores full key.

! Collapse (and split) 1-way branches at bottom?

! Search hit ends at leaf with given key.

! Search miss ends at null node or leaf with different key.

s by h the e (^) shells l sea sells 36

TST: Collapsing 1 - Way Branches

Collapsing 1-way branches.

! Internal node stores char; external node stores full key.

! Collapse (and split) 1-way branches at bottom?

! Search hit ends at leaf with given key.

! Search miss ends at null node or leaf with different key.

s by h the e shells e l shore sea sells