Lecture 02 - Set and Operations
Lecture 02 - Set and Operations
Example:
▪ For the set 𝐴={1,2,3} the cardinality is: ∣𝐴∣=3
#include <iostream>
A= {1, 2, 3, 4…….99, 100} |A| #include <set>
=100 int main() {
// Define a set with some elements
B= {2, 4, 6}; |B| std::set<int> mySet = {10, 20, 30, 40};
=3
// Find the cardinality (size) of the set
C= {{1}}; | int cardinality = mySet.size();
C|=1
// Output the cardinality
D= {2, {21}}; std::cout << "The cardinality of the set is: " << cardinality <<
std::endl;
|D|=2
return 0;
}
The cardinality of the set is: 4
Here 2 and {21} are the two
elements of set D.
Activity
Teacher collects submissions from students but notices that some students submitted
more than once. How can the teacher determine the actual number of students who
submitted?
A store manager has a list of product IDs for every item on the shelves. How can the
manager determine the total number of different products in the store?
Activity
Activity - Solution
2^n
Example
If a set A={1,2,3} has n=3 elements, then the power set of A has:
2^3 = 8
subsets, which are:
{},{1},{2},{3},{1,2},{1,3},{2,3},{1,2,3}.
So, the power set of a set with n elements has 2^n subsets in total.
Cartesian Product
Cartesian Product
The Cartesian product of two sets A and B,
denoted by A × B,
The Cartesian Product of two sets A and B is the set of all possible ordered pairs where the first element is
from A and the second element is from B.
Cartesian Products
1. A×B ={(1,1),(1,2),(2,1),(2,2)}
2. B×A={(1,1),(1,2),(2,1),(2,2)}
● In this case, both products yield the same set of ordered pairs:
𝐴={1,2}
𝐵={𝑥,𝑦,𝑧} then:
∣𝐴×𝐵∣ = ∣𝐴∣ × ∣𝐵∣
=2×3
=6
Cartesian Product - Example
• Product Options: For a product with different color and size options:
• Colors: C = {Red, Blue}
• Sizes: S = {Small, Medium}
• Cartesian product C × S gives all possible product combinations: (Red, Small),
(Red, Medium), (Blue, Small), (Blue, Medium).
Tuples
Cartesian Product
Tuples
▪ Unlike sets, Tuples are ordered
When we say that tuples are ordered, it means that the sequence in which the elements of the tuple are
arranged is important. The position of each element matters, and changing the order of the elements will result
in a different tuple.
Number of Always contains exactly two elements (e.g., (a,b)) Can contain any number of elements (e.g., (a,b,c) or (a,b,c,d))
Elements
Dimensions Specifically a 2D structure Can represent n-dimensional structures (e.g., 1D, 2D, 3D, etc.)
Context of Often used in mathematics and coordinate systems Used in various fields, including programming and
Use
data representation
Order The order of the two elements is significant; changing The order of the elements is significant; changing
the order creates a different pair the order creates a different tuple
Example (3,4) (1,2,3) or (a,b,c,d)
Summary
● An ordered pair (a, b) is a specific case of a tuple that contains exactly two elements, while a tuple can contain any number of
elements.
● Ordered pair (a, b) represents a relationship between the two values
● Both are ordered, meaning that the arrangement of elements matters, but tuples are more versatile in terms of the number of elements
they can include.
Set Operators
Venn diagrams
▪ a set can be visualized with a circle, which we regard as enclosing all the elements
of the set.
▪ Such diagrams can illustrate how sets combine using various operations
▪ To Visualizing Sets and set operations:
▪ Graphical representation of sets
▪ Box: Universal set of English Alphabets
▪ Circle: Set of vowels
▪ Use to visually represent union, intersection, and difference.
▪ Note: individual elements are usually not written in a venn Diagram
▪ Examples in coming slides: A Venn diagram illustrating A ∪ B, A ∩ B, and A - B.
Venn diagrams
▪
Set Operators
• Union (∪):
The union of two sets A and B is a set containing all
elements of A and B.
• Notation: A ∪ B.
Example:
• If A = {1, 2, 3} and B = {3, 4, 5}
• then A ∪ B = {1, 2, 3, 4, 5}.
Union of sets
Set Operators
• Intersection (∩):
The intersection of two sets A and B is a set
containing all elements that are both in A and B.
• Notation: A ∩ B.
• Example: If A = {1, 2, 3} and B = {3, 4, 5},
• then A ∩ B = {3}.
Set Operators
• Difference (or Relative Difference) (A - B):
The difference of sets A and B is a set containing all
elements that are in A but not in B.
• Notation: A - B ={x ∣ x ∈ A and x ∉ B}
Example:
If A = {1, 2, 3} and B = {3, 4, 5},
then A - B = {1, 2}
Draw Venn Diagram of B-A
Set operations: Symmetric Difference
▪ The symmetric difference of two sets A and B, denoted by A △ B or
A\B
▪ For example,
▪ the symmetric difference of the sets {1,2,3} and {3,4} is {1,2,4}
Set Operators
Complement (A' or Ac):
The complement of a set A refers to all elements in
the universal set U that are not in A.
• Notation: A'.
• Example: If U = {1, 2, 3, 4, 5} and A = {1, 2, 3},
• then A' = {4, 5}
▪
U A
Set operations: Complement of a
set
Class Activity
▪
▪
Set Identities
Set Identities - Self study
Textbook:
▪ Main Text:
▪ K. Rosen: Discrete Structures and its Applications
▪ Weekly Readings:
▪ L. Lovasz: Discrete Mathematics, Elementary and Beyond
▪ Reference:
▪ R. J. Wilson: Graph Theory