Presentation is loading. Please wait.

Presentation is loading. Please wait.

4.4 Symbol Tables Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright ©

Similar presentations


Presentation on theme: "4.4 Symbol Tables Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright ©"— Presentation transcript:

1 4.4 Symbol Tables Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © · April 1, :34 tt

2 Symbol Table Symbol table. Key-value pair abstraction.
Insert a key with specified value. Given a key, search for the corresponding value. Ex. [DNS lookup] Insert URL with specified IP address. Given URL, find corresponding IP address. URL IP address DNS = domain name system We’ll focus on insert, search key value

3 Symbol Table Applications
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 dictionary look up word word definition web search find relevant documents keyword list of documents genomics find markers DNA string known positions DNS find IP address given URL URL IP address reverse DNS find URL given IP address book index find relevant pages list of pages web cache download filename file contents compiler find properties of variable variable name value and type file system find file on disk location on disk routing table route Internet packets destination best route Google: database of over 3 billion pages, 140 million queries per day! Search problem core of many real-world applications (Think Google). "Associative memory." Index of any kind.

4 Symbol Table API public static void main(String[] args) {
ST<String, String> st = new ST<String, String>(); st.put(" " "); st.put(" " "); st.put(" " "); StdOut.println(st.get(" StdOut.println(st.get(" StdOut.println(st.get(" } We'll implement the ADT next, but first let's see how the client should work. put works like an array, but index can be arbitrary string get works like an array, but index can be arbitrary string Note: symbol table returns null if not found st[" = " " null st["

5 Symbol Table Client: Frequency Counter
Frequency counter. [e.g., web traffic analysis, linguistic analysis] Read in a key. If key is in symbol table, increment counter by one; If key is not in symbol table, insert it with count = 1. public class Freq { public static void main(String[] args) { ST<String, Integer> st = new ST<String, Integer>(); while (!StdIn.isEmpty()) { String key = StdIn.readString(); if (st.contains(key)) st.put(key, st.get(key) + 1); else st.put(key, 1); } for (String s : st) StdOut.println(st.get(s) + " " + s); key type value type incrementing counter exploits fact that duplicates are not permitted calculate frequencies enhanced for loop (stay tuned) print results

6 Datasets Linguistic analysis. Compute word frequencies in a piece of text. File Description Words Distinct mobydick.txt Melville's Moby Dick 210,028 16,834 leipzig100k.txt 100K random sentences 2,121,054 144,256 leipzig200k.txt 200K random sentences 4,238,435 215,515 leipzig1m.txt 1M random sentences 21,191,455 534,580 Leipzing corpora collection 100K, 300K, and 1M sentences, randomly chosen from Newspaper articles Reference: Wortschatz corpus, Univesität Leipzig

7 Zipf's Law Linguistic analysis. Compute word frequencies in a piece of text. Zipf's law. In natural language, frequency of ith most common word is inversely proportional to i. % java Freq < mobydick.txt 4583 a 2 aback 2 abaft 3 abandon 7 abandoned 1 abandonedly 2 abandonment 2 abased 1 abasement 2 abashed 1 abate % java Freq < mobydick.txt | sort -rn 13967 the 6415 of 6247 and 4583 a 4508 to 4037 in 2911 that 2481 his 2370 it 1940 i 1793 but Moby Dick by Herman Melville (all lower case) e.g., most frequent word occurs about twice as often as second most frequent one

8 Zipf's Law Linguistic analysis. Compute word frequencies in a piece of text. Zipf's law. In natural language, frequency of ith most common word is inversely proportional to i. % java Freq < leipzig1m.txt | sort -rn the of to a and in for The that is said on was by e.g., most frequent word occurs about twice as often as second most frequent one

9 Symbol Table: Elementary Implementations
Unsorted array. Put: add key to the end (if not already there). Get: scan through all keys to find desired value. Sorted array. Put: find insertion point, and shift all larger keys right. Get: binary search to find desired key. 32 26 47 82 4 20 58 56 14 6 55 recall: binary search covered in sorting/searching lecture 4 6 14 20 26 32 47 55 56 58 82 4 6 14 20 26 28 32 47 55 56 58 82 insert 28

10 Symbol Table: Implementations Cost Summary
Unordered array. Hopelessly slow for large inputs. Ordered array. Acceptable if many more searches than inserts; too slow if many inserts. Challenge. Make all ops logarithmic. Running Time Frequency Count implementation get put Moby 100K 200K 1M unordered array N N 170 sec 4.1 hr - - ordered array log N N 5.8 sec 5.8 min 15 min 2.1 hr

11 Binary Search Trees Reference: Knuth, The Art of Computer Programming

12 Binary Search Trees Def. A binary search tree is a binary tree in symmetric order. Binary tree is either: Empty. A key-value pair and two binary trees. Symmetric order. Keys in left subtree are smaller than parent. Keys in right subtree are larger than parent. hi (values hidden) at no do if pi we suppress values from figures be go me of we node x A B smaller keys larger keys

13 BST Search

14 BST Insert

15 BST Construction

16 Binary Search Tree: Java Implementation
To implement: use two links per Node. A Node is comprised of: A key. A value. A reference to the left subtree. A reference to the right subtree. private class Node { private Key key; private Val val; private Node left; private Node right; } root

17 BST: Skeleton BST. Allow generic keys and values.
requires Key to provide compareTo() method; see book for details public class BST<Key extends Comparable<Key>, Val> { private Node root; // root of the BST private class Node { private Key key; private Val val; private Node left, right; private Node(Key key, Val val) { this.key = key; this.val = val; } public void put(Key key, Val val) { … } public Val get(Key key) { … } public boolean contains(Key key) { … } the extends Comparable means that Key must implement the Comparable interface, which means that it must have a method compareTo()

18 BST: Search Get. Return val corresponding to given key, or null if no such key. public Val get(Key key) { return get(root, key); } private Val get(Node x, Key key) { if (x == null) return null; int cmp = key.compareTo(x.key); if (cmp < 0) return get(x.left, key); else if (cmp > 0) return get(x.right, key); else if (cmp > 0) return x.val; public boolean contains(Key key) { return (get(key) != null); negative if less, zero if equal, positive if greater Iterative version not hard to write either. Use GrowingTree WebStart application to demo

19 BST: Insert Put. Associate val with key. Search, then insert.
Concise (but tricky) recursive code. public void put(Key key, Val val) { root = insert(root, key, val); } private Node insert(Node x, Key key, Val val) { if (x == null) return new Node(key, val); int cmp = key.compareTo(x.key); ifse if (cmp < 0) x.left = insert(x.left, key, val); else if (cmp > 0) x.right = insert(x.right, key, val); else x.val = val; return x; Use GrowingTree WebStart application to demo iterative version more cumbersome, but not difficult overwrite old value with new value

20 BST Implementation: Practice
Bottom line. Difference between a practical solution and no solution. Running Time Frequency Count implementation get put Moby 100K 200K 1M unordered array N N 170 sec 4.1 hr - - ordered array log N N 5.8 sec 5.8 min 15 min 2.1 hr BST ? ? .95 sec 7.1 sec 14 sec 69 sec

21 BST: Analysis Running time per put/get.
There are many BSTs that correspond to same set of keys. Cost is proportional to depth of node. number of nodes on path from root to node depth = 1 hi be depth = 2 at no at no depth = 3 do if pi go pi depth = 4 be go me of we do if of we depth = 5 hi me

22 BST: Analysis Best case. If tree is perfectly balanced, depth is at most lg N. average depth => inserting N keys at random takes 2 N ln N comparisons (just like quicksort) variance of height is O(1) so extremely unlikely that height is far away from mean

23 BST: Analysis Worst case. If tree is unbalanced, depth is N.
average depth => inserting N keys at random takes 2 N ln N comparisons (just like quicksort) variance of height is O(1) so extremely unlikely that height is far away from mean

24 BST: Analysis Average case. If keys are inserted in random order, average depth is 2 ln N. average depth => inserting N keys at random takes 2 N ln N comparisons (just like quicksort) variance of height is O(1) so extremely unlikely that height is far away from mean

25 Symbol Table: Implementations Cost Summary
BST. Logarithmic time ops if keys inserted in random order. Q. Can we guarantee logarithmic performance? Running Time Frequency Count implementation get put Moby 100K 200K 1M unordered array N N 170 sec 4.1 hr - - ordered array log N N 5.8 sec 5.8 min 15 min 2.1 hr BST log N † log N † .95 sec 7.1 sec 14 sec 69 sec Q. What input(s) makes BST unbalanced? † assumes keys inserted in random order

26 Red-Black Tree Red-black tree. A clever BST variant that guarantees depth  2 lg N. see COS 226 import java.util.TreeMap; import java.util.Iterator; public class ST<Key extends Comparable<Key>, Val> implements Iterable<Key> { private TreeMap<Key, Val> st = new TreeMap<Key, Val>(); public void put(Key key, Val val) { if (val == null) st.remove(key); else st.put(key, val); } public Val get(Key key) { return st.get(key); } public Val remove(Key key) { return st.remove(key); } public boolean contains(Key key) { return st.containsKey(key); } public Iterator<Key> iterator() { return st.keySet().iterator(); } Java red-black tree library implementation don't worry about details

27 Red-Black Tree Red-black tree. A clever BST variant that guarantees depth  2 lg N. see COS 226 Running Time Frequency Count implementation get put Moby 100K 200K 1M unordered array N N 170 sec 4.1 hr - - ordered array log N N 5.8 sec 5.8 min 15 min 2.1 hr BST log N † log N † .95 sec 7.1 sec 14 sec 69 sec red-black log N log N .95 sec 7.0 sec 14 sec 74 sec † assumes keys inserted in random order

28 Iteration

29 Inorder Traversal Inorder traversal. Recursively visit left subtree.
Visit node. Recursively visit right subtree. hi at no do if pi be go me of we inorder: at be do go hi if me no of pi we public inorder() { inorder(root); } private void inorder(Node x) { if (x == null) return; inorder(x.left); StdOut.println(x.key); inorder(x.right); } Remark: inorder traversal of BST yields keys in ascending order!

30 Enhanced For Loop Enhanced for loop. Enable client to iterate over items in a collection. ST<String, Integer> st = new ST<String, Integer>(); for (String s : st) { StdOut.println(st.get(s) + " " + s); }

31 Enhanced For Loop with BST
BST. Add following code to support enhanced for loop. see COS 226 for details import java.util.Iterator; import java.util.NoSuchElementException; public class BST<Key extends Comparable<Key>, Val> implements Iterable<Key> { private Node root; private class Node { … } public void put(Key key, Val val) { … } public Val get(Key key) { … } public boolean contains(Key key) { … } public Iterator<Key> iterator() { return new Inorder(); } private class Inorder implements Iterator<Key> { Inorder() { pushLeft(root); } public void remove() { throw new UnsupportedOperationException(); } public boolean hasNext() { return !stack.isEmpty(); } public Key next() { if (!hasNext()) throw new NoSuchElementException(); Node x = stack.pop(); pushLeft(x.right); return x.key; } public void pushLeft(Node x) { while (x != null) { stack.push(x); x = x.left; COS 126 students: not responsible for details of implementing Iterable, just how to use iterators

32 Symbol Table: Summary Symbol table. Quintessential database lookup data type. Choices. Ordered array, unordered array, BST, red-black, hash, …. Different performance characteristics. Java libraries: TreeMap, HashMap. Remark. Better symbol table implementation improves all clients.

33 Extra Slides

34 BST: Iterative Search Get. Return val corresponding to given key, or null if no such key. public Val get(Key key) { Node x = root; while (x != null) { int cmp = key.compareTo(x.key); if (cmp < 0) x = x.left; else if (cmp > 0) x = x.right; else return x.val; } return null; public boolean contains(Key key) { return (get(key) != null); Recursive version not hard to write either. Use GrowingTree WebStart application to demo

35 Preorder Traversal Preorder traversal. Visit node.
Recursively visit left subtree. Recursively visit right subtree. hi at no do if pi be go me of we preorder: hi at do be go no if me pi of we public preorder() { preorder(root); } private void preorder(Node x) { if (x == null) return; StdOut.println(x.key); preorder(x.left); preorder(x.right); } Remark: inorder traversal of BST yields keys in ascending order!

36 Postorder Traversal Postorder traversal.
Recursively visit left subtree. Recursively visit right subtree. Visit node. hi at no do if pi be go me of we postorder: be go do at me if of we pi no hi public postorder() { postorder(root); } private void postorder(Node x) { if (x == null) return; postorder(x.left); postorder(x.right); StdOut.println(x.key); } Remark: inorder traversal of BST yields keys in ascending order!

37 Set

38 Set API Set. Unordered collection of distinct keys.
Efficient implementation. Same as symbol table, but ignore value.

39 Dedup Application. Remove duplicates from an input.
public static void main(String[] args) { // create set of distinct words SET<String> set = new SET<String>(); while (!StdIn.isEmpty()) { String key = StdIn.readString(); set.add(key); } // print them out for (String s : set) { StdOut.println(s);

40 Set Client: Exception Filter
Exception filter. [spell check, spam blacklist, website filter, etc.] Read in a whitelist/blacklist of words from one file. Print out all words from stdin that aren't in list. public class ExceptionFilter { public static void main(String[] args) { SET<String> set = new SET<String>(); In in = new In(args[0]); while (!in.isEmpty()) set.add(in.readString()); while (!StdIn.isEmpty()) { String word = StdIn.readString(); if (!set.contains(word)) System.out.println(word); } Applications: removing duplicates in a commercial mailing list. Note: do not insert null as value since then get() would return null whether it found the key (since this is now its value) or not (default behavior when not found)

41 Inverted Index enter phone number into Google, get their home address
for DNS, the question of given URL finding the IP address is called "reverse DNS" to implement, exchange the role of key and value

42 Inverted Index Inverted index. Given a list of pages, preprocess them so that you can quickly find all pages containing a given query word. Ex 1. Book index. Ex 2. Web search engine index. Ex 3. File index (e.g, Spotlight). Symbol table. Key = query word. Value = set of pages. no duplicates

43 Inverted Index: Java Implementation
public class InvertedIndex { public static void main(String[] args) { ST<String, SET<String>> st = new ST<String, SET<String>>(); for (String filename : args) { In in = new In(filename); while (!in.isEmpty()) { String word = in.readString(); if (!st.contains(word)) st.put(word, new SET<String>()); st.get(word).add(filename); } while (!Stdin.isEmpty()) { String query = StdIn.readString(); StdOut.println(st.get(query)); build inverted index process queries

44 Inverted Index: Example
Ex. Index all your .java files. % java InvertedIndex *.java set DeDup.java ExceptionFilter.java InvertedIndex.java SET.java vector SparseVector.java SparseMatrix.java spotlight NOT FOUND

45 Inverted Index Extensions. Ignore case.
Ignore stopwords: the, on, of, … Boolean queries: set intersection (AND), set union (OR). Proximity search: multiple words must appear nearby. Record position and number of occurrences of word in document.

46 Other Types of Trees

47 Other Types of Trees Other types of trees. Family tree. root Charles
dad mom Philip Elizabeth II Central data structure in computer science. Arrays and linked lists capture order of elements, some applications require more structure. Trees capture hierarchical structures. Useful and versatile; Naturally recursive. Computer scientists draw root at top, genealogists at right, biologists at bottom. Use genealogical and arboreal terminology. Binary tree = a.k.a. "topological bifurcating arborescence"  Royal pedigree (ancestors of a given individual). Note: because of "in-breeding" it may not really be a tree.  Queen Victoria is great-great-great grandmother of Charles on both father's and mother's side! Why is ancestor tree never really a binary tree? If you go back n generations, you need 2^n people. Blows up. A family tree analysis indicates that President Bush, at left, and his front-running Democratic challenger, John Kerry, are 16th cousins, three times removed. Such links aren't all that unusual, genealogy buffs say. Andrew Alice George VI Elizabeth George I Olga Louis Victoria George V Mary Claude Celia

48 Other Types of Trees Other types of trees. Family tree.
Parse tree: represents the syntactic structure of a statement, sentence, or expression. + * 7 Note: demo takes a little while to fire up. Be patient, powerpoint is working hard.  10 12 (10 * 12) + 7

49 Other Types of Trees Other types of trees. Family tree. Parse tree.
Unix file hierarchy. / bin lib etc u aaclarke cos126 zrnye files grades submit parent may have more than two children sequence dsp tsp Point.java TSP.java tsp13509.txt

50 Other Types of Trees Other types of trees. Family tree. Parse tree.
Unix file hierarchy. Phylogeny tree. biologists draw their tree from left to right The phylogeny states that there was an ancestral species that gave rise to mammals and birds, but not to the other species shown in the tree (that is, mammals and birds share a common ancestor that they do not share with other species on the tree), that all animals are descended from an ancestor not shared with mushrooms, trees, and bacteria, and so on.

51 Other Types of Trees Other types of trees. Family tree. Parse tree.
Unix file hierarchy. Phylogeny tree. GUI containment hierarchy. parent may have more than two children Reference:

52 Other Types of Trees Other types of trees. Family tree. Parse tree.
Unix file hierarchy. Phylogeny tree. GUI containment hierarchy. Tournament trees. Argentinien 2 Deutschland 5 Frankreich 6 Italien 1 Niederlande 3 Polen 7 Spanien 4 USA 8 Reference: Tobias Lauer

53 America's Favorite Binary Tree
not a tree in 2003 because BYU would swap brackets if it won its first 3 games to avoid playing on Sunday

54

55 Binary Search Binary search. Examine the middle key.
If it matches, return its index. Otherwise, search either the left or right half. 6 13 14 25 33 43 51 53 64 72 84 93 95 96 97 1 2 3 4 5 6 7 8 9 10 11 12 13 14 lo mid hi public Val get(Key key) { int lo = 0, hi = N-1; while (lo <= hi) { int mid = lo + (hi - lo) / 2; int cmp = key.compareTo(keys[mid]); if (cmp < 0) hi = mid - 1; else if (cmp > 0) lo = mid + 1; else return vals[mid]; } return null;

56 Binary Search Binary search. Examine the middle key.
If it matches, return its index. Otherwise, search either the left or right half. Analysis. To binary search in an array of size N, need to do 1 comparison and binary search in an array of size N/2. N  N/2  N/4  N/8  …  1 Q. How many times can you divide a number by 2 until you reach 1? A. lg N. base 2 logarithm


Download ppt "4.4 Symbol Tables Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright ©"

Similar presentations


Ads by Google