2-1 Week 2 Sets Set concepts (you should know these!) Set applications. A set ADT (abstract data type): requirements, contract. Implementations of sets:

Slides:



Advertisements
Similar presentations
Chapter 7. Binary Search Trees
Advertisements

Transparency No. 1 Java Collection API : Built-in Data Structures for Java.
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)
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 The Collections API. Simple Container/ Iterator Simple Container Shape [] v = new Shape[10]; Simple Iterator For( int i=0 ; i< v.length ; i++)
6-1 6 Stack ADTs Stack concepts. Stack applications. A stack ADT: requirements, contract. Implementations of stacks: using arrays, linked lists. Stacks.
USERDEVELOPERADVERTISER.
9-1 9 Queue ADTs Queue concepts. Queue applications. A queue ADT: requirements, contract. Implementations of queues: using arrays, linked lists. Queues.
Collections Sets - no duplicates Lists - duplicates allowed Maps - key / value pairs A collection is an Object which contains other Objects. There are.
CS 307 Fundamentals of Computer Science 1 Abstract Data Types many slides taken from Mike Scott, UT Austin.
CSci 143 Sets & Maps Adapted from Marty Stepp, University of Washington
Fall 2007CS 2251 Iterators and Tree Traversals. Fall 2007CS 2252 Binary Trees In a binary tree, each node has at most two subtrees A set of nodes T is.
24-Jun-15 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
CSE 143 Lecture 7 Sets and Maps reading: ; 13.2 slides created by Marty Stepp
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.
Set, TreeSet, TreeMap, Comparable, Comparator. Def: The abstract data type set is a structure that holds objects and satifies ARC: Objects can be added.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
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 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.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 22 Java Collections.
Bags Chapter 1 Copyright ©2012 by Pearson Education, Inc. All rights reserved.
Collections in Java. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection with no duplicates SortedSet.
6 Stack ADTs  Stack concepts  Stack applications  Stack ADTs: requirements, contracts  Implementations of stacks: using arrays and linked-lists  Stacks.
Java Collections An Introduction to Abstract Data Types, Data Structures, and Algorithms David A Watt and Deryck F Brown © 2001, D.A. Watt and D.F. Brown.
Jan 12, 2012 Introduction to Collections. 2 Collections A collection is a structured group of objects Java 1.2 introduced the Collections Framework Collections.
CSE 143 Lecture 2 More ArrayList ; classes and objects reading: 10.1; slides created by Marty Stepp and Hélène Martin
CSE 143 Lecture 4 ArrayList Reading: 10.1 slides created by Marty Stepp
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.
13-1 Sets, Bags, and Tables Exam 1 due Friday, March 16 Wellesley College CS230 Lecture 13 Thursday, March 15 Handout #23.
10 Binary-Search-Tree Data Structure  Binary-trees and binary-search-trees  Searching  Insertion  Deletion  Traversal  Implementation of sets using.
CSE 143 Lecture 2 ArrayList reading: 10.1 slides created by Marty Stepp
CSS446 Spring 2014 Nan Wang.  To understand the implementation of linked lists and array lists  To analyze the efficiency of fundamental operations.
11 Map ADTs  Map concepts  Map applications  A map ADT: requirements, contract.  Implementations of maps: using key-indexed arrays, entry arrays, linked-lists,
Sets and Maps Computer Science 4 Mr. Gerb Reference: Objective: Understand the two basic applications of searching.
Chapter 3 Collections. Objectives  Define the concepts and terminology related to collections  Explore the basic structures of the Java Collections.
Tries Data Structure. Tries  Trie is a special structure to represent sets of character strings.  Can also be used to represent data types that are.
1 Introduction  Algorithms  Data structures  Abstract data types  Programming with lists and sets © 2008 David A Watt, University of Glasgow Algorithms.
9-1 9 Set ADTs Set concepts. Set applications. A set ADT: requirements, contract. Implementations of sets: using arrays, linked lists, boolean arrays.
9 Set ADTs  Set concepts  Set applications  A set ADT: requirements, contract  Implementations of sets: using member arrays, linked lists, boolean.
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.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of CHAPTER 15: Sets and Maps Java Software Structures: Designing and Using.
Module #3 - Sets 3/2/2016(c) , Michael P. Frank 2. Sets and Set Operations.
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.
CSE 373 Data Structures and Algorithms Lecture 9: Set ADT / Trees.
3-1 Java's Collection Framework Another use of polymorphism and interfaces Rick Mercer.
CS2005 Week 7 Lectures Set Abstract Data Type.
Linked Data Structures
11 Map ADTs Map concepts. Map applications.
Efficiency of in Binary Trees
Week 2: 10/1-10/5 Monday Tuesday Wednesday Thursday Friday
Introduction to Collections
Introduction to Collections
JAVA Collections Framework Set Interfaces & Implementations
TCSS 342, Winter 2006 Lecture Notes
Introduction to Collections
Adapted from Pearson Education, Inc.
Introduction to Collections
CSE 1020: The Collection Framework
Introduction to Collections
ETS Working Group meeting 24-25/9/2007 Agenda point 7 CVTS3 brief update /09/ 2007 ETS working group.
2015 Update of Union Lists of Sites of Community Interest
Copyright ©2012 by Pearson Education, Inc. All rights reserved
8 List ADTs List concepts. List applications.
Presentation transcript:

