cs2010: algorithms and data structures



Similar documents
dictionary find definition word definition book index find relevant pages term list of page numbers

DNS LOOKUP SYSTEM DATA STRUCTURES AND ALGORITHMS PROJECT REPORT

Hashing. hash functions collision resolution applications. References: Algorithms in Java, Chapter 14.

4.5 Symbol Table Applications

4.2 Sorting and Searching

CS 111 Classes I 1. Software Organization View to this point:

LINKED DATA STRUCTURES

API for java.util.iterator. ! hasnext() Are there more items in the list? ! next() Return the next item in the list.

Stacks. Data Structures and Data Types. Collections

6 March Array Implementation of Binary Trees

STORM. Simulation TOol for Real-time Multiprocessor scheduling. Designer Guide V3.3.1 September 2009

Basic Programming and PC Skills: Basic Programming and PC Skills:

Moving from CS 61A Scheme to CS 61B Java

CSE 2123 Collections: Sets and Iterators (Hash functions and Trees) Jeremy Morris

Searching Algorithms

Algorithms and Data Structures Written Exam Proposed SOLUTION

CompuScholar, Inc. Alignment to Utah's Computer Programming II Standards

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Thomas Jefferson High School for Science and Technology Program of Studies Foundations of Computer Science. Unit of Study / Textbook Correlation

Outline. Computer Science 331. Stack ADT. Definition of a Stack ADT. Stacks. Parenthesis Matching. Mike Jacobson

Analysis of Binary Search algorithm and Selection Sort algorithm

AP Computer Science Java Subset

2! Multimedia Programming with! Python and SDL

PostgreSQL Functions By Example

Project 4 DB A Simple database program

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

COSC 6397 Big Data Analytics. Mahout and 3 rd homework assignment. Edgar Gabriel Spring Mahout

1.4 Arrays Introduction to Programming in Java: An Interdisciplinary Approach Robert Sedgewick and Kevin Wayne Copyright /6/11 12:33 PM!

Java Map and Set collections

CS 2112 Spring Instructions. Assignment 3 Data Structures and Web Filtering. 0.1 Grading. 0.2 Partners. 0.3 Restrictions

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Data Structures in the Java API

Binary Search Trees. basic implementations randomized BSTs deletion in BSTs

Computers. An Introduction to Programming with Python. Programming Languages. Programs and Programming. CCHSG Visit June Dr.-Ing.

Binary Heap Algorithms

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Chapter 14 The Binary Search Tree

This lecture. Abstract data types Stacks Queues. ADTs, Stacks, Queues Goodrich, Tamassia

Zabin Visram Room CS115 CS126 Searching. Binary Search

Binary Trees and Huffman Encoding Binary Search Trees

Lecture 9. Semantic Analysis Scoping and Symbol Table

Introduction to Java

Introduction to Programming System Design. CSCI 455x (4 Units)

: provid.ir

Analysis of a Search Algorithm

Computer Programming I

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Binary Heaps. CSE 373 Data Structures

CSCI 3136 Principles of Programming Languages

The Tower of Hanoi. Recursion Solution. Recursive Function. Time Complexity. Recursive Thinking. Why Recursion? n! = n* (n-1)!

Getting Started with the Internet Communications Engine

Data Structures Fibonacci Heaps, Amortized Analysis

Problem 1. CS 61b Summer 2005 Homework #2 Due July 5th at the beginning of class

Raima Database Manager Version 14.0 In-memory Database Engine

Object Oriented Software Design

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

7.1 Our Current Model

D06 PROGRAMMING with JAVA

The C Programming Language course syllabus associate level

Getting Started with Telerik Data Access. Contents

Certified PHP/MySQL Web Developer Course

Vim, Emacs, and JUnit Testing. Audience: Students in CS 331 Written by: Kathleen Lockhart, CS Tutor

AP Computer Science Java Mr. Clausen Program 9A, 9B

Physical Design. Meeting the needs of the users is the gold standard against which we measure our success in creating a database.

Collections in Java. Arrays. Iterators. Collections (also called containers) Has special language support. Iterator (i) Collection (i) Set (i),

1.00 Lecture 1. Course information Course staff (TA, instructor names on syllabus/faq): 2 instructors, 4 TAs, 2 Lab TAs, graders

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

Sources: On the Web: Slides will be available on:

Tables so far. set() get() delete() BST Average O(lg n) O(lg n) O(lg n) Worst O(n) O(n) O(n) RB Tree Average O(lg n) O(lg n) O(lg n)

Object Oriented Software Design

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Software Engineering Techniques

Ordered Lists and Binary Trees

American International Journal of Research in Science, Technology, Engineering & Mathematics

Basics of Java Programming Input and the Scanner class

Vector storage and access; algorithms in GIS. This is lecture 6

