The Wayback Machine - https://web.archive.org/web/20241118223344/https://www.geeksforgeeks.org/reflexive-relation-on-set/
Open In App

Reflexive Relation on Set

Last Updated : 02 Jan, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

A relation is a subset of the cartesian product of a set with another set. A relation contains ordered pairs of elements of the set it is defined on. To learn more about relations refer to the article on “Relation and their types“.

What is a Reflexive Relation?

A relation R on a set A is called reflexive relation if 

(a, a) ∈ R ∀ a ∈ A, i.e. aRa for all a ∈ A, 
where R is a subset of (A x A), i.e. the cartesian product of set A with itself.

This means if element “a” is present in set A, then a relation “a” to “a” (aRa) should be present in relation R. If any such aRa is not present in R then R is not a reflexive relation.

A reflexive relation is denoted as:

IA = {(a, a): a ∈ A}

Example:

Consider set A = {a, b} and R = {(a, a), (b, b)}.
Here R is a reflexive relation as for both a and b, aRa and bRb are present in the set.

Properties of a Reflexive Relation

  1. Empty relation on a non-empty relation set is never reflexive.
  2. Relation defined on an empty set is always reflexive.
  3. Universal relation defined on any set is always reflexive.

How to verify a Reflexive Relation?

The process of identifying/verifying if any given relation is reflexive:

  • Check for the existence of every aRa tuple in the relation for all a present in the set.
  • If every tuple exists, only then the relation is reflexive. Otherwise, not reflexive.

Follow the below illustration for a better understanding:

Illustration:

Consider set A = {a, b} and a relation R = {{a, a}, {a, b}}.

For the element a in A: 
    => The pair {a, a} is present in R.
    => Hence aRa is satisfied.

For the element b in A:
    => The pair {b, b} is not present int R.
    => Hence bRb is not satisfied.

As the condition for ‘b’ is not satisfied, the relation is not reflexive.

Below is a code implementation of the approach.

C++




// C++ code to check if a set is reflexive
 
#include <bits/stdc++.h>
using namespace std;
 
class Relation {
public:
    bool checkReflexive(set<int> A, set<pair<int, int> > R)
    {
        // Property 1
        if (A.size() > 0 && R.size() == 0) {
            return false;
        }
        // Property 2
        else if (A.size() == 0) {
            return true;
        }
 
        for (auto i = A.begin(); i != A.end(); i++) {
 
            // Making a tuple of same element
            auto temp = make_pair(*i, *i);
 
            if (R.find(temp) == R.end()) {
 
                // If aRa tuple not exists in relation R
                return false;
            }
        }
 
        // All aRa tuples exists in relation R
        return true;
    }
};
 
// Driver code
int main()
{
    // Creating a set A
    set<int> A{ 1, 2, 3, 4 };
 
    // Creating relation R
    set<pair<int, int> > R;
 
    // Inserting tuples in relation R
    R.insert(make_pair(1, 1));
    R.insert(make_pair(1, 2));
    R.insert(make_pair(2, 2));
    R.insert(make_pair(2, 3));
    R.insert(make_pair(3, 2));
    R.insert(make_pair(3, 3));
 
    Relation obj;
 
    // R in not reflexive as (4, 4) tuple is not present
    if (obj.checkReflexive(A, R)) {
        cout << "Reflexive Relation" << endl;
    }
    else {
        cout << "Not a Reflexive Relation" << endl;
    }
 
    return 0;
}


Java




// Java code implementation for the above approach
import java.io.*;
import java.util.*;
 
class pair {
  int first, second;
  pair(int first, int second)
  {
    this.first = first;
    this.second = second;
  }
}
 
class GFG {
 
  static class Relation {
    boolean checkReflexive(Set<Integer> A, Set<pair> R)
    {
      // Property 1
      if (A.size() > 0 && R.size() == 0) {
        return false;
      }
 
      // Property 2
      else if (A.size() == 0) {
        return true;
      }
 
      for (var i : A) {
        if (!R.contains(new pair(i, i))) {
          // If aRa tuple not exists in relation R
          return false;
        }
      }
      // All aRa tuples exists in relation R
      return true;
    }
  }
 
