Ed. 2. and 3.: Chapter 7 - Ed. 4.: Chapter 8

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 41

Sets

- Ed. 2. and 3.: Chapter 7 - Ed. 4.: Chapter 8

The Set Abstract Data Type


Suppose that we have two containers of objects shown below. C1 {4,3,5,2,9} C2 {8,4,6,2,1} How can we find out all the objects that are in both containers?

set: A container of distinct objects. 1. There are no duplicate elements in a set. 2. There is no explicit notion of keys or even an order. Examples: 1. 2. 3. 4. 5. A group of unique integers A group of unique web pages A collection of unique books A group of students A hockey team

Union of two sets A and B

A B {x : x A or x B}
9 0 3 5 6 4 B 3 1

2 6

2 3 0 6 5 1

Intersection of two sets A and B

A B {x : x A and x B}
9 0 3 5 6 4 B 3 1

2 6

Subtraction of two sets A and B

A B {x : x A and x B}
9 0 3 5 6 4 B 3 1

2 6

9 5

The Set Abstract Data Type


The set abstract data type supports the following fundamental methods acting on a set A.
union(B): Replace A with the union of A and B, that is, assign A B to A. Input: Set; output: None intersect(B): Replace A with the intersection of A and B, that is, assign A B to A. Input: Set; output: None subtract(B): Replace A with the difference of A and B, that is, assign A - B to A. Input: Set; output: None

Java provides a Set interface in java.util package. The table below shows the mapping of the set ADT methods to the methods of the java.util.Set interface. Set ADT Method union(B) intersect(B) subtract(B) java.util.Set Method addAll(Collection B) retainAll(Collection B) removeAll(Collection B)

A Simple Set Implementation


Recall that in merge sort, we have to merge two sorted sequences. Operations on sets can also be viewed as merging two sets. For example, the union operation A B can be viewed as merging sets A and B into one set by following the definition of set union. Therefore, one of the simplest ways of implementing a set is to store its elements in an ordered sequence.

Example: Union with sorted sequences


A B A B 0 3 1 2 5 6 9 6

3 4

The first element in A is less than the first element in B. We move it to the resulting sequence.
A a<b B A B 1 2 3 4 6 0 3 5 6 9

The first element in B is less than the first element in A. We move it to the resulting sequence.
A b<a B A 1 2 3 4 6 3 5 6 9

B 0

The first element in B is less than the first element in A. We move it to the resulting sequence.
A b<a B A 2 3 1 4 6 3 5 6 9

B 0

The first element in B is equal to the first element in A. We move it to the resulting sequence. Also remove it from both A and B.
A a=b B A 3 4 1 6 2 3 5 6 9

B 0

The first element in B is less than the first element in A. We move it to the resulting sequence.
A b<a B A 4 6 1 2 3 5 6 9

B 0

The first element in A is less than the first element in B. We move it to the resulting sequence.
A a<b B A 6 1 2 3 4 5 6 9

B 0

The first element in A is equal to the first element in B. We move it to the resulting sequence. Remove it from both A and B.
A a=b B A 6 1 2 3 4 5 6 9

B 0

Now B is empty. We move the rest from A to the resulting sequence.


A B A B 0 1 2 3 4 5 6 9

The result is the union of A and B.


A B 0 1 2 3 4 5 6 9

Example: Intersection with sorted sequences


A B A B 0 3 1 2 5 6 9 6

3 4

The first element in A is less than the first element in B. We remove it from A.
A a<b A B B 0 3 5 6 9 6

1 2

3 4

The first element in B is less than the first element in A. We remove it from B.
A b<a A B B 3 5 6 9 3 4 6

1 2

The first element in B is less than the first element in A. We remove it from B.
A b<a A B B 3 2 5 6 9 6

3 4

The first element in A is equal to the first element in B. We move it to the resulting sequence. Also we remove it from both A and B.
A a=b A B B 3 3 5 4 6 9 6

The first element in B is equal to the first element in A. We remove it from B.


A b<a A B B 5 4 3 6 9 6

The first element in A is less than the first element in B. We remove it from A.
A a<b A B B 5 6 3 6 9

The first element in A is equal to the first element in B. We move it to the resulting sequence. Also we remove it from both A and B.
A a=b A B B 6 6 3 9

Now B is empty. We remove the rest from A.


A B A B 3 6 9

And here is the result:


A B 3 6

In the examples we see that when we compare elements a and b from sets A and B, respectively, we have three cases: 1. a < b 2. a = b 3. a > b For each case, an action will be taken depending on the set operation (union, intersection, or subtraction). Therefore, we need a generic merge method that allows us to merge two sets in different ways.

Algorithm genericMerge(A,B): Input: Sets represented by sorted sequences A and B Output: Set represented by a sorted sequence C copy A to A' copy B to B' loop until either A' or B' is empty get the first element from A' and assign it to a get the first element from B' and assign it to b if a < b action for this case remove a from A' else if a = b action for this case remove a from A' remove b from B' else action for this case remove b from B'

loop until A' is empty get the first element from A' and assign it to a action as if a < b remove a from A' loop until B' is empty get the first element from B' and assign it to b action as if b < a remove b from B'

