Elementary Symbol Tables-Data Representation And Algorithm Design-Lecture Slides, Slides of Data Representation and Algorithm Design

This lecture was delivered by Dr. Ameet Shashank at B R Ambedkar National Institute of Technology. Its relate to Data Representation and Algorithm Design course. Its main points are: Elementary, Symbol, tables, DNS, URL, IP, Frequency, Counter, API, Associative, Array, Abstraction

Typology: Slides

2011/2012

Uploaded on 07/15/2012

saandeep
saandeep 🇮🇳

4.5

(6)

99 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Elementary Symbol Tables
2
Symbol Table
Symbol table: key-value pair abstraction.
!Insert a value with specified key.
!Given a key, search for the corresponding value.
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 Client: DNS Lookup
DNS lookup client program.
!st.put(key, val) inserts a key-value pair into symbol table.
!st.get(key) searches for the given key and returns the value.
public static void main(String[] args) {
ST<String, String> st = new ST<String, String>();
st.put("www.cs.princeton.edu", "128.112.136.11 ");
st.put("www.princeton.edu", "128.112.128.1 5");
st.put("www.yale.edu", "130.132.143.2 1");
st.put("www.simpsons.com", "209.052.165. 60");
System.out.println(st.get("www.cs.princeton.edu"));
System.out.println(st.get("www.harvardsucks.com "));
System.out.println(st.get("www.simpsons.com"));
}
128.112.136.11
null
209.052.165.60
st["www.simpsons.com"] = "209.052.165.60"
st["www.simpsons.com"]
docsity.com
pf3
pf4
pf5

Partial preview of the text

Download Elementary Symbol Tables-Data Representation And Algorithm Design-Lecture Slides and more Slides Data Representation and Algorithm Design in PDF only on Docsity!

Elementary Symbol Tables

2

Symbol Table

Symbol table: key-value pair abstraction.

! Insert a value with specified key.

! Given a key, search for the corresponding value.

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 Client: DNS Lookup

DNS lookup client program.

! st.put(key, val) inserts a key-value pair into symbol table.

! st.get(key) searches for the given key and returns the value.

public static void main(String[] args) { ST<String, String> st = new ST<String, String>(); st.put("www.cs.princeton.edu", " 128. 112. 136. 11 "); st.put("www.princeton.edu", " 128. 112. 128. 15 "); st.put("www.yale.edu", " 130. 132. 143. 21 "); st.put("www.simpsons.com", " 209. 052. 165. 60 "); System.out.println(st.get("www.cs.princeton.edu")); System.out.println(st.get("www.harvardsucks.com")); System.out.println(st.get("www.simpsons.com")); } 128.112.136. null 209.052.165. st["www.simpsons.com"] = "209.052.165.60" st["www.simpsons.com"]

5

Symbol Table Client: Frequency Counter

Frequency counter. [e.g., web traffic 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 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) System.out.println(st.get(s) + " " + s); } calculate frequencies print results 6

Symbol Table API

Associative array abstraction. Unique value associated with each key.

Symbol table API.

! put(key, val) insert the key-value pair

! get(key) return value associated with given key

! remove(key) remove the key

! contains(key) is given key present?

! iterator() return iterator over all keys

Our conventions.

! Values are not null.

! Method get() return null if key not present.

! Method put() overwrites old value with new value.

Keys and Values

Key type.

! Some implementations assumes keys are a generic Comparable type,

and use the compareTo() method.

! Other implementations assume keys are any generic type,

and use the equals() and hashCode() methods.

Value type. Any generic type.

Best practices. Use immutable types for symbol table keys.

! Immutable in Java: String, Integer, BigInteger.

! Mutable in Java: Date, GregorianCalendar, StringBuilder.

Sorted Array Implementation

13

Equals

Equivalence relation. For any references x, y and z:

! Reflexive: x.equals(x) is true.

! Symmetric: x.equals(y) iff y.equals(x).

! Transitive: If x.equals(y) and y.equals(z), then x.equals(z).

! Non-null: x.equals(null) is false.

! Consistency: Multiple calls to x.equals(y) return same value.

Default implementation. (x == y)

Customized implementations. String, URL, Integer.

User-defined implementations. Some care needed.

14 public final class WrongPhoneNumber { private final int area; // 609 private final int exch; // 867 private final int ext; // 5309 public WrongPhoneNumber(int area, int exch, int ext) { this.area = area; this.exch = exch; this.ext = ext; } public boolean equals(WrongPhoneNumber b) { WrongPhoneNumber a = this; return (a.ext == b.ext) && (a.exch == b.exch) && (a.area == b.area); } }

Implementing Equals: US Phone Numbers

Phone numbers. (609) 867-5309.

broken implementation of equals (won't get called by java.util.HashMap) helps enforce immutability 15 public final class PhoneNumber { private final int area, exch, ext; public PhoneNumber(int area, int exch, int ext) { this.area = area; this.exch = exch; this.ext = ext; } public boolean equals(Object y) { if (y == this) return true; if (y == null) return false; if (y.getClass() != this.getClass()) return false; PhoneNumber a = this; PhoneNumber b = (PhoneNumber) y; return (a.area == b.area) && (a.exch == b.exch) return (a.area == b.area) && (a.ext == b.ext); } }

Implementing Equals: US Phone Numbers

Phone numbers. (609) 867-5309.

can't throw an exception must be Object, not PhoneNumber 16

Best Practices

Comparable. If class implements Comparable interface,

make equals() consistent with compareTo().

Hashcode. If class overrides equals(), also override hashCode().

stay tuned

Linked List Implementation

18

Symbol Table: Linked List Implementation

Maintain a linked list of key-value pairs.

! Insert new key-value pair at beginning of list.

! Use exhaustive search to find a given key.

www.princeton.edu 128.112.128. www.yale.edu 130.132.143. www.harvard.edu 128.103.060. null

key value next first 19

Symbol Table: Linked List Implementation

public class ListST<Key, Val> implements Iterable { private Node first; private class Node { Key key; Val val; Node next; Node(Key key, Val val, Node next) { this.key = key; this.val = val; this.next = next; } } public Iterator iterator() { return new ListIterator(); } (^) similar to Sequence iterator 20

Symbol Table: Linked List Implementation (cont)

public Val get(Key key) { for (Node x = first; x != null; x = x.next) if (key.equals(x.key)) return x.val; return null; } public void put(Key key, Val val) { for (Node x = first; x != null; x = x.next) { if (key.equals(x.key)) { x.val = val; return; } } first = new Node(key, val, first); } } if key is already present, replace value