  public static void main(String[] args)
  {
    // Creating a set A
    Set<Integer> A = new HashSet<>();
    A.add(1);
    A.add(2);
    A.add(3);
    A.add(4);
 
    // Creating relation R
    Set<pair> R = new HashSet<>();
 
    // Inserting tuples in relation R
    R.add(new pair(1, 1));
    R.add(new pair(1, 2));
    R.add(new pair(2, 2));
    R.add(new pair(2, 3));
    R.add(new pair(3, 2));
    R.add(new pair(3, 3));
 
    Relation obj = new Relation();
 
    // R in not reflexive as (4, 4) tuple is not present
    if (obj.checkReflexive(A, R)) {
      System.out.println("Reflexive Relation");
    }
    else {
      System.out.println("Not a Reflexive Relation");
    }
  }
}
 
// This code is contributed by lokeshmvs21.


Python3




class Relation:
    def checkReflexive(self, A, R):
        # Property 1
        if len(A) > 0 and len(R) == 0:
            return False
        # Property 2
        elif len(A) == 0:
            return True
 
        for i in A:
            if (i, i) not in R:
 
                # If aRa tuple not exists in relation R
                return False
 
        # All aRa tuples exists in relation R
        return True
 
 
# Driver code
if __name__ == '__main__':
 
    # Creating a set A
    A = {1, 2, 3, 4}
 
    # Creating relation R
    R = {(1, 1), (1, 2), (2, 2), (2, 3), (3, 2), (3, 3)}
 
    obj = Relation()
 
    # R in not reflexive as (4, 4) tuple is not present
    if obj.checkReflexive(A, R):
        print("Reflexive Relation")
    else:
        print("Not a Reflexive Relation")


C#




// C# code implementation for the above approach
 
using System;
using System.Collections.Generic;
 
class pair {
  public int first, second;
  public pair(int first, int second)
  {
    this.first = first;
    this.second = second;
  }
}
 
public class GFG {
 
  class Relation {
    public bool checkReflexive(HashSet<int> A,
                               HashSet<pair> R)
    {
      // Property 1
      if (A.Count > 0 && R.Count == 0) {
        return false;
      }
 
      // Property 2
      else if (A.Count == 0) {
        return true;
      }
 
      foreach(var i in A)
      {
        if (!R.Contains(new pair(i, i)))
        {
           
          // If aRa tuple not exists in relation R
          return false;
        }
      }
       
      // All aRa tuples exists in relation R
      return true;
    }
  }
 
  static public void Main()
  {
 
    // Creating a set A
    HashSet<int> A = new HashSet<int>();
    A.Add(1);
    A.Add(2);
    A.Add(3);
    A.Add(4);
 
    // Creating relation R
    HashSet<pair> R = new HashSet<pair>();
 
    // Inserting tuples in relation R
    R.Add(new pair(1, 1));
    R.Add(new pair(1, 2));
    R.Add(new pair(2, 2));
    R.Add(new pair(2, 3));
    R.Add(new pair(3, 2));
    R.Add(new pair(3, 3));
 
    Relation obj = new Relation();
 
    // R in not reflexive as (4, 4) tuple is not present
    if (obj.checkReflexive(A, R)) {
      Console.WriteLine("Reflexive Relation");
    }
    else {
      Console.WriteLine("Not a Reflexive Relation");
    }
  }
}
 
// This code is contributed by lokesh


Javascript




// JS code to check if a set is reflexive
 
function checkReflexive(A, R)
{
    let cnt = 0;
    // Property 1
    if (A.size > 0 && R.size == 0) {
        return false;
    }
    // Property 2
    else if (A.size == 0) {
        return true;
    }
 
    A.forEach(i => {
         
        // Making a tuple of same element
        let temp = [i, i];
 
        if (!R.has(temp)) {
 
            // If aRa tuple not exists in relation R
            cnt++;
        }
 
    });
 
 
    // All aRa tuples exists in relation R
    if(cnt==0)
        return true;
    else
        return false;
}
 
 
// Driver code
// Creating a set A
let A = new Set([ 1, 2, 3, 4 ]);
 
