AC1202 Sets 3' Set operations Union Intersection - PowerPoint PPT Presentation

1 / 53
About This Presentation
Title:

AC1202 Sets 3' Set operations Union Intersection

Description:

setI to store results */ repeat. until markerA or markerB at ... add info at markerA to setI. move both markers on. else. if info at markerA info at markerB ... – PowerPoint PPT presentation

Number of Views:92
Avg rating:3.0/5.0
Slides: 54
Provided by: glenn63
Category:

less

Transcript and Presenter's Notes

Title: AC1202 Sets 3' Set operations Union Intersection


1
AC1202Sets3. Set operationsUnion -
Intersection
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
Intersection of 2 setsMethodology
5
Intersection
  • Set intersection A ? B

U
B
A
10
1
5
11
9
3
13
7
12
6
Intersection 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
7
Intersection 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
8
Intersection 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
9
Intersection 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
10
Intersection 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
11
Intersection 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
12
Intersection 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
13
Intersection 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
14
Intersection 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
15
Intersection 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
16
Intersection 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
17
Intersection 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
18
Intersection 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
19
Intersection 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
20
Intersection 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
21
Pseudocode 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?

22
Psuedocode 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

24
After repeat
  • Dont need to do anything else
  • Intersection fully performed when we get to end
    of set marker A or B

25
Pseudocode 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

26
Now 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

27
Union of 2 setsMethodology
28
Union
  • Set union A ? B

U
B
A
10
1
5
11
9
13
3
7
12
29
Union 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
30
Union 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
31
Union 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
32
Union 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
33
Union 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
34
Union 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
35
Union 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
36
Union 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
37
Union 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
38
Union 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
39
Union 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
40
Union 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
41
Union 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
42
Union 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
43
Union 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
44
Union 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
45
Union 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
46
Pseudocode
  • 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?

47
Pseudocode 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

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

50
Pseudocode 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

51
Now 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

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