2-1 Week 2 Sets Set concepts (you should know these!) Set applications. A set ADT (abstract data type): requirements, contract. Implementations of sets: using member arrays, linked lists. Sets in the Java class library. Example © 2001, D.A. Watt and D.F. Brown

2-2 Set concepts (1) A set is a collection of distinct members (values or objects), whose order is insignificant. Notation for sets: {a, b, …, z}. The empty set is { }.  Set notation is used here, but not supported by Java.

2-3 Set concepts (2) Examples of sets: evens= {0, 2, 4, 6, 8} punct= {‘.’, ‘!’, ‘?’, ‘:’, ‘;’, ‘,’} EU= {AT, BE, DE, DK, ES, FI, FR, GR, IE, IT, LU, NL, PT, SE, UK} NAFTA= {CA, MX, US} NATO= {BE, CA, CZ, DE, DK, ES, FR, GR, HU, IS, IT, LU, NL, NO, PL, PT, TR, UK, US} a set of integers a set of characters sets of countries

2-4 Set concepts (3) The cardinality of a set s is the number of members of s. This is written #s. E.g.: #EU = 15 #{red, white, red} = 2 Duplicate members are not counted. An empty set has cardinality zero. We can test whether x is a member of set s (i.e., s contains x). This is the membership test, written x  s. E.g.: UK  EU SE  EU SE  NATO SE is not a member of NATO.

2-5 Set concepts (4) Two sets are equal if they contain exactly the same members. E.g.: NAFTA = {US, CA, MX} NAFTA  {CA, US} Order of members does not matter. These two sets are not equal. Set s 1 subsumes (is a superset of) set s 2 if every member of s 2 is also a member of s 1. This is written s 1  s 2. E.g.: NATO  {CA, US} NATO  EU NATO does not subsume EU.

2-6 Set concepts (5) The union of sets s 1 and s 2 is a set containing just those values that are members of s 1 or s 2 or both. This is written s 1  s 2. e.g.: {DK, NO, SE}  {FI, IS} = {DK, FI, IS, NO, SE} {DK, NO, SE}  {IS, NO} = {DK, IS, NO, SE}

2-7 Set concepts (6) The intersection of sets s 1 and s 2 is a set containing just those values that are members of both s 1 and s 2. This is written s 1  s 2. E.g.: NAFTA  NATO= {CA, US} NAFTA  EU= {} Two sets are disjoint if they have no common member, i.e., if their intersection is empty. E.g.: NAFTA and EU are disjoint NATO and EU are not disjoint.

