Computer Science and IT‹›⌂ 0:00 studied

Module 1 · 16 min read · Free chapter

Sets, relations, functions, posets and lattices

In short

A relation on n elements is a subset of A × A, so there are 2 to the power n² of them. Reflexive relations number 2 to the power n(n − 1) and symmetric ones 2 to the power n(n + 1)/2. A partial order is reflexive, antisymmetric and transitive, and a lattice needs a join and meet for every pair.

What are you preparing for? We show the practice and previous year questions of your exam.

Start from zero: lists, pairs and rules

A school keeps one register for the cricket club and another for the quiz club. Some students are in both, some in only one, and the principal wants to know how many students take part in at least one club. If cricket has 25 names, quiz has 18 and 7 names appear in both, the answer is 25+18−7=3625 + 18 - 7 = 36, because the 7 were counted twice.

Each register is a set: a collection of distinct objects. A pairing such as "student XX is the captain of team YY" is a relation, and a rule such as "each student gets exactly one roll number" is a function. When we arrange things by a rule like "is a prerequisite of", we get an order.

This chapter builds these four ideas carefully. They are the vocabulary of the whole of computer science, from database tables to type systems.

Why it matters. A database table is a relation, a hash function is a function, and a task schedule is a partial order. GATE asks you to count relations and functions, test their properties and read Hasse diagrams, all in a few minutes.

Sets and their operations

A set is a collection of distinct elements, written like A={1,2,3}A = \{1, 2, 3\}. Order and repetition do not matter, so {1,2,2,3}\{1, 2, 2, 3\} is the same set as {3,1,2}\{3, 1, 2\}. The empty set ∅\emptyset has no elements, and A is a subset of BB, written A⊆BA \subseteq B, when every element of A is in BB.

The basic operations are union A∪BA \cup B (in A or BB), intersection A∩BA \cap B (in both), difference A−BA - B (in A but not in BB) and complement A′A' (not in A, inside a fixed universal set UU). De Morgan's laws hold here just as in logic: (A∪B)′=A′∩B′(A \cup B)' = A' \cap B' and (A∩B)′=A′∪B′(A \cap B)' = A' \cup B'.

For counting, the key rule is ∣A∪B∣=∣A∣+∣B∣−∣A∩B∣\lvert A \cup B\rvert = \lvert A\rvert + \lvert B\rvert - \lvert A \cap B\rvert. The power set P(A)P(A) is the set of all subsets of A, and if ∣A∣=n\lvert A\rvert = n then ∣P(A)∣=2n\lvert P(A)\rvert = 2^n, because each element is either in or out of a subset. The Cartesian product A×BA \times B is the set of ordered pairs (a,b)(a, b), and it has ∣A∣×∣B∣\lvert A\rvert \times \lvert B\rvert elements.

Worked example. Let A={a,b,c}A = \{a, b, c\}. P(A)P(A) has 23=82^3 = 8 elements: ∅\emptyset, three singletons, three pairs and A itself. A×AA \times A has 3×3=93 \times 3 = 9 ordered pairs, which is the starting point for counting relations.

Why it matters. Every relation and function in this chapter is a subset of a Cartesian product. If you can count subsets, you can count relations, and that is a favourite GATE question type.

Relations and their four properties

A relation RR on a set A is any subset of A×AA \times A. We write aRba R b, or (a,b)∈R(a, b) \in R, when a is related to bb. Since A×AA \times A has n2n^2 pairs when ∣A∣=n\lvert A\rvert = n, the number of relations on A is 2n22^{n^2}. For n=3n = 3 that is 29=5122^9 = 512.

Four properties decide what kind of relation we have:

  • RR is reflexive when (a,a)∈R(a, a) \in R for every a in A, so every element is related to itself.
  • RR is symmetric when (a,b)∈R(a, b) \in R always brings (b,a)∈R(b, a) \in R with it.
  • RR is antisymmetric when (a,b)(a, b) and (b,a)(b, a) can both be in RR only if a=ba = b.
  • RR is transitive when (a,b)(a, b) and (b,c)(b, c) in RR always force (a,c)∈R(a, c) \in R.

It helps to picture RR as an n×nn \times n matrix of 0s0s and 1s1s. Reflexive means the whole diagonal is 1. Symmetric means the matrix equals its transpose, and antisymmetric means no off-diagonal cell and its mirror are both 1.

Worked example. On the integers, "a≤ba \le b" is reflexive, antisymmetric and transitive but not symmetric. "a and bb have the same remainder mod 5" is reflexive, symmetric and transitive. "a<ba < b" is not reflexive, but it is antisymmetric and transitive, because the antisymmetric condition is never triggered.

Why it matters. These four properties decide whether a relation is an equivalence (for grouping) or an order (for ranking). Every later idea in the chapter is built from them.

Counting relations of each kind

Counting relations becomes easy once you split the n×nn \times n matrix into two parts. There are nn diagonal cells (a, a), and the remaining n2−nn^2 - n cells form n(n−1)2\dfrac{n(n - 1)}{2} mirror pairs {(a,b),(b,a)}\{(a, b), (b, a)\} with a≠ba \ne b. Each property puts a rule on one or both parts, and we multiply the free choices.

Kind of relation Diagonal choices Each mirror pair Count
All relations 2 each 4 2n22^{n^2}
Reflexive 1 each (must be 1) 4 2n(n−1)2^{n(n - 1)}
Symmetric 2 each 2 (both or neither) 2n(n+1)22^{\frac{n(n + 1)}{2}}
Antisymmetric 2 each 3 (not both) 2n×3n(n−1)22^n \times 3^{\frac{n(n - 1)}{2}}
Asymmetric 1 each (must be 0) 3 3n(n−1)23^{\frac{n(n - 1)}{2}}
Reflexive and symmetric 1 each 2 2n(n−1)22^{\frac{n(n - 1)}{2}}

Worked example. Take n=3n = 3. There are 3 diagonal cells and 3 mirror pairs. Reflexive relations number 26=642^6 = 64, symmetric relations 23×23=642^3 \times 2^3 = 64, antisymmetric relations 23×33=2162^3 \times 3^3 = 216, and relations that are both reflexive and symmetric 23=82^3 = 8.

Transitive relations have no simple formula, so exams ask only small cases. On a 2-element set, 13 of the 16 relations are transitive. The three that fail are {(a,b),(b,a)}\{(a, b), (b, a)\}, {(a,b),(b,a),(a,a)}\{(a, b), (b, a), (a, a)\} and {(a,b),(b,a),(b,b)}\{(a, b), (b, a), (b, b)\}, since each has (a,b)(a, b) and (b,a)(b, a) without both loops.

Why it matters. This "split the matrix" trick turns a scary counting question into one line of multiplication. It also trains the habit of counting by independent choices, which the combinatorics chapter uses everywhere.

Equivalence relations, partitions and closures

A relation that is reflexive, symmetric and transitive is an equivalence relation. It groups elements that are "the same in some respect". The equivalence class [a] is the set of all elements related to a, and any two classes are either identical or disjoint.

So an equivalence relation cuts the set into non-overlapping, non-empty pieces that cover everything, which is a partition. The reverse is also true: every partition defines the equivalence relation "is in the same piece as". Counting equivalence relations is therefore the same as counting partitions, and these counts are the Bell numbers 1, 2, 5, 15, 52 for n=1n = 1, 2, 3, 4, 5.

Worked example. "Same remainder when divided by 3" on the integers gives three classes: [0]={…,−3,0,3,6,… }[0] = \{\dots, -3, 0, 3, 6, \dots \}, [1] and [2]. On the set {a,b,c}\{a, b, c\} the 5 equivalence relations match the 5 partitions: {abc}, {a}{bc}, {b}{ac}, {c}{ab} and {a}{b}{c}\{a\}\{b\}\{c\}.

When a relation lacks a property, the closure is the smallest relation containing it that has the property. The reflexive closure adds all loops (a, a), and the symmetric closure adds every reversed pair, giving R∪R−1R \cup R^{-1}. The transitive closure R+R^{+} adds every pair (a,c)(a, c) joined by a path, and on nn elements it equals R∪R2∪⋯∪RnR \cup R^2 \cup \dots \cup R^n, which Warshall's algorithm finds in Θ(n3)\Theta (n^3) steps.

Why it matters. Equivalence classes are behind modular arithmetic, minimising automata and grouping records by key. The transitive closure answers "can I reach cc from a?", which is the heart of reachability in graphs and of recursive database queries.

Functions and how to count them

A function ff from A to BB assigns to every element of A exactly one element of BB. A is the domain, BB the codomain, and the set of values actually taken is the range. A function is one-one (injective) when different inputs give different outputs, onto (surjective) when every element of BB is hit, and a bijection when it is both.

Now let ∣A∣=m\lvert A\rvert = m and ∣B∣=n\lvert B\rvert = n, so each of the mm inputs can go to any of nn outputs, so there are nmn^m functions in all. For a one-one function the first input has nn choices, the next n−1n - 1, and so on, so there are n!(n−m)!\dfrac{n!}{(n - m)!} one-one functions when m≤nm \le n and none when m>nm > n. When m=nm = n, the one-one functions are exactly the bijections, and there are n!n! of them.

Counting onto functions needs inclusion–exclusion. We subtract the functions that miss at least one output, add back those that miss two, and so on. The result is the sum over k=0k = 0 to nn of (−1)k(nk)(n−k)m(-1)^k \binom{n}{k}(n - k)^m.

Worked example. From a 4-element set to a 3-element set there are 34=813^4 = 81 functions and no one-one functions. The onto functions number 34−3×24+3×14=81−48+3=363^4 - 3 \times 2^4 + 3 \times 1^4 = 81 - 48 + 3 = 36.

A function has an inverse f−1f^{-1} exactly when it is a bijection. Composition (g∘f)(x)=g(f(x))(g \circ f)(x) = g(f(x)) keeps the good properties: if ff and gg are both one-one, so is g∘fg \circ f, and the same holds for onto.

Why it matters. Hash functions, encodings and encryption are all functions, and whether they are one-one decides whether data can be recovered. Counting functions also shows up in GATE as "how many ways to assign mm jobs to nn machines".

Partial orders and Hasse diagrams

A relation that is reflexive, antisymmetric and transitive is a partial order, usually written ≤\le, and a set with one is a poset. The classic examples are numbers under ≤\le, subsets under ⊆\subseteq, and positive integers under "divides". It is "partial" because some pairs may be incomparable: neither 2 divides 3 nor 3 divides 2.

If every pair is comparable, the order is a total order or chain, like the integers under ≤\le. A Hasse diagram draws a poset economically. It leaves out the loops (reflexivity is understood), leaves out every edge implied by transitivity, and puts smaller elements lower so that arrows are not needed.

In a poset, an element is maximal if nothing is above it and greatest if it is above everything. Minimal and least are defined the same way downward. A poset can have several maximal elements but at most one greatest element.

For two elements a and bb, an upper bound is any element above both, and the least upper bound (lub, or join a∨ba \lor b) is the smallest such element when it exists. The greatest lower bound (glb, or meet a∧ba \land b) is defined the same way downward. In the divisors of 12 under "divides", the join of 4 and 6 is 12 (their lcm) and the meet is 2 (their gcd).

Worked example. Take {2,3,4,6,9}\{2, 3, 4, 6, 9\} under "divides". The minimal elements are 2 and 3, and the maximal elements are 4, 6 and 9. There is no least or greatest element, and the pair 4, 9 has no upper bound at all inside this set.

Why it matters. Task scheduling with prerequisites, class inheritance and version histories are all posets. Topological sorting, which every build system and course planner uses, is just extending a partial order to a total order.

Lattices, distributive and complemented lattices

A lattice is a poset in which every pair of elements has both a join and a meet. Subsets of SS under ⊆\subseteq form a lattice with join ∪\cup and meet ∩\cap. The divisors of nn under "divides" form a lattice DnD_n with join lcm and meet gcd. Every finite lattice has a least element 0 and a greatest element 1, so it is bounded.

A lattice is distributive when a∧(b∨c)=(a∧b)∨(a∧c)a \land (b \lor c) = (a \land b) \lor (a \land c) for all a, bb, cc. Two five-element lattices are the standard failures. The diamond M3M_3 has 0, 1 and three incomparable middle elements, and the pentagon N5N_5 has 0, 1, a two-element chain on one side and a single element on the other. A lattice is distributive exactly when it contains neither of these as a sublattice.

In a bounded lattice, bb is a complement of a when a∨b=1a \lor b = 1 and a∧b=0a \land b = 0. A lattice where every element has a complement is complemented. In a distributive lattice an element has at most one complement, while in M3M_3 each middle element has two.

Worked example. In D30={1,2,3,5,6,10,15,30}D_{30} = \{1, 2, 3, 5, 6, 10, 15, 30\}, the complement of 6 is 5, since lcm⁡(6,5)=30\operatorname{lcm} (6, 5) = 30 and gcd⁡(6,5)=1\gcd (6, 5) = 1. In D12D_{12}, the element 2 has no complement, because the only dd with gcd⁡(2,d)=1\gcd (2, d) = 1 are 1 and 3, and neither gives lcm 12. In general DnD_n is always distributive and is complemented exactly when nn is square-free.

A Boolean algebra is a lattice that is both distributive and complemented. Every finite Boolean algebra has 2n2^n elements and is a copy of the power set of an n-element set, with ∪\cup, ∩\cap and complement. So D30D_{30}, with 8=238 = 2^3 elements, is a Boolean algebra, and this structure is exactly the algebra of logic gates used in Boolean algebra and minimisation.

Why it matters. Lattices appear in data-flow analysis in compilers, in security levels and in the algebra of circuits. GATE asks whether a given Hasse diagram is a lattice, whether it is distributive, and how many complements an element has.

Descriptive lens: how written papers ask this

Written papers for scientist posts and university-level tests ask about this chapter in four ways:

  1. Prove that a given relation is an equivalence relation and describe its classes.
  2. Derive a counting formula, such as the number of symmetric or antisymmetric relations, with a clear argument.
  3. Draw a Hasse diagram and identify maximal, minimal, greatest and least elements and bounds.
  4. Decide whether a lattice is distributive or complemented, with a reason or a counter-example.

Practice questions (our own, not previous-year questions).

  1. Prove that the relation "a−ba - b is divisible by 4" on the integers is an equivalence relation, and list its equivalence classes. (8 marks)
  2. Derive the number of antisymmetric relations on a set of nn elements, and evaluate it for n=4n = 4. (10 marks)
  3. Draw the Hasse diagram of D36D_{36} and decide whether it is a distributive lattice and whether it is complemented. (12 marks)

Answer plan for question 2.

  • Explain that a relation on nn elements is a choice of 0 or 1 for each cell of an n×nn \times n matrix.
  • Note that the nn diagonal cells are not restricted by antisymmetry, so they give 2n2^n choices.
  • Group the other cells into n(n−1)2\dfrac{n(n - 1)}{2} mirror pairs, each of which may hold neither, only (a,b)(a, b) or only (b,a)(b, a).
  • Multiply the independent choices to get 2n×3n(n−1)22^n \times 3^{\frac{n(n - 1)}{2}}.
  • Evaluate it for n=4n = 4 as 24×36=16×729=11,6642^4 \times 3^6 = 16 \times 729 = 11{,}664.

Quick revision

  • ∣A∪B∣=∣A∣+∣B∣−∣A∩B∣\lvert A \cup B\rvert = \lvert A\rvert + \lvert B\rvert - \lvert A \cap B\rvert, ∣P(A)∣=2n\lvert P(A)\rvert = 2^n and ∣A×B∣=mn\lvert A \times B\rvert = mn.
  • A set of nn elements has 2n22^{n^2} relations.
  • Reflexive relations number 2n(n−1)2^{n(n - 1)}, and symmetric relations number 2n(n+1)22^{\frac{n(n + 1)}{2}}.
  • Antisymmetric relations number 2n×3n(n−1)22^n \times 3^{\frac{n(n - 1)}{2}}.
  • Equivalence relations match partitions, and their counts are the Bell numbers 1, 2, 5, 15, 52.
  • There are nmn^m functions from mm elements to nn elements, and n!(n−m)!\dfrac{n!}{(n - m)!} of them are one-one.
  • Onto functions from 4 elements to 3 elements number 36 by inclusion–exclusion.
  • A partial order is reflexive, antisymmetric and transitive, and a Hasse diagram drops loops and transitive edges.
  • A lattice needs a join and a meet for every pair, and M3M_3 and N5N_5 are the non-distributive tests.
  • DnD_n is complemented exactly when nn is square-free, and a finite Boolean algebra has 2n2^n elements.

Check yourself

Exam-style questions on this chapter. Pick an answer to see the explanation. 0 of 15 answered

  1. If ∣A∣=5\lvert A\rvert = 5, the number of elements in the power set of A is:

  2. In a class, 25 students play cricket, 18 play chess and 7 play both. How many play at least one?

  3. The relation 'a<ba < b' on the integers is:

  4. How many relations are there on a set of 3 elements?

  5. The number of symmetric relations on a set of 3 elements is:

  6. The number of antisymmetric relations on a set of 3 elements is:

  7. How many equivalence relations are there on a set of 4 elements?

  8. The symmetric closure of a relation RR is:

  9. The number of one-one functions from a 3-element set to a 5-element set is:

  10. The number of onto functions from a 4-element set to a 3-element set is:

  11. In a Hasse diagram, which of these is drawn?

  12. In the poset {2,3,4,6,9}\{2, 3, 4, 6, 9\} under 'divides', the maximal elements are:

  13. Which divisor lattice DnD_n is complemented?

  14. A finite Boolean algebra can have how many elements?

  15. In GATE CS, partial orders and lattices are part of:

Previous year questions

Previous year questions for UPSC CSE will appear here once each one is checked word for word against the official question paper.

Learn with AI

A tutor that asks instead of telling, a partner that argues the other side, and this chapter told through your own district.

Tell the tutor what you are stuck on. It will guide you with questions and give the answer after a few tries.

🙋 Raise a doubt or feedback

Stuck somewhere, or spotted a mistake? Write to the ExamHike team. We usually reply within a day, and you will see the reply on My progress.

Log in to ask a doubt or send feedback →

Frequently asked questions

How many relations are there on a set of n elements?

There are 2 to the power n², since each of the n² ordered pairs is in or out.

How many onto functions go from 4 elements to 3 elements?

By inclusion–exclusion, 3⁴ − 3 × 2⁴ + 3 = 36.

What is the number of equivalence relations on 4 elements?

It is the Bell number B₄ = 15, the number of partitions of the set.

When is the divisor lattice Dₙ a Boolean algebra?

Exactly when n is square-free, as with D₃₀; it is always distributive.

More in this module: Discrete mathematics

Official sources

Facts in this chapter were checked against these official sources.

  1. GATE 2026 Computer Science and Information Technology (CS) syllabus, IIT Guwahati
  2. NPTEL Discrete Mathematics (IIT Ropar, 106106183): sets, relations, functions, partial orders and lattices