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

Post on 20-Dec-2015

221 views 0 download

Tags:

Transcript of Sets - Ed. 2. and 3.: Chapter 7 - Ed. 4.: Chapter 8.

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. A group of unique integers 2. A group of unique web pages 3. A collection of unique books 4. A group of students 5. A hockey team

Union of two sets A and B

}or :{ BxAxxBA

63 5

0

A

63

1

2B

49A B

63

50

9 4

2

1

Intersection of two sets A and B

} and :{ BxAxxBA

63 5

0

A

63

B

49A B

63

2

1

Subtraction of two sets A and B

} and :{ BxAxxBA

63

5

0

A

63

B

4

9

A B2

1

9

0

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 java.util.Set Method union(B) addAll(Collection B) intersect(B) retainAll(Collection B) subtract(B) removeAll(Collection B)

A Simple Set Implementation

R e c a ll th a t in m e rg e s o r t , w e h a v e to m e rg e tw o s o r te d s e q u e n c e s . O p e ra t io n s o n s e ts c a n a ls o b e v ie w e d a s m e rg in g tw o s e ts . F o r e x a m p le , th e u n io n o p e ra t io n BA c a n b e v ie w e d a s m e rg in g s e ts A a n d B in to o n e s e t b y fo l lo w in g th e d e f in i t io n o f s e t u n io n . T h e re fo re , o n e o f th e s im p le s t w a y s o f im p le m e n tin g a s e t is to s to re i ts e le m e n ts in a n o rd e re d s e q u e n c e .

E x a m p le : U n io n w ith so rte d se q u e n c e s

630

63 421

95

B

A

A B

T h e f i r s t e le m e n t in A i s le s s th a n th e f i r s t e le m e n t in B . W e m o v e i t to th e r e s u l t in g s e q u e n c e .

630

63 4

a < b

21

95

B

A

A B

T h e f i r s t e le m e n t in B i s le s s th a n th e f i r s t e le m e n t in A . W e m o v e i t to th e r e s u l t in g s e q u e n c e .

63

0

63 4

b < a

21

95

B

A

A B

T h e f i r s t e le m e n t in B i s le s s th a n th e f i r s t e le m e n t in A . W e m o v e i t to th e r e s u l t in g s e q u e n c e .

63

0

63 4

b < a2

95

B

A

A B 1

T h e f i r s t e le m e n t in B i s e q u a l to th e f i r s t e le m e n t in A . W e m o v e i t to th e r e s u l t in g s e q u e n c e . A ls o r e m o v e i t f r o m b o t h A a n d B .

63

0

63 4a = b

2

95

B

A

A B 1

T h e f i r s t e le m e n t in B i s le s s th a n th e f i r s t e le m e n t in A . W e m o v e i t to th e r e s u l t in g s e q u e n c e .

6

30

64b < a

2

95

B

A

A B 1

T h e f i r s t e le m e n t in A i s le s s th a n th e f i r s t e le m e n t in B . W e m o v e i t to th e r e s u l t in g s e q u e n c e .

6

30

6

4

a < b

2

95

B

A

A B 1

T h e f i r s t e le m e n t in A i s e q u a l to th e f i r s t e le m e n t in B . W e m o v e i t to th e r e s u l t in g s e q u e n c e . R e m o v e i t f r o m b o t h A a n d B .

6

30

6

4

a = b

2

9

5

B

A

A B 1

N o w B i s e m p t y . W e m o v e t h e r e s t f r o m A t o t h e r e s u l t i n g s e q u e n c e .

630 42

9

5

B

A

A B 1

T h e r e s u l t i s t h e u n i o n o f A a n d B .

630 42 95A B 1

E x a m p le : In te rse c tio n w ith so rte d se q u e n c e s

630

63 421

95

B

A

A B

T h e f i r s t e l e m e n t i n A i s l e s s t h a n t h e f i r s t e l e m e n t i n B . W e r e m o v e i t f r o m A .

630

63 421

95

B

A

A B

a < b

T h e f i r s t e l e m e n t i n B i s l e s s t h a n t h e f i r s t e l e m e n t i n A . W e r e m o v e i t f r o m B .

63

63 421

95

B

A

A B

b < a

T h e f i r s t e l e m e n t i n B i s l e s s t h a n t h e f i r s t e l e m e n t i n A . W e r e m o v e i t f r o m B .

63

63 42

95

B

A

A B

b < a

T h e f i r s t e l e m e n t i n A i s e q u a l t o t h e f i r s t e l e m e n t i n B . W e m o v e i t t o t h e r e s u l t i n g s e q u e n c e . A l s o w e r e m o v e i t f r o m b o t h A a n d B .

63

63 4

95

B

A

A B

a = b

T h e f i r s t e l e m e n t i n B i s e q u a l t o t h e f i r s t e l e m e n t i n A . W e r e m o v e i t f r o m B .

6

6

3

4

95

B

A

A B

b < a

T h e f i r s t e l e m e n t i n A i s l e s s t h a n t h e f i r s t e l e m e n t i n B . W e r e m o v e i t f r o m A .

6

6

3

95

B

A

A B

a < b

T h e f i r s t e l e m e n t i n A i s e q u a l t o t h e f i r s t e l e m e n t i n B . W e m o v e i t t o t h e r e s u l t i n g s e q u e n c e . A l s o w e r e m o v e i t f r o m b o t h A a n d B .

6

6

3

9

B

A

A B

a = b

N o w B i s e m p t y . W e r e m o v e t h e r e s t f r o m A .

63

9

B

A

A B

A n d h e r e i s t h e r e s u l t :

63A B

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 union intersection subtraction a < b insert a to C no action insert a to C a = b insert a to C insert a to C no action b < a insert b to C no action no action

A Java implementation public abstract class Merger { private Object a, b; private PositionIterator1 iterA, iterB; public void merger( 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(); } } 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 A' is emptyget the first element from A' and assignit to aaction as if a < bremove a from A'

loop until B' is emptyget the first element from B' and assign it to baction as if b < aremove 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 union

a < b insert a to C

a = b insert a to C

a > b 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 intersection

a < b no action

a = b insert a to C

a > b 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 subtraction

a < b insert a to C

a = b no action

a > b 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.merger(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.merger(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.merger(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