The actions for the three cases are listed below for set operations.
Case a<b a=b b<a union insert a to C insert a to C insert b to C intersection no action insert a to C no action subtraction insert a to C no action no action

A Java implementation
public abstract class Merger { private Object a, b; private PositionIterator1 iterA, iterB; public void merge( Sequence A, Sequence B, Comparator comp, Sequence C ) { iterA = A.elements(); iterB = B.elements(); boolean aExists = advanceA(); boolean bExists = advanceB(); while( aExists && bExists ) { if( comp.isLessThan( a, b )) { aIsLess( a, C ); aExists = advanceA(); } else if( comp.isEqualTo( a, b )) { bothAreEqual( a, b, C ); aExists = advanceA(); bExists = advanceB(); } else { bIsLess( b, C ); bExists = advanceB(); } }

while( aExists ) { aIsLess( a, C ); aExists = advanceA(); } while( bExists ) { bIsLess( b, C ); bExists = advanceB(); } }

loop until A' is empty get the first element from A' and assign it to a action as if a < b remove a from A'

protected void aIsLess( Object a, Sequence C ) {} protected void bothAreEqual( Object a, Object b, Sequence C ) {} protected void bIsLess( Object b, Sequence C ) {} private boolean advanceA() { if( iterA.hasNext()) { a = iterA.next(); return true; } return false; } private boolean advanceB() { if( iterB.hasNext()) { b = iterB.next(); return true; } return false; } }

loop until B' is empty get the first element from B' and assign it to b action as if b < a remove b from B'

Implementation of union with class Merger


public class UnionMerger extends Merger { protected void aIsLess( Object a, Sequence C ) { C.insertLast( a ); } protected void bothAreEqual( Object a, Object b, Sequence C ) { C.insertLast( a ); } protected void bIsLess( Object b, Sequence C ) { C.insertLast( b ); } }

Case a < b a = b a > b

union insert a to C insert a to C insert b to C

Implementation of intersection with class Merger


public class IntersectMerger extends Merger { protected void aIsLess( Object a, Sequence C ) {} protected void bothAreEqual( Object a, Object b, Sequence C ) { C.insertLast( a ); } protected void bIsLess( Object b, Sequence C ) {} }

Case
a < b a = b a > b

intersection
no action insert a to C no action

Implementation of subtraction with class Merger


public class SubtractMerger extends Merger { protected void aIsLess( Object a, Sequence C ) { C.insertLast( a ); } protected void bothAreEqual( Object a, Object b, Sequence C ) { } protected void bIsLess( Object b, Sequence C ) {} }

Case
a < b a = b a > b

subtraction
insert a to C no action no action

public class SetOperations { public static void main (String[] args) { NodeSequence a = new NodeSequence(); NodeSequence b = new NodeSequence(); int i = 0; int j[] = {1, 2, 3, 4, 5, 4, 5, 6, 7, 8}; Position p = a.insertFirst(new Integer(j[0])); for(i = 0; i < 4; i++) {Integer k = new Integer(j[i+1]); p = a.insertAfter(p, k); } System.out.print("set1: {"); p = a.first(); for (i = 0; i < 5; i++) { try { System.out.print(((Integer)(p.element())).intValue() + " "); p = a.next(p); }

catch(BoundaryViolationException e) {break;} } System.out.print("}"); System.out.println('\n'); p = b.insertFirst(new Integer(j[5])); for(i = 5; i < 9; i++) {Integer k = new Integer(j[i+1]); p = b.insertAfter(p, k); } System.out.print("set2: {"); p = b.first(); for (i = 0; i < 5; i++) { try{ System.out.print(((Integer)(p.element())).intValue() + " "); p = b.next(p);} catch(BoundaryViolationException e) {break;} }

System.out.print("}"); System.out.println('\n'); Comparator c = new Comparator(); NodeSequence s1 = new NodeSequence(); UnionMerger u1 = new UnionMerger(); u1.merge(a, b, c, s1); System.out.print("Union of sets: {"); p = s1.first(); for (i = 0; i < 10; i++) { try{ System.out.print(((Integer)(p.element())).intValue() + " "); p = s1.next(p); } catch(BoundaryViolationException e) {break;}} System.out.print("}"); System.out.println('\n');

IntersectMerger u2 = new IntersectMerger(); NodeSequence s2 = new NodeSequence(); u2.merge(a, b, c, s2); System.out.print("Intersection of sets: {"); p = s2.first(); for (i = 0; i < 10; i++) { try{ System.out.print(((Integer)(p.element())).intValue() + " "); p = s2.next(p); } catch(BoundaryViolationException e) {break;} } System.out.print("}"); System.out.println('\n');

SubtractMerger u3 = new SubtractMerger(); NodeSequence s3 = new NodeSequence(); u3.merge(a, b, c, s3); System.out.print("Difference of sets: {"); p = s3.first(); for (i = 0; i < 10; i++) { try{ System.out.print(((Integer)(p.element())).intValue() + " "); p = s3.next(p); } catch(BoundaryViolationException e) {break;} } System.out.print("}"); System.out.println('\n'); } }

Data Structure Exercises 18.1

You might also like