Number of Irreflexive Relations on a Set - GeeksforGeeks

30 Apr, 2021
Given a positive integer N, the task is to find the number of irreflexive 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.
Examples:
Input: N = 2Output: 4Explanation:Considering the set {1, 2}, the total possible irreflexive relations are:
{}
{(1, 2)}
{(2, 1)}
{(1, 2), (2, 1)}
Input: N = 5Output: 1048576
Approach: Follow the steps below to solve the problem:
A relation R on a set A is a subset of the cartesian product of a set, i.e. A * A with N2 elements.
Irreflexive Relation: A relation R on a set A is called Irreflexive if and only if x R x [(x, x) does not belong to R] for every element x in A.
There are total N pairs of (x, x) are present in the Cartesian product which should not be included in an irreflexive relation. Therefore, for the remaining (N2 – N) elements, each element has two choices i.e., either to include or exclude it in the subset.
Hence, the total number of possible irreflexive relations is given by 2(N2 – N).
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 x^y// modulo 1000000007 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;     // If x is divisible by mod    if (x == 0)        return 0;     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// irreflixive relations in a set// consisting of N elementsint irreflexiveRelation(int N){     // Return the resultant count    return power(2, N * N - N);} // Driver Codeint main(){    int N = 2;    cout << irreflexiveRelation(N);     return 0;}
// Java program for the above approachimport java.io.*;import java.util.*;class GFG{ static int mod = 1000000007; // Function to calculate x^y// modulo 1000000007 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;     // If x is divisible by mod    if (x == 0)        return 0;     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// irreflixive relations in a set// consisting of N elementsstatic int irreflexiveRelation(int N){     // Return the resultant count    return power(2, N * N - N);}  // Driver Codepublic static void main(String[] args){    int N = 2;    System.out.println(irreflexiveRelation(N));}} // This code is contributed by code_hunt.
# Python3 program for the above approachmod = 1000000007 # Function to calculate x^y# modulo 1000000007 in O(log y)def power(x, y):    global mod         # Stores the result of x^y    res = 1     # Update x if it exceeds mod    x = x % mod     # If x is divisible by mod    if (x == 0):        return 0     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# irreflixive relations in a set# consisting of N elementsdef irreflexiveRelation(N):     # Return the resultant count    return power(2, N * N - N) # Driver Codeif __name__ == '__main__':    N = 2    print(irreflexiveRelation(N))     # This code is contributed by mohit kumar 29.
// C# program for above approachusing System; public class GFG{   static int mod = 1000000007;   // Function to calculate x^y  // modulo 1000000007 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;     // If x is divisible by mod    if (x == 0)      return 0;     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  // irreflixive relations in a set  // consisting of N elements  static int irreflexiveRelation(int N)  {     // Return the resultant count    return power(2, N * N - N);  }   // Driver code  public static void Main(String[] args)  {    int N = 2;    Console.WriteLine(irreflexiveRelation(N));  }} // This code is contributed by sanjoy_62.
<script> // Javascript program for the above approach let mod = 1000000007; // Function to calculate x^y// modulo 1000000007 in O(log y)function power(x, y){       // Stores the result of x^y    let res = 1;     // Update x if it exceeds mod    x = x % mod;     // If x is divisible by mod    if (x == 0)        return 0;     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// irreflixive relations in a set// consisting of N elementsfunction irreflexiveRelation(N){     // Return the resultant count    return power(2, N * N - N);} // Driver code         let N = 2;    document.write(irreflexiveRelation(N));     </script>
4
 
Time Complexity: O(log N)Auxiliary Space: O(1)
mohit kumar 29
code_hunt
sanjoy_62
chinmoy1997pal
maths-power
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Merge two sorted arrays
Program to find GCD or HCF of two numbers
Prime Numbers
Modulo Operator (%) in C/C++ with Examples
Sieve of Eratosthenes
Program for Decimal to Binary Conversion
Find all factors of a natural number | Set 1
Program to find sum of elements in a given array
Modulo 10^9+7 (1000000007)
The Knight's tour problem | Backtracking-1