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 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 not be present in the relation R. If any such aRa is present in R then R is not an irreflexive relation.
Example:
Consider set A = {a, b}.
Then R = { (a, a), (a, b) } is not irreflexive relation.
and R1 = { (a, b), (b, a) } is a irreflexive relation
Properties of Irreflexive Relation
- Empty relation on any set is always irreflexive.
- Universal relation on any non-empty set is never irreflexive.
- An empty relation defined on any set is always irreflexive.
How to verify Irrefelxive Relation?
The process of verifying irreflexive relation is as follows:
- Manually check for the existence of every aRa in the relation.
- If any of the tuples exist then the relation is not irreflexive else it is irreflexive.
Follow the below illustration for a better understanding
Consider set A = { 1, 2, 3, 4 } and relation R = { (1, 2), (2, 1), (2, 3), (3, 2), (3, 4), (4, 4) }
For R to be irreflexive, it should not have any of (1, 1), (2, 2), (3, 3), (4, 4).
For 1 in A:
=> {1, 1} is not present.
=> So not 1R1 is satisfied.
For 2 in A:
=> {2, 2} is not present.
=> So not 2R2 is satisfied.
For 3 in A:
=> {3, 3} is not present.
=> So not 3R3 is satisfied.
For 4 in A:
=> {4, 4} is present.
=> So not 2R2 is not satisfied.
So R is not a irreflexive relation.
Below is the code implementation of the idea:
C++
#include <bits/stdc++.h>
using namespace std;
class Relation {
public :
bool checkIrreflexive(set< int > A,
set<pair< int , int > > R)
{
if (R.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, 2));
R.insert(make_pair(2, 1));
R.insert(make_pair(2, 3));
R.insert(make_pair(3, 2));
R.insert(make_pair(3, 4));
Relation obj;
if (obj.checkIrreflexive(A, R)) {
cout << "Irreflexive Relation" << endl;
}
else {
cout << "Not a Irreflexive 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 checkIrreflexive(Set<Integer> A,
Set<pair> R)
{
if (R.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 , 2 ));
R.add( new pair( 2 , 1 ));
R.add( new pair( 2 , 3 ));
R.add( new pair( 3 , 2 ));
R.add( new pair( 3 , 4 ));
Relation obj = new Relation();
if (obj.checkIrreflexive(A, R)) {
System.out.println( "Irreflexive Relation" );
}
else {
System.out.println(
"Not a Irreflexive Relation" );
}
}
}
|
Python3
class Relation:
def checkIrreflexive( 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) in R:
return False
return True
if __name__ = = '__main__' :
A = { 1 , 2 , 3 , 4 }
R = {( 1 , 4 ), ( 1 , 2 ), ( 3 , 2 ), ( 2 , 3 ), ( 3 , 4 ), ( 1 , 3 )}
obj = Relation()
if obj.checkIrreflexive(A, R):
print ( "Irreflexive Relation" )
else :
print ( "Not Irreflexive 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 checkIrreflexive(HashSet< int > A,
HashSet<pair> R)
{
if (R.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, 2));
R.Add( new pair(2, 1));
R.Add( new pair(2, 3));
R.Add( new pair(3, 2));
R.Add( new pair(3, 4));
Relation obj = new Relation();
if (obj.checkIrreflexive(A, R)) {
Console.WriteLine( "Irreflexive Relation" );
}
else {
Console.WriteLine( "Not a Irreflexive Relation" );
}
}
}
|
Javascript
class Relation {
constructor() { }
checkIrreflexive(A, R) {
if (R.size === 0) {
return true ;
}
for (const i of A) {
const temp = [i, i];
if (R.has(temp)) {
return false ;
}
}
return true ;
}
}
function main() {
const A = new Set([1, 2, 3, 4]);
const R = new Set();
R.add([1, 2]);
R.add([2, 1]);
R.add([2, 3]);
R.add([3, 2]);
R.add([3, 4]);
const obj = new Relation();
if (obj.checkIrreflexive(A, R)) {
console.log( "Irreflexive Relation" );
} else {
console.log( "Not a Irreflexive Relation" );
}
}
main();
|
Output
Irreflexive Relation
Time Complexity: O(N * log M) where N is the size of set and M is number of pairs in 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