Anti-Symmetric 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 Anti-Symmetric Relation?
A relation R on a set A is called anti-symmetric relation if
∀ a, b ∈ A, if (a, b) ∈ R then (b, a) ∉ R or a = b,
where R is a subset of (A x A), i.e. the cartesian product of set A with itself.
This means 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 unless a = b.
If any such bRa is present for any aRb in R then R is not an anti-symmetric relation.
Example:
Consider set A = {a, b}
R = {(a, b), (b, a)} is not anti-symmetric relation as for (a, b) tuple (b, a) tuple is present but
R = {(a, a), (a, b)} is an anti-symmetric relation.
Properties of Anti-Symmetric Relation
- Empty relation on any set is always anti-symmetric.
- Universal relation over set may or may not be anti-symmetric.
- If the relation is reflexive/irreflexive then it need not be anti-symmetric.
- A relation may be anti-symmetric and symmetric at the same time.
How to verify an Anti-Symmetric Relation?
To verify anti-symmetric relation:
- Manually check for the existence of every bRa tuple for every aRb tuple (if a ≠b) in the relation.
- If any of the tuples exist then the relation is not anti-symmetric. Otherwise, it is anti-symmetric.
Follow the below illustration for a better understanding
Consider set A = { 1, 2, 3, 4 } and a relation R = { (1, 2), (1, 3), (2, 3), (3, 4), (4, 4) }
For (1, 2) in R:
=> The reversed pair (2, 1) is not present.
=> This satisfies the condition.
For (1, 3) in R:
=> The reversed pair (3, 1) is not present.
=> This satisfies the condition.
For (2, 3) in R:
=> The reversed pair (3, 2) is not present.
=> This satisfies the condition.
For (3, 4) in R:
=> The reversed pair (4, 3) is not present.
=> This satisfies the condition.
For (4, 4) in R:
=> The reversed pair (4, 4) is present but see here both the elements of the tuple are same.
=> So this also satisfies the condition.
So R is an anti-symmetric relation.
Below is the code implementation of the idea:
C++
#include <bits/stdc++.h>
using namespace std;
class Relation {
public :
bool checkAntiSymmetric(set<pair< int , int > > R)
{
if (R.size() == 0) {
return true ;
}
for ( auto i = R.begin(); i != R.end(); i++) {
if (i->second != i->first) {
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, 1));
R.insert(make_pair(1, 2));
R.insert(make_pair(2, 3));
R.insert(make_pair(3, 4));
Relation obj;
if (obj.checkAntiSymmetric(R)) {
cout << "Anti-Symmetric Relation" << endl;
}
else {
cout << "Not a Anti-Symmetric 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 checkAntiSymmetric(Set<pair> R)
{
if (R.size() == 0 ) {
return true ;
}
for (var i : R) {
int one = i.first;
int two = i.second;
if (one != two)
{
if (R.contains( new pair(two, one)))
{
return false ;
}
}
}
return true ;
}
}
public static void main(String[] args)
{
Set<pair> R = new HashSet<>();
R.add( new pair( 1 , 1 ));
R.add( new pair( 1 , 2 ));
R.add( new pair( 2 , 3 ));
R.add( new pair( 3 , 4 ));
Relation obj = new Relation();
if (obj.checkAntiSymmetric(R)) {
System.out.println( "Anti-Symmetric Relation" );
}
else {
System.out.println(
"Not a Anti-Symmetric Relation" );
}
}
}
|
Python3
class Relation:
def checkAntiSymmetric( self , R):
if len (R) = = 0 :
return True
for i in R:
if i[ 0 ] ! = i[ 1 ]:
if (i[ 1 ], i[ 0 ]) in R:
return False
return True
if __name__ = = '__main__' :
R = {( 1 , 1 ), ( 1 , 2 ), ( 2 , 3 ), ( 3 , 4 )}
obj = Relation()
if obj.checkAntiSymmetric(R):
print ( "Anti-Symmetric Relation" )
else :
print ( "Not a Anti-Symmetric 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 checkAntiSymmetric(HashSet<pair> R)
{
if (R.Count == 0) {
return true ;
}
foreach ( var i in R)
{
int one = i.first;
int two = i.second;
if (one != two) {
if (R.Contains( new pair(two, one))) {
return false ;
}
}
}
return true ;
}
}
static public void Main()
{
HashSet<pair> R = new HashSet<pair>();
R.Add( new pair(1, 1));
R.Add( new pair(1, 2));
R.Add( new pair(2, 3));
R.Add( new pair(3, 4));
Relation obj = new Relation();
if (obj.checkAntiSymmetric(R)) {
Console.WriteLine( "Anti-Symmetric Relation" );
}
else {
Console.WriteLine(
"Not a Anti-Symmetric Relation" );
}
}
}
|
Javascript
class Relation {
constructor() {}
checkAntiSymmetric(R) {
if (R.size === 0) {
return true ;
}
for (const i of R) {
if (i[1] !== i[0]) {
const temp = [i[1], i[0]];
if (R.has(temp)) {
return false ;
}
}
}
return true ;
}
}
function main() {
const R = new Set();
R.add([1, 1]);
R.add([1, 2]);
R.add([2, 3]);
R.add([3, 4]);
const obj = new Relation();
if (obj.checkAntiSymmetric(R)) {
console.log( "Anti-Symmetric Relation" );
} else {
console.log( "Not a Anti-Symmetric Relation" );
}
}
main();
|
Output
Anti-Symmetric Relation
Time Complexity: O(N * log N) where N is the number of elements in the relation
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...