Given an integer **N** which is the number of vertices. The task is to find the number of distinct graphs that can be formed. Since the answer can be very large, print the **answer % 1000000007**.

**Examples:**

Input:N = 3

Output:8

Input:N = 4

Output:64

**Approach:**

- The maximum number of edges a graph with
**N**vertices can contain is**X = N * (N – 1) / 2.** - The total number of graphs containing
**0**edge and**N**vertices will be^{X}C_{0} - The total number of graphs containing
**1**edge and**N**vertices will be^{X}C_{1} - And so on from number of edges 1 to
**X**with**N**vertices - Hence, the total number of graphs that can be formed with n vertices will be:

^{X}C_{0}+^{X}C_{1}+^{X}C_{2}+ … +^{X}C_{X}= 2^{X}.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `const` `int` `MOD = 1e9 + 7; ` ` ` `// Function to return (x^y) % MOD ` `// in O(log(y)) ` `long` `long` `power(` `long` `long` `x, ` ` ` `long` `long` `y, ` ` ` `const` `int` `& MOD) ` `{ ` ` ` `long` `long` `res = 1; ` ` ` `while` `(y > 0) { ` ` ` `if` `(y & 1) ` ` ` `res = (res * x) % MOD; ` ` ` `x = (x * x) % MOD; ` ` ` `y /= 2; ` ` ` `} ` ` ` `return` `res; ` `} ` ` ` `// Function to return the count of distinct ` `// graphs possible with n vertices ` `long` `long` `countGraphs(` `int` `n) ` `{ ` ` ` ` ` `// Maximum number of edges for a ` ` ` `// graph with n vertices ` ` ` `long` `long` `x = n * (n - 1) / 2; ` ` ` ` ` `// Function to calculate ` ` ` `// (2^x) % mod ` ` ` `return` `power(2, x, MOD); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 5; ` ` ` ` ` `cout << countGraphs(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` `static` `final` `int` `MOD = (` `int` `)1e9 + ` `7` `; ` ` ` ` ` `// Function to return (x^y) % MOD ` ` ` `// in O(log(y)) ` ` ` `static` `long` `power(` `long` `x, ` ` ` `long` `y) ` ` ` `{ ` ` ` `long` `res = ` `1` `; ` ` ` `while` `(y > ` `0` `) ` ` ` `{ ` ` ` `if` `((y & ` `1` `) != ` `0` `) ` ` ` `res = (res * x) % MOD; ` ` ` `x = (x * x) % MOD; ` ` ` `y /= ` `2` `; ` ` ` `} ` ` ` `return` `res; ` ` ` `} ` ` ` ` ` `// Function to return the count of distinct ` ` ` `// graphs possible with n vertices ` ` ` `static` `long` `countGraphs(` `int` `n) ` ` ` `{ ` ` ` ` ` `// Maximum number of edges for a ` ` ` `// graph with n vertices ` ` ` `long` `x = n * (n - ` `1` `) / ` `2` `; ` ` ` ` ` `// Function to calculate ` ` ` `// (2^x) % mod ` ` ` `return` `power(` `2` `, x); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `n = ` `5` `; ` ` ` ` ` `System.out.println(countGraphs(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by AnkitRai01 ` |

*chevron_right*

*filter_none*

## Python

`MOD ` `=` `int` `(` `1e9` `+` `7` `) ` ` ` `# Function to return the count of distinct ` `# graphs possible with n vertices ` `def` `countGraphs(n): ` ` ` ` ` `# Maximum number of edges for a ` ` ` `# graph with n vertices ` ` ` `x ` `=` `(n ` `*` `( n ` `-` `1` `)) ` `/` `/` `2` ` ` ` ` `# Return 2 ^ x ` ` ` `return` `(` `pow` `(` `2` `, x, MOD)) ` ` ` `# Driver code ` `n ` `=` `5` `print` `(countGraphs(n)) ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` `static` `int` `MOD = (` `int` `)1e9 + 7; ` ` ` ` ` `// Function to return (x^y) % MOD ` ` ` `// in O(log(y)) ` ` ` `static` `long` `power(` `long` `x, ` `long` `y) ` ` ` `{ ` ` ` `long` `res = 1; ` ` ` `while` `(y > 0) ` ` ` `{ ` ` ` `if` `((y & 1) != 0) ` ` ` `res = (res * x) % MOD; ` ` ` `x = (x * x) % MOD; ` ` ` `y /= 2; ` ` ` `} ` ` ` `return` `res; ` ` ` `} ` ` ` ` ` `// Function to return the count of distinct ` ` ` `// graphs possible with n vertices ` ` ` `static` `long` `countGraphs(` `int` `n) ` ` ` `{ ` ` ` ` ` `// Maximum number of edges for a ` ` ` `// graph with n vertices ` ` ` `long` `x = n * (n - 1) / 2; ` ` ` ` ` `// Function to calculate ` ` ` `// (2^x) % mod ` ` ` `return` `power(2, x); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `static` `public` `void` `Main () ` ` ` `{ ` ` ` `int` `n = 5; ` ` ` ` ` `Console.Write(countGraphs(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by ajit. ` |

*chevron_right*

*filter_none*

**Output:**

1024

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find the remaining vertices of a square from two given vertices
- Count of nested polygons that can be drawn by joining vertices internally
- Number of triangles formed by joining vertices of n-sided polygon with one side common
- Number of triangles formed by joining vertices of n-sided polygon with two common sides and no common sides
- Number of occurrences of a given angle formed using 3 vertices of a n-sided regular polygon
- Number of cycles formed by joining vertices of n sided polygon at the center
- Find the number of distinct pairs of vertices which have a distance of exactly k in a tree
- Count of distinct XORs formed by rearranging two Binary strings
- Count of distinct numbers formed by shuffling the digits of a large number N
- Number of ways a convex polygon of n+2 sides can split into triangles by connecting vertices
- Count of alphabets whose ASCII values can be formed with the digits of N
- Find the count of numbers that can be formed using digits 3, 4 only and having length at max N.
- Count of times second string can be formed from the characters of first string
- Count of Substrings that can be formed without using the given list of Characters
- Maximum count of Equilateral Triangles that can be formed within given Equilateral Triangle
- Maximize count of strings of length 3 that can be formed from N 1s and M 0s
- Length of array pair formed where one contains all distinct elements and other all same elements
- Erdos Renyl Model (for generating Random Graphs)
- Find maximum number that can be formed using digits of a given number
- Find if a molecule can be formed from 3 atoms using their valence numbers

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.