AC1202 Sets 4' Set operations Difference Subset - PowerPoint PPT Presentation

1 / 40
About This Presentation
Title:

AC1202 Sets 4' Set operations Difference Subset

Description:

Marker1 = Set1.elements; Marker2 = Set2.elements; while (Marker1 && Marker2) ... if (nMatches == Set1.cardinality) return true; return false; Summary ... – PowerPoint PPT presentation

Number of Views:44
Avg rating:3.0/5.0
Slides: 41
Provided by: glenn63
Category:

less

Transcript and Presenter's Notes

Title: AC1202 Sets 4' Set operations Difference Subset


1
AC1202Sets4. Set operationsDifference -
Subset
2
C 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
3
C 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)
4
Calculating a set difference
5
Difference 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
6
Difference 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
7
Difference 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
8
Difference 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
9
Difference 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
10
Difference 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
11
Difference 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
12
Difference 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
13
Difference 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
14
Difference 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
15
Psuedocode 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

17
After 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

19
Calculating a subset
20
Is set A a subset of set B?
4
7
12
19
Set A
Set B
1
4
7
11
12
14
19
27
21
Is 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
22
Is 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
23
Is 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
24
Is 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
25
Is 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
26
Is 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
27
Is 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
28
Is 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
29
Is 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
30
Subset contd
31
Is 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
32
Is 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
33
Is 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
34
Is 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
35
Is 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
36
Is 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
37
Psuedocode 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

40
Summary
  • 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!
Write a Comment
User Comments (0)
About PowerShow.com