Number of Asymmetric Relations on a set of N elements
Last Updated :
27 Apr, 2021
Given a positive integer N, the task is to find the number of Asymmetric Relations in a set of N elements. Since the number of relations can be very large, print it modulo 109+7.
A relation R on a set A is called Asymmetric if and only if x R y exists, then y R x for every (x, y) € A.
For Example: If set A = {a, b}, then R = {(a, b)} is asymmetric relation.
Examples:
Input: N = 2
Output: 3
Explanation: Considering the set {1, 2}, the total number of possible asymmetric relations are {{}, {(1, 2)}, {(2, 1)}}.
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.
- There are total N pairs of type (x, x) that are present in the Cartesian product, where any of (x, x) should not be included in the subset.
- Now, one is left with (N2 – N) elements of the Cartesian product.
- To satisfy the property of asymmetric relation, one has three possibilities of either to include only of type (x, y) or only of type (y, x) or none from a single group into the subset.
- Hence, the total number of possible asymmetric relations is equal to 3 (N2 – N) / 2.
Therefore, the idea is to print the value of 3(N2 – N)/2 modulo 109 + 7 as the result.
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;
if (x == 0)
return 0;
while (y > 0) {
if (y & 1)
res = (res * x) % mod;
y = y >> 1;
x = (x * x) % mod;
}
return res;
}
int asymmetricRelation( int N)
{
return power(3, (N * N - N) / 2);
}
int main()
{
int N = 2;
cout << asymmetricRelation(N);
return 0;
}
|
Java
import java.io.*;
class GFG{
final static int mod = 1000000007 ;
public static int power( int x, int y)
{
int res = 1 ;
x = x % mod;
if (x == 0 )
return 0 ;
while (y > 0 )
{
if (y % 2 == 1 )
res = (res * x) % mod;
y = y >> 1 ;
x = (x * x) % mod;
}
return res;
}
public static int asymmetricRelation( int N)
{
return power( 3 , (N * N - N) / 2 );
}
public static void main (String[] args)
{
int N = 2 ;
System.out.print(asymmetricRelation(N));
}
}
|
Python3
mod = 1000000007
def power(x, y):
res = 1
x = x % mod
if (x = = 0 ):
return 0
while (y > 0 ):
if (y & 1 ):
res = (res * x) % mod;
y = y >> 1
x = (x * x) % mod
return res
def asymmetricRelation(N):
return power( 3 , (N * N - N) / / 2 )
if __name__ = = '__main__' :
N = 2
print (asymmetricRelation(N))
|
C#
using System;
class GFG{
const int mod = 1000000007;
static int power( int x, int y)
{
int res = 1;
x = x % mod;
if (x == 0)
return 0;
while (y > 0)
{
if ((y & 1) != 0)
res = (res * x) % mod;
y = y >> 1;
x = (x * x) % mod;
}
return res;
}
static int asymmetricRelation( int N)
{
return power(3, (N * N - N) / 2);
}
public static void Main( string [] args)
{
int N = 2;
Console.WriteLine(asymmetricRelation(N));
}
}
|
Javascript
<script>
var mod = 1000000007;
function power(x, y)
{
var res = 1;
x = x % mod;
if (x == 0)
return 0;
while (y > 0) {
if (y & 1)
res = (res * x) % mod;
y = y >> 1;
x = (x * x) % mod;
}
return res;
}
function asymmetricRelation( N)
{
return power(3, (N * N - N) / 2);
}
var N = 2;
document.write( asymmetricRelation(N));
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...