Skip to content
Related Articles

Related Articles

Ways to Remove Edges from a Complete Graph to make Odd Edges
  • Difficulty Level : Medium
  • Last Updated : 08 Mar, 2019

Given a complete graph with N vertices, the task is to count the number of ways to remove edges such that the resulting graph has odd number of edges.

Examples:

Input: N = 3
Output: 4
The initial graph has 3 edges as it is a complete graph. We can remove edges (1, 2) and (1, 3) or (1, 2) and (2, 3) or (1, 3) and (2, 3) or we do not remove any of the edges.

Input: N = 4
Output: 32

Approach: As the graph is complete so the total number of edges will be E = N * (N – 1) / 2. Now there are two cases,



  1. If E is even then you have to remove odd number of edges, so the total number of ways will be  ^EC_1 + ^EC_3 + ^EC_5 + .... + ^EC_{E-1} which is equivalent to 2^{(E-1)}.
  2. If E is odd then you have to remove even number of edges, so the total number of ways will be  ^EC_0 + ^EC_2 + ^EC_4 + .... + ^EC_{E-1} which is equivalent to 2^{(E-1)}.

Note that if N = 1 then the answer will be 0.

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 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




<?php
// PHP 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 
function countWays($N
    // Total number of edges 
    $E = ($N * ($N - 1)) / 2; 
  
    if ($N == 1) 
        return 0; 
  
    return (int)pow(2, $E - 1); 
  
// Driver code 
$N = 4; 
echo(countWays($N)); 
  
// This code is contributed 
// by Code_Mech.
?>


Output:

32

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.

My Personal Notes arrow_drop_up
Recommended Articles
Page :