MCS-212 · Block 2 Unit 1

Sets, Relations and Functions

Section 3 of 9

Block 2 · Unit 1Sets, Relations and Functions11 min · Section 3 of 9

1.3Operations on sets

Covers IGNOU MCS-212 §1.3.1, E9–E14

Just as numbers can be added and multiplied, sets can be combined. A search engine combining “pages with word X” and “pages with word Y”, or a database query joining two tables, is doing set operations. This section defines the five basic ones.

The five operations

Sets with A∩B=∅A \cap B = \emptyset are called disjoint.

OperationLogic wordVenn regionCommutative?
A∪BA \cup Borboth circlesyes
A∩BA \cap Bandoverlapyes
A∖BA \setminus Band notAA without the overlapno
A′A'notoutside AA—
A Δ BA \,\Delta\, Bexclusive orboth circles without the overlapyes

Venn diagrams of the five set operations

Set operations in a program

E13 asks for a C program. A compact way to store a set of small numbers 0,…,310, \dots, 31 is one unsigned int whose bit ii is 1 when ii is in the set. Then union is |, intersection is &, difference is & ~, and symmetric difference is ^ — exactly the Boolean algebra B32B^{32} of Block 1, Unit 3.

C · set_ops.c
#include <stdio.h>

/* Print the elements 0..31 whose bits are 1 in s, e.g. {1, 2, 3} */
static void print_set(const char *name, unsigned int s)
{
    printf("%s = {", name);
    int first = 1;
    for (int i = 0; i < 32; i++) {
        if (s & (1u << i)) {
            printf(first ? "%d" : ", %d", i);
            first = 0;
        }
    }
    printf("}\n");
}

/* Build a set from an array of small numbers */
static unsigned int make_set(const int *items, int n)
{
    unsigned int s = 0;
    for (int i = 0; i < n; i++) {
        s |= 1u << items[i];
    }
    return s;
}

int main(void)
{
    const int a_items[] = {1, 2, 3, 4, 5, 6};
    const int b_items[] = {4, 5, 6, 7, 8, 9};
    unsigned int a = make_set(a_items, 6);
    unsigned int b = make_set(b_items, 6);

    print_set("A union B", a | b);
    print_set("A intersect B", a & b);
    print_set("A minus B", a & ~b);
    print_set("B minus A", b & ~a);
    print_set("A delta B", a ^ b);
    return 0;
}
Output
A union B = {1, 2, 3, 4, 5, 6, 7, 8, 9}
A intersect B = {4, 5, 6}
A minus B = {1, 2, 3}
B minus A = {7, 8, 9}
A delta B = {1, 2, 3, 7, 8, 9}

Key takeaways

  • A∪BA \cup B: in either; A∩BA \cap B: in both; A∖BA \setminus B: in AA but not BB; A′A': in UU but not AA.
  • A Δ B=(A∖B)∪(B∖A)A \,\Delta\, B = (A \setminus B) \cup (B \setminus A): in exactly one of the two sets.
  • Union, intersection and symmetric difference are commutative; difference is not.
  • A∖B=B∖AA \setminus B = B \setminus A and A∩B=A∪BA \cap B = A \cup B both hold only when A=BA = B.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

If A={1,2,3}A = \{1, 2, 3\} and B={2,3,4}B = \{2, 3, 4\}, what is A Δ BA \,\Delta\, B?

Show answer

Answer: B. {1,4}\{1, 4\}

Elements in exactly one set: 1 (only in AA) and 4 (only in BB). Option 1 is A∩BA \cap B, the elements in both.

Question 2 · multiple choice

With U={1,…,8}U = \{1, \dots, 8\} and A={2,4,6,8}A = \{2, 4, 6, 8\}, what is A′A'?

Show answer

Answer: A. {1,3,5,7}\{1, 3, 5, 7\}

A′=U∖AA' = U \setminus A. Option 4 includes 9, which is not in UU.

Question 3 · multiple choice

For which sets is A∖B=B∖AA \setminus B = B \setminus A?

Show answer

Answer: C. Exactly when A=BA = B

Both differences must be empty, which gives A⊆BA \subseteq B and B⊆AB \subseteq A. Option 1 is the printed answer to E12; it misses cases such as A=B={1}A = B = \{1\}.

Question 4 · multiple choice

In the C program, which expression gives the set A∖BA \setminus B?

Show answer

Answer: D. a & ~b

Keep the bits of a whose bits in b are 0. Option 3 is B∖AB \setminus A.

Question 5 · multiple choice

If AA and BB are disjoint, what is A Δ BA \,\Delta\, B?

Show answer

Answer: C. A∪BA \cup B

With no common elements, every element of A∪BA \cup B is in exactly one set. Option 1 is A∩BA \cap B for disjoint sets.

Question 6 · short answer

Prove that A∩B=A∪BA \cap B = A \cup B implies A=BA = B.

Show worked answer

Assume A∩B=A∪BA \cap B = A \cup B. Take any x∈Ax \in A. Then x∈A∪B=A∩Bx \in A \cup B = A \cap B, so x∈Bx \in B; hence A⊆BA \subseteq B. Similarly, any x∈Bx \in B is in A∪B=A∩BA \cup B = A \cap B, so x∈Ax \in A; hence B⊆AB \subseteq A. Therefore A=BA = B. (Conversely, if A=BA = B then A∩B=A=A∪BA \cap B = A = A \cup B.)

↑ ↓ move↵ openTab switch type