DS Sets
DS Sets
DS Sets
Compiled By
Ujjwal Rijal
rijalujjwal09@gmail.com
rijalujjwal09@gmail.com 1
Introduction to Set
rijalujjwal09@gmail.com 2
Meaning of a Set
rijalujjwal09@gmail.com 3
Notations of a Set
Description Method
In this method, a set is specified by a verbal
description.
For example: the set S of numbers 1, 2 and 3 is
designated as:
S = the set of positive integers less than
4. rijalujjwal09@gmail.com 5
Contd. ….
Tabulation Method
In this method, a set is specified by listing all the
elements in a set.
For example: we can write the set S of numbers
1, 2 and 3 as:
S = {1, 2, 3}.
Note that, each of the sets {1, 2, 3}, {2, 3, 1}
and {3, 2, 1} are all same.
rijalujjwal09@gmail.com 6
Contd. ….
rijalujjwal09@gmail.com 7
Contd. ….
rijalujjwal09@gmail.com 8
Different Set’s Terminologies
rijalujjwal09@gmail.com 15
Contd. ….
rijalujjwal09@gmail.com 16
Contd. ….
rijalujjwal09@gmail.com 18
Contd. ….
Union
• The union of two sets A and B, denoted by A∪B, is the set of
only those elements which belongs to either A or B or both A
and B i.e. consisting of the elements of at least one of the
sets A and B.
• Symbolically, we write this as:
A∪B = {x | x ∈ A or x ∈ B or x ∈ both A and B}
Here, A∪B is also called the logical sum of A and B and
sometimes read as A cup B.
Examples:
If A = {1, 2} and B = {a, b, c}, then A∪B = {1, 2, a, b,
c}.
If A = {a, b, c} and B = {c, d, e}, then A∪B = {a, b, c, d,
e}. Fig: Venn diagram of A∪B
rijalujjwal09@gmail.com 19
Contd. ….
Intersection
• The intersection of two sets A and B, denoted by A∩B, is the
set of only those elements which belongs to both A and B.
• Symbolically, we write this as:
A∩B = {x | x ∈ A and x ∈ B}
• Here, A∩B is also called the logical multiplication of A and
B and sometimes read as A cap B.
Examples:
If A = {1, 2, 3} and B = {3, 4}, then A∩B = {3}.
If A = {a, b, c, d} and B = {a, b, e, f}, then A∩B = {a,
b}.
If A = {1, 3, 5} and B = {2, 4}, then A∩B = Φ.
rijalujjwal09@gmail.com 20
Contd. ….
Complement of a Set
• Let set A be the subset of a universal set U. Then, the
complement of A with respect to U is the set of those
elements of U which do not belong to A.
• The complement of the set A is denoted by Ā or A’ or
Ac.
• Symbolically, we write this as:
Ac = {x | x ∈ U and x ∉ A}.
Examples:
If U = {1, 2, 3, 4, 5} and A = {2, 4}, then Ac = {1, 3,
5}.
If U = set of English alphabets and V = set of
Fig: Venn diagram of A c
or Ā
vowels, then , then Vc = set of consonants.
rijalujjwal09@gmail.com 21
Contd. ….
Difference
• Let A and B be the two sets and each set is the subset of a
universal set U. Then, A difference B denoted by A – B, is the
set of all those elements which belong to A but not to B.
• Symbolically, we write this as:
A – B = {x | x ∈ A and x ∉ B} = A∩Bc.
Also, B – A = {x | x ∈ B and x ∉ A} = B∩Ac.
• The difference, A – B, which is sometimes called A minus B is
also known as Complement of B with respect to A i.e.
A∩Bc.
Examples:
If A = {a, b, x, y} and B = {c, d, x, y}, then
A – B = {a, b} and B – A = {c, d}.
Thus, A – B ≠ B – A.
Fig: Venn diagram of A – B
rijalujjwal09@gmail.com 22
Set Identities
4. Associative Laws
i. A∪(B∪C) = (A∪B)∪C ii. A∩(B∩C) =
(A∩B)∩C
5. Distributive Laws
i. A∪(B∩C) = (A∪B)∩(A∪C)
ii. A∩(B∪C) = (A∩B)∪(A∩C)
rijalujjwal09@gmail.com 24
Contd. ….
rijalujjwal09@gmail.com 25
Contd. ….
rijalujjwal09@gmail.com 27
Contd. ….
Properties
• Computes the total number of elements that satisfy at
least one of several properties.
• It prevents the problem of double counting.
rijalujjwal09@gmail.com 31
Contd. ….
rijalujjwal09@gmail.com 33
Contd. ….
(Test Yourself)
rijalujjwal09@gmail.com 34
Applications of Inclusion-
Exclusion Principle
Counting derangements or permutations
• To determine the number of derangements or
permutations of n objects such that no object is in its
original position.
• As an example, we can consider the
derangements of the number in the following
cases:
For i = 1, the total number of derangements is 0.
For i = 2, the total number of derangements is 1. This is
21.
For i = 3, the total number of derangements is 2. This is
231 and 312.
And so on. 35
rijalujjwal09@gmail.com
Contd. ….
Counting intersections
• The principle of Inclusion-Exclusion, combined with
De-Morgan’s Law, can be used to count the
cardinality of the intersection of sets as well.
• Let be the complement of Ai with respect to some
universal set A such that Ai ⊆ A for each i. Then, we
have the following case, thereby turning the problem
of finding an intersection into the problem of finding a
union:
=
rijalujjwal09@gmail.com 36
Contd. ….
Graph coloring
• The inclusion-exclusion principle forms the basis of algorithms
for a number of NP-hard graph partitioning problems, such as
graph coloring.
• A well known application of the principle is the construction of
the chromatic polynomial of a given graph.
Bipartite graph perfect matchings
• The number of perfect matchings of a bipartite graph can be
calculated using this principle.
Number of onto functions
• The principle of inclusion-exclusion gives the number of onto
functions (surjective functions) between A and B, for each i in B
(i is not the image of the function) as:
(–1)i (n – j)k.
37
rijalujjwal09@gmail.com
Computer Representation of
Sets
• One method to represent set in computer is to store the
elements of set in an unordered list.
• If this method is used, then the operations of computing the
union, intersection and complement of sets would be time
consuming because each of these operations would require large
time in searching for elements.
• So, we use method for storing elements using an arbitrary
ordering of the elements of the universal set.
• Specify an arbitrary ordering of the elements of universal set U,
for instance a1, a2, a3, ……., an represent a subset A of U with bit
string of length n, where ith bit in the string is 1 if ai ∈ A and 0 if
ai ∉ A.
Computer Representation of Complement
• The bit string for the complement of a set is
obtained by replacing 0’s with 1’s and 1’s with 0’s.
38
rijalujjwal09@gmail.com
Contd. ….
rijalujjwal09@gmail.com 42