2-8 Set concepts (7) The difference of sets s 1 and s 2 is a set containing just those values that are members of s 1 but not of s 2. This is written s 1 – s 2 (the symbol \ was used last year on SDM) E.g.: NATO – EU = {CA, CZ, HU, IS, NO, PL, TR, US} EU – NATO = {AT, FI, IE, SE}

2-9 Set applications Spelling checker:  A spelling checker’s dictionary is a set of words.  The spelling checker highlights any words in the document that are not in the dictionary.  The spelling checker might allow the user to add words to the dictionary. Relational database system:  A relation is essentially a set of tuples.  Each tuple is distinct.  The tuples are in no particular order.

2-10 Set ADT: requirements Requirements: 1)It must be possible to make a set empty. 2)It must be possible to test whether a set is empty. 3)It must be possible to obtain the cardinality of a set. 4)It must be possible to perform a membership test. 5)It must be possible to add or remove a member of a set. 6)It must be possible to test whether two sets are equal. 7)It must be possible to test whether one set subsumes another. 8)It must be possible to compute the union, intersection, or difference of two sets. 9)It must be possible to traverse a set.

2-11 Set class Diagram Set ? boolean isEmpty (); int size (); boolean contains (Object obj); Etc. We cannot complete this! Why?

2-12 Set ADT: contract/specification (1) public interface Set { // Each Set object is a set whose members are objects. //////////// Accessors //////////// public boolean isEmpty (); // Return true if and only if this set is empty. public int size (); // Return the cardinality of this set. public boolean contains (Object obj); // Return true if and only if obj is a member of this set.

2-13 Set ADT: contract (2) Possible contract (continued): public boolean equals (Set that); // Return true if and only if this set is equal to that. public boolean containsAll (Set that); // Return true if and only if this set subsumes that.

2-14 Set ADT: contract (3) Possible contract (continued): //////////// Transformers //////////// public void clear (); // Make this set empty. public void add (Object obj); // Add obj as a member of this set. public void remove (Object obj); // Remove obj from this set. public void addAll (Set that); // Make this set the union of itself and that.

2-15 Set ADT: contract (4) Possible contract (continued): public void removeAll (Set that); // Make this set the difference of itself and that. public void retainAll (Set that); // Make this set the intersection of itself and that. //////////// Iterator //////////// public Iterator iterator(); // Return an iterator that will visit all members of this set, in no // particular order. }

2-16 Summary of set implementations The array representation is suitable only for small or static sets.  A static set is one in which members are never (or at least infrequently) added or removed. The SLL (singly link list, covered in a few weeks time) representation is suitable only for small sets. For general applications, we need a more efficient set representation: search tree (week 10) or hash table (not considered in this module).

2-17 Sets in the Java class library The java.util.Set interface is similar to the Set interface above. The java.util.TreeSet class implements the java.util.Set interface, representing each set by a search tree (covered later in the module). The java.util.HashSet class implements the java.util.Set interface, representing each set by a hash table (we will not consider this).

2-18 Example: (1) import java.util.TreeSet; class Colours { public static void main (String [] args) { TreeSet rainbow = new TreeSet(); TreeSet primary = new TreeSet(); TreeSet flag = new TreeSet(); // set up the colours of the rainbow rainbow.add("red"); rainbow.add("orange"); rainbow.add("yellow");rainbow.add("green"); rainbow.add("blue");rainbow.add("indigo"); rainbow.add("violet");

2-19 Example: (2) primary.add("red"); primary.add("green"); primary.add("blue"); flag.add("red"); flag.add("white"); flag.add("blue"); // printout the cardinality of the sets System.out.println( "the cardinality of set rainbow is “ +rainbow.size()); System.out.println( "the cardinality of set primary is “ +primary.size()); System.out.println( "the cardinality of set flag is “ +flag.size());

2-20 Example: (3) System.out.println(); System.out.println("rainbow colours in “ + “ the national flag"); rainbow.retainAll(flag); System.out.println(rainbow.toString()); } } This prints out the set, how would you print out the individual colours? Go to the sun java site to find out