Docsity

Symbol Table-Programming and Data Storage and Processes-Lecture Slides, Slides of Computer Programming

Toggle sidebar
bg1
4.4 Symbol Tables
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.
key value
www.cs.princeton.edu
URL IP address
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
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
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 IP address URL
book index find relevant pages keyword 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 filename location on disk
routing table route Internet packets destination best route
4
Symbol Table API
Bob 1234
Dave 9876
Carl 5665
Alice 5665
key value
symbol table
stores a set of
key-value pairs
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Symbol Table-Programming and Data Storage and Processes-Lecture Slides and more Slides Computer Programming in PDF only on Docsity!

4.4 Symbol Tables

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.

key (^) value www.cs.princeton.edu URL IP address 128.112.136. www.princeton.edu 128.112.128. www.yale.edu 130.132.143. www.harvard.edu 128.103.060. www.simpsons.com 209.052.165. 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 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 IP address URL book index find relevant pages keyword 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 filename location on disk routing table route Internet packets destination best route 4

Symbol Table API

Bob 1234 Dave 9876 Carl 5665 Alice 5665 key value symbol table stores a set of key-value pairs

5 Symbol Table API Bob 1234 Dave 9876 Carl 5665 Alice 2927 Zeke 1001 put(“Zeke”, 1001) adds key-value pair 6 Symbol Table API Bob 1234 Dave 9876 Carl 5665 Alice 2927 Zeke 1001 get(“Alice”) returns 5665 7 Symbol Table API Bob 1234 Dave 9876 Carl 5665 Alice 2927 Zeke 1001 contains(“Alice”) returns true contains(“Kevin”) returns false 8 Symbol Table API Bob 2927 Dave 9876 Carl 5665 Alice 2927 Zeke 1001 put(“Bob”, 2927) changes Bob’s value associative array notation st[“Bob”] = 2927 is legal in some languages (not Java)

13

Symbol Table: Elementary Implementations

Unordered array.

 Put: add key to the end (if not already there).

 Get: scan through all keys to find desired value.

Ordered 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 4 6 14 20 26 32 47 55 56 58 82 (^4 614 20 26 28 32 47 55 56 58 82) insert 28 14

Binary Search: Mathematical Analysis

Analysis. To binary search in an array of size N : do one compare,

then 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. log 2 N.

15

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.

Symbol Table: Implementations Cost Summary

Running Time Frequency Count implementation get put Moby 100K 200K 1M unordered array ordered array

N

log N

N

N

170 sec 5.8 sec 4.1 hr 5.8 min

15 min

2.1 hr too slow (N^2 to build table) doubling test (quadratic) Reference: Knuth, The Art of Computer Programming

Binary Search Trees

17

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.

Binary Search Trees A smaller keys

B

larger keys x node hi at no do if pi be go me of we we suppress values from figures (values hidden) 18 BST Search 19 BST Insert 20 BST Construction

25 BST Implementation: Practice

Bottom line. Difference between a practical solution and no solution.

Running Time BST?? Frequency Count implementation get put Moby 100K 200K 1M .95 sec 7.1 sec 14 sec 69 sec unordered array ordered array

N

log N

N

N

170 sec 5.8 sec 4.1 hr 5.8 min

15 min

2.1 hr 26 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.

we be at no go pi do if of hi me hi at no do if pi be go me of we number of nodes on path from root to node depth = 4 depth = 3 depth = 2 depth = 5 depth = 1 27 BST: Analysis

Best case. If tree is perfectly balanced, depth is at most lg N.

28 BST: Analysis

Worst case. If tree is unbalanced, depth is N.

29 BST: Analysis

Average case. If keys are inserted in random order, trees stay flat.

30 BST: Analysis

Average case. If keys are inserted in random order,

average depth is 2 ln N.

requires proof (see COS 226) 31 Symbol Table: Implementations Cost Summary

BST. Logarithmic time ops if keys inserted in random order.

Q. Can we guarantee logarithmic performance?

Running Time BST log N †^ log N † Frequency Count † assumes keys inserted in random order implementation get put Moby 100K 200K 1M .95 sec 7.1 sec 14 sec 69 sec unordered array ordered array

N

log N

N

N

170 sec 5.8 sec 4.1 hr 5.8 min

15 min

2.1 hr 32 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, Value> implements Iterable { private TreeMap<Key, Value> st = new TreeMap<Key, Val>(); public void put(Key key, Value val) { if (val == null) st.remove(key); else st.put(key, val); } public Value get(Key key) { return st.get(key); } public Value remove(Key key) { return st.remove(key); } public boolean contains(Key key) { return st.containsKey(key); } public Iterator iterator() { return st.keySet().iterator(); } } Java red-black tree library implementation

37 Enhanced For Loop with BST

BST. Add following code to support enhanced for loop.

import java.util.Iterator; import java.util.NoSuchElementException; public class BST<Key extends Comparable, Value> implements Iterable { private Node root; private class Node { … } public void put(Key key, Value val) { … } public Value get(Key key) { … } public boolean contains(Key key) { … } public Iterator iterator() { return new Inorder(); } private class Inorder implements Iterator { 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; } } } } see COS 226 for details 38 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.