Question? Leave a message!




Symbol table applications

Symbol table applications
ROBERT SEDGEWICK KEVIN WAYNE Algorithms 3.1 SYMBOL TABLES API ‣ elementary implementations ‣ ordered operations ‣ Algorithms FOUR TH EDITION ROBERT SEDGEWICK KEVIN WAYNE http://algs4.cs.princeton.edu3.1 SYMBOL TABLES API ‣ elementary implementations ‣ ordered operations ‣ Algorithms ROBERT SEDGEWICK KEVIN WAYNE http://algs4.cs.princeton.eduSymbol tables Keyvalue pair abstraction. Insert a value with specified key. Given a key, search for the corresponding value. Ex. DNS lookup. Insert domain name with specified IP address. Given domain name, find corresponding IP address. domain name IP address www.cs.princeton.edu 128.112.136.11 www.princeton.edu 128.112.128.15 www.yale.edu 130.132.143.21 www.harvard.edu 128.103.060.55 www.simpsons.com 209.052.165.60 key value 3Symbol table applications application purpose of search key value find definition word definition dictionary find relevant pages term list of page numbers book index find song to download name of song computer ID file share process transactions account number transaction details financial account find relevant web pages keyword list of page names web search find properties of variables variable name type and value compiler route Internet packets destination best route routing table find IP address domain name IP address DNS find domain name IP address domain name reverse DNS find markers DNA string known positions genomics find file on disk filename location on disk file system 4Symbol tables: context Also known as: maps, dictionaries, associative arrays. Generalizes arrays. Keys need not be between 0 and N – 1. Language support. External libraries: C, VisualBasic, Standard ML, bash, ... Builtin libraries: Java, C, C++, Scala, ... Builtin to language: Awk, Perl, PHP, Tcl, JavaScript, Python, Ruby, Lua. every object is an table is the only every array is an associative array primitive data structure associative array hasNiceSyntaxForAssociativeArrays"Python" = true hasNiceSyntaxForAssociativeArrays"Java" = false legal Python code 5Basic symbol table API Associative array abstraction. Associate one value with each key. public class public class public class ST ST STKey, Value Key, Value Key, Value ST() create an empty symbol table akey = val; void put(Key key, Value val) put keyvalue pair into the table akey Value get(Key key) value paired with key boolean contains(Key key) is there a value paired with key void delete(Key key) remove key (and its value) from table boolean isEmpty() is the table empty int size() number of keyvalue pairs in the table IterableKey keys() all the keys in the table 6Conventions Java allows null value Values are not null. Method get() returns null if key not present. Method put() overwrites old value with new value. Intended consequences. Easy to implement contains(). public boolean contains(Key key) return get(key) = null; Can implement lazy version of delete(). public void delete(Key key) put(key, null); 7Keys and values Value type. Any generic type. specify Comparable in API. Key type: several natural assumptions. Assume keys are Comparable, use compareTo(). Assume keys are any generic type, use equals() to test equality. Assume keys are any generic type, use equals() to test equality; use hashCode() to scramble key. builtin to Java (stay tuned) Best practices. Use immutable types for symbol table keys. Immutable in Java: Integer, Double, String, java.io.File, … Mutable in Java: StringBuilder, java.net.URL, arrays, ... 8Equality test All Java classes inherit a method equals(). Java requirements. For any references x, y and z: Reflexive: x.equals(x) is true. equivalence relation Symmetric: x.equals(y) iff y.equals(x). Transitive: if x.equals(y) and y.equals(z), then x.equals(z). Nonnull: x.equals(null) is false. do x and y refer to the same object Default implementation. (x == y) Customized implementations. Integer, Double, String, java.io.File, … Userdefined implementations. Some care needed. 9Implementing equals for userdefined types Seems easy. public class Date implements ComparableDate private final int month; private final int day; private final int year; ... public boolean equals(Date that) check that all significant if (this.day = that.day ) return false; fields are the same if (this.month = that.month) return false; if (this.year = that.year ) return false; return true; 10Implementing equals for userdefined types typically unsafe to use equals() with inheritance Seems easy, but requires some care. (would violate symmetry) public final class Date implements ComparableDate private final int month; must be Object. private final int day; Why Experts still debate. private final int year; ... public boolean equals(Object y) optimize for true object equality if (y == this) return true; check for null if (y == null) return false; objects must be in the same class if (y.getClass() = this.getClass()) (religion: getClass() vs. instanceof) return false; cast is guaranteed to succeed Date that = (Date) y; if (this.day = that.day ) return false; check that all significant if (this.month = that.month) return false; fields are the same if (this.year = that.year ) return false; return true; 11Equals design "Standard" recipe for userdefined types. Optimization for reference equality. Check against null. Check that two objects are of the same type and cast. Compare each significant field: but use Double.compare() with double – if field is a primitive type, use == (or otherwise deal with 0.0 and NaN) apply rule recursively – if field is an object, use equals() can use Arrays.deepEquals(a, b) – if field is an array, apply to each entry but not a.equals(b) e.g., cached Manhattan distance Best practices. No need to use calculated fields that depend on other fields. Compare fields mostly likely to differ first. Make compareTo() consistent with equals(). x.equals(y) if and only if (x.compareTo(y) == 0) 12ST test client for traces th Build ST by associating value i with i string from standard input. public static void main(String args) STString, Integer st = new STString, Integer(); for (int i = 0; StdIn.isEmpty(); i++) keys S E A R C H E X A M P L E values 0 1 2 3 4 5 6 7 8 9 10 11 12 String key = StdIn.readString(); st.put(key, i); output for output for output basic symbol table ordered (one possibility) symbol table for (String s : st.keys()) A 8 L 11 StdOut.println(s + " " + st.get(s)); C 4 P 10 E 12 M 9 H 5 X 7 L 11 H 5 M 9 C 4 keys S E A R C H E X A M P L E P 10 R 3 values 0 1 2 3 4 5 6 7 8 9 10 11 12 R 3 A 8 S 0 E 12 output for output for X 7 S 0 basic symbol table ordered (one possibility) symbol table Keys, values, and output for test client 13 A 8 L 11 C 4 P 10 E 12 M 9 H 5 X 7 L 11 H 5 M 9 C 4 P 10 R 3 R 3 A 8 S 0 E 12 X 7 S 0 Keys, values, and output for test clientST test client for analysis Frequency counter. Read a sequence of strings from standard input and print out one that occurs with highest frequency. more tinyTale.txt it was the best of times it was the worst of times it was the age of wisdom it was the age of foolishness it was the epoch of belief it was the epoch of incredulity it was the season of light it was the season of darkness it was the spring of hope it was the winter of despair tiny example java FrequencyCounter 1 tinyTale.txt (60 words, 20 distinct) it 10 real example java FrequencyCounter 8 tale.txt (135,635 words, 10,769 distinct) business 122 real example java FrequencyCounter 10 leipzig1M.txt (21,191,455 words, 534,580 distinct) government 24763 14Frequency counter implementation public class FrequencyCounter public static void main(String args) int minlen = Integer.parseInt(args0); create ST STString, Integer st = new STString, Integer(); while (StdIn.isEmpty()) String word = StdIn.readString(); ignore short strings if (word.length() minlen) continue; read string and if (st.contains(word)) st.put(word, 1); update frequency else st.put(word, st.get(word) + 1); String max = ""; st.put(max, 0); print a string for (String word : st.keys()) with max freq if (st.get(word) st.get(max)) max = word; StdOut.println(max + " " + st.get(max)); 153.1 SYMBOL TABLES API ‣ elementary implementations ‣ ordered operations ‣ Algorithms ROBERT SEDGEWICK KEVIN WAYNE http://algs4.cs.princeton.eduSequential search in a linked list Data structure. Maintain an (unordered) linked list of keyvalue pairs. Search. Scan through all keys until find a match. Insert. Scan through all keys until find a match; if no match add to front. key value first red nodes are new S 0 S 0 black nodes E 1 E 1 S 0 are accessed in search A 2 A 2 E 1 S 0 R 3 R 3 A 2 E 1 S 0 C 4 C 4 R 3 A 2 E 1 S 0 circled entries are H 5 H 5 C 4 R 3 A 2 E 1 S 0 changed values E 6 H 5 C 4 R 3 A 2 E 6 S 0 X 7 X 7 H 5 C 4 R 3 A 2 E 6 S 0 gray nodes A 8 X 7 H 5 C 4 R 3 A 8 E 6 S 0 are untouched M 9 M 9 X 7 H 5 C 4 R 3 A 8 E 6 S 0 P 10 P 10 M 9 X 7 H 5 C 4 R 3 A 8 E 6 S 0 L 11 L 11 P 10 M 9 X 7 H 5 C 4 R 3 A 8 E 6 S 0 E 12 L 11 P 10 M 9 X 7 H 5 C 4 R 3 A 8 E 12 S 0 Trace of linkedlist ST implementation for standard indexing client 17Elementary ST implementations: summary guarantee guarantee average case average case key key ST implementation ST implementation interface interface search insert search hit insert sequential search equals() N N N / 2 N (unordered list) Challenge. Efficient implementations of both search and insert. 18Binary search in an ordered array Data structure. Maintain an ordered array of keyvalue pairs. Rank helper function. How many keys k keys 0 1 2 3 4 5 6 7 8 9 successful search for P keys lo hi m A C E H L M P R S X successful search for P entries in black 0 1 2 3 4 5 6 7 8 9 successful search for P 0 9 4 A C E H L M P R S X keys are alo..hi lo hi m 5 9 7 A C E H L M P R S X 0 1 2 3 4 5 6 7 8 9 successful search for P entries in black 0 9 4 A C E H L M P R S X 5 6 5 A C E H L M P R S X are alo..hi lo hi m entry in red is am 5 9 7 A C E H L M P R S X entries in black 6 6 6 A C E H L M P R S X 0 9 4 A C E H L M P R S X are alo..hi 5 6 5 A C E H L M P R S X loop exits with keysm = P: return 6 unsuccessful search for Q 5 9 7 A C E H L M P R S X entry in red is am 6 6 6 A C E H L M P R S X lo hi m 5 6 5 A C E H L M P R S X loop exits with keysm = P: return 6 unsuccessful search for Q entry in red is am 0 9 4 A C E H L M P R S X 6 6 6 A C E H L M P R S X lo hi m 5 9 7 A C E H L M P R S X unsuccessful search for Q loop exits with keysm = P: return 6 unsuccessful search for Q 0 9 4 A C E H L M P R S X 5 6 5 A C E H L M P R S X lo hi m 5 9 7 A C E H L M P R S X 7 6 6 A C E H L M P R S X 0 9 4 A C E H L M P R S X 5 6 5 A C E H L M P R S X loop exits with lo hi: return 7 5 9 7 A C E H L M P R S X 7 6 6 A C E H L M P R S X 5 6 5 A C E H L M P R S X Trace of binary search for rank in an ordered array loop exits with lo hi: return 7 7 6 6 A C E H L M P R S X loop exits with lo hi: return 7 Trace of binary search for rank in an ordered array 19 Trace of binary search for rank in an ordered arrayBinary search: Java implementation public Value get(Key key) if (isEmpty()) return null; int i = rank(key); if (i N keysi.compareTo(key) == 0) return valsi; else return null; number of keys key private int rank(Key key) int lo = 0, hi = N1; while (lo = hi) int mid = lo + (hi lo) / 2; int cmp = key.compareTo(keysmid); if (cmp 0) hi = mid 1; else if (cmp 0) lo = mid + 1; else if (cmp == 0) return mid; return lo; 20Binary search: trace of standard indexing client Problem. To insert, need to shift all greater keys over. keys vals 0 1 2 3 4 5 6 7 8 9 N 0 1 2 3 4 5 6 7 8 9 key value S 0 S 1 0 entries in black E 1 E S 2 1 0 entries in red moved to the right A 2 A E S 3 2 1 0 were inserted R 3 A E R S 4 2 1 3 0 C 4 A C E R S 5 2 4 1 3 0 entries in gray circled entries are did not move H 5 A C E H R S 6 2 4 1 5 3 0 changed values E 6 A C E H R S 6 2 4 6 5 3 0 X 7 A C E H R S X 7 2 4 6 5 3 0 7 A 8 A C E H R S X 7 8 4 6 5 3 0 7 M 9 A C E H M R S X 8 8 4 6 5 9 3 0 7 P 10 A C E H M P R S X 9 8 4 6 5 9 10 3 0 7 L 11 A C E H L M P R S X 10 8 4 6 5 11 9 10 3 0 7 E 12 A C E H L M P R S X 10 8 4 12 5 11 9 10 3 0 7 A C E H L M P R S X 8 4 12 5 11 9 10 3 0 7 Trace of orderedarray ST implementation for standard indexing client 21Elementary ST implementations: summary guarantee guarantee average case average case key key ST implementation ST implementation interface interface search insert search hit insert sequential search equals() N N N / 2 N (unordered list) binary search compareTo() log N N log N N / 2 (ordered array) Challenge. Efficient implementations of both search and insert. 223.1 SYMBOL TABLES API ‣ elementary implementations ‣ ordered operations ‣ Algorithms ROBERT SEDGEWICK KEVIN WAYNE http://algs4.cs.princeton.eduExamples of ordered symbol table API keys values min() 09:00:00 Chicago 09:00:03 Phoenix 09:00:13 Houston get(09:00:13) 09:00:59 Chicago 09:01:10 Houston floor(09:05:00) 09:03:13 Chicago 09:10:11 Seattle select(7) 09:10:25 Seattle 09:14:25 Phoenix 09:19:32 Chicago 09:19:46 Chicago keys(09:15:00, 09:25:00) 09:21:05 Chicago 09:22:43 Seattle 09:22:54 Seattle 09:25:52 Chicago ceiling(09:30:00) 09:35:21 Chicago 09:36:14 Seattle max() 09:37:44 Phoenix size(09:15:00, 09:25:00) is 5 rank(09:10:25) is 7 24 Examples of ordered symboltable operationsOrdered symbol table API public class public class public class ST ST STKey extends ComparableKey, Value Key extends ComparableKey, Value Key extends ComparableKey, Value ... Key min() smallest key Key max() largest key Key floor(Key key) largest key less than or equal to key Key ceiling(Key key) smallest key greater than or equal to key int rank(Key key) number of keys less than key Key select(int k) key of rank k void deleteMin() delete smallest key void deleteMax() delete largest key int size(Key lo, Key hi) number of keys between lo and hi IterableKey keys() all keys, in sorted order IterableKey keys(Key lo, Key hi) keys between lo and hi, in sorted order 25Binary search: ordered symbol table operations summary sequential binary search search search N log N insert / delete N N min / max N 1 floor / ceiling N log N rank N log N select N 1 ordered iteration N log N N order of growth of the running time for ordered symbol table operations 26
Website URL
Comment