<<

Algorithms ROBERT SEDGEWICK | KEVIN WAYNE

3.1 SYMBOL TABLES 3.1 SYMBOL TABLES

‣ API ‣ API ‣ elementary implementations ‣ elementary implementations Algorithms ‣ ordered operations Algorithms ‣ ordered operations FOURTH EDITION

ROBERT SEDGEWICK | KEVIN WAYNE ROBERT SEDGEWICK | KEVIN WAYNE http://algs4.cs.princeton.edu http://algs4.cs.princeton.edu

Symbol tables Symbol table applications

Key-value pair abstraction. ・Insert a value with specified key. application purpose of search key value Given a key, search for the corresponding value. ・ dictionary find definition word definition

book index find relevant pages term list of page numbers Ex. DNS lookup. ・Insert domain name with specified IP address. file share find song to download name of song computer ID ・Given domain name, find corresponding IP address. financial account process transactions account number transaction details

web search find relevant web pages keyword list of page names domain name IP address compiler find properties of variables variable name type and value www.cs.princeton.edu 128.112.136.11 route Internet packets destination best route www.princeton.edu 128.112.128.15 routing table

www.yale.edu 130.132.143.21 DNS find IP address domain name IP address

www.harvard.edu 128.103.060.55 reverse DNS find domain name IP address domain name

www.simpsons.com 209.052.165.60 genomics find markers DNA string known positions

file system find file on disk filename location on disk key value 3 4 Symbol tables: context Basic symbol table API

Also known as: maps, dictionaries, associative arrays. abstraction. Associate one value with each key.

Generalizes arrays. Keys need not be between 0 and N – 1. public class ST

ST() Language support. create an empty symbol table ・External libraries: , VisualBasic, Standard ML, bash, ... void put(Key key, Value val) put key-value pair into the table a[key] = val;

・Built-in libraries: Java, C#, C++, Scala, ... Value get(Key key) value paired with key a[key] ・Built-in to language: Awk, Perl, PHP, Tcl, JavaScript, Python, Ruby, Lua. boolean contains(Key key) is there a value paired with key?

every array is an every object is an table is the only void delete(Key key) remove key (and its value) from table associative array associative array primitive boolean isEmpty() is the table empty?

int size() number of key-value pairs in the table hasNiceSyntaxForAssociativeArrays["Python"] = true Iterable keys() all the keys in the table hasNiceSyntaxForAssociativeArrays["Java"] = false

legal Python code

5 6

Conventions Keys and values

・Values are not null. Java allows null value Value type. Any generic type. ・Method get() returns null if key not present. ・Method put() overwrites old value with new value. specify Comparable in API. Key type: several natural assumptions. ・Assume keys are Comparable, use compareTo(). Intended consequences. ・Assume keys are any generic type, use equals() to test equality. ・Easy to implement contains(). ・Assume keys are any generic type, use equals() to test equality; use hashCode() to scramble key. public boolean contains(Key key)

{ return get(key) != null; } built-in to Java (stay tuned)

・Can implement lazy version of delete(). Best practices. Use immutable types for symbol table keys. public void delete(Key key) ・Immutable in Java: Integer, Double, String, java.io.File, … { put(key, null); } ・Mutable in Java: StringBuilder, java.net.URL, arrays, ...

7 8 Equality test Implementing equals for user-defined types

All Java classes inherit a method equals(). Seems easy.

public class Date implements Comparable { Java requirements. For any references x, y and z: private final int month; private final int day; Reflexive: x.equals(x) is true. private final int year; ・ equivalence ... ・Symmetric: x.equals(y) iff y.equals(x). relation ・Transitive: if x.equals(y) and y.equals(z), then x.equals(z). public boolean equals(Date that) { ・Non-null: 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, … if (this.day != that.day ) return false; check that all significant User-defined implementations. Some care needed. if (this.month != that.month) return false; fields are the same if (this.year != that.year ) return false; return true; } } 9 10

Implementing equals for user-defined types Equals design

Seems easy, but requires some care. typically unsafe to use equals() with inheritance "Standard" recipe for user-defined types. (would violate symmetry) ・Optimization for reference equality. public final class Date implements Comparable { ・Check against null. private final int month; must be Object. Check that two objects are of the same type and cast. private final int day; ・ Why? Experts still debate. private final int year; ・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) public boolean equals(Object y) – if field is an object, use equals() apply rule recursively { can use Arrays.deepEquals(a, b) if (y == this) return true; optimize for true object equality – if field is an array, apply to each entry but not a.equals(b)

if (y == null) return false; check for null

objects must be in the same class if (y.getClass() != this.getClass()) e.g., cached Manhattan distance (religion: getClass() vs. instanceof) Best practices. return false; ・No need to use calculated fields that depend on other fields. Date that = (Date) y; cast is guaranteed to succeed ・Compare fields mostly likely to differ first. if (this.day != that.day ) return false; check that all significant if (this.month != that.month) return false; fields are the same ・Make compareTo() consistent with equals(). if (this.year != that.year ) return false; return true; } x.equals(y) if and only if (x.compareTo(y) == 0) } 11 12 ST test client for traces ST test client for analysis

Build ST by associating value i with ith string from standard input. Frequency counter. Read a sequence of strings from standard input and print out one that occurs with highest frequency.

public static void main(String[] args) { % more tinyTale.txt ST st = new ST(); it was the best of times for (int i = 0; !StdIn.isEmpty(); i++) it was the worst of times { keys S E A R C H E X A M P L E it was the age of wisdom String key = StdIn.readString(); values 0 1 2 3 4 5 6 7 8 9 10 11 12 it was the age of foolishness st.put(key, i); it was the epoch of belief output for outputoutput for } basic symbol table ordered it was the epoch of incredulity for (String s : st.keys()) (one possibility) symbol table it was the season of light StdOut.println(s + " " + st.get(s)); L 11 A 8 it was the season of darkness } P 10 C 4 it was the spring of hope M 9 E 12 it was the winter of despair X 7 H 5 % java FrequencyCounter 1 < tinyTale.txt tiny example L 11 H 5 it 10 (60 words, 20 distinct) C 4 M 9 keys S E A R C H E X A M P L E R 3 P 10 % java FrequencyCounter 8 < tale.txt real example (135,635 words, 10,769 distinct) values 0 1 2 3 4 5 6 7 8 9 10 11 12A 8 R 3 business 122 S 0 output for E 12 real example output for % java FrequencyCounter 10 < leipzig1M.txt basic symbol table ordered S 0 X 7 (21,191,455 words, 534,580 distinct) government 24763 (one possibility) symbol table Keys, values, and output for test client 13 14 L 11 A 8 P 10 C 4 M 9 E 12 Frequency counter implementation X 7 H 5 H 5 L 11 C 4 M 9 public class FrequencyCounterR 3 P 10 { public static void main(String[]A 8 args) R 3 { E 12 S 0 int minlen = Integer.parseInt(args[0]); S 0 X 7 ST st = new ST(); create ST 3.1 SYMBOL TABLES while (!StdIn.isEmpty()) { Keys, values, and output for test client String word = StdIn.readString(); ignore short strings ‣ API if (word.length() < minlen) continue; read string and if (!st.contains(word)) st.put(word, 1); update frequency ‣ elementary implementations else st.put(word, st.get(word) + 1); } ‣ ordered operations String max = ""; Algorithms st.put(max, 0); for (String word : st.keys()) print a string if (st.get(word) > st.get(max)) with max freq ROBERT SEDGEWICK | KEVIN WAYNE max = word; http://algs4.cs.princeton.edu StdOut.println(max + " " + st.get(max)); } }

15 Sequential search in a Elementary ST implementations: summary

Data structure. Maintain an (unordered) linked list of key-value pairs.

Search. Scan through all keys until find a match. guarantee average case key ST implementation Insert. Scan through all keys until find a match; if no match add to front. interface search insert search hit insert

key value first sequential search red nodes N N N / 2 N equals() S 0 S 0 are new (unordered list) 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 Challenge. Efficient implementations of both search and insert. E 12 L 11 P 10 M 9 X 7 H 5 C 4 R 3 A 8 E 12 S 0

Trace of linked-list ST implementation for standard indexing client 17 18

Binary search in an ordered array Binary search: Java implementation

Data structure. Maintain an ordered array of key-value pairs. public Value get(Key key) { Rank helper function. How many keys < k ? if (isEmpty()) return null; int i = rank(key); if (i < N && keys[i].compareTo(key) == 0) return vals[i]; keys[] else return null; successful search for P 0 1 2 3 4 5 6 7 8 9 } keys[] lo hi m A C E H L M P R S X successful search for P successful0 search 9 for 4 P A0 C1 E2 H3 L4 M5 P6 R7 S8 X9 entries in black keys[] are a[lo..hi] private int rank(Key key) number of keys < key lo5 hi9 m7 A C E H L M P R S X successful search for P 0 1 2 3 4 5 6 7 8 9 entries in black { 50 69 54 A C E H L M P R S X lo hi m are a[lo..hi] 5 9 7 A C E H L M P R S X entry in red is a[m] int lo = 0, hi = N-1; 60 69 64 A C E H L M P R S X entries in black are a[lo..hi] while (lo <= hi) 5 6 5 A C E H L M P R loop S exits X with keys[m] = P: return 6 unsuccessful5 search9 7 for Q A C E H L M P R S X entry in red is a[m] 6 6 6 A C E H L M P R S X { lo5 hi6 m5 A C E H L M P R S X unsuccessful search for Q loop exits withentry keys[m] in red is= a[m] P: return 6 int mid = lo + (hi - lo) / 2; 06 96 46 A C E H L M P R S X lo5 hi9 m7 A C E H L M P R S X int cmp = key.compareTo(keys[mid]); unsuccessfulunsuccessful search search for forQ Q loop exits with keys[m] = P: return 6 50 69 54 A C E H L M P R S X if (cmp < 0) hi = mid - 1; lo hi m 5 9 7 A C E H L M P R S X 70 69 64 A C E H L M P R S X else if (cmp > 0) lo = mid + 1; 5 6 5 A C E H L M P R S X 5 9 7 loop exitsA withC Elo >H hi: L returnM P 7 R S X else if (cmp == 0) return mid; 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 7 6 6 loop exitsA withC Elo >H hi: L returnM P 7 R S X return lo;

loopTrace exits of with binary lo > search hi: return for rank 7 in an ordered array }

Trace of binary search for rank in an ordered array 19 20 Binary search: trace of standard indexing client Elementary ST implementations: summary

Problem. To insert, need to shift all greater keys over.

guarantee average case key ST implementation keys[] vals[] interface search insert search hit insert key value 0 1 2 3 4 5 6 7 8 9 N 0 1 2 3 4 5 6 7 8 9 S 0 S 1 0 entries in black sequential search E 1 E S 2 1 0 equals() entries in red moved to the right (unordered list) N N N / 2 N A 2 A E S were inserted 3 2 1 0 R 3 A E R S 4 2 1 3 0 C 4 A C E R S 5 2 4 1 3 0 binary search entries in gray compareTo() H 5 A C E H R S did not move 6 2 4 1 5 3 0 circled entries are (ordered array) log N N log N N / 2 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 ordered-array ST implementation for standard indexing client Challenge. Efficient implementations of both search and insert.

21 22

Examples 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 3.1 SYMBOL TABLES 09:01:10 Houston floor(09:05:00) 09:03:13 Chicago 09:10:11 Seattle ‣ API select(7) 09:10:25 Seattle 09:14:25 Phoenix ‣ elementary implementations 09:19:32 Chicago ‣ ordered operations 09:19:46 Chicago Algorithms keys(09:15:00, 09:25:00) 09:21:05 Chicago 09:22:43 Seattle 09:22:54 Seattle 09:25:52 Chicago ROBERT SEDGEWICK | KEVIN WAYNE ceiling(09:30:00) 09:35:21 Chicago http://algs4.cs.princeton.edu 09:36:14 Seattle max() 09:37:44 Phoenix

size(09:15:00, 09:25:00) is 5 rank(09:10:25) is 7

Examples of ordered symbol-table operations 24 Ordered symbol table API Binary search: ordered symbol table operations summary

public class ST, Value>

... sequential binary search search Key min() smallest key search N log N Key max() largest key insert / delete N N Key floor(Key key) largest key less than or equal to key

Key ceiling(Key key) smallest key greater than or equal to key min / max N 1

int rank(Key key) number of keys less than key floor / ceiling N log N Key select(int k) key of rank k rank N log N void deleteMin() delete smallest key

void deleteMax() delete largest key select N 1

int size(Key lo, Key hi) number of keys between lo and hi ordered iteration N log N N Iterable keys() all keys, in sorted order order of growth of the running time for ordered symbol table operations Iterable keys(Key lo, Key hi) keys between lo and hi, in sorted order

25 26