📌  相关文章
📜  具有N个顶点和M个边的简单图的数量

📅  最后修改于: 2021-04-29 03:30:21             🧑  作者: Mango

给定两个整数NM ,任务是计算可以用N个顶点M个边绘制的简单无向图的数量。简单图是不包含多个边和自环的图。

例子:

方法: N个顶点从1N编号。由于没有自环或多个边,因此该边必须存在于两个不同的顶点之间。因此,我们可以选择两个不同顶点的方式是N C 2 ,它等于(N *(N – 1))/ 2 。假设它为P。
现在必须将M个边与这对顶点一起使用,因此在P对之间选择M对顶点的方式将为P C M。
如果P 则答案将为0,因为多余的边缘不能单独留下。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
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;
}


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


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


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


PHP
 $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
?>


输出:
10