Exercise 1: Python Language Basics

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

Programming Languages CIS 443

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

El Dorado Union High School District Educational Services

6.170 Tutorial 3 - Ruby Basics

Habanero Extreme Scale Software Research Project

Java Application Developer Certificate Program Competencies

Glossary of Object Oriented Terms

Pemrograman Dasar. Basic Elements Of Java

Minimum Spanning Trees

[1] Learned how to set up our computer for scripting with python et al. [3] Solved a simple data logistics problem using natural language/pseudocode.

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

The Sun Certified Associate for the Java Platform, Standard Edition, Exam Version 1.0

JDK 1.5 Updates for Introduction to Java Programming with SUN ONE Studio 4

Symbol Tables. Introduction

Class 32: The Java Collections Framework

Chapter 7: Sequential Data Structures

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015

Introduction to Python

CompSci-61B, Data Structures Final Exam

Java the UML Way: Integrating Object-Oriented Design and Programming

Transcription:

cs2010: algorithms and data structures Lecture 11: Symbol Table ADT. Vasileios Koutavas 28 Oct 2015 School of Computer Science and Statistics Trinity College Dublin

Algorithms ROBERT SEDGEWICK KEVIN WAYNE 3.1 SYMBOL TABLES Algorithms F O U R T H E D I T I O N API elementary implementations ordered operations ROBERT SEDGEWICK KEVIN WAYNE http://algs4.cs.princeton.edu

3.1 SYMBOL TABLES Algorithms API elementary implementations ordered operations ROBERT SEDGEWICK KEVIN WAYNE http://algs4.cs.princeton.edu

Symbol tables Key-value pair abstraction. Insert a value with specified key. Given a key, search for the corresponding value. Ex. DNS lookup. Insert domain name with specified IP address. Given domain name, find corresponding IP address. domain name IP address www.cs.princeton.edu 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 key value 3

Symbol table applications application purpose 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 compiler find 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 4

Symbol tables: context Also known as: maps, dictionaries, associative arrays. Generalizes arrays. Keys need not be between 0 and N 1. Language support. External libraries: C, VisualBasic, Standard ML, bash,... Built-in libraries: Java, C#, C++, Scala,... Built-in to language: Awk, Perl, PHP, Tcl, JavaScript, Python, Ruby, Lua. every array is an associative array every object is an associative array table is the only primitive data structure hasnicesyntaxforassociativearrays["python"] = true hasnicesyntaxforassociativearrays["java"] = false legal Python code 5

Basic symbol table API Associative array abstraction. Associate one value with each key. public class ST<Key, Value> ST() create an empty symbol table void put(key key, Value val) put key-value pair into the table Value get(key key) value paired with key a[key] = val; a[key] boolean contains(key key) is there a value paired with key? void delete(key key) remove key (and its value) from table boolean isempty() is the table empty? int size() number of key-value pairs in the table Iterable<Key> keys() all the keys in the table 6

Conventions Values are not null. Java allows null value Method get() returns null if key not present. 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); } 7

Keys and values Value type. Any generic type. Key type: several natural assumptions. Assume keys are Comparable, use compareto(). Assume keys are any generic type, use equals() to test equality. Assume keys are any generic type, use equals() to test equality; use hashcode() to scramble key. specify Comparable in API. built-in to Java (stay tuned) Best practices. Use immutable types for symbol table keys. Immutable in Java: Integer, Double, String, java.io.file, Mutable in Java: StringBuilder, java.net.url, arrays,... 8

Equality test All Java classes inherit a method equals(). Java requirements. 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. equivalence relation Default implementation. (x == y) do x and y refer to the same object? Customized implementations. Integer, Double, String, java.io.file, User-defined implementations. Some care needed. 9

Implementing equals for user-defined types Seems easy. public class Date implements Comparable<Date> { private final int month; private final int day; private final int year;... public boolean equals(date that) { } } if (this.day!= that.day ) return false; if (this.month!= that.month) return false; if (this.year!= that.year ) return false; return true; check that all significant fields are the same 10

