ICOM 4035 – Data Structures Lecture 4 – Set ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

Singly linked lists Doubly linked lists
DATA STRUCTURES USING C++ Chapter 5
AITI Lecture 19 Linked List Adapted from MIT Course 1.00 Spring 2003 Lecture 26 and Tutorial Note 9 (Teachers: Please do not erase the above note)
Lists Chapter 4 Carrano, Data Structures and Abstractions with Java, Second Edition, (c) 2007 Pearson Education, Inc. All rights reserved X.
Generics, Lists, Interfaces
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.
Java Review Interface, Casting, Generics, Iterator.
ICOM 4035 – Data Structures Lecture 15 – Graph ADT
Written by: Dr. JJ Shepherd
ICOM 6005 – Database Management Systems Design Dr. Manuel Rodríguez-Martínez Electrical and Computer Engineering Department Lecture 11 – Hash-based Indexing.
ICOM 4035 – Data Structures Lecture 12 – Hashtables & Map ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico,
ICOM 4035 – Data Structures Lecture 8 – Doubly Linked Lists Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez.
Generic programming in Java
CPSC 388 – Compiler Design and Construction
An Array-Based Implementation of the ADT List public class ListArrayBased implements ListInterface { private static final int MAX_LIST = 50; private Object.
Sets and Maps Chapter 9. Chapter 9: Sets and Maps2 Chapter Objectives To understand the Java Map and Set interfaces and how to use them To learn about.
CMPT 225 ADT List using Dynamic arrays A dynamic data structure is one that changes size, as needed, as items are inserted or removed The Java ArrayList.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 17: Linked Lists.
Week 4-5 Java Programming. Loops What is a loop? Loop is code that repeats itself a certain number of times There are two types of loops: For loop Used.
(c) University of Washingtonhashing-1 CSC 143 Java Hashing Set Implementation via Hashing.
ICOM 4035 – Data Structures Lecture 2 – ADT and Interfaces Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez.
CS2110: SW Development Methods Textbook readings: MSD, Chapter 8 (Sect. 8.1 and 8.2) But we won’t implement our own, so study the section on Java’s Map.
Chapter 7 More Lists. Chapter 7: More Lists 7.1 – Circular Linked Lists 7.2 – Doubly Linked Lists 7.3 – Linked Lists with Headers and Trailers 7.4 – A.
Implementing Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
ICOM 4035 – Data Structures Lecture 9 – Stack ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.
ICOM 4035 – Data Structures Lecture 5 – List ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.
Chapter 3 List Stacks and Queues. Data Structures Data structure is a representation of data and the operations allowed on that data. Data structure is.
Data Structures and Abstract Data Types "Get your data structures correct first, and the rest of the program will write itself." - David Jones.
ICOM 4035 – Data Structures Lecture 6 – Big-O Notation Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 6 – September 6 th, 2001.
ICOM 4035 – Data Structures Lecture 13 – Trees ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.
ArrayList, Multidimensional Arrays
ICOM 4035 – Data Structures Lecture 14 – Binary Search Tree ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico,
ICOM 4035 – Data Structures Lecture 11 – Map ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.
ICOM 4035 – Data Structures Lecture 1 - Introduction Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez.
Hash Tables1   © 2010 Goodrich, Tamassia.
Java 5 Part 1 CSE301 University of Sunderland Harry Erwin, PhD.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 9 – September 18, 2001.
ICOM 4035 – Data Structures Lecture 3 – Bag ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.
13-1 Sets, Bags, and Tables Exam 1 due Friday, March 16 Wellesley College CS230 Lecture 13 Thursday, March 15 Handout #23.
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
Dynamic Data Structures and Generics Chapter 10. Outline Vectors Linked Data Structures Introduction to Generics.
ICOM 4035 – Data Structures Lecture 7 – Linked Lists Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
ICOM 4035 – Data Structures Lecture 10 – Queue ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 10 – September 20, 2001.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
Sets and Maps Computer Science 4 Mr. Gerb Reference: Objective: Understand the two basic applications of searching.
Chapter 15 Linked Data Structures Slides prepared by Rose Williams, Binghamton University Kenrick Mock University of Alaska Anchorage Copyright © 2008.
M1G Introduction to Programming 2 3. Creating Classes: Room and Item.
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
Written by: Dr. JJ Shepherd
Chapter 17: Linked Lists. Objectives In this chapter, you will: – Learn about linked lists – Learn the basic properties of linked lists – Explore insertion.
Sets and Maps Chapter 9. Chapter Objectives  To understand the Java Map and Set interfaces and how to use them  To learn about hash coding and its use.
M180: Data Structures & Algorithms in Java Stacks Arab Open University 1.
1 Chapter 4 Unordered List. 2 Learning Objectives ● Describe the properties of an unordered list. ● Study sequential search and analyze its worst- case.
Arrays, Link Lists, and Recursion Chapter 3. Sorting Arrays: Insertion Sort Insertion Sort: Insertion sort is an elementary sorting algorithm that sorts.
LINKED LISTS.
CSC 212 – Data Structures Lecture 28: More Hash and Dictionaries.
Building Java Programs Generics, hashing reading: 18.1.
Review Array Array Elements Accessing array elements
Implementing ArrayList Part 1
Lecture 3 – Data collection List ADT
Presentation transcript:

ICOM 4035 – Data Structures Lecture 4 – Set ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel Rodriguez – All rights reserved

Lecture Organization Part I – Sets and using Java Generics in collections Part II – Design and implementation the Set ADT Part III – Iterating over a Set Part IV - Implementing a Dynamic Set M. Rodriguez-MartinezICOM

Objectives Keep focus on the concept of collections Introduce the concept of Generics Understand the design and implementation of the Set ADT – Unordered collection of items with no repetitions Provide motivating examples ICOM 4035M. Rodriguez-Martinez 3

Companion videos Lecture4 videos – Contains the coding process associated with this lecture – Shows how to build the interfaces, concrete classes, and factory classes mentioned here M. Rodriguez-MartinezICOM

Part I Sets and using Java Generics in collections M. Rodriguez-MartinezICOM

Set ADT Set: – unordered collection of things – repetitions are NOT allowed – Mathematical term: set M. Rodriguez-MartinezICOM Set Set with candy canes Set with name tags Joe Ned Amy

Uses for sets Any application that needs to collect items that cannot be repeated more than once Applications for sets – Keep courses enrolled in a given semester – Tracks family members – Store information about cars owned – Assign meetings to a room M. Rodriguez-MartinezICOM

Data in Set Like the Set, the Set can have – fixed number of items (static-size Set) Fixed at construction time – Variable number of items (dynamic-size Set) Size can be increased at running time M. Rodriguez-MartinezICOM Static Set Dynamic Set

Static vs. Dynamic Set How to store values in Set? – We need an array of Object! Store any type of value – Static: Fixed sized array that never grows – Dynamic: Array can grow as needed – These are details for the concrete classes! M. Rodriguez-MartinezICOM Fields elements currentSize Methods Dynamic Set Fields elements Methods Static Set currentSize 5 maxSize

Using Generics vs. Object Using array of Object make collection type unsafe – You want store string in a collection – You can have: Object elements[] = new Object[5]; elements[0] = new String(“Bob”); elements[1] = new String(“Jil”); elements[2] = new Integer(20); – Notice that adding integer object is legal Everything in Java inherits from Object so assignment is correct M. Rodriguez-MartinezICOM

Generics allow type safety Generics let you parameterized the data type to be stored in a collection You can define Bag, or Set – Here E is a placeholder for the type used in the collection Now, one implementation works for any type associated with the container – Bag, Set, Set You setup the array as follows – E elements[] = (E[]) new Object[arraySize]; Casting is needed to array of E Java will give you a warning about safe types, live with it! M. Rodriguez-MartinezICOM

Part II Design and implementation of the Set ADT M. Rodriguez-MartinezICOM

Design of Set ADT M. Rodriguez-MartinezICOM StaticSetDynamicSet > Set As before, we will do static set first, then implement dynamic Set

Operations on the Set Supported: – Add new element – Remove a copy of an element – Clear Set – Test for element membership – Get Set size – Test if empty – Test if subset – Compute Union – Compute Intersection – Compute Difference – Iterate over all stored values M. Rodriguez-MartinezICOM

Moe Add new element Adding Moe to Set S – Return true if added, or false if already present For static set, need to check if you have space Dynamic set grows as needed M. Rodriguez-MartinezICOM Set with name tags Joe Ned Amy Set with name tags Joe Ned Amy S.add(“Moe”);

Add new element (2) First find it! Key idea is to keep array packed – In use section All elements in range [0,currentSize-1] – Unused (free) section Current size tells us – Current size – Next slot available! You add to next slot and increment currentSize M. Rodriguez-MartinezICOM JoeNedAmy unusedin use currentSize: 3 JoeNedAmyMoe unusedin use currentSize: 4 add Moe

Remove element Remove Ned from Set S – Return true if erased or false if not found Works the same for both static and dynamic Sets Search and replace with last element M. Rodriguez-MartinezICOM Set with name tags Joe Ned Amy Set with name tags Joe Amy S.erase(“Ned”);

Remove element (2) First find it! Key idea – overwrite target with last element – decrement currentSize Last element is still there but inaccessible – Valid range: [0, currenSize-1] – Best to make it null Prevent memory leaks M. Rodriguez-MartinezICOM JoeNedAmy unusedin use currentSize: 3 JoeAmy unusedin use currentSize: 2 remove Ned

Ned Bob Membership Test Determine if element e is in the Set Works the same for both static and dynamic Sets Simply loop until element is found (if it is there) M. Rodriguez-MartinezICOM Set with name tags Joe Ned Amy S.isMember(“Ned”); true S.isMember(“Jil”); false

Membership Test (2) Loop from 0 to currentSize-1 Stop when you see target value – Return true If you get out of the loop return false – Element is not there M. Rodriguez-MartinezICOM JoeNedAmyApuLi unused in use currentSize: 5 Ned is found in slot 1 Jil is never found

Ned Bob Clear Set Remove all copies from Set (becomes empty Set) Works the same for both static and dynamic Set Simply loop thru array nulling everything and then set currentSize to 0 M. Rodriguez-MartinezICOM Set with name tags Joe Amy Set with name tags S.clear();

Clear Set (2) Key idea – Loop setting all values to null This prevent memory leaks – Set current size to zero Memory Leak – Only setting currentSize to zero leaves a bunch of references active – Garsete collector will not be able to reclaim them M. Rodriguez-MartinezICOM JoeNedAmyNed unused in use currentSize: unused currentSize: 0 clear

Set Union Given two sets S 1 and S 2 the union is: Notice that S 3 is also a set (no duplicates) – Also notice M. Rodriguez-MartinezICOM S1S1 S2S2

Joe Ned Union Operation Only retain 1 copy per element Idea: – Copy S 1 into S 3 – Add elements in S 2 to S 3 if not already there M. Rodriguez-MartinezICOM Joe Amy Bob Ned Joe Amy Bob Ned S1.union(S2); S1 S2

Set Difference Given two sets S 1 and S 2 the difference is: Notice that S 3 is also a set (no duplicates) – Also S1 – S2 ≠ S2 – S1 M. Rodriguez-MartinezICOM S1S1 S2S2

Joe Ned Difference Operation Idea: – Make S3 be empty set – Iterate over each x in S1 If not present in S2 – Add to S3 M. Rodriguez-MartinezICOM Joe Amy Bob Ned S1.difference(S2); S1 S2

Set Intersection Given two sets S 1 and S 2 the intersection is: Notice that S 3 is also a set (no duplicates) – Also notice, M. Rodriguez-MartinezICOM S1S1 S2S2

Bob Ned Intersection Operation Idea: – Compute as S1 – (S1-S2) M. Rodriguez-MartinezICOM Joe Amy Bob Ned Bob Ned S1.intersection(S2); S1 S2 Apu Mel

Easy Set operations Set size – just return the value of currentSize Empty Set test – just determine if set size is equal to zero Subset – iterate over S1 and see if every element is also in S2. If true, S1 is subset of S2 Always a good practice to write operations in terms of previous ones – Software reuse (Do Not Repeat Yourself (DNRY)!) M. Rodriguez-MartinezICOM

Part III Iterating over Set elements M. Rodriguez-MartinezICOM

Iterating over Set elements We want to iterate over elements inside Set – To use them for whatever reason we need them Make Set iterable – Set interface extends Java Iterable interface – Enables Set to return an iterator How do we do this? Like in the Set case! – Need to have an inner class that helps you iterate over Set elements Set Iterator class that implements Java Iterator interface M. Rodriguez-MartinezICOM

Set Iterator SetIterator is inner class to static or dynamic Set Instances of SetIterator have permission to see private fields of Set – Used to iterate over the array SetIterator just needs an integer as private field – Keeps track of current value being inspected Current Position M. Rodriguez-MartinezICOM Fields elements currentSize Methods currentPosition SetIterator inner class

Part IV Implementing Dynamic Set M. Rodriguez-MartinezICOM

Apu Tom Jil Dynamic Set Keeps accepting new elements as long as there is memory. Idea: Grow the array of elements as needed and copy values from old array to new one Only method to change is add() M. Rodriguez-MartinezICOM Ned Set with name tags Joe Ned Amy Set with name tags Ned Joe Ned Amy Multiple inserts

Add new element to full Set Key idea is to crate new array with twice the size Process: – First create new array – Copy all elements from old to new array – Erase old array – Insert new element Now, you have room to grow in the future M. Rodriguez-MartinezICOM JoeNedAmy in use currentSize: 3 JoeNedAmyMoe unusedin use currentSize: 4 add Moe maxSize: 3 maxSize: 6

How to implement Dynamic Set Option 1: – Copy all methods from static set, but change add() method Ugly! Option 2: – Use inheritance to make static set a parent class for dynamic set and change add() method Better, but you need to make array of elements protected Option 3: – Use object composition and make a static set instance be a private member of dynamic set – Map all methods to static set except for add() which reallocates static set Best option! M. Rodriguez-MartinezICOM

Summary Collection classes – Provides objects whose role is to store other objects Set – unordered collection of items – No repetitions – Static set – sized cannot be changed – Dynamic set – size can be changed as needed Double the size of previous array Better done via object composition M. Rodriguez-MartinezICOM

Questions? – ICOM 4035M. Rodriguez-Martinez 38