Asymmetric Relation on a Set
Last Updated :
02 Jan, 2023
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 then (b, a) ∉ R and vice versa,
where R is a subset of (A x A), i.e. the cartesian product of set A with itself.
This if an ordered pair of elements “a” to “b” (aRb) is present in relation R then an ordered pair of elements “b” to “a” (bRa) should not be present in relation R.
If any such bRa is present for any aRb in R then R is not an asymmetric relation. Also, if any aRa is present in R then R is not an asymmetric relation.
Example:
Consider set A = {a, b}
R = {(a, b), (b, a)} is not asymmetric relation but
R = {(a, b)} is symmetric relation.
Properties of Asymmetric Relation
- Empty relation on any set is always asymmetric.
- Every asymmetric relation is also irreflexive and anti-symmetric.
- Universal relation over a non-empty set is never asymmetric.
- A non-empty relation can not be both symmetric and asymmetric.
How to verify Asymmetric Relation?
To verify asymmetric relation follow the below method:
- Manually check for the existence of every bRa tuple for every aRb tuple in the relation.
- If any of the tuples exist or (a = b) then the relation is not asymmetric else it is asymmetric.
Follow the below illustration for a better understanding:
Illustration:
Consider set A = { 1, 2, 3, 4 } and relation R = { (1, 2), (1, 3), (2, 3), (3, 4) }
For (1, 2) in set R:
=> The reversed pair (2, 1) is not present in R.
=> This satisfies the condition.
For (1, 3) in set R:
=> The reversed pair (3, 1) is not present in R.
=> This satisfies the condition.
For (2, 3) in set R:
=> The reversed pair (3, 2) is not present in R.
=> This satisfies the condition.
For (3, 4) in set R:
=> The reversed pair (4, 3) is not present in R.
=> This satisfies the condition.
So R is an asymmetric relation.
Below is the code implementation of the idea:
C++
#include <bits/stdc++.h>
using namespace std;
class Relation {
public :
bool checkAsymmetric(set<pair< int , int > > R)
{
if (R.size() == 0) {
return true ;
}
for ( auto i = R.begin(); i != R.end(); i++) {
auto temp = make_pair(i->second, i->first);
if (R.find(temp) != R.end()) {
return false ;
}
}
return true ;
}
};
int main()
{
set<pair< int , int > > R;
R.insert(make_pair(1, 2));
R.insert(make_pair(2, 3));
R.insert(make_pair(3, 4));
Relation obj;
if (obj.checkAsymmetric(R)) {
cout << "Asymmetric Relation" << endl;
}
else {
cout << "Not a Asymmetric 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 checkAsymmetric(Set<pair> R)
{
if (R.size() == 0 ) {
return true ;
}
for (var i : R) {
pair temp = new pair(i.second, i.first);
if (R.contains(temp)) {
return false ;
}
}
return true ;
}
}
public static void main(String[] args)
{
Set<pair> R = new HashSet<>();
R.add( new pair( 1 , 2 ));
R.add( new pair( 2 , 3 ));
R.add( new pair( 3 , 4 ));
Relation obj = new Relation();
if (obj.checkAsymmetric(R)) {
System.out.println( "Asymmetric Relation" );
}
else {
System.out.println( "Not a Asymmetric Relation" );
}
}
}
|
Python3
class Relation:
def checkAsymmetric( self , R):
if len (R) = = 0 :
return True
for i in R:
if (i[ 1 ], i[ 0 ]) in R:
return False
return True
if __name__ = = '__main__' :
R = {( 1 , 2 ), ( 2 , 3 ), ( 3 , 4 )}
obj = Relation()
if obj.checkAsymmetric(R):
print ( "Asymmetric Relation" )
else :
print ( "Not a Asymmetric 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 checkAsymmetric(HashSet<pair> R)
{
if (R.Count == 0) {
return true ;
}
foreach ( var i in R)
{
pair temp = new pair(i.second, i.first);
if (R.Contains(temp)) {
return false ;
}
}
return true ;
}
}
static public void Main()
{
HashSet<pair> R = new HashSet<pair>();
R.Add( new pair(1, 2));
R.Add( new pair(2, 3));
R.Add( new pair(3, 4));
Relation obj = new Relation();
if (obj.checkAsymmetric(R)) {
Console.WriteLine( "Asymmetric Relation" );
}
else {
Console.WriteLine( "Not a Asymmetric Relation" );
}
}
}
|
Javascript
class Relation {
constructor() {}
checkAsymmetric(R) {
if (R.size === 0) {
return true ;
}
for (const i of R) {
const temp = [i[1], i[0]];
if (R.has(temp)) {
return false ;
}
}
return true ;
}
}
function main() {
const R = new Set();
R.add([1, 2]);
R.add([2, 3]);
R.add([3, 4]);
const obj = new Relation();
if (obj.checkAsymmetric(R)) {
console.log( "Asymmetric Relation" );
} else {
console.log( "Not a Asymmetric Relation" );
}
}
main();
|
Output
Asymmetric Relation
Time Complexity: O(N * log N), Where N is the number of elements in relation R.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...