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
- Empty relation on a non-empty relation set is never reflexive.
- Relation defined on an empty set is always reflexive.
- 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++
#include <bits/stdc++.h>
using namespace std;
class Relation {
public :
bool checkReflexive(set< int > A, set<pair< int , int > > R)
{
if (A.size() > 0 && R.size() == 0) {
return false ;
}
else if (A.size() == 0) {
return true ;
}
for ( auto i = A.begin(); i != A.end(); i++) {
auto temp = make_pair(*i, *i);
if (R.find(temp) == R.end()) {
return false ;
}
}
return true ;
}
};
int main()
{
set< int > A{ 1, 2, 3, 4 };
set<pair< int , int > > 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;
if (obj.checkReflexive(A, R)) {
cout << "Reflexive Relation" << endl;
}
else {
cout << "Not a Reflexive Relation" << endl;
}
return 0;
}
|
Java
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)
{
if (A.size() > 0 && R.size() == 0 ) {
return false ;
}
else if (A.size() == 0 ) {
return true ;
}
for (var i : A) {
if (!R.contains( new pair(i, i))) {
return false ;
}
}
return true ;
}
}
public static void main(String[] args)
{
Set<Integer> A = new HashSet<>();
A.add( 1 );
A.add( 2 );
A.add( 3 );
A.add( 4 );
Set<pair> R = new HashSet<>();
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();
if (obj.checkReflexive(A, R)) {
System.out.println( "Reflexive Relation" );
}
else {
System.out.println( "Not a Reflexive Relation" );
}
}
}
|
Python3
class Relation:
def checkReflexive( self , A, R):
if len (A) > 0 and len (R) = = 0 :
return False
elif len (A) = = 0 :
return True
for i in A:
if (i, i) not in R:
return False
return True
if __name__ = = '__main__' :
A = { 1 , 2 , 3 , 4 }
R = {( 1 , 1 ), ( 1 , 2 ), ( 2 , 2 ), ( 2 , 3 ), ( 3 , 2 ), ( 3 , 3 )}
obj = Relation()
if obj.checkReflexive(A, R):
print ( "Reflexive Relation" )
else :
print ( "Not a Reflexive Relation" )
|
C#
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)
{
if (A.Count > 0 && R.Count == 0) {
return false ;
}
else if (A.Count == 0) {
return true ;
}
foreach ( var i in A)
{
if (!R.Contains( new pair(i, i)))
{
return false ;
}
}
return true ;
}
}
static public void Main()
{
HashSet< int > A = new HashSet< int >();
A.Add(1);
A.Add(2);
A.Add(3);
A.Add(4);
HashSet<pair> R = new HashSet<pair>();
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();
if (obj.checkReflexive(A, R)) {
Console.WriteLine( "Reflexive Relation" );
}
else {
Console.WriteLine( "Not a Reflexive Relation" );
}
}
}
|
Javascript
function checkReflexive(A, R)
{
let cnt = 0;
if (A.size > 0 && R.size == 0) {
return false ;
}
else if (A.size == 0) {
return true ;
}
A.forEach(i => {
let temp = [i, i];
if (!R.has(temp)) {
cnt++;
}
});
if (cnt==0)
return true ;
else
return false ;
}
let A = new Set([ 1, 2, 3, 4 ]);
let R = new Set();
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]);
if (checkReflexive(A, R)) {
console.log( "Reflexive Relation" );
}
else {
console.log( "Not a Reflexive Relation" );
}
|
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)
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
02 Jan, 2023
Like Article
Save Article