// Creating relation R
let R = new Set();
 
// Inserting tuples in relation R
R.add([1,1]);
R.add([1,2]);
R.add([2,2]);
R.add([2,3]);
R.add([3,2]);
R.add([3,3]);
R.add([3,3]);
 
// R in not reflexive as (4, 4) tuple is not present
if (checkReflexive(A, R)) {
    console.log("Reflexive Relation");
}
else {
    console.log("Not a Reflexive Relation");
}
 
// This code is contributed by akashish__


Output

Not a Reflexive Relation

Time Complexity: O(N * log M) where N is the size of the set and M is the number of pairs in the relation
Auxiliary Space: O(1)



Previous Article
Next Article

Similar Reads

Number of relations that are neither Reflexive nor Irreflexive on a Set
Given a positive integer N, the task is to find the number of relations that are neither reflexive nor irreflexive on a set of first N natural numbers. Since the count of relations can be very large, print it to modulo 109 + 7. A relation R on a set A is called reflexive, if no (a, a) € R holds for every element a € A.For Example: If set A = {a, b}
7 min read
Number of Reflexive Relations on a Set
Given a number n, find out the number of Reflexive Relation on a set of first n natural numbers {1, 2, ..n}. Examples : Input: n = 2Output: 4The given set A = {1, 2}. The following are reflexive relations on A * A :{{1, 1), (2, 2)}{(1, 1), (2, 2), (1, 2)}{(1, 1), (2, 2), (1, 2), (2, 1)}{(1, 1), (2, 2), (2, 1)} Input: n = 3Output: 64 Explanation :Re
3 min read
Reflexive Relations in Mathematics
Reflexive Relation is a mathematical or set-theoretical relation in which every element is related to itself, meaning that for every element 'x' in the set, the pair (x, x) is a part of the relation. Other than Reflexive, there are many types of relations such as Symmetric, Transitive, Identity, etc. In maths, relation is a subset of the cartesian
10 min read
Irreflexive Relation on a Set
A relation is a subset of the cartesian product of a set with another set. A relation contains ordered pairs of elements of the set it is defined on. To learn more about relations refer to the article on "Relation and their types". What is Irreflexive Relation? A relation R on a set A is called irreflexive relation if (a, a) ∉ R ∀ a ∈ A, where R is
6 min read
Symmetric Relation on a Set
A relation is a subset of the cartesian product of a set with another set. A relation contains ordered pairs of elements of the set it is defined on. To learn more about relations refer to the article on "Relation and their types". What is a Symmetric Relation? A relation R on a set A is called symmetric relation if and only if ∀ a, b ∈ A, if (a, b
7 min read
Anti-Symmetric Relation on a Set
A relation is a subset of the cartesian product of a set with another set. A relation contains ordered pairs of elements of the set it is defined on. To learn more about relations refer to the article on "Relation and their types". What is an Anti-Symmetric Relation? A relation R on a set A is called anti-symmetric relation if ∀ a, b ∈ A, if (a, b)
6 min read
Asymmetric Relation on a Set
A relation is a subset of the cartesian product of a set with another set. A relation contains ordered pairs of elements of the set it is defined on. To learn more about relations refer to the article on "Relation and their types". What is an Asymmetric Relation? A relation R on a set A is called asymmetric relation if ∀ a, b ∈ A, if (a, b) ∈ R the
6 min read
Transitive Relation on a Set
A relation is a subset of the cartesian product of a set with another set. A relation contains ordered pairs of elements of the set it is defined on. To learn more about relations refer to the article on "Relation and their types". What is a Transitive Relation? A relation R on a set A is called transitive relation if and only if ∀ a, b, c ∈ A, if
9 min read
Equivalence Relation on a Set
A relation is a subset of the cartesian product of a set with another set. A relation contains ordered pairs of elements of the set it is defined on. What is an Equivalence Relation?A relation R on a set A is called an equivalence relation if it is Reflexive Relation: (a, a) ∈ R ∀ a ∈ A, i.e. aRa for all a ∈ A.Symmetric Relation: ∀ a, b ∈ A, (a, b)
14 min read
Partial Order Relation on a Set
A relation is a subset of the cartesian product of a set with another set. A relation contains ordered pairs of elements of the set it is defined on. What is a Partial Order Relation? A relation R on a set A is called a partial order relation if it is Reflexive Relation: (a, a) ∈ R ∀ a ∈ A, i.e. aRa for all a ∈ A.Anti-Symmetric Relation: ∀ a, b ∈ A
15 min read
Find nth term of a given recurrence relation
Let an be a sequence of numbers, which is defined by the recurrence relation a1=1 and an+1/an=2n. The task is to find the value of log2(an) for a given n. Examples: Input: 5 Output: 10 Explanation: log2(an) = (n * (n - 1)) / 2 = (5*(5-1))/2 = 10Input: 100 Output: 4950 [Tex]\frac{a_{n+1}}{a_{n}}=2^{n} [/Tex][Tex]\frac{a_{n}}{a_{n-1}}=2^{n-1} [/Tex][
3 min read
What is the relation between the diagonals of a rhombus?
It is the subject of mathematics that deals with the study of geometrical shapes, calculations and figures. It discusses all equations and formulas based on different geometrical shapes and figures. It deals with both 2-dimensional shapes and 3 Dimensional figures. It deals with some of the parameters of geometrical shapes such as length, height, A
3 min read
Nth term of a recurrence relation generated by two given arrays
Given an integer N and two arrays F[] and C[] of size K that represent the first K terms and coefficient of first K terms of the below recurrence relation respectively. FN = C1*FN - 1 + C2*FN - 2 + C3*FN - 3 +....+ CK*FN - K. The task is to find the Nth term of the recurrence relation. Since the number can be very large take modulo to 109 + 7.Examp
15+ min read
Nth term of given recurrence relation having each term equal to the product of previous K terms
Given two positive integers N and K and an array F[] consisting of K positive integers. The Nth term of the recurrence relation is given by: FN = FN - 1 * FN - 2 * FN - 3 *.......* FN - K The task is to find the Nth term of the given recurrence relation. As the Nth term can be very large, print the Nth term modulo 109 + 7. Examples: Input: N = 5, K
15+ min read
Count of sets possible using integers from a range [2, N] using given operations that are in Equivalence Relation
Given an integer N, repeatedly choose two distinct integers from the range 2 to N and if their GCD is found to be greater than 1, insert them into the same set, as long as possible. The sets formed in Equivalence Relation. Therefore, if integers a and b are in the same set and integers b and c are in the same set, then integers a and c are also sai
7 min read
Find a relation between x and y such that the point (x, y) is equidistant from the points (7, 1) and (3, 5)
Mathematics is related to numbers and calculations. Mathematics is divided into different branches on the basis of the type of operations or calculations like algebra, geometry, arithmetic, etc. Geometry It is the branch of mathematics that deals with shape and their properties. Geometry that deals with points, lines, and planes in which coordinate
5 min read
Lexicographically smallest string formed by replacing characters according to the given relation
Given a string Str of N characters and two strings S1 and S2 of equal length where S1[i] and S2[i] are related to each other, the task is to find the lexicographically smallest string that can be obtained by replacing characters in Str with their related character. Examples: Input: S1 = "rat", S2 = "cbb", Str = "trrb"Output: accaExplanation: For th
10 min read
Check if every pair in Array B follows the same relation as their corresponding values in A
Given two Arrays A[] and B[] each of size N, the task is to check if the given arrays are valid or not, based on the following conditions: Every element in A at index i, will be mapped with the element in B at the same index only, i.e. (A[i] can be mapped with B[i] only)For any pair in A (A[i], A[j]), if A[i] > A[j], then its corresponding value
9 min read
Minimize sum of Array formed using given relation between adjacent elements
Given a binary string S of length N, consisting of 0's and 1's, the task is to find the minimum sum of the array of non-negative integers of length N+1 created by following the below conditions: If the ith number in the given binary string is 0, then the (i + 1)th number in the array must be less than the ith number.If the ith number in the given b
11 min read
Minimize Array sum by replacing elements such that Relation among adjacent elements in maintained
Given an array arr[] of size N, the task is to minimize the array sum after replacing array elements with positive integers in a way such the relation among the adjacent elements (pattern of the array)is maintained. Note: If arr[i] = arr[i+1] then in the new array they both can be the same or one can be smaller than the other. Examples: Input: arr[
7 min read
Generate lexicographically smallest Permutation of 1 to N where elements follow given relation
Given an integer N and an array arr[] of M pairs of type (Ai, Bi), the task is to generate the lexicographically smallest possible permutation of 1 to N such that every Ai, occurs before every Bi. Examples: Input: N = 4, arr[] = { {2, 1}, {3, 4}, {2, 4} }Output: 2 1 3 4Explanation: The following five permutations P satisfy the condition: (2, 1, 3,
10 min read
Create Binary Tree from given Array of relation between nodes
Given a 2D integer array where each row represents the relation between the nodes (relation[i] = [parenti, childi, isLefti]). The task is to construct the binary tree described by the 2D matrix and print the LevelOrder Traversal of the formed Binary Tree. Examples: Input: Relation[] = [[20, 15, 1], [20, 17, 0], [50, 20, 1], [50, 80, 0], [80, 19, 1]
12 min read
Find a relation between x and y such that the point (x, y) is equidistant from (6, 5) and (-4, 3)
Mathematics is a subject that is associated with numbers and calculations. And, according to the type of calculation mathematics is divided into different branches like algebra, geometry, arithmetic, etc. Geometry is the branch of mathematics that deals with shape and their properties. Geometry that deals with a point, lines, and planes in which co
5 min read
What is the relation between the perimeter and the side length of an equilateral triangle?
An equilateral triangle is a two-dimensional figure, where all the angles and the sides are exactly equal to each other. It is also known as the equiangular triangle since all of its angles are equivalent to 60° each. It is a regular polygon composed of three equal sides.  Properties of an equilateral triangle All three sides are equal.All the thre
4 min read
How to find a relation between two points?
Geometry is without a doubt one of the most significant areas of mathematics. Its importance is as great as that of arithmetic and algebra, both in our daily lives and in mathematical theory and practice. Geometry is used in mathematical theory and problems to determine the distance between two forms, the amount of space they take up, their sizes,
5 min read
Find a relation between x and y if the point (x, y) is equidistant from (3, 6) and (-3, 4)
Geometry is one of the oldest fields of mathematics, with a wide range of applications and influences on our daily lives. Geometry is a discipline of mathematics that studies the proportions, sizes, dimensions, forms, shapes, and angles of many objects in our environment. All the objects around us have a distinct shape, occupy a substantial amount
5 min read
Asymmetric Relation
A relation is a subset of the cartesian product of a set with another set. A relation contains ordered pairs of elements of the set it is defined on. A relation is a subset of the cartesian product of a set with another set. A relation contains ordered pairs of elements of the set it is defined on. Table of Content What is Relation in Maths?What is
7 min read
Antisymmetric Relation
Antisymmetric Relation is a type of binary relation on a set where any two distinct elements related to each other in one direction cannot be related in the opposite direction. For example, consider the relation "less than or equal to" (≤) on the set of integers. This relation is antisymmetric because if a ≤ b and b ≤ a, then a must be equal to b.
7 min read
Difference between Relation and Function
Relation defines how elements of one set relate to elements of another set whereas a Function is a special type of relation in which each element in the domain (input) is related to exactly one element in the codomain (output). This article explores relations and functions, highlighting their definitions, properties, differences, and applications i
7 min read
Inverse Relation
Inverse Relation: An inverse relation is the opposite of a given relation obtained by interchanging or swapping the elements of each ordered pair. In simple terms, if (x, y) is a point in a relation R, then (y, x) is an element in the inverse relation. In this article, we will learn about Inverse Relation including their domain, range, and other pr
8 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg