# Number of Relations that are both Irreflexive and Antisymmetric on a Set

• Last Updated : 10 May, 2021

Given a positive integer N, the task is to find the number of relations that are irreflexive antisymmetric relations that can be formed over the given set of elements.  Since the count can be very large, print it to modulo 109 + 7.

A relation R on a set A is called reflexive if no (a, a) R holds for every element a € A.
For Example: If set A = {a, b} then R = {(a, b), (b, a)} is irreflexive relation.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

A relation R on a set A is called Antisymmetric if and only if (a, b) € R and (b, a) € R, then a = b is called antisymmetric, i.e., the relation R = {(a, b)→ R | a ≤ b} is anti-symmetric, since a ≤ b and b ≤ a implies a = b.

Examples:

Input: N = 2
Output: 3
Explanation:
Considering the set {a, b}, all possible relations that are both irreflexive and antisymmetric relations are:

1. {}
2. {{a, b}}
3. {{b, a}}

Input: N = 5
Output: 59049

Approach: The given problem can be solved based on the following observations:

• A relation R on a set A is a subset of the Cartesian Product of a set, i.e., A * A with N2 elements.
• No (x, x) pair should be included in the subset to make sure the relation is irreflexive.
• For the remaining (N2 – N) pairs, divide them into (N2 – N)/2 groups where each group consists of a pair (x, y) and its symmetric pair (y, x).
• Now, there are three choices, either include one of the ordered pairs or neither of them from a group.
• Therefore, the total number of possible relations that are both irreflexive and antisymmetric is given by 3(N2 – N)/2.

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach` `#include ``using` `namespace` `std;` `const` `int` `mod = 1000000007;` `// Function to calculate``// x ^ y % mod in O(log y)``int` `power(``long` `long` `x,``unsigned ``int` `y)``{``    ``// Stores the result of x^y``    ``int` `res = 1;` `    ``// Update x if it exceeds mod``    ``x = x % mod;` `    ``while` `(y > 0) {` `        ``// If y is odd, then multiply``        ``// x with result``        ``if` `(y & 1)``            ``res = (res * x) % mod;` `        ``// Divide y by 2``        ``y = y >> 1;` `        ``// Update the value of x``        ``x = (x * x) % mod;``    ``}` `    ``// Return the value of x^y``    ``return` `res;``}` `// Function to count relations that``// are  irreflexive and antisymmetric``// in a set consisting of N elements``int` `numberOfRelations(``int` `N)``{``    ``// Return the resultant count``    ``return` `power(3, (N * N - N) / 2);``}` `// Driver Code``int` `main()``{``    ``int` `N = 2;``    ``cout << numberOfRelations(N);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach``import` `java.util.*;` `class` `GFG{` `static` `int` `mod = ``1000000007``;` `// Function to calculate``// x ^ y % mod in O(log y)``static` `int` `power(``long` `x, ``int` `y)``{``    ` `    ``// Stores the result of x^y``    ``int` `res = ``1``;` `    ``// Update x if it exceeds mod``    ``x = x % mod;` `    ``while` `(y > ``0``)``    ``{` `        ``// If y is odd, then multiply``        ``// x with result``        ``if` `(y % ``2` `== ``1``)``            ``res = (``int``)(res  * x) % mod;` `        ``// Divide y by 2``        ``y = y >> ``1``;` `        ``// Update the value of x``        ``x = (x * x) % mod;``    ``}` `    ``// Return the value of x^y``    ``return` `res;``}` `// Function to count relations that``// are  irreflexive and antisymmetric``// in a set consisting of N elements``static` `int` `numberOfRelations(``int` `N)``{``    ` `    ``// Return the resultant count``    ``return` `power(``3``, (N * N - N) / ``2``);``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ``int` `N = ``2``;``    ``System.out.print(numberOfRelations(N));``}``}` `// This code is contributed by 29AjayKumar`

## Python3

 `# Python 3 program for the above approach``mod ``=` `1000000007` `# Function to calculate``# x ^ y % mod in O(log y)``def` `power(x,  y):` `    ``# Stores the result of x^y``    ``res ``=` `1` `    ``# Update x if it exceeds mod``    ``x ``=` `x ``%` `mod` `    ``while` `(y > ``0``):` `        ``# If y is odd, then multiply``        ``# x with result``        ``if` `(y & ``1``):``            ``res ``=` `(res ``*` `x) ``%` `mod` `        ``# Divide y by 2``        ``y ``=` `y >> ``1` `        ``# Update the value of x``        ``x ``=` `(x ``*` `x) ``%` `mod` `    ``# Return the value of x^y``    ``return` `res` `# Function to count relations that``# are  irreflexive and antisymmetric``# in a set consisting of N elements``def` `numberOfRelations(N):` `    ``# Return the resultant count``    ``return` `power(``3``, (N ``*` `N ``-` `N) ``/``/` `2``)` `# Driver Code``if` `__name__ ``=``=` `"__main__"``:` `    ``N ``=` `2``    ``print``(numberOfRelations(N))` `    ``# This code is contributed by ukasp.`

## C#

 `// C# program for the above approach``using` `System;` `class` `GFG{` `static` `int` `mod = 1000000007;` `// Function to calculate``// x ^ y % mod in O(log y)``static` `int` `power(``long` `x, ``int` `y)``{``    ` `    ``// Stores the result of x^y``    ``int` `res = 1;` `    ``// Update x if it exceeds mod``    ``x = x % mod;` `    ``while` `(y > 0)``    ``{` `        ``// If y is odd, then multiply``        ``// x with result``        ``if` `(y % 2 == 1)``            ``res = (``int``)(res  * x) % mod;` `        ``// Divide y by 2``        ``y = y >> 1;` `        ``// Update the value of x``        ``x = (x * x) % mod;``    ``}` `    ``// Return the value of x^y``    ``return` `res;``}` `// Function to count relations that``// are  irreflexive and antisymmetric``// in a set consisting of N elements``static` `int` `numberOfRelations(``int` `N)``{``    ` `    ``// Return the resultant count``    ``return` `power(3, (N * N - N) / 2);``}` `// Driver Code``public` `static` `void` `Main(String[] args)``{``    ``int` `N = 2;``    ``Console.Write(numberOfRelations(N));``}``}` `// This code is contributed by Princi Singh`

## Javascript

 ``
Output:
`3`

Time Complexity: O(log N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up