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 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

1. Empty relation on any set is always irreflexive.
2. Universal relation on any non-empty set is never irreflexive.
3. 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 ` `using` `namespace` `std;`   `class` `Relation {` `public``:` `    ``bool` `checkIrreflexive(set<``int``> A,` `                          ``set > R)` `    ``{` `        ``// Property 1` `        ``if` `(R.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 exists in relation R` `                ``return` `false``;` `            ``}` `        ``}`   `        ``// None of 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 > R;`   `    ``// Inserting tuples in relation 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;`   `    ``// R is irreflexive as aRa tuple is not present` `    ``if` `(obj.checkIrreflexive(A, R)) {` `        ``cout << ``"Irreflexive Relation"` `<< endl;` `    ``}` `    ``else` `{` `        ``cout << ``"Not a Irreflexive 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` `checkIrreflexive(Set A,` `                             ``Set R)` `    ``{` `      ``// Property 1` `      ``if` `(R.size() == ``0``) {` `        ``return` `true``;` `      ``}` `      ``for` `(var i : A) {` `        ``if` `(R.contains(``new` `pair(i, i))) {` `          ``// If aRa tuple exists in relation R` `          ``return` `false``;` `        ``}` `      ``}` `      ``// None of aRa tuples exists in relation R` `      ``return` `true``;` `    ``}` `  ``}`   `  ``public` `static` `void` `main(String[] args)` `  ``{` `    ``// Creating a set A` `    ``Set A = ``new` `HashSet<>();` `    ``A.add(``1``);` `    ``A.add(``2``);` `    ``A.add(``3``);` `    ``A.add(``4``);`   `    ``// Creating relation R` `    ``Set R = ``new` `HashSet<>();`   `    ``// Inserting tuples in relation R` `    ``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();`   `    ``// R is irreflexive as aRa tuple is not present` `    ``if` `(obj.checkIrreflexive(A, R)) {` `      ``System.out.println(``"Irreflexive Relation"``);` `    ``}` `    ``else` `{` `      ``System.out.println(` `        ``"Not a Irreflexive Relation"``);` `    ``}` `  ``}` `}`   `// This code is contributed by lokeshmvs21.`

Python3

 `class` `Relation:` `    ``def` `checkIrreflexive(``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) ``in` `R:` `                `  `                ``# If aRa tuple exists in relation R` `                ``return` `False` `        `  `        ``# None of 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``, ``4``), (``1``, ``2``), (``3``, ``2``), (``2``, ``3``), (``3``, ``4``), (``1``, ``3``)}`   `    ``obj ``=` `Relation()`   `    ``# R in not reflexive as (4, 4) tuple is not present` `    ``if` `obj.checkIrreflexive(A, R):` `        ``print``(``"Irreflexive Relation"``)` `    ``else``:` `        ``print``(``"Not Irreflexive 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` `checkIrreflexive(HashSet<``int``> A,` `                                 ``HashSet R)` `    ``{` `      `  `      ``// Property 1` `      ``if` `(R.Count == 0) {` `        ``return` `true``;` `      ``}` `      ``foreach``(``var` `i ``in` `A)` `      ``{` `        ``if` `(R.Contains(``new` `pair(i, i))) ` `        ``{` `          `  `          ``// If aRa tuple exists in relation R` `          ``return` `false``;` `        ``}` `      ``}` `      ``// None of 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 R = ``new` `HashSet();`   `    ``// Inserting tuples in relation R` `    ``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();`   `    ``// R is irreflexive as aRa tuple is not present` `    ``if` `(obj.checkIrreflexive(A, R)) {` `      ``Console.WriteLine(``"Irreflexive Relation"``);` `    ``}` `    ``else` `{` `      ``Console.WriteLine(``"Not a Irreflexive Relation"``);` `    ``}` `  ``}` `}`   `// This code is contributed by lokesh`

Javascript

 `class Relation {` `    ``constructor() { }`   `    ``checkIrreflexive(A, R) {` `        ``// Property 1` `        ``if` `(R.size === 0) {` `            ``return` `true``;` `        ``}`   `        ``for` `(const i of A) {` `            ``// Making a tuple of same element` `            ``const temp = [i, i];`   `            ``if` `(R.has(temp)) {` `                ``// If aRa tuple exists in relation R` `                ``return` `false``;` `            ``}` `        ``}`   `        ``// None of aRa tuples exists in relation R` `        ``return` `true``;` `    ``}` `}`   `// Driver code` `function` `main() {` `    ``// Creating a set A` `    ``const A = ``new` `Set([1, 2, 3, 4]);`   `    ``// Creating relation R` `    ``const R = ``new` `Set();`   `    ``// Inserting tuples in relation R` `    ``R.add([1, 2]);` `    ``R.add([2, 1]);` `    ``R.add([2, 3]);` `    ``R.add([3, 2]);` `    ``R.add([3, 4]);`   `    ``const obj = ``new` `Relation();`   `    ``// R is irreflexive as aRa tuple is not present` `    ``if` `(obj.checkIrreflexive(A, R)) {` `        ``console.log(``"Irreflexive Relation"``);` `    ``} ``else` `{` `        ``console.log(``"Not a Irreflexive Relation"``);` `    ``}` `}`   `main();`   `//  This code is contributed by akashish__`

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!

Previous
Next