EE 422C Sets.

Slides:



Advertisements
Similar presentations
Priority Queues. 2 Priority queue A stack is first in, last out A queue is first in, first out A priority queue is least-first-out The “smallest” element.
Advertisements

An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
CSE 143 Lecture 15 Sets and Maps reading: ; 13.2 slides created by Marty Stepp
CSE 143 Lecture 7 Sets and Maps reading: ; 13.2 slides created by Marty Stepp
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
Data Design and Implementation. Definitions of Java TYPES Atomic or primitive type A data type whose elements are single, non-decomposable data items.
Building Java Programs Bonus Slides Hashing. 2 Recall: ADTs (11.1) abstract data type (ADT): A specification of a collection of data and the operations.
Ordered Linked Lists using Abstract Data Types (ADT) in Java Presented by: Andrew Aken.
CSE 143 Lecture 3 Implementing ArrayIntList reading: slides created by Marty Stepp and Hélène Martin
1 CS162: Introduction to Computer Science II Abstract Data Types.
An Array-Based Implementation of the ADT List
Linked Lists Chapter 5 (continued)
Wednesday Notecards. Wednesday Notecards Wednesday Notecards.
Building Java Programs
Efficiency of in Binary Trees
Data Structures Lakshmish Ramaswamy.
CSc 110, Spring 2018 Lecture 32: Sets and Dictionaries
Week 2: 10/1-10/5 Monday Tuesday Wednesday Thursday Friday
Road Map CS Concepts Data Structures Java Language Java Collections
Building Java Programs
TCSS 143, Autumn 2004 Lecture Notes
Sets (11.2) set: A collection of unique values (no duplicates allowed) that can perform the following operations efficiently: add, remove, search (contains)
Lecture 2: Implementing ArrayIntList reading:
Building Java Programs
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Building Java Programs
Building Java Programs
slides created by Marty Stepp and Hélène Martin
CSE 143 Lecture 2 Collections and ArrayIntList
CSE 373: Data Structures and Algorithms
slides adapted from Marty Stepp and Hélène Martin
Lecture 2: Implementing ArrayIntList reading:
CSE 373 Data Structures and Algorithms
CSE 373: Data Structures and Algorithms
Exercise Write a program that counts the number of unique words in a large text file (say, Moby Dick or the King James Bible). Store the words in a collection.
The Vector Class An object of class Vector is similar to an array in that it stores multiple values However, a vector only stores objects does not have.
CSE 373 Hash set implementation, continued
CS2110: Software Development Methods
CSE 373: Data Structures and Algorithms
ArrayLists 22-Feb-19.
Collections Framework
CSE 143 Lecture 4 Implementing ArrayIntList
CSE 143 Lecture 25 Set ADT implementation; hashing read 11.2
Sets (11.2) set: A collection of unique values (no duplicates allowed) that can perform the following operations efficiently: add, remove, search (contains)
Based on slides by Alyssa Harding & Marty Stepp
JCF Collection classes and interfaces
CSE 373 Separate chaining; hash codes; hash maps
CSE 143 Lecture 3 Implementing ArrayIntList reading:
Priority Queues.
Linked Lists Chapter 5 (continued)
slides created by Marty Stepp
CSE 143 Lecture 4 Implementing ArrayIntList; Binary Search
ArrayLists 27-Apr-19.
CSE 143 Lecture 2 ArrayIntList, binary search
CSE 373 Priority queue implementation; Intro to heaps
slides created by Marty Stepp
Priority Queues.
Building Java Programs
slides created by Marty Stepp and Hélène Martin
Lecture 7: Linked List Basics reading: 16.2
slides adapted from Marty Stepp
CSE 373 Java Collection Framework reading: Weiss Ch. 3, 4.8
slides created by Marty Stepp
Linked Lists Chapter 5 (continued)
slides created by Marty Stepp
CSE 373 Set implementation; intro to hashing
Java Generics & Iterators
Copyright ©2012 by Pearson Education, Inc. All rights reserved
Implementing ArrayIntList
slides created by Marty Stepp and Hélène Martin
Presentation transcript:

EE 422C Sets

Sets set: A collection of unique values (no duplicates allowed) that can perform the following operations efficiently: add, remove, search (contains) The client doesn't think of a set as having indexes; we just add things to the set in general and don't worry about order set.contains("to") true set "the" "of" "from" "to" "she" "you" "him" "why" "in" "down" "by" "if" set.contains("be") false

Int Set ADT interface Let's think about how to write our own implementation of a set. To simplify the problem, we only store ints in our set for now. As is (usually) done in the Java Collection Framework, we will define sets as an ADT by creating a Set interface. Core operations are: add, contains, remove. public interface IntSet { void add(int value); boolean contains(int value); void clear(); boolean isEmpty(); void remove(int value); int size(); }

Unfilled array set Consider storing a set in an unfilled array. It doesn't really matter what order the elements appear in a set, so long as they can be added and searched quickly. What would make a good ordering for the elements? If we store them in the next available index, as in a list, ... set.add(9); set.add(23); set.add(8); set.add(-3); set.add(49); set.add(12); How efficient is add? contains? remove? O(1), O(N), O(N) (contains must loop over the array; remove must shift elements.) index 1 2 3 4 5 6 7 8 9 value 23 -3 49 12 size

Sorted array set Suppose we store the elements in an unfilled array, but in sorted order rather than order of insertion. set.add(9); set.add(23); set.add(8); set.add(-3); set.add(49); set.add(12); How efficient is add? contains? remove? O(N), O(log N), O(N) (You can do an O(log N) binary search to find elements in contains, and to find the proper index in add/remove; but add/remove still need to shift elements right/left to make room, which is O(N) on average.) index 1 2 3 4 5 6 7 8 9 value -3 12 23 49 size

A strange idea Silly idea: When client adds value i, store it at index i in the array. Would this work? Problems / drawbacks of this approach? How to work around them? set.add(7); set.add(1); set.add(9); … set.add(18); set.add(12); index 1 2 3 4 5 6 7 8 9 value size index 1 2 3 4 5 6 7 8 9 value 12 18 size