Symbol Tables From “Algorithms” (4 th Ed.) by R. Sedgewick and K. Wayne.

Slides:



Advertisements
Similar presentations
1 Symbol Tables Chapter Sedgewick. 2 Symbol Tables Searching Searching is a fundamental element of many computational tasks looking up a name.
Advertisements

Searching for Data Relationship between searching and sorting Simple linear searching Linear searching of sorted data Searching for string or numeric data.
Problem Solving 5 Using Java API for Searching and Sorting Applications ICS-201 Introduction to Computing II Semester 071.
Effective Java, Chapter 3: Methods Common to All Objects.
1 Class Design CS 3331 Fall Outline  Organizing classes  Design guidelines  Canonical forms of classes equals method hashCode method.
Effective Java, Chapter 3: Methods Common to All Objects Paul Ammann.
Searching Kruse and Ryba Ch and 9.6. Problem: Search We are given a list of records. Each record has an associated key. Give efficient algorithm.
Searching. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an element in it static.
Searching and Sorting I 1 Searching and Sorting 1.
ICS201 Lecture 20 : Searching King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
hashing1 Hashing It’s not just for breakfast anymore!
1 Java Object Model Part 2: the Object class. 2 Object class Superclass for all Java classes Any class without explicit extends clause is a direct subclass.
Searching. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an element in it static.
Searching. Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an element in it static.
CSE 373 Data Structures and Algorithms Lecture 18: Hashing III.
Comparing Objects in Java. The == operator When you define an object, for instance Person p = new Person("John", 23); we talk about p as if its value.
1 CSC 222: Computer Programming II Spring 2005 Searching and efficiency  sequential search  big-Oh, rate-of-growth  binary search  example: spell checker.
Searching Also: Logarithms. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an.
Maps A map is an object that maps keys to values Each key can map to at most one value, and a map cannot contain duplicate keys KeyValue Map Examples Dictionaries:
Set, TreeSet, TreeMap, Comparable, Comparator. Def: The abstract data type set is a structure that holds objects and satifies ARC: Objects can be added.
CS-2851 Dr. Mark L. Hornick 1 Tree Maps and Tree Sets The JCF Binary Tree classes.
COSC 2007 Data Structures II
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
11: Holding Your Objects Introduction to containers Container disadvantage: unknown type Iterators Container taxonomy Collection functionality List functionality.
Searching Also: Logarithms. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an.
Hash Functions and the HashMap Class A Brief Overview On Green Marble John W. Benning.
CS212: DATA STRUCTURES Lecture 10:Hashing 1. Outline 2  Map Abstract Data type  Map Abstract Data type methods  What is hash  Hash tables  Bucket.
Week 6 - Friday.  What did we talk about last time?  Recursive running time  Fast exponentiation  Merge sort  Introduced the Master theorem.
Puzzle 3 1  Write the class Enigma, which extends Object, so that the following program prints false: public class Conundrum { public static void main(String[]
Non-static classes Part 2 1. Methods  like constructors, all non-static methods have an implicit parameter named this  for methods, this refers to the.
Arrays and ArrayLists in Java L. Kedigh. Array Characteristics List of values. A list of values where every member is of the same type. Each member in.
Copyright © 2002, Systems and Computer Engineering, Carleton University Hashtable.ppt * Object-Oriented Software Development Unit 8.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
90-723: Data Structures and Algorithms for Information Processing Copyright © 1999, Carnegie Mellon. All Rights Reserved. 1 Lecture 9: Searching Data Structures.
30 May Stacks (5.1) CSE 2011 Winter Stacks2 Abstract Data Types (ADTs) An abstract data type (ADT) is an abstraction of a data structure An.
Symbol Tables and Search Trees CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
13-1 Sets, Bags, and Tables Exam 1 due Friday, March 16 Wellesley College CS230 Lecture 13 Thursday, March 15 Handout #23.
HashCode() 1  if you override equals() you must override hashCode()  otherwise, the hashed containers won't work properly  recall that we did not override.
Author: Takdir, S.ST. © Sekolah Tinggi Ilmu Statistik.
“Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought.
Hash Tables. 2 Exercise 2 /* Exercise 1 */ void mystery(int n) { int i, j, k; for (i = 1; i
U n i v e r s i t y o f H a i l 1 ICS 202  2011 spring  Data Structures and Algorithms 
Effective Java: Methods Common to All Objects SWE 619: Fall 2008 Paul Ammann.
Java Type System and Object Model Horstmann ch , 7.7.
Hash Tables From “Algorithms” (4 th Ed.) by R. Sedgewick and K. Wayne.
1 Class Design CS 3331 Sec 6.1 & 6.3 of [Jia03]. 2 Outline  Organizing classes  Design guidelines  Canonical forms of classes equals method hashCode.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Relations.
Week 15 – Wednesday.  What did we talk about last time?  Review up to Exam 1.
1 i206: Lecture 12: Hash Tables (Dictionaries); Intro to Recursion Marti Hearst Spring 2012.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 14 Searching and Sorting.
Chapter 8 Searching and Sorting © 2006 Pearson Education Inc., Upper Saddle River, NJ. All rights reserved.
Dictionaries CS 110: Data Structures and Algorithms First Semester,
COMP 110 Some notes on inheritance, review Luv Kohli December 1, 2008 MWF 2-2:50 pm Sitterson 014.
CSE 373: Data Structures and Algorithms Lecture 16: Hashing III 1.
Implementing the Map ADT.  The Map ADT  Implementation with Java Generics  A Hash Function  translation of a string key into an integer  Consider.
Building Java Programs Generics, hashing reading: 18.1.
Data Structures and Algorithms I
More on Objects Static and equals.. Each object takes memory Each time you make an object of a given class – you create a space in memory. E.g. public.
Searching.
CSC 222: Object-Oriented Programming
Data Structures and Algorithms for Information Processing
COMP 110 Some notes on inheritance, review
Non-static classes.
"He's off the map!" - Eternal Sunshine of the Spotless Mind
Effective Java, Chapter 3, 3rd Edition: Methods Common to All Objects
Searching.
Review for Midterm 3.
Data Structures and Algorithms I
Chapter 7 Java Object Model
Presentation transcript:

Symbol Tables From “Algorithms” (4 th Ed.) by R. Sedgewick and K. Wayne

・ Given a key, search for the corresponding value. ・ Given domain name, find corresponding IP address. Symbol tables Key-value pair abstraction. ・ Insert a value with specified key. Ex. DNS lookup. ・ Insert domain name with specified IP address. domain name IP address key value

Symbol table applications applicationpurpose of search key value dictionary find definition word definition book index find relevant pages term list of page numbers file share find song to download name of song computer ID financial account process transactions account number transaction details web search find relevant web pages keyword list of page names compilerfind properties of variables variable name type and value routing table route Internet packets destination best route DNS find IP address domain name IP address reverse DNS find domain name IP address domain name genomics find markers DNA string known positions file system find file on disk filename location on disk

・ Method get() returns null if key not present. Conventions ・ Values are not null. ・ Method put() overwrites old value with new value. Intended consequences. ・ Easy to implement contains(). public boolean contains(Key key) { return get(key) != null; } ・ Can implement lazy version of delete(). public void delete(Key key) { put(key, null); }

・ Assume keys areareanygenerictype,use equals() totestequality. ・ Mutable in Java: StringBuilder, java.net.URL, arrays,... Keys and values Value type. Any generic type. specify Comparable in API. Key type: several natural assumptions. ・ Assume keys are Comparable, use compareTo(). ・ Assume keys are any generic type, use equals() to test equality; use hashCode() to scramble key. built-in to Java Best practices. Use immutable types for symbol table keys. ・ Immutable in Java: Integer, Double, String, java.io.File, …

・ Symmetric: ・ Non-null: x.equals(y) iff y.equals(x). x.equals(null) is false. Equality test All Java classes inherit a method equals(). Java requirements. For any references x, y and z : ・ Reflexive: x.equals(x) is true. ・ Transitive: if x.equals(y) and y.equals(z), then x.equals(z). do x and y refer to the same object? Default implementation. (x == y) Customized implementations. Integer, Double, String, java.io.File, … User-defined implementations. Some care needed.

(would violate symmetry) private final int year; return false; if (this.month != that.month) return false; Implementing equals for user-defined types Seems easy, but requires some care. typically unsafe to use equals() with inheritance public final class Date implements Comparable { private final int month; private final int day;... public boolean equals(Object y) { if (y == this) return true; if (y == null) return false; if (y.getClass() != this.getClass()) Date that = (Date) y; if (this.day != that.day ) return false; if (this.year != that.year ) return false; return true; }

・ Check against null. ・ Compare eachsignificantfield: or Arrays.deepEquals(a, b), ・ Compare fields mostly likely todiffer first. Equals design "Standard" recipe for user-defined types. ・ Optimization for reference equality. ・ Check that two objects are of the same type and cast. – if field is a primitive type, use == – if field is an object, use equals() apply rule recursively – if field is an array, apply to each entry alternatively, use Arrays.equals(a, b) but not a.equals(b) Best practices. ・ No need to use calculated fields that depend on other fields. ・ Make compareTo() consistent with equals(). x.equals(y) if and only if (x.compareTo(y) == 0)

iteration?interface (unordered list) NNN / 2N no equals() Elementary ST implementations: summary worst-case cost average case ST implementation (after N inserts) (after N random inserts) ordered key search insert search hit insert sequential search Challenge. Efficient implementations of both search and insert.

Binary search: Java implementation public Value get(Key key) { if (isEmpty()) return null; int i = rank(key); if (i < N && keys[i].compareTo(key) == 0) return vals[i]; else return null; } private int rank(Key key) number of keys < 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 if (cmp == 0) return mid; } return lo; }

iteration?interface (unordered list) NNN / 2N no equals() (ordered array) log NN N / 2 yes compareTo() Elementary ST implementations: summary worst-case cost average case ST implementation (after N inserts) (after N random inserts) ordered key search insert search hit insert sequential search binary search Challenge. Efficient implementations of both search and insert. 22