13-1 Sets, Bags, and Tables Exam 1 due Friday, March 16 Wellesley College CS230 Lecture 13 Thursday, March 15 Handout #23.

Slides:



Advertisements
Similar presentations
Chapter 23 Organizing list implementations. This chapter discusses n The notion of an iterator. n The standard Java library interface Collection, and.
Advertisements

Linear Lists – Array Representation
Sequence of characters Generalized form Expresses Pattern of strings in a Generalized notation.
1 Todays Objectives Announcements Homework #1 is due next week Return Quiz 1 – answers are posted on the Yahoo discussion page site Basic Java Programming.
Sets and Maps Part of the Collections Framework. 2 The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
Data Structures A data structure is a collection of data organized in some fashion that permits access to individual elements stored in the structure This.
Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
Collections Framework A very brief look at Java’s Collection Framework David Davenport May 2010.
Chapter 5 Queues Modified. Chapter Scope Queue processing Comparing queue implementations 5 - 2Java Software Structures, 4th Edition, Lewis/Chase.
Introduction to Computation and Problem Solving Class 28: Binary Search Trees Prof. Steven R. Lerman and Dr. V. Judson Harward.
12-Jul-15 Lists in Java Part of the Collections Framework.
CSE 143 Lecture 7 Sets and Maps reading: ; 13.2 slides created by Marty Stepp
Java's Collection Framework
Sets and Maps Part of the Collections Framework. The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
CS-2851 Dr. Mark L. Hornick 1 Tree Maps and Tree Sets The JCF Binary Tree classes.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
08 1 Abstract Data Types Problem Sets: PS2 due due Monday, Feburary 26 PS3 due Wednesday, March 7 Wellesley College CS230 Lecture 08 Monday, February 26.
Chapter 3 Introduction to Collections – Stacks Modified
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Chapter 8 Lists. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Examine list processing and various ordering techniques.
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
(c) University of Washington14-1 CSC 143 Java Collections.
1 Sets and Maps Starring: keySet Co-Starring: Collections.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Extensible Arrays: Vectors and StringBuffers Problem Set:Assignment #1 due Tuesday, Feburary 13 Wellesley College CS230 Lecture 05 Monday, February.
2-1 Week 2 Sets Set concepts (you should know these!) Set applications. A set ADT (abstract data type): requirements, contract. Implementations of sets:
Copyright © 2002, Systems and Computer Engineering, Carleton University Hashtable.ppt * Object-Oriented Software Development Unit 8.
CSS446 Spring 2014 Nan Wang.  Java Collection Framework ◦ Set ◦ Map 2.
Chapter 18 Java Collections Framework
1 TCSS 143, Autumn 2004 Lecture Notes Java Collection Framework: Maps and Sets.
Data structures Abstract data types Java classes for Data structures and ADTs.
09-1 Queues and List-Based ADT Implementations Problem Set: PS3 due Wednesday, March 7 Wellesley College CS230 Lecture 09 Monday, February 26 Handout #18.
12-1 Priority Queues, Sets, and Bags Exam 1 due Friday, March 16 Wellesley College CS230 Lecture 12 Monday, March 12 Handout #22.
And other languages….  Array literals/initialization a = [1,2,3] a2 = [-10..0, 0..10] a3 = [[1,2],[3,4]] a4 = [w*h, w, h] a5 = [] empty = Array.new zeros.
Chapter 2 Collections. Copyright © 2004 Pearson Addison-Wesley. All rights reserved.1-2 Chapter Objectives Define the concept and terminology related.
ICOM 4035 – Data Structures Lecture 4 – Set ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.
Lecture 7 February 24, Javadoc version and author Tags These go in the comments before named classes. –Put your SS# on a separate line from the.
Lecture 8: Advanced OOP Part 2. Overview Review of Subtypes Interfaces Packages Sorting.
9 Set ADTs  Set concepts  Set applications  A set ADT: requirements, contract  Implementations of sets: using member arrays, linked lists, boolean.
Introduction to Collections. Collections Collections provide a way of organizing related data in a model Different types of collections have different.
Sets and Maps Part of the Collections Framework. 2 The Set interface A Set is unordered and has no duplicates Operations are exactly those for Collection.
CMSC 202 Containers and Iterators. Container Definition A “container” is a data structure whose purpose is to hold objects. Most languages support several.
1 Maps, Stacks and Queues Maps Reading:  2 nd Ed: 20.4, 21.2, 21.7  3 rd Ed: 15.4, 16.2, 16.7 Additional references: Online Java Tutorial at
Collections Dwight Deugo Nesa Matic
CSCI  Sequence Containers – store sequences of values ◦ vector ◦ deque ◦ list  Associative Containers – use “keys” to access data rather than.
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
Java Collections Framework The client view. The Big Picture.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
1 M255 Object-oriented programming with Java Prepared By: Prof. Dr. Shehab Gamalel-Din Unit 10 Collections: Sets & Maps.
Linked Data Structures
Ordered Structures Wellesley College CS230 Lecture 11
The List ADT.
Ruth Anderson CSE 140 University of Washington
Programming in Java Lecture 11: ArrayList
Java Collections Framework
Maps.
Ruth Anderson UW CSE 160 Winter 2017
"He's off the map!" - Eternal Sunshine of the Spotless Mind
CS2110: Software Development Methods
Programming II (CS300) Chapter 02: Using Objects Java ArrayList Class
Ruth Anderson CSE 160 University of Washington
CS100J Lecture 13 Previous Lecture Java constructs
Ruth Anderson UW CSE 160 Spring 2018
CS210- Lecture 15 July 7, 2005 Agenda Median Heaps Adaptable PQ
Ordered Structures Wellesley College CS230 Lecture 10
Part of the Collections Framework
Sets CS3240, L. grewe.
Ruth Anderson UW CSE 160 Winter 2016
Presentation transcript:

13-1 Sets, Bags, and Tables Exam 1 due Friday, March 16 Wellesley College CS230 Lecture 13 Thursday, March 15 Handout #23

13-2 Overview of Today’s Lecture 1.Sets = conceptually unordered collections of elements without duplicates (but may need order on elements to implement efficiently). 2.Bags = conceptually unordered collections of elements with duplicates. 3.Tables = associations of keys and values

13-3 Mathematical Sets In mathematics, a set is an unordered collection of elements w/o duplicates. A set is written by enumerating its elements between braces. E.g., the empty set (with no elements) is written {}. Insertion: inserts an element into a set if it’s not already there: insert(“a”, {}) = {“a”} insert(“b”, {“a”}) = {“a”, “b”} = {“b”, “a”} // order is irrelevant insert(“a”, {“a”, “b”}) = {“a”, “b”} = {“b”, “a”} insert(“c”, {“a”,”b”}) = {“a”, “b”, “c”} = {“a”, “c”, “b”} = {“b”, “a”, “c”} = {“b”, “c”, “a”} = {“c”, “a”, “b”} = {“c”, “b”, “a”} Deletion: removes an element if it’s in the set: delete(“a”, {}) = {} delete(“a”, {“a”, ”b”}) = {“b”} delete(“b”, {“a”, ”b”}) = {“a”} delete(“c”, {“a”, “b”}) = {“a”, “b”} Size (Cardinality): |S| is the number of elements in S. |{}| = 0; |{“a”}| = 1; |{“a”, “b”}| = 2; |{“a”, “b”, “c”}| = 3;

13-4 More Sets Membership: x  S is true if x is in the set S and false otherwise. “a”  {} = false; “a”  {“a”, “b”} = true; “c”  {“a”, “b”} = false; Subset: S1  S2 is true if all elements in S1 are also in S2. {}  {“a”, “b”} is true; {“a”}  {“a”, “b”} is true; {“b”}  {“a”, “b”} is true; {“a”, “b”}  {“a”, “b”} is true; {“a”, “b”}  {“a”} is false; {“a”}  {} is false; Union: S1  S2 = all elements in at least one of S1 and S2. {“a”, “b”, “d”, “e”}  {“b”, “c”, “d”, “f”} = {“a”, “b”, “c”, “d”, “e”, “f”} Intersection: S1  S2 = all elements in at both S1 and S2. {“a”, “b”, “d”, “e”}  {“b”, “c”, “d”, “f”} = {“b”, “d”} Difference: S1 - S2 or S1/S2 = all elements in S1 that are not in S2. {“a”, “b”, “d”, “e”} / {“b”, “c”, “d”, “f”} = {“a”, “e”}

