Title: AC1202 Sets 4' Set operations Difference Subset
1AC1202Sets4. Set operationsDifference -
Subset
2C code for sets
- typedef struct mylist
-
- int element
- struct mylist next
- MYLIST
- typedef MYLIST LISTPTR
Linked list node definition as before
- typedef struct set
-
- LISTPTR elements
- SET
int cardinality
3C code for sets
CreateSet AddToSet Intersection Union Differen
ce IsSubset IsSetEmpty
(SET set, int newElement)
(SET setA, SET setB, SET newSet)
(SET setA, SET setB, SET newSet)
(SET setA, SET setB, SET newSet)
(SET setA, SET setB)
(SET set)
or IsListEmpty(LISTPTR head)
4Calculating a set difference
5Difference of 2 sets using ordered linked list
A-B
1
4
7
11
12
14
19
27
Set A
Define two markers for sets A and B. Start
them off at the first elements in each set.
Set B
1
4
8
12
Difference
6Difference of 2 sets using ordered linked list
A-B
1
4
7
11
12
14
19
27
Set A
If info at A B same move markers on
Set B
1
4
8
12
Difference
7Difference of 2 sets using ordered linked list
A-B
1
4
7
11
12
14
19
27
Set A
and advance BOTH markers...
Set B
1
4
8
12
Difference
8Difference of 2 sets using ordered linked list
A-B
1
4
7
11
12
14
19
27
Set A
If info at A lt info a B add info at markerA to
setD and ...
Set B
1
4
8
12
Difference
7
9Difference of 2 sets using ordered linked list
A-B
1
4
7
11
12
14
19
27
Set A
... advance MarkerA
Set B
1
4
8
12
Difference
7
10Difference of 2 sets using ordered linked list
A-B
1
4
7
11
12
14
19
27
Set A
If info at A gt info at B ...
Set B
1
4
8
12
Difference
7
11Difference of 2 sets using ordered linked list
A-B
1
4
7
11
12
14
19
27
Set A
... advance MarkerB
Set B
1
4
8
12
Difference
7
12Difference of 2 sets using ordered linked list
A-B
1
4
7
11
12
14
19
27
Set A
If info at A lt info at B add info at markerA to
setD, and advance A
Set B
1
4
8
12
Difference
7
11
13Difference of 2 sets using ordered linked list
A-B
1
4
7
11
12
14
19
27
Set A
Continue until one Marker is NULL...
Set B
1
4
8
12
NULL
Difference
7
11
14Difference of 2 sets using ordered linked list
A-B
1
4
7
11
12
14
19
27
Set A
NULL
If more elements in setA, add to setD
until markerANULL
Set B
1
4
8
12
NULL
Difference
27
7
11
14
14
19
15Psuedocode for difference
- markerA points to setA
- markerB points to setB
- create empty setD
- markerD points to setD
- repeat
-
- until markerA or markerB at end of sets
16 in difference
- If info at markerA info at markerB
- move both markers on
- else
- if info at markerA lt info at markerB
- add info at markerA to setD
- move markerA on
- else
- move markerB on
17After repeat
- If markerA not at end of setA
- add info to setD
- End
18- void Difference(SET Set1, SET Set2, SET Set3)
- LISTPTR Marker1, Marker2
- Marker1 Set1.elements
- Marker2 Set2.elements
- while (Marker1 Marker2)
- if (Marker1-gtdata Marker2-gtdata)
- Marker1 Marker1-gtnext
- Marker2 Marker2-gtnext
-
- else if(Marker1-gtdata lt Marker2-gtdata)
- AddInOrder(Set3, Marker1-gtdata)
- Marker1 Marker1-gtnext
-
- else
- Marker2 Marker2-gtnext
-
-
19Calculating a subset
20Is set A a subset of set B?
4
7
12
19
Set A
Set B
1
4
7
11
12
14
19
27
21Is set A a subset of set B?
If info at B lt info at Amove along B
4
7
12
19
Set A
Set B
1
4
7
11
12
14
19
27
Counter
0
22Is set A a subset of set B?
If info at B lt info at Amove along B
4
7
12
19
Set A
Set B
1
4
7
11
12
14
19
27
Counter
0
23Is set A a subset of set B?
If info at B info at Aadd one to counter move
along both
4
7
12
19
Set A
Set B
1
4
7
11
12
14
19
27
Counter
0
24Is set A a subset of set B?
If info at B info at Aadd one to counter move
along both
4
7
12
19
Set A
Set B
1
4
7
11
12
14
19
27
Counter
1
25Is set A a subset of set B?
If info at B info at Aadd one to counter move
along both
4
7
12
19
Set A
Set B
1
4
7
11
12
14
19
27
Counter
2
26Is set A a subset of set B?
If info at B lt info at Amove along B
4
7
12
19
Set A
Set B
1
4
7
11
12
14
19
27
Counter
2
27Is set A a subset of set B?
If info at B lt info at Amove along B etc. until
reach end of one of them
4
7
12
19
Set A
Set B
1
4
7
11
12
14
19
27
Counter
2
28Is set A a subset of set B?
4
7
12
19
Set A
Set B
1
4
7
11
12
14
19
27
Counter
4
29Is set A a subset of set B?
Test counter cardinality of A Yes A subset of
B No A not subset of B
4
7
12
19
Set A
Set B
1
4
7
11
12
14
19
27
Counter
4
30Subset contd
31Is set A a subset of set B?
Define two markers for sets A and B. Start them
off at the first elements in each set. Set
counter to 0
4
8
12
19
Set A
Set B
1
4
11
12
14
19
27
Counter
0
32Is set A a subset of set B?
If info at B lt info at Amove along B
4
8
12
19
Set A
Set B
1
4
11
12
14
19
27
Counter
0
33Is set A a subset of set B?
If info at B lt info at Amove along B
4
8
12
19
Set A
Set B
1
4
11
12
14
19
27
Counter
0
34Is set A a subset of set B?
If info at B info at Aadd one to counter move
along both
4
8
12
19
Set A
Set B
1
4
11
12
14
19
27
Counter
0
35Is set A a subset of set B?
If info at B info at Aadd one to counter move
along both
4
8
12
19
Set A
Set B
1
4
11
12
14
19
27
Counter
1
36Is set A a subset of set B?
If info at A lt info at B means there is
something in A that isn't in B so we can stop
4
8
12
19
Set A
Set B
1
4
11
12
14
19
27
Counter
1
37Psuedocode for IsSubset
- If cardinality of A gt B then
- A cant be a subset of B
- else
- markerA points to setA markerB points to setB
- matches0 mismatchFALSE
- Repeat
-
- until markerA or markerB at end of sets or
mismatch TRUE - Subset if matches cardinality of A
- end
38 in IsSubset
- If info at markerA info at markerB
- increment matches
- move both markers on
- else
- if info at markerA lt info at markerB
- mismatchTRUE
- else
- move markerB on
39- int IsSubSet(SET Set1, SET Set2)
- LISTPTR Marker1, Marker2
- int nMatches, nMisMatch
- if (Set1.cardinality gt Set2.cardinality)
- return false
- Marker1 Set1.elements
- Marker2 Set2.elements
- nMatches0 nMisMatch0
- while (Marker1 Marker2 !nMisMatch)
- if (Marker1-gtdata Marker2-gtdata)
- Marker1 Marker1-gtnext
- Marker2 Marker2-gtnext
- nMatches
-
- else if(Marker1-gtdata lt Marker2-gtdata)
- nMisMatch1
40Summary
- Pseudocode and implementation for
- Difference, Subset
- Key points
- Work through sets in order
- If data at markerA lt data markerB then move
markerA on - Remember to store results in ordered set
- What to do after getting to the end of one set
- Difference if any elements left in setA after
subtracting setB, copy these into difference
set - Subset
- Check count of elements each set had in common,
does it equal the cardinality of setA - Special case if element in SetA lt element in
SetB - stop - Next lecture recursion!