Implementing equals for user-defined types Seems easy, but requires some care. public final class Date implements Comparable<Date> { private final int month; private final int day; private final int year;.... @Override. public boolean equals(object y) { if (y == this) return true; typically unsafe to use equals() with inheritance (would violate symmetry).use @Override annotation http://docs.oracle.com/javase /tutorial/java/annotations/predefined.html must be Object. Why? Experts still debate. optimize for true object equality if (y == null) return false; if (y.getclass()!= this.getclass()) return false; check for null objects must be in the same class (religion: getclass() vs. instanceof) } } Date that = (Date) y; if (this.day!= that.day ) return false; if (this.month!= that.month) return false; if (this.year!= that.year ) return false; return true; cast is guaranteed to succeed check that all significant fields are the same 11

Equals design "Standard" recipe for user-defined types. Optimization for reference equality. Check against null. Check that two objects are of the same type and cast. Compare each significant field: if field is a primitive type, use == if field is an object, use equals() if field is an array, apply to each entry but use Double.compare() with double (or otherwise deal with -0.0 and NaN) apply rule recursively can use Arrays.deepEquals(a, b) but not a.equals(b) Best practices. No need to use calculated fields that depend on other fields. Compare fields mostly likely to differ first. Make compareto() consistent with equals(). e.g., cached Manhattan distance x.equals(y) if and only if (x.compareto(y) == 0) 12

ST test client for traces Build ST by associating value i with i th string from standard input. public static void main(string[] args) { ST<String, Integer> st = new ST<String, Integer>(); } for (int i = 0;!StdIn.isEmpty(); i++) { keys String key = StdIn.readString(); values st.put(key, i); } for (String s : st.keys()) StdOut.println(s + " " + st.get(s)); S E A R C H E X A M P L 0 1 2 3 4 5 6 7 8 9 10 11 1 output for basic symbol table (one possibility) L 11 P 10 M 9 X 7 H 5 C 4 keys S E A R C H E X A M P L E R 3 values 0 1 2 3 4 5 6 7 8 9 10 11 12A 8 output for basic symbol table (one possibility) output for ordered symbol table A 8 E 12 S 0 output output for ordered symbol table A 8 C 4 E 12 H 5 L 11 M 9 P 10 R 3 S 0 X 7 Keys, values, and output for test client 13

ST test client for analysis Frequency counter. Read a sequence of strings from standard input and print out one that occurs with highest frequency. % more tinytale.txt it was the best of times it was the worst of times it was the age of wisdom it was the age of foolishness it was the epoch of belief it was the epoch of incredulity it was the season of light it was the season of darkness it was the spring of hope it was the winter of despair % java FrequencyCounter 1 < tinytale.txt it 10 % java FrequencyCounter 8 < tale.txt business 122 % java FrequencyCounter 10 < leipzig1m.txt government 24763 tiny example (60 words, 20 distinct) real example (135,635 words, 10,769 distinct) real example (21,191,455 words, 534,580 distinct) 14

Frequency counter implementation public class FrequencyCounter { public static void main(string[] args) { int minlen = Integer.parseInt(args[0]); ST<String, Integer> st = new ST<String, Integer>(); while (!StdIn.isEmpty()) { String word = StdIn.readString(); ignore short strings if (word.length() < minlen) continue; if (!st.contains(word)) st.put(word, 1); else st.put(word, st.get(word) + 1); } String max = ""; st.put(max, 0); for (String word : st.keys()) if (st.get(word) > st.get(max)) max = word; StdOut.println(max + " " + st.get(max)); } } create ST read string and update frequency print a string with max freq 15

3.1 SYMBOL TABLES Algorithms API elementary implementations ordered operations ROBERT SEDGEWICK KEVIN WAYNE http://algs4.cs.princeton.edu

Sequential search in a linked list Data structure. Maintain an (unordered) linked list of key-value pairs. Search. Scan through all keys until find a match. Insert. Scan through all keys until find a match; if no match add to front. key value S 0 E 1 A 2 R 3 C 4 H 5 E 6 X 7 A 8 M 9 P 10 L 11 E 12 first S 0 E 1 A 2 R 3 C 4 H 5 H 5 X 7 X 7 M 9 P 10 L 11 L 11 red nodes are new S 0 E 1 A 2 R 3 C 4 C 4 H 5 H 5 X 7 S 0 E 1 A 2 R 3 R 3 C 4 C 4 H 5 S 0 E 1 A 2 A 2 R 3 R 3 C 4 black nodes are accessed in search S 0 E 1 E 6 A 2 A 8 R 3 S 0 S 0 E 6 E 6 A 8 circled entries are changed values S 0 S 0 E 6 gray nodes are untouched S 0 M 9 X 7 H 5 C 4 R 3 A 8 E 6 S 0 P 10 M 9 X 7 H 5 C 4 R 3 A 8 E 6 S 0 P 10 M 9 X 7 H 5 C 4 R 3 A 8 E 12 S 0 Trace of linked-list ST implementation for standard indexing client 17

Elementary ST implementations: summary ST implementation guarantee average case search insert search hit insert key interface sequential search (unordered list) N N N / 2 N equals() Challenge. Efficient implementations of both search and insert. 18

Binary search in an ordered array Data structure. Maintain an ordered array of key-value pairs. Rank helper function. How many keys < k? keys[] successful search for P 0 1 2 3 4 5 6 7 8 9 keys[] lo hi m A C E H L M P R S X successful successful search 0 search for 9 for P 4 P A 0 C 1 E 2 H 3 L 4 M 5 P 6 R 7 S 8 X9 entries in black keys[] are a[lo..hi] lo 5 hi 9 m7 A C E H L M P R S X successful search for P 0 1 2 3 4 5 6 7 8 9 50 69 54 A C E H L M P R S X entries in black lo hi m are a[lo..hi] entry in red is a[m] 65 69 67 A C E H L M P R S X 0 4 entries in black 5 6 5 A C E H L M P R unsuccessful search for Q loop S exits X with are a[lo..hi] 9 7 keys[m] = P: return 6 entry in red is a[m] lo 6 hi 6 m 6 A C E H L M P R S X 5 5 unsuccessful 0 search 9 4 for Q loop exits with entry keys[m] in red is = a[m] P: return 6 6 6 6 A C E H L M P R S X unsuccessful 5 9 7 A C E H L M P R loop S exits X unsuccessful search lo hi search for m for Q Q with keys[m] = P: return 6 50 69 54 A C E H L M P R S X lo hi m 75 0 69 67 4 A C E H L M P R S X 5 6 5 A C E H L M P R S X 9 7 loop exits with lo > hi: return 7 7 6 6 A C E H L M P R S X 5 5 Trace of binary search for rank in an ordered array 7 6 6 loop exits A with lo > hi: C E H L return 7 M P R S X loop Trace exits of with binary lo > search hi: return for rank 7 in an ordered array Trace of binary search for rank in an ordered array 19

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; } 20

Binary search: trace of standard indexing client Problem. To insert, need to shift all greater keys over. key value keys[] vals[] 0 1 2 3 4 5 6 7 8 9 N 0 1 2 3 4 5 6 7 8 9 S 0 S 1 0 E 1 E S entries in red 2 1 0 A 2 A E S were inserted 3 2 1 0 R 3 A E R S 4 2 1 3 0 C 4 A C E R S entries in gray 5 2 4 1 3 0 H 5 A C E H R S did not move 6 2 4 1 5 3 0 E 6 A C E H R S 6 2 4 6 5 3 0 X 7 A C E H R S X 7 2 4 6 5 3 0 7 A 8 A C E H R S X 7 8 4 6 5 3 0 7 M 9 A C E H M R S X 8 8 4 6 5 9 3 0 7 P 10 A C E H M P R S X 9 8 4 6 5 9 10 3 0 7 L 11 A C E H L M P R S X 10 8 4 6 5 11 9 10 3 0 7 E 12 A C E H L M P R S X 10 8 4 12 5 11 9 10 3 0 7 A C E H L M P R S X 8 4 12 5 11 9 10 3 0 7 Trace of ordered-array ST implementation for standard indexing client entries in black moved to the right circled entries are changed values 21

Elementary ST implementations: summary ST implementation guarantee average case search insert search hit insert key interface sequential search (unordered list) N N N / 2 N equals() binary search (ordered array) log N N log N N / 2 compareto() Challenge. Efficient implementations of both search and insert. 22

3.1 SYMBOL TABLES Algorithms API elementary implementations ordered operations ROBERT SEDGEWICK KEVIN WAYNE http://algs4.cs.princeton.edu

Examples of ordered symbol table API min() get(09:00:13) floor(09:05:00) select(7) keys(09:15:00, 09:25:00) ceiling(09:30:00) max() keys values 09:00:00 Chicago 09:00:03 Phoenix 09:00:13 Houston 09:00:59 Chicago 09:01:10 Houston 09:03:13 Chicago 09:10:11 Seattle 09:10:25 Seattle 09:14:25 Phoenix 09:19:32 Chicago 09:19:46 Chicago 09:21:05 Chicago 09:22:43 Seattle 09:22:54 Seattle 09:25:52 Chicago 09:35:21 Chicago 09:36:14 Seattle 09:37:44 Phoenix size(09:15:00, 09:25:00) is 5 rank(09:10:25) is 7 Examples of ordered symbol-table operations 24

Ordered symbol table API public class ST<Key extends Comparable<Key>, Value>... Key min() smallest key Key max() largest key Key floor(key key) largest key less than or equal to key Key ceiling(key key) smallest key greater than or equal to key int rank(key key) number of keys less than key Key select(int k) key of rank k void deletemin() delete smallest key void deletemax() delete largest key int size(key lo, Key hi) number of keys between lo and hi Iterable<Key> keys() all keys, in sorted order Iterable<Key> keys(key lo, Key hi) keys between lo and hi, in sorted order 25

Binary search: ordered symbol table operations summary search sequential search N binary search log N insert / delete N N min / max N 1 floor / ceiling N log N rank N log N select N 1 ordered iteration N log N N order of growth of the running time for ordered symbol table operations 26