13-5 A Set Interface in Java, Part 1 /** Interface to mutable sets of elements of type T. A set is an unordered collection that does not contain duplicate elements. WARNING This is different than the standard java.util.Set interface. */ import java.util.*; // import Iterator, Iterable public interface Set extends Iterable { public boolean insert (T x); // Inserts x into this set; returns true if new member public boolean delete (T x); // Deletes x from this set; returns true if successful public boolean isMember(T x); // Returns true if x is a member of this set, else false. public T choose(); // Returns an arbitrary elt from this collection; // throws RuntimeException if empty. public T deleteOne(); // Deletes and returns an arbitrary elt from this collection. // throws RuntimeException if empty. public void union (Set s); // Modify this set to contain the union of its // elements with those of s. public void intersection (Set s); // Modify this set to contain the intersection of its // elements with those of s. public void difference (Set s); // Modify this set to contain the difference of its // elements with those of s. … more methods on next page …

13-6 A Set Interface in Java, Part 2 // The remaining Set methods are similar to Stack, Queue, PQueue methods public int size(); // Returns the number of elements in this set. public boolean isEmpty(); // Returns true if this set empty, false otherwise public void clear(); // Removes all elemets from this set public Set copy(); // Returns a "shallow" copy of this set. public Iterator iterator(); // Returns an iterator yielding the elements of this set. public String toString(); // Returns a string indicating type and contents of this set. // The elements may be listed in any order. }

13-7 How to Implement A Set? As a sorted vector: How long does isMember() take? insert()? As a sorted list: How long does isMember() take? insert()?

13-8 Mathematical Bags (Multi-Sets) In mathematics, a bag (multi-set) is an unordered collection of elements with duplicates. A bag is written by enumerating its elements (with duplicates) between braces. E.g., the empty bag (with no elements) is written {}. The bag with one “a” is written {“a”, “a”}. The bag with two “a”s is written {“a”, “a”}. Most set notions (insertion, deletion, membership, subset, union, intersection, difference, cardinality) generalize to bags as long as duplicates are accounted for. E.g: insert(“a”, {“a”, “b”}) = {“a”,“a”, “b”} = {“a”, “b”, “a”} = {“b”, “a”, “a”} delete(“a”, {“a”, “a”, “a”, “b”, “c”, “c”}) = {“a”,“a”, “b”, “c”, “c”} {“a”, “b”}  {“a”, “a”, “b”} = true; {“a”, “a”, “b”}  {“a”, “b”} = false; {“a”, “a”, “b”, “d”}  {“a”, “d”, “e”} = {“a”, “a”, “a”, “b”, “d”, “d”, “e”} {“a”, “a”, “a”, “b”, “c”}  {“a”, “a”, “b”, “b”} = {“a”, “a”, “b”} {“a”, “a”, “a”, “b”, “c”} / {“a”, “a”, “b”, “b”} = {“a”, “c”} |{“a”, “a”, “a”, “b”, “c”, “c”}| = 6

13-9 New Bag Operations Multiplicity: we shall write #(x,B) (my nonstandard notation) for the number of occurrences of element x in bag B, a.k.a. the multiplicity of x in B. Suppose B1 = {“a”,”a”, “a”, “b”, “c”, “c”}. Then: #(“a”, B1) = 3; #(“b”, B1) = 1; #(“c”, B1) = 2; #(“d”, B1) = 0; DeleteAll: delete(x,B) deletes one occurrence of x from B. Sometimes we want to delete all occurrences of x from B. For this we use deleteAll(x,B). delete(“a”, {“a”, “a”, “a”, “b”, “c”, “c”}) = {“a”,“a”, “b”, “c”, “c”} deleteAll(“a”, {“a”, “a”, “a”, “b”, “c”, “c”}) = {“b”, “c”, “c”} deleteAll(“d”, {“a”, “a”, “a”, “b”, “c”, “c”}) = {“a”, “a”, “a”, “b”, “c”, “c”} CountDistinct: |B| counts all element occurrences in B, including duplicates. Sometimes we want just the number of distinct elements. For this we use countDistinct(B). |{“a”, “a”, “a”, “b”, “c”, “c”}| = 6 countDistinct({“a”, “a”, “a”, “b”, “c”, “c”}) = 3

13-10 A Bag Interface in Java /** Interface to mutable bags of elements of type T. A bag is an unordered collection that does not contain duplicate elements. */ import java.util.*; // import Iterator, Iterable public interface Bag extends Iterable { // Methods that are new to bags public int multiplicity (T x); // Return the multiplicity of x in this bag. public void deleteAll (T x); // Delete all occurrences of x in this bag. public int countDistinct (); // Return the number of distinct elements in this bag. public Iterator distinctElements (); // Return an iterator yielding the // distinct elements of this bag. // All the remaining methods of Bag are those of Set … other methods go here … }

13-11 Bags are not Sets! Why not use inheritance to relate Bag and Set ? public interface Bag extends Set { public int multiplicity (T x); // Return the multiplicity of x in this bag. … other bag-specific methods … } Because bags generally don’t behave like sets! A bag instance cannot always be used where a set is expected. For example: public static boolean isItASet (Set s) { s.delete(“foo”); return ! s.isMember(“foo”); } isItASet() should return true for every set. But it will return false for a bag that contains more than one occurrence of “foo”! A bag implementation might inherit methods from a set implementation. But the type of a bag is different than the type of a set.

13-12 Bag Example: Word Frequencies // Print the frequencies of words in the give file in alphabetical order public static void frequencies (String filename) { }

13-13 Digression: Ordered Sets and Bags Sets and bags are unordered collections of elements. However, we can also have ordered notions of sets and bags, in which the order of elements matters. The order might be specified by a Comparator or the Comparable compareTo() method, in which case elements would have a well-defined index. For instance, in the ordered bag {“a”, “a”, “a”, “b”, “c”, “c”} “a”s are at indices 0, 1, 2; “b” is at index 3, and “c”s are at indices 4 & 5. Since elements have a well-defined index, we could request the element at a given index or delete the element at an index from an ordered set/bag. Inserting elements into or deleting elements from an ordered set/bag can shift the indices of other elements, similar to what happens in a vector. We will not consider implementations of ordered sets or bags this semester.

13-14 Tables Associate Keys with Values Key (name)Value (extension) Ann3924 Bob8763 Carol5816 …… Example: a phone directory Call each row a table entry. There is at most one entry in a table with a given key. Conceptually entries are not ordered by key (but they may be ordered in implementation for efficiency.) Core table operations: Get a value from the table for a given key. Put a value into the table for a given key (overwriting any existing entry with that key)

13-15 A Table Interface in Java /** Interface to mutable tables that map keys of type T to values of type V. This is somewhat similar to the standard java.util.Map interface in Java. Unlike Maps, Tables do not allow null values to be associated with a key. */ public interface Table { public void put(K key, V value); // Modifies table to associate key with non-null value. // If an entry with key already exists, it is overwritten. // If value is null, a RuntimeException is thrown. public V get(K key); // Return the value associated key in this table, else null. public boolean remove (K key); // Remove the entry with the given key from this table. // The table is unchanged if there is no such key. // Returns true if there was an entry, else false. public Iterator keys(); // Returns an iterator of all keys in the table. public int size(); // Returns the number of entries in this table. public boolean isEmpty(); // Returns true if this table has no entries, else false. public void clear(); // Removes all entries from this table. public Table copy(); // Returns a "shallow" copy of this table. public String toString(); // Returns a string representation of this table. }

13-16 Our Phone Directory in Java Key (name)Value (extension) Ann3924 Bob8763 Carol5816 …… Table directory = new TableVectorSorted (); directory.put(“Bob”, new Integer(8763)); directory.put(“Carol”, new Integer(2673)); // Carol’s old extension directory.put(“Ann”, new Integer(3924)); directory.put(“Carol”, new Integer(5816)); // Carol’s new extension directory.get(“Bob”) → 8763 // An Integer, not an int directory.get(“Carol”) → 5816 // Most recent extension directory.get(“Diana”) → null // No extension in table

13-17 Table Keys and Values can be of Any Type Key (buggle)Value (favorite color) …… Table favorites = new TableVectorSorted (); Buggle b1 = new Buggle(); Buggle b2 = new Buggle(); favorites.put(b1, Color.red); favorites.put(b2, Color.blue); Buggle 1 Buggle 2 Color.re d Color.blue

13-18 What are Tables Good For? Adding virtual instance variables to objects (e.g. person’s favorite ice cream flavor) All sorts of directories (phone directories, Linux file directories) Databases provide sophisticated table management Indexing files and web pages Program analysis and execution (symbol tables, environments) Implementing bags