Sunday, November 17, 2024
Google search engine
HomeData Modelling & AINumber of Irreflexive Relations on a Set

Number of Irreflexive Relations on a Set

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 = 2
Output: 4
Explanation:
Considering the set {1, 2}, the total possible irreflexive relations are:

  • {}
  • {(1, 2)}
  • {(2, 1)}
  • {(1, 2), (2, 1)}

Input: N = 5
Output: 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++




// 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 elements
int irreflexiveRelation(int N)
{
 
    // Return the resultant count
    return power(2, N * N - N);
}
 
// Driver Code
int main()
{
    int N = 2;
    cout << irreflexiveRelation(N);
 
    return 0;
}


Java




// Java program for the above approach
import 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 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;
    System.out.println(irreflexiveRelation(N));
}
}
 
// This code is contributed by code_hunt.


Python3




# Python3 program for the above approach
mod = 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 elements
def irreflexiveRelation(N):
 
    # Return the resultant count
    return power(2, N * N - N)
 
# Driver Code
if __name__ == '__main__':
    N = 2
    print(irreflexiveRelation(N))
 
    # This code is contributed by mohit kumar 29.


C#




// C# program for above approach
using 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.


Javascript




<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 elements
function irreflexiveRelation(N)
{
 
    // Return the resultant count
    return power(2, N * N - N);
}
 
// Driver code
     
    let N = 2;
    document.write(irreflexiveRelation(N));
     
</script>


Output: 

4

 

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

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments