Symbol Tables

Symbol Tables

Symbol Tables Key-value pair abstraction. ! Insert a value with specified key. Symbol Tables ! Given a key, search for the corresponding value. Example: DNS lookup. ! Insert URL with specified IP address. ! Given URL, find corresponding IP address • API URL IP address • basic implementations www.cs.princeton.edu 128.112.136.11 • iterators www.princeton.edu 128.112.128.15 • Comparable keys www.yale.edu 130.132.143.21 • challenges www.harvard.edu 128.103.060.55 www.simpsons.com 209.052.165.60 key value References: Algorithms in Java, Chapter 12. Intro to Algs and Data Structs, Chapter 4. Intro to Programming, Section 4.4. Can interchange roles: given IP address find corresponding URL Copyright © 2007 by Robert Sedgewick and Kevin Wayne. 1 3 Symbol Table Applications Application Purpose Key Value Phone book Look up phone number Name Phone number Bank Process transaction Account number Transaction details File share Find song to download Name of song Computer ID API File system Find file on disk Filename Location on disk Dictionary Look up word Word Definition basic implementations Web search Find relevant documents Keyword List of documents iterators Book index Find relevant pages Keyword List of pages Comparable keys Web cache Download Filename File contents challenges Genomics Find markers DNA string Known positions DNS Find IP address given URL URL IP address Reverse DNS Find URL given IP address IP address URL Compiler Find properties of variable Variable name Value and type Routing table Route Internet packets Destination Best route 2 4 Symbol Table API ST client: make a dictionary and process lookup requests public class Lookup Associative array abstraction. Unique value associated with each key. { public static void main(String[] args) { Symbol table API. In in = new In(args[0]); process input ! put(key, val) insert the key-value pair int keyField = Integer.parseInt(args[1]); int valField = Integer.parseInt(args[2]); ! get(key) search: return value associated with given key String[] database = in.readAll().split("\n"); ! remove(key) remove the key ST<String, String> st = new ST<String, String>(); ! build symbol table contains(key) is given key present? for (int i = 0; i < database.length; i++) ! iterator() return iterator over all keys { String[] tokens = database[i].split(","); String key = tokens[keyField]; Our conventions. String val = tokens[valField]; st.put(key, val); ! Values are not null. } ! Method get() returns null if key not present. while (!StdIn.isEmpty()) ! Implementations all have { process lookups String s = StdIn.readString(); public boolean contains(Key key) if (!st.contains(s)) StdOut.println("Not found"); { return get(key) != null; } else StdOut.println(st.get(s)); } ! put() Method overwrites old value with new value. } } a[key] = val; Some languages (not Java) allow this notation 5 7 ST client: make a dictionary and process lookup requests ST client: make a dictionary and process lookup requests % more amino.csv Command line arguments Command line arguments TTT,Phe,F,Phenylalanine ! ! TTC,Phe,F,Phenylalanine a comma-separated value (CSV) file a comma-separated value (CSV) file TTA,Leu,L,Leucine TTG,Leu,L,Leucine ! key field ! key field TCT,Ser,S,Serine TCC,Ser,S,Serine ! value field ! value field TCA,Ser,S,Serine TCG,Ser,S,Serine TAT,Tyr,Y,Tyrosine TAC,Tyr,Y,Tyrosine % more ip.csv TAA,Stop,Stop,Stop www.princeton.edu,128.112.128.15 Example 1: DNS lookup Example 2: Amino acids TAG,Stop,Stop,Stop www.cs.princeton.edu,128.112.136.35 TGT,Cys,C,Cysteine www.math.princeton.edu,128.112.18.11 URL is key IP is value codon is key name is value TGC,Cys,C,Cysteine www.cs.harvard.edu,140.247.50.127 TGA,Stop,Stop,Stop www.harvard.edu,128.103.60.24 TGG,Trp,W,Tryptophan www.yale.edu,130.132.51.8 CTT,Leu,L,Leucine www.econ.yale.edu,128.36.236.74 % java Lookup ip.csv 0 1 % % java Lookup amino.csv 0 3 CTC,Leu,L,Leucine www.cs.yale.edu,128.36.229.30 CTA,Leu,L,Leucine adobe.com espn.com,199.181.135.201 ACT CTG,Leu,L,Leucine yahoo.com,66.94.234.13 192.150.18.60 Threonine CCT,Pro,P,Proline msn.com,207.68.172.246 CCC,Pro,P,Proline www.princeton.edu google.com,64.233.167.99 TAG CCA,Pro,P,Proline baidu.com,202.108.22.33 128.112.128.15 Stop CCG,Pro,P,Proline yahoo.co.jp,202.93.91.141 CAT,His,H,Histidine ebay.edu sina.com.cn,202.108.33.32 CAT IP is key URL is value CAC,His,H,Histidine ebay.com,66.135.192.87 Not found Histidine CAA,Gln,Q,Glutamine sohu.com,61.135.133.103 CAG,Gln,Q,Glutamine 163.com,220.181.29.154 CGT,Arg,R,Arginine passport.net,65.54.179.226 % java Lookup ip.csv 1 0 CGC,Arg,R,Arginine tom.com,61.135.158.237 CGA,Arg,R,Arginine 128.112.128.15 nate.com,203.226.253.11 CGG,Arg,R,Arginine cnn.com,64.236.16.20 www.princeton.edu ATT,Ile,I,Isoleucine daum.net,211.115.77.211 ATC,Ile,I,Isoleucine 999.999.999.99 blogger.com,66.102.15.100 ATA,Ile,I,Isoleucine fastclick.com,205.180.86.4 Not found ATG,Met,M,Methionine wikipedia.org,66.230.200.100 ... rakuten.co.jp,202.72.51.22 ... 6 8 ST client: make a dictionary and process lookup requests Elementary ST implementations Command line arguments % more classlist.csv Unordered array 10,Bo Ling,P03,bling ! a comma-separated value (CSV) file 10,Steven A Ross,P01,saross Ordered array 10,Thomas Oliver Horton ! key field Conway,P03,oconway Unordered linked list 08,Michael R. Corces ! value field Zimmerman,P01A,mcorces Ordered linked list 09,Bruce David Halperin,P02,bhalperi 09,Glenn Charles Snyders Jr.,P03,gsnyders 09,Siyu Yang,P01A,siyuyang 08,Taofik O. Kolade,P01,tkolade Example 3: Class lists 09,Katharine Paris Why study elementary implementations? Klosterman,P01A,kkloster ! login is key name is value SP,Daniel Gopstein,P01,dgtwo API details need to be worked out 10,Sauhard Sahi,P01,ssahi ! 10,Eric Daniel Cohen,P01A,edcohen performance benchmarks 09,Brian Anthony Geistwhite,P02,bgeistwh % java Lookup classlist.csv 3 1 09,Boris Pivtorak,P01A,pivtorak ! method of choice can be one of these in many situations jsh 09,Jonathan Patrick Zebrowski,P01A,jzebrows ! basis for advanced implementations Jeffrey Scott Harris 09,Dexter James Doyle,P01A,ddoyle dgtwo 09,Michael Weiyang Ye,P03,ye 08,Delwin Uy Olivan,P02,dolivan Daniel Gopstein 08,Edward George Conbeer,P01A,econbeer ye 09,Mark Daniel Stefanski,P01,mstefans login is key precept is value 09,Carter Adams Cleveland,P03,cclevela Michael Weiyang Ye 10,Jacob Stephen Lewellen,P02,jlewelle 10,Ilya Trubov,P02,itrubov 09,Kenton William Murray,P03,kwmurray Always good practice to study elementary implementations % java Lookup classlist.csv 3 2 07,Daniel Steven Marks,P02,dmarks jsh 09,Vittal Kadapakkam,P01,vkadapak 10,Eric Ruben Domb,P01A,edomb P01A 07,Jie Wu,P03,jiewu dgtwo 08,Pritha Ghosh,P02,prithag 10,Minh Quang Anh Do,P01,mqdo P01 ... 9 11 Keys and Values Associative array abstraction. a[key] = val; ! Unique value associated with each key ! If client presents duplicate key, overwrite to change value. Key type: several possibilities API 1. Assume keys are any generic type, use equals() to test equality. 2. Assume keys are Comparable, use compareTo(). basic implementations 3. Use equals() to test equality, make some other assumptions. iterators Comparable keys Value type. Any generic type. challenges Best practices. Use immutable types for symbol table keys. ! Immutable in Java: String, Integer, BigInteger. ! Mutable in Java: Date, GregorianCalendar, StringBuilder. 10 12 Unordered array ST implementation Unordered array ST implementation (search) Maintain parallel arrays of keys and values. public Val get(Key key) { for (int i = 0; i < N; i++) Instance variables if (keys[i].equals(key)) ! array keys[] holds the keys. return vals[i]; return null; ! array vals[] holds the values. } ! integer N holds the number of entries. 0 1 2 3 4 5 Need to use standard array-doubling technique keys[] it was the best of times N = 6 vals[] 2 2 1 1 1 1 0 1 2 3 4 5 keys[] it was the best of times 0 1 2 3 4 5 vals[] 2 2 1 1 1 1 keys[] it was the best of times vals[] 2 2 1 1 1 1 get(“worst”) returns null Alternative: define inner type for entries ! space overhead for entry objects Key, Value are generic and can be any type ! more complicated code Java convention: all objects implement equals() 13 15 Unordered array ST implementation (skeleton) Unordered array ST implementation (insert) @SuppressWarnings("unchecked") suppress annoying message about ugly casts public void put(Key key, Val, val) { public class UnorderedST<Key, Val> int i; { for (int i = 0; i < N; i++) parallel arrays lead to cleaner code private Val[] vals; if (key.equals(keys[i])) than defining a type for entries private Key[] keys; break; private int N = 0; vals[i] = val; 0 1 2 3 4 5 keys[i] = key; keys[] it was the best of times public UnorderedST(int maxN) standard array doubling code omitted if (i == N) N++; vals[] 2 2 1 1 1 1 { } keys = (Key[]) new Object[maxN]; standard ugly casts vals = (Val[]) new Object[maxN]; 0 1 2 3 4 5 } keys[] it was the best of times vals[] 2 2 2 1 1 1 public boolean isEmpty() { return N == 0; } put(“the”, 2) overwrites the 1 public void put(Key key, Val, val) 0 1 2 3 4 5 6 // see next slide keys[] it was the best of times worst vals[] 2 2 2 1 1 1 1 public Val get(Key key) // see next slide put(“worst”, 1) } Associative array abstraction adds a new entry ! must search for key and overwrite with new value if it is there ! otherwise, add new key, value at the end (as in stack) 14 16 Java conventions for equals() Implementing equals() Seems easy, but requires some care All objects implement equals() but default implementation is (x == y) enforce immutability is the object referred to by x public final class PhoneNumber Customized implementations.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    12 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us