Fundamentals of Programming: Set operators

From Wikibooks, open books for an open world
Jump to: navigation, search

UNIT 1 - ⇑ Fundamentals of Programming ⇑

← Logical bitwise operators Set operators


Sets describe collections of things such as numbers or people. For the exam you need to be familiar with the following terminology:

  • Membership
  • Union
  • Difference
  • Intersection

For the following examples we are going to use two sets. To define a set use the curly brackets {} and place all values of that set inside them.

Set A: All even naturals up to and including 12 = {0,2,4,6,8,10,12}
Set B: Natural multiples of 3 up to and including 12 = {0,3,6,9,12}

Membership[edit]

Membership defines what is contained in a particular set, it makes claims as to a particular things set membership. Set membership means "is an element of" and is written by ∈. The reverse "is NOT an element of" is written by ∉. Let's take a look at some examples:

Monday ∈ WeekDays (Monday is an element of WeekDays)
Monday ∉ Colours   (Monday is NOT an element of Colours)

Let us take a look at the two sets we are using as examples:

  • 0 ∈ A
  • 2 ∈ A
  • 0 ∈ B
  • 6 ∈ A AND 6 ∈ B
  • 7 ∉ B
  • 89 ∉ A

Union[edit]

The union of two sets:
~A \cup B

When we have sets we often want to perform actions on them, for example trying to find all the unique members contained in two sets. The union(∪) of A and B, denoted by A ∪ B, is the set of all things which are members of either A or B. It is similar to the boolean function OR. The union command allows us to perform this action:

  • {1, 2} ∪ {2,3,4} ={1,2,3,4}. (notice that we don't count 2 twice)
  • {1, 2} ∪ {red, white} ={1, 2, red, white}.
  • {1, 2, green} ∪ {red, white, green} ={1, 2, red, white, green}.
  • {1, 2} ∪ {1, 2} = {1, 2}.
Exercise: Union

Let us take a look at the two sets we are using as examples:

A ∪ B

Answer :

={0,2,3,4,6,8,9,10,12}.

{3,4} ∪ B

Answer :

={0,3,4,6,9,12}.

A ∪ {7,13,red}

Answer :

={0,2,4,6,7,8,10,12,13,red}.

B ∪ B

Answer :

={0,3,6,9,12}.

Difference[edit]

The difference of two sets:
A \setminus B

Difference(\) is used to work out the members unique to one set, members that are not also in the other set. It is similar to the boolean function NAND. A \setminus B = \{ x\in A \, | \, x \notin B \}. . Meaning elements that are members of A but not members of B. Let us look at some examples:

  • {1,2,3} ∖ {2,3,4}   =   {1}
  • {2,3,4} ∖ {1,2,3}   =   {4}
Exercise: Difference

Let us take a look at the two sets we are using as examples:

A ∖  B    =

Answer :

{2,4,8,10}

B ∖  A    =

Answer :

{3,9}

B ∖  {1,2,3,5,7,11}    =

Answer :

{0,6,9,12}

A ∖  {0,1,1,2,3,5,8,13}    =

Answer :

{4,6,10,12}

Intersection[edit]

The intersection of two sets:
~A \cap B

We use intersection(∩) to find out all the elements that are members of one set, and also at the same time members of another set. It is similar to the boolean function AND. : xAB = xA AND xB. Let us look at some examples:

  • {1, 2, 3} ∩ {2, 3, 4} = {2, 3}
  • {1, 2, 3} ∩ {bear,hen,squirrel} = Ø (this means an empty set)
  • {cat, dog, canary} ∩ {wolf, canary, whale, cat} = {cat, canary}
Exercise: Intersection
Let us take a look at the two sets we are using as examples: A ∩ B =

Answer :

{0,6,12}

A ∩ {goat,4,6,9} =

Answer :

{4,6}

A ∩ {goat,3,9} =

Answer :

It has no intersected values (Ø)

B ∩ {3,5,7,9,11} =

Answer :

{3,9}

Programming[edit]

Not every programming language offers the ability to perform set theory calculations out of the box. In Visual Basic it has only been available since VB.NET 2008, using the LINQ libraries that you have to import to get it working:

Imports System.Linq
Imports System.Xml.Linq
 
Module Module1
    Sub Main()
 
    Dim A() As Integer = {0,2,4,6,8,10,12}
    Dim B() As Integer = {0,3,6,9,12}
 
    Dim unionAB = A.Union(B)
    Console.WriteLine("Union of A and B:")
    For Each n In unionAB
       Console.WriteLine(n)
    Next
 
    Dim differenceAB = A.Except(B) 'Except performs the same function as Difference
    Console.WriteLine("Difference of A and B:")
    For Each n In differenceAB
       Console.WriteLine(n)
    Next
 
    Dim intersectAB = A.Intersect(B)
    Console.WriteLine("Intersection of A and B:")
    For Each n In intersectAB
       Console.WriteLine(n)
    Next
 
    End Sub
End Module