Title: CS5539: Data Structures and Algorithms
1CS5539 Data Structures and Algorithms
2Reading
3Definition of a Set
- A collection of objects
- No duplicates
- Ordering is unimportant
4Sets Some Terminology
- Cardinality
- Number of members of a set
- Base type
- The data type of the objects in the set
- Equality
- Sets where membership is identical
set_a 5, 3, 9, 1 set_b 1, 9, 3, 5 set_a
and set_b are identical sets.
5Sets Some Terminology
- Empty set (written as ) (null set)
- Has no members therefore cardinality of 0
- Disjoint sets
- Sets which have no members in common
- Universal set
- Contains all possible members of an associated
data type - Subset
- All members in a subset also are members of the
superset
6Typical Set Operations
OPERATION PRE-CONDITION POST-CONDITION boolean
add(Object item) set not full, item in
set item is not null boolean
remove(Object item) set not empty item not in
set item not null
If the item is already in the set add() does
nothing and returns false to indicate that
nothing has been done. If the item is not in the
set remove() does nothing and returns false to
indicate that nothing has been done.
7Union
8Intersection
9Difference
10Set Operations
OPERATION PRE-CONDITION POST-CONDITION union(Set
set2) set2 is not null all members of set2
added into set1 intersection(Set
set2) set2 is not null new set contains
common members difference(Set set2) set2 is
not null new set contains no members of
set2 only members of set1
Assume set1 is the original set set2 is a second
set
11Set Operations
OPERATION PRE-CONDITION POST-CONDITION boolean
isEmpty() none set same boolean
contains (Object item) item not null set
same boolean subset(Set set2) set2 not null set
same int size() none set same
12Examples
A 4,3,8,6,7 B 9,3,6,0 C
Result Value Returned
false
- B.subset(A)
- B.contains(9)
- B.add(4)
- A.remove(0)
- A.difference(B)
- B.difference(A)
- C.isEmpty()
- A.union(B)
- B.intersection(C)
B 9,3,6,0
B 9,3,6,0
true
B 9,3,6,0,4
true
A 4,3,8,6,7
false
4,8,7
void
void
9,0
true
C
void
4,3,8,6,7,9,0
void
13Set ADT
- Essential Operations
- create ? Set
- add Element ? Set ? Set
- contains Element ? Set ? Boolean
- remove Element ? Set ? Set
- isEmpty Set ? Boolean
14Set ADT (ii)
- Other Operations
- size Set ? Integer
- equals Set ? Set ? Boolean
- copy Set ? Set
- areDisjoint Set ? Set ? Boolean
- isSubset Set ? Set ? Boolean
- union Set ? Set ? Set
- intersection Set ? Set ? Set
- difference Set ? Set ? Set
15Sets
A set may be implemented as an array. A set
may be implemented as a linked list. A set may
be implemented as a bit vector (bitmap).
16Implementation Array
- The above array is used to implement the set
7,6,9,3
17Implementation Array
- to represent the set baker, butcher,
grocer
18Implementation Linked List
- The above linked list is used to implement the
set - 7,6,9,3
19Implementation Bit Vector (Bitmap)
- Constraints
- The number of distinct values is finite
- The number of distinct values is relatively small
- There is a bit position for each member of the
base set, and a 1 bit indicates that that member
is present in the set. - Add element
- set corresponding bit to 1
- Remove element
- set corresponding bit to 0
20Set Implementations Bitmap
- Bitmap
- e.g. to represent the set C, X
21Implementation Bit Vector (Bitmap)
- Using a boolean array to simulate bitmap
- True element present False element absent.
1,3,5
private boolean intSet new boolean6
NOTE each array element occupies a byte
22Bit Vector (Bitmap) Implementation
More compact storage may be obtained using the
BitSet class contained in the java.util
package Each data element must be converted into
a unique non-negative integer value
23Sets
- Sets are useful
- as minor properties underlined, bold, italic
- as sets of char vowels 'a','e','i','o','u
- Keyword searching
- Finding prime numbers less than a given integer
24Sample code for Array Implementation(
illustration purposes only)
- public class ArraySet
- private Object data
- private int size
- public ArraySet(int length)
- data new Objectlength
- size 0
-
25The add method
- public void add(Comparable x)
- int pos findPos(x)
- if (pos gt size)
- pos pos(size1)
- if (size gt data.length)
- increaseDataLength()
-
- for (int isize igtpos i--)
- datai datai-1
-
- datapos x
- size
-
-
26The contains method
- public boolean contains(Comparable x)
-
- return (findPos(x) lt size)
-
27The remove method
- public void remove(Comparable x)
- int pos findPos(x)
- if (pos lt size)
- for (int ipos1 iltsize i)
- datai-1 datai
- size--
-
-
28The isEmpty method
- public boolean isEmpty()
-
- return (size 0)
-
29An Iterator is required to visit all members in
the set
- public java.util.Iterator iterator()
-
- return new ArraySet.MyIterator()
-
30Methods of the MyIterator Class
- private class MyIterator implements
java.util.Iterator - private int current private boolean
isRemoveValid - private MyIterator()
- current 0 isRemoveValid false
- public boolean hasNext()
- return (current lt size)
- public Object next()
- if (current gt size) throw new
java.util.NoSuchElementException() - isRemoveValid true return datacurrent
- public void remove()
- if (!isRemoveValid) throw new
IllegalStateException() - for (int icurrent iltsize i)
- datai-1 datai
- size-- current--
- isRemoveValid false
31The findPos Method
- private int findPos(Comparable x)
-
- int left -1, right size, m
- while (left lt right-1)
- m (left right)/2
- if (x.compareTo(datam) lt 0)
- right m
- else left m
-
- if (left lt0) return size1
- if (x.equals(dataleft))
- return left
- else
- return left size 2
-
32Method to increase size of array
- private void increaseDataLength()
-
- Object newData new Object2data.length
- for (int i0 iltdata.length i)
- newDatai datai
- data newData
-
-
33Sets in the Java Library
The BitSet Class http//java.sun.com/j2se/1.3/docs
/api/java/util/BitSet.html
The HashSet Class http//java.sun.com/j2se/1.3/doc
s/api/java/util/HashSet.html
The TreeSet Class http//java.sun.com/j2se/1.3/doc
s/api/java/util/TreeSet.html
34(No Transcript)
35(No Transcript)