Number of Antisymmetric Relations on a set of N elements
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 = 2
Output: 12
Explanation: 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 = 5
Output: 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++
#include <iostream>
using namespace std;
const int mod = 1000000007;
int power( long long x, unsigned int y)
{
int res = 1;
x = x % mod;
while (y > 0) {
if (y & 1)
res = (res * x) % mod;
y = y >> 1;
x = (x * x) % mod;
}
return res;
}
int antisymmetricRelation( int N)
{
return (power(2, N) * 1LL * power(3, (N * N - N) / 2)) % mod;
}
int main()
{
int N = 2;
cout << antisymmetricRelation(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int mod = 1000000007 ;
static int power( int x, int y)
{
int res = 1 ;
x = x % mod;
while (y > 0 )
{
if ((y & 1 ) != 0 )
res = (res * x) % mod;
y = y >> 1 ;
x = (x * x) % mod;
}
return res;
}
static int antisymmetricRelation( int N)
{
return (power( 2 , N) *
power( 3 , (N * N - N) / 2 )) % mod;
}
public static void main(String []args)
{
int N = 2 ;
System.out.print(antisymmetricRelation(N));
}
}
|
Python3
mod = 1000000007
def power(x, y):
res = 1
x = x % mod
while (y > 0 ):
if (y & 1 ):
res = (res * x) % mod
y = y >> 1
x = (x * x) % mod
return res
def antisymmetricRelation(N):
return (power( 2 , N) *
power( 3 , (N * N - N) / / 2 )) % mod
if __name__ = = "__main__" :
N = 2
print (antisymmetricRelation(N))
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int mod = 1000000007;
static int power( int x, int y)
{
int res = 1;
x = x % mod;
while (y > 0)
{
if ((y & 1)>0)
res = (res * x) % mod;
y = y >> 1;
x = (x * x) % mod;
}
return res;
}
static int antisymmetricRelation( int N)
{
return (power(2, N) *
power(3, (N * N - N) / 2)) % mod;
}
public static void Main()
{
int N = 2;
Console.Write(antisymmetricRelation(N));
}
}
|
Javascript
<script>
var mod = 1000000007;
function power(x, y)
{
var res = 1;
x = x % mod;
while (y > 0) {
if (y & 1)
res = (res * x) % mod;
y = y >> 1;
x = (x * x) % mod;
}
return res;
}
function antisymmetricRelation(N)
{
return (power(2, N) * power(3, (N * N - N) / 2)) % mod;
}
var N = 2;
document.write( antisymmetricRelation(N));
</script>
|
Time Complexity: O(log N)
Auxiliary Space: O(1)
Last Updated :
27 Apr, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...