Number of Antisymmetric Relations on a set of N elements - GeeksforGeeks

27 Apr, 2021
Given a positive integer N, the task is to find the number of Antisymmetric Relations on the given set of N elements.  Since the number of relations can be very large, so print it modulo 109+7.
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 = 2Output: 12Explanation: Considering the set {a, b}, all possible antisymmetric relations are:{}, {(a, b)}, {(b, a)}, {(a, a)}, {(a, a), (a, b)}, {(a, a), (b, a)}, {(b, b)}, {(b, b), (a, b)}, {(b, b), (b, a)}, {(a, a), (b, b)}, {(a, a), (b, b), (a, b)}, {(a, a), (b, b), (b, a)}.
Input: N = 5Output: 1889568
Approach: The given problem can be solved based on the following observations:
Considering an antisymmetric relation R on set S, say a, b ∈ A with a ≠ b, then relation R must not contain both (a, b) and (b, a). It may contain one of the ordered pairs or neither of them.
There are 3 possible choices for all pairs.
Therefore, the count of all combinations of these choices is equal to 3(N*(N – 1))/2.
The number of subsets of pairs of the form (a, a) is equal to 2N.
Therefore, the total count of possible antisymmetric relations is equal to 2N * 3(N*(N – 1))/2.
Below is the implementation of the above approach:
C++
Java
Python3
C#
Javascript
// C++ program for the above approach#include <iostream>using namespace std; const int mod = 1000000007; // Function to calculate the// value of 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 resultant    // value of x^y    return res;} // Function to count the number of// antisymmetric relations in a set// consisting of N elementsint antisymmetricRelation(int N){     // Print the count of    // antisymmetric relations    return (power(2, N) * 1LL * power(3, (N * N - N) / 2)) % mod;} // Driver Codeint main(){    int N = 2;    cout << antisymmetricRelation(N);     return 0;}
// Java program for the above approachimport java.util.*; class GFG{  static int mod = 1000000007; // Function to calculate the// value of x ^ y % mod in O(log y)static int power(int 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 & 1) != 0)            res = (res * x) % mod;         // Divide y by 2        y = y >> 1;         // Update the value of x        x = (x * x) % mod;    }     // Return the resultant    // value of x^y    return res;} // Function to count the number of// antisymmetric relations in a set// consisting of N elementsstatic int antisymmetricRelation(int N){         // Print the count of    // antisymmetric relations    return (power(2, N) *            power(3, (N * N - N) / 2)) % mod;} // Driver Codepublic static void main(String []args){    int N = 2;         System.out.print(antisymmetricRelation(N));}} // This code is contributed by ipg2016107
# Python3 program for the above approachmod = 1000000007 # Function to calculate the# value of 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 resultant    # value of x^y    return res # Function to count the number of# antisymmetric relations in a set# consisting of N elementsdef antisymmetricRelation(N):     # Print the count of    # antisymmetric relations    return (power(2, N) *            power(3, (N * N - N) // 2)) % mod # Driver Codeif __name__ == "__main__":     N = 2         print(antisymmetricRelation(N)) # This code is contributed by ukasp
// C# program for the above approachusing System;using System.Collections.Generic; class GFG{ static int mod = 1000000007; // Function to calculate the// value of x ^ y % mod in O(log y)static int power(int 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 & 1)>0)            res = (res * x) % mod;         // Divide y by 2        y = y >> 1;         // Update the value of x        x = (x * x) % mod;    }     // Return the resultant    // value of x^y    return res;} // Function to count the number of// antisymmetric relations in a set// consisting of N elementsstatic int antisymmetricRelation(int N){     // Print the count of    // antisymmetric relations    return (power(2, N) *            power(3, (N * N - N) / 2)) % mod;} // Driver Codepublic static void Main(){    int N = 2;         Console.Write(antisymmetricRelation(N));}} // This code is contributed by SURENDRA_GANGWAR
<script> // Javascript program for the above approach var mod = 1000000007; // Function to calculate the// value of x ^ y % mod in O(log y)function power(x, y){    // Stores the result of x^y    var 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 resultant    // value of x^y    return res;} // Function to count the number of// antisymmetric relations in a set// consisting of N elementsfunction antisymmetricRelation(N){     // Print the count of    // antisymmetric relations    return (power(2, N) * power(3, (N * N - N) / 2)) % mod;} // Driver Codevar N = 2;document.write( antisymmetricRelation(N)); </script>
12
 
Time Complexity: O(log N)Auxiliary Space: O(1)
 
ukasp
SURENDRA_GANGWAR
ipg2016107
noob2000
Algebra
maths-power
Combinatorial
Mathematical
Mathematical
Combinatorial
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Ways to sum to N using Natural Numbers up to K with repetitions allowed
Generate all possible combinations of K numbers that sums to N
Combinations with repetitions
Generate all possible combinations of at most X characters from a given array
Print all possible permutations of an array with duplicates using Backtracking
Program for Fibonacci numbers
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7
Merge two sorted arrays