Title: AC1202 Sets 3' Set operations Union Intersection
1AC1202Sets3. Set operationsUnion -
Intersection
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)
4Intersection of 2 setsMethodology
5Intersection
U
B
A
10
1
5
11
9
3
13
7
12
6Intersection of 2 sets using ordered linked list
1
4
7
11
12
14
19
27
Set A
Set B
1
4
8
12
2 things to remember
Want to identify common elements
Keep sets/results in order
Intersection
7Intersection of 2 sets using ordered linked list
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. And, create new intersection set to store
results
Set B
1
4
8
12
Intersection
8Intersection of 2 sets using ordered linked list
1
4
7
11
12
14
19
27
Set A
If info at A B same then
Set B
1
4
8
12
Intersection
9Intersection of 2 sets using ordered linked list
1
4
7
11
12
14
19
27
Set A
... and advance BOTH markers
Set B
1
4
8
12
Intersection
1
10Intersection of 2 sets using ordered linked list
1
4
7
11
12
14
19
27
Set A
Set B
1
4
8
12
Remember to Add InOrder!
Intersection
1
11Intersection of 2 sets using ordered linked list
1
4
7
11
12
14
19
27
Set A
Set B
1
4
8
12
Remember to Add InOrder!
Intersection
1
12Intersection of 2 sets using ordered linked list
1
4
7
11
12
14
19
27
Set A
Dont add elements to intersection
Set B
1
4
8
12
Intersection
1
4
13Intersection of 2 sets using ordered linked list
1
4
7
11
12
14
19
27
Set A
Dont add elements to intersection
Set B
1
4
8
12
If info at A lt info at B advance MarkerA
Remember we are going through these sets in
order
Intersection
1
4
14Intersection of 2 sets using ordered linked list
1
4
7
11
12
14
19
27
Set A
Set B
1
4
8
12
Intersection
1
4
15Intersection of 2 sets using ordered linked list
1
4
7
11
12
14
19
27
Set A
If info at A gt info at B advance MarkerB
Set B
1
4
8
12
Intersection
1
4
16Intersection of 2 sets using ordered linked list
1
4
7
11
12
14
19
27
Set A
Set B
1
4
8
12
Intersection
1
4
17Intersection of 2 sets using ordered linked list
1
4
7
11
12
14
19
27
Set A
If info at A lt info at B advance MarkerA
Set B
1
4
8
12
Intersection
1
4
18Intersection of 2 sets using ordered linked list
1
4
7
11
12
14
19
27
Set A
If info at A info at B Add element to
intersection Increment MarkerA and MarkerB
Set B
1
4
8
12
Intersection
1
4
19Intersection of 2 sets using ordered linked list
1
4
7
11
12
14
19
27
Set A
Continue until one Marker is NULL... Then..?
Set B
1
4
8
12
NULL
Intersection
1
4
12
20Intersection of 2 sets using ordered linked list
1
4
7
11
12
14
19
27
Set A
Then STOP, since intersection contains elements
common to both sets.
Set B
1
4
8
12
NULL
Intersection
1
4
12
21Pseudocode for Intersection
- 3 sets
- a, b, Intersect
- Intersection(SET a, SET b, SET Intersect)
- Define markers for a and b
- Travel through both sets and copy common elements
into Intersect - Move through sets until?
- Compare elements
- if element in a lt element in b then?
- After the end of one set has been reached do
what?
22Psuedocode for intersection
- markerA points to setA
- markerB points to setB
- / setI to store results /
- repeat
-
- until markerA or markerB at end of sets
23 within repeat section
- If info at markerA info at markerB
- add info at markerA to setI
- move both markers on
- else
- if info at markerA lt info at markerB
- move markerA on
- else
- move markerB on
24After repeat
- Dont need to do anything else
- Intersection fully performed when we get to end
of set marker A or B
25Pseudocode summary
- markerA points to setA
- markerB points to setB
- repeat
- If info at markerA info at markerB
- add info at markerA to setI
- move both markers on
- else
- if info at markerA lt info at markerB
- move markerA on
- else
- move markerB on
- until markerA or markerB at end of sets
26Now try writing code
- void Intersection(SET SetA, SET SetB, SET
Intersection) -
- LISTPTR MarkerA, MarkerB, MarkerI
- MarkerA SetA.elements MarkerB
SetB.elements - while (MarkerA MarkerB)
- if (MarkerA-gtelement MarkerB-gtelement)
- AddInOrder(Intersection, MarkerA-gtelement)
- MarkerA MarkerA-gtnext
- MarkerB MarkerB-gtnext
-
- else if(MarkerA-gtelement lt MarkerB-gtelement)
-
- MarkerA MarkerA-gtnext
-
- else
-
- MarkerB MarkerB-gtnext
-
27Union of 2 setsMethodology
28Union
U
B
A
10
1
5
11
9
13
3
7
12
29Union of 2 sets using ordered linked list
1
4
7
11
12
14
19
27
Set A
Set B
1
4
8
12
2 things to remember
Avoid duplicates
Keep sets/results in order
Union
30Union of 2 sets using ordered linked list
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
Union
31Union of 2 sets using ordered linked list
1
4
7
11
12
14
19
27
Set A
If both markers point to equal values, add
element once to union...
Set B
1
4
8
12
1
Union
32Union of 2 sets using ordered linked list
1
4
7
11
12
14
19
27
Set A
...and move both markers along one space
Set B
1
4
8
12
1
Union
33Union of 2 sets using ordered linked list
1
4
7
11
12
14
19
27
Set A
Repeat process for next element.
Set B
1
4
8
12
1
4
Union
34Union of 2 sets using ordered linked list
1
4
7
11
12
14
19
27
Set A
Set B
1
4
8
12
1
4
Union
35Union of 2 sets using ordered linked list
1
4
7
11
12
14
19
27
Set A
...and move only one marker along one space.
Set B
1
4
8
12
1
4
7
Union
36Union of 2 sets using ordered linked list
1
4
7
11
12
14
19
27
Set A
Continue in same way...
Set B
1
4
8
12
1
4
7
Union
37Union of 2 sets using ordered linked list
1
4
7
11
12
14
19
27
Set A
Continue in same way...
Set B
1
4
8
12
1
4
7
8
Union
38Union of 2 sets using ordered linked list
1
4
7
11
12
14
19
27
Set A
Continue in same way...
Set B
1
4
8
12
1
4
7
8
11
Union
39Union of 2 sets using ordered linked list
1
4
7
11
12
14
19
27
Set A
Continue in same way...
Set B
1
4
8
12
1
4
7
8
11
Union
40Union of 2 sets using ordered linked list
1
4
7
11
12
14
19
27
Set A
...until we reach the end of one of the sets...
Set B
1
4
8
12
1
4
7
8
11
12
Union
41Union of 2 sets using ordered linked list
1
4
7
11
12
14
19
27
Set A
Find which set still has elements in it (other
set will have pointer to a NULL or zero value)
Set B
1
4
8
12
NULL
1
4
7
8
11
12
Union
42Union of 2 sets using ordered linked list
1
4
7
11
12
14
19
27
Set A
Copy rest of remaining set into union
Set B
1
4
8
12
NULL
1
4
7
8
11
12
Union
43Union of 2 sets using ordered linked list
1
4
7
11
12
14
19
27
Set A
Copy rest of remaining set into union
Set B
1
4
8
12
NULL
1
4
7
8
11
12
14
Union
44Union of 2 sets using ordered linked list
1
4
7
11
12
14
19
27
Set A
Copy rest of remaining set into union
Set B
1
4
8
12
NULL
1
4
7
8
11
12
14
19
Union
45Union of 2 sets using ordered linked list
1
4
7
11
12
14
19
27
Set A
NULL
Until NULL pointer encountered.
Set B
1
4
8
12
NULL
1
4
7
8
11
12
14
19
27
Union
46Pseudocode
- 3 sets
- a, b, u (u for union)
- Union(SET a, SET b, SET u)
- Define markers for a and b
- Traverse through and copy all elements from both
into u (avoid duplicates) - Move through sets until?
- Compare elements
- if element in a lt element in b then?
- After the end of one set has been reached do
what?
47Pseudocode for union
- markerA points to setA
- markerB points to setB
- / have setU to store results /
- repeat
-
- until markerA or markerB at end of sets
48 in union
- If info at markerA info at markerB
- add info at markerA to setU
- move both markers on
- Else if info at markerA lt info at markerB
- add info at markerA to setU
- move markerA on
- Else
- add info at markerB to setU
- move markerB on
49After repeat
- If markerA not at end of setA
- add remaining setA info to setU
- If markerB not at end of setB
- add remaining setB info to setU
- End
50Pseudocode summary
- markerA points to setA
- markerB points to setB
- create empty setU
- repeat
- If info at markerA info at markerB
- add info at markerA to setU
- move both markers on
- Else if info at markerA lt info at markerB
- add info at markerA to setU
- move markerA on
- Else
- add info at markerB to setU
- move markerB on
- until markerA or markerB at end of sets
- If markerA not at end of setA
- add info to setU
- If markerB not at end of setB
- add info to setU
- End
51Now try writing code
52- void Union(SET Set1, SET Set2, SET Set3)
-
- LISTPTR Marker1, Marker2, Marker3
- Marker1 Set1.elements Marker2
Set2.elements - while (Marker1 Marker2)
- if (Marker1-gtelement Marker2-gtelement)
- AddInOrder(Set3, Marker1-gtelement)
- Marker1 Marker1-gtnext
- Marker2 Marker2-gtnext
-
- else if(Marker1-gtelement lt Marker2-gtelement)
- AddInOrder(Set3, Marker1-gtelement)
- Marker1 Marker1-gtnext
- else
- AddInOrder(Set3, Marker2-gtelement)
- Marker2 Marker2-gtnext
-
-
53Summary
- Pseudocode and implementation for
- Union
- Intersection
- 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
- Intersection stop! Thats it.
- Union continue until set with elements still
left in it has been traversed - Next lecture implementation of Difference and
Subset