📜  从完整图形中删除边以生成奇数边的方法

📅  最后修改于: 2021-04-22 08:09:08             🧑  作者: Mango

给定一个具有N个顶点的完整图,任务是计算去除边的方法的数量,以使生成的图具有奇数个边。

例子:

方法:由于图是完整的,所以边的总数将为E = N *(N – 1)/ 2 。现在有两种情况,

  1. 如果E偶数,则必须去除奇数个边,因此总的路数为 ^EC_1 + ^EC_3 + ^EC_5 + .... + ^EC_{E-1} 相当于2^{(E-1)}
  2. 如果E奇数,则必须去除偶数个边,因此总的路数为 ^EC_0 + ^EC_2 + ^EC_4 + .... + ^EC_{E-1} 相当于2^{(E-1)}

请注意,如果N = 1,则答案将为0

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the number of ways
// to remove edges from the graph so that
// odd number of edges are left in the graph
int countWays(int N)
{
    // Total number of edges
    int E = (N * (N - 1)) / 2;
  
    if (N == 1)
        return 0;
  
    return pow(2, E - 1);
}
  
// Driver code
int main()
{
    int N = 4;
    cout << countWays(N);
  
    return 0;
}


Java
// Java implementation of the approach 
class GfG 
{ 
  
// Function to return the number of ways 
// to remove edges from the graph so that 
// odd number of edges are left in the graph 
static int countWays(int N) 
{ 
    // Total number of edges 
    int E = (N * (N - 1)) / 2; 
  
    if (N == 1) 
        return 0; 
  
    return (int)Math.pow(2, E - 1); 
} 
  
// Driver code 
public static void main(String[] args) 
{ 
    int N = 4; 
    System.out.println(countWays(N)); 
}
} 
  
// This code is contributed by Prerna Saini


Python3
# Python3 implementation of the approach
  
# Function to return the number of ways
# to remove edges from the graph so that
# odd number of edges are left in the graph
def countWays(N):
      
    # Total number of edges
    E = (N * (N - 1)) / 2
  
    if (N == 1):
        return 0
  
    return int(pow(2, E - 1))
  
# Driver code
if __name__ == '__main__':
    N = 4
    print(countWays(N))
  
# This code contributed by PrinciRaj1992


C#
// C# implementation of the approach
  
using System;
  
public class GFG{
      
// Function to return the number of ways 
// to remove edges from the graph so that 
// odd number of edges are left in the graph 
static int countWays(int N) 
{ 
    // Total number of edges 
    int E = (N * (N - 1)) / 2; 
  
    if (N == 1) 
        return 0; 
  
    return (int)Math.Pow(2, E - 1); 
} 
  
// Driver code 
    static public void Main (){
      
    int N = 4; 
    Console.WriteLine(countWays(N)); 
    }
} 
// This code is contributed by ajit.


PHP


输出:
32