# Number of Simple Graph with N Vertices and M Edges

Given two integers **N** and **M**, the task is to count the number of simple undirected graphs that can be drawn with **N vertices** and **M edges**. A simple graph is a graph that does not contain multiple edges and self loops.

**Examples:**

Input:N = 3, M = 1

Output:3

The 3 graphs are {1-2, 3}, {2-3, 1}, {1-3, 2}.

Input:N = 5, M = 1

Output:10

**Approach:** The **N** vertices are numbered from **1** to **N**. As there is no self loops or multiple edges, the edge must be present between two different vertices. So the number of ways we can choose two different vertices are ** ^{N}C_{2}** which is equal to

**(N * (N – 1)) / 2**. Assume it

**P**.

Now

**M**edges must be used with these pair of vertices, so the number of ways to choose

**M**pairs of vertices between

**P**pairs will be

**.**

^{P}C_{M}If

**P < M**then the answer will be

**0**as the extra edges can not be left alone.

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to return the value of ` `// Binomial Coefficient C(n, k) ` `int` `binomialCoeff(` `int` `n, ` `int` `k) ` `{ ` ` ` ` ` `if` `(k > n) ` ` ` `return` `0; ` ` ` ` ` `int` `res = 1; ` ` ` ` ` `// Since C(n, k) = C(n, n-k) ` ` ` `if` `(k > n - k) ` ` ` `k = n - k; ` ` ` ` ` `// Calculate the value of ` ` ` `// [n * (n - 1) *---* (n - k + 1)] / [k * (k - 1) * ... * 1] ` ` ` `for` `(` `int` `i = 0; i < k; ++i) { ` ` ` `res *= (n - i); ` ` ` `res /= (i + 1); ` ` ` `} ` ` ` ` ` `return` `res; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `N = 5, M = 1; ` ` ` ` ` `int` `P = (N * (N - 1)) / 2; ` ` ` ` ` `cout << binomialCoeff(P, M); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return the value of ` ` ` `// Binomial Coefficient C(n, k) ` ` ` `static` `int` `binomialCoeff(` `int` `n, ` `int` `k) ` ` ` `{ ` ` ` ` ` `if` `(k > n) ` ` ` `return` `0` `; ` ` ` ` ` `int` `res = ` `1` `; ` ` ` ` ` `// Since C(n, k) = C(n, n-k) ` ` ` `if` `(k > n - k) ` ` ` `k = n - k; ` ` ` ` ` `// Calculate the value of ` ` ` `// [n * (n - 1) *---* (n - k + 1)] / ` ` ` `// [k * (k - 1) * ... * 1] ` ` ` `for` `(` `int` `i = ` `0` `; i < k; ++i) ` ` ` `{ ` ` ` `res *= (n - i); ` ` ` `res /= (i + ` `1` `); ` ` ` `} ` ` ` `return` `res; ` ` ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `N = ` `5` `, M = ` `1` `; ` ` ` `int` `P = (N * (N - ` `1` `)) / ` `2` `; ` ` ` ` ` `System.out.println(binomialCoeff(P, M)); ` `} ` `} ` ` ` `// This code is contributed by Shivi_Aggarwal ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 implementation of the approach ` ` ` `# Function to return the value of ` `# Binomial Coefficient C(n, k) ` `def` `binomialCoeff(n, k): ` ` ` ` ` `if` `(k > n): ` ` ` `return` `0` ` ` ` ` `res ` `=` `1` ` ` ` ` `# Since C(n, k) = C(n, n-k) ` ` ` `if` `(k > n ` `-` `k): ` ` ` `k ` `=` `n ` `-` `k ` ` ` ` ` `# Calculate the value of ` ` ` `# [n * (n - 1) *---* (n - k + 1)] / ` ` ` `# [k * (k - 1) * ... * 1] ` ` ` `for` `i ` `in` `range` `( k): ` ` ` `res ` `*` `=` `(n ` `-` `i) ` ` ` `res ` `/` `/` `=` `(i ` `+` `1` `) ` ` ` ` ` `return` `res ` ` ` `# Driver Code ` `if` `__name__` `=` `=` `"__main__"` `: ` ` ` ` ` `N ` `=` `5` ` ` `M ` `=` `1` ` ` ` ` `P ` `=` `(N ` `*` `(N ` `-` `1` `)) ` `/` `/` `2` ` ` ` ` `print` `(binomialCoeff(P, M)) ` ` ` `# This code is contributed by ita_c ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of the approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` `// Function to return the value of ` `// Binomial Coefficient C(n, k) ` `static` `int` `binomialCoeff(` `int` `n, ` `int` `k) ` `{ ` ` ` ` ` `if` `(k > n) ` ` ` `return` `0; ` ` ` ` ` `int` `res = 1; ` ` ` ` ` `// Since C(n, k) = C(n, n-k) ` ` ` `if` `(k > n - k) ` ` ` `k = n - k; ` ` ` ` ` `// Calculate the value of ` ` ` `// [n * (n - 1) *---* (n - k + 1)] / ` ` ` `// [k * (k - 1) * ... * 1] ` ` ` `for` `(` `int` `i = 0; i < k; ++i) ` ` ` `{ ` ` ` `res *= (n - i); ` ` ` `res /= (i + 1); ` ` ` `} ` ` ` ` ` `return` `res; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `N = 5, M = 1; ` ` ` ` ` `int` `P = (N * (N - 1)) / 2; ` ` ` ` ` `Console.Write(binomialCoeff(P, M)); ` `} ` `} ` ` ` `// This code is contributed ` `// by Akanksha Rai ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of the approach ` ` ` `// Function to return the value of ` `// Binomial Coefficient C(n, k) ` `function` `binomialCoeff(` `$n` `, ` `$k` `) ` `{ ` ` ` `if` `(` `$k` `> ` `$n` `) ` ` ` `return` `0; ` ` ` ` ` `$res` `= 1; ` ` ` ` ` `// Since C(n, k) = C(n, n-k) ` ` ` `if` `(` `$k` `> ` `$n` `- ` `$k` `) ` ` ` `$k` `= ` `$n` `- ` `$k` `; ` ` ` ` ` `// Calculate the value of ` ` ` `// [n * (n - 1) *---* (n - k + 1)] / ` ` ` `// [k * (k - 1) * ... * 1] ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$k` `; ++` `$i` `) ` ` ` `{ ` ` ` `$res` `*= (` `$n` `- ` `$i` `); ` ` ` `$res` `/= (` `$i` `+ 1); ` ` ` `} ` ` ` ` ` `return` `$res` `; ` `} ` ` ` `// Driver Code ` `$N` `= 5; ` `$M` `= 1; ` ` ` `$P` `= ` `floor` `((` `$N` `* (` `$N` `- 1)) / 2); ` ` ` `echo` `binomialCoeff(` `$P` `, ` `$M` `); ` ` ` `// This code is contributed by Ryuga ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

10

## Recommended Posts:

- Minimum number of edges between two vertices of a graph using DFS
- Minimum number of edges between two vertices of a Graph
- Largest subset of Graph vertices with edges of 2 or more colors
- Count number of edges in an undirected graph
- Program to find total number of edges in a Complete Graph
- Maximum number of edges among all connected components of an undirected graph
- Maximum number of edges to be added to a tree so that it stays a Bipartite graph
- Calculate number of nodes between two vertices in an acyclic Graph by Disjoint Union method
- Finding in and out degrees of all vertices in a graph
- Construct a graph from given degrees of all vertices
- Articulation Points (or Cut Vertices) in a Graph
- Maximum and minimum isolated vertices in a graph
- Find if there is a path between two vertices in a directed graph
- All vertex pairs connected with exactly k edges in a graph
- Check whether given degrees of vertices represent a Graph or Tree

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.