Hypercube Graph

Last Updated : 8 Feb, 2023

You are given input as order of graph n (highest number of edges connected to a node), you have to find the number of vertices in a Hypercube graph of order n. 

Examples:  

Input : n = 3
Output : 8

Input : n = 2
Output : 4

In hypercube graph Q(n), n represents the degree of the graph. Hypercube graph represents the maximum number of edges that can be connected to a graph to make it an n degree graph, every vertex has the same degree n and in that representation, only a fixed number of edges and vertices are added as shown in the figure below:

Hypercube Graph

All hypercube graphs are Hamiltonian, hypercube graph of order n has (2^n) vertices, , for input n as the order of graph we have to find the corresponding power of 2.  

Recursive Approach

C++
// C++ program to find vertices in a hypercube 
// graph of order n
#include <iostream>
using namespace std;

// function to find power of 2
int power(int n)
{
    if (n == 1)
        return 2;
    return 2 * power(n - 1);
}

// driver program
int main()
{
    // n is the order of the graph
    int n = 4;
    cout << power(n);
    return 0;
}
Java
// Java program to find vertices in 
// a hypercube graph of order n 
class GfG
{

    // Function to find power of 2 
    static int power(int n) 
    { 
        if (n == 1) 
            return 2; 
        return 2 * power(n - 1); 
    } 
    
    // Driver program 
    public static void main(String []args)
    {
        
        // n is the order of the graph 
        int n = 4;
        System.out.println(power(n));
    }
}

// This code is contributed by Rituraj Jain
Python3
 # Python3 program to find vertices in a hypercube 
#  graph of order n

# function to find power of 2
def power(n):
    if n==1:
        return 2
    return 2*power(n-1)


# Driver code
n =4
print(power(n))


# This code is contributed by Shrikant13
C#
// C# program to find vertices in 
// a hypercube graph of order n 
using System;

class GfG
{

    // Function to find power of 2 
    static int power(int n) 
    { 
        if (n == 1) 
            return 2; 
        return 2 * power(n - 1); 
    } 
    
    // Driver code 
    public static void Main()
    {
        
        // n is the order of the graph 
        int n = 4;
        Console.WriteLine(power(n));
    }
}

// This code is contributed by Mukul Singh
PHP
<?php
// PHP program to find vertices in 
// a hypercube graph of order n 
{

    // Function to find power of 2 
    function power($n) 
    { 
        if ($n == 1) 
            return 2; 
        return 2 * power($n - 1); 
    } 
    
    // Driver Code
    {
        
        // n is the order of the graph 
        $n = 4;
        echo(power($n));
    }
}

// This code is contributed by Code_Mech
?>
JavaScript
<script>


// Javascript program to find vertices in a hypercube 
// graph of order n

// function to find power of 2
function power(n)
{
    if (n == 1)
        return 2;
    return 2 * power(n - 1);
}

// driver program
// n is the order of the graph
var n = 4;
document.write( power(n));

</script> 

Output
16

Iterative Approach

C++
// C++ program to find vertices in
// a hypercube graph of order n
#include <bits/stdc++.h>
using namespace std;

// Function to find power of 2
int power(int n)
{

    if (n == 0)
        return 0;

    int pow = 1;

    for (int i = 1; i <= n; i++)
    {
        pow *= 2;
    }

    return pow;
}

int main()
{
    // n is the order of the graph
    int n = 4;
    cout << power(n);
    return 0;
}

// This code is contributed by akashish__
Java
/*package whatever //do not write package name here */

import java.io.*;

// Java program to find vertices in 
// a hypercube graph of order n 
class GFG {
  
  // Function to find power of 2 
    static int power(int n) 
    { 
      
      if(n==0) return 0;
      
      int pow = 1;
      
      for(int i=1;i<=n;i++){
          pow *= 2;
      }
      
      return pow;
    } 
  
    public static void main (String[] args) {
         // n is the order of the graph 
        int n = 4;
        System.out.println(power(n));
    }
}
    
Python3
# Python program to find vertices in
# a hypercube graph of order n

# Function to find power of 2
def power(n):

    if n == 0:
        return 0

    pow = 1

    for i in range(1, n+1):
        pow *= 2

    return pow

# n is the order of the graph
n = 4
print(power(n))

# This code is contributed by akashish__
C#
// C# program to find vertices in
// a hypercube graph of order n
using System;

public class GFG {

  // Function to find power of 2
  public static int power(int n)
  {

    if (n == 0)
      return 0;

    int pow = 1;

    for (int i = 1; i <= n; i++) {
      pow *= 2;
    }

    return pow;
  }

  static public void Main()
  {

    // n is the order of the graph
    int n = 4;
    Console.WriteLine(power(n));
  }
}

// This code is contributed by akashish__
JavaScript
// Function to find power of 2
function power(n) {
  if (n === 0) return 0;

  let pow = 1;

  for (let i = 1; i <= n; i++) {
    pow *= 2;
  }

  return pow;
}


// n is the order of the graph
const n = 4;
console.log(power(n));

// This code is contributed by akashish__

Output
16

Java Using Math.pow()

C++
#include <bits/stdc++.h>
using namespace std;

// Function to find power of 2
int power(int n) { return pow(2, n); }

int main()
{
  
    // n is the order of the graph
    int n = 4;
    cout << power(n) << endl;
    return 0;
}

// This code is contributed by akashish__
Java
/*package whatever //do not write package name here */

import java.io.*;

// Java program to find vertices in 
// a hypercube graph of order n 
class GFG {
  
  // Function to find power of 2 
    static int power(int n) 
    { 
      return (int)Math.pow(2,n);
    } 
  
    public static void main (String[] args) {
         // n is the order of the graph 
        int n = 4;
        System.out.println(power(n));
    }
}
    
Python3
# Function to find power of 2
def power(n):
    return pow(2, n)

# n is the order of the graph
n = 4
print(power(n))
# contributed by akashish__
C#
using System;

// Java program to find vertices in
// a hypercube graph of order n
public class GFG {

  // Function to find power of 2
  static int power(int n) { return (int)Math.Pow(2, n); }

  static public void Main()
  {

    // n is the order of the graph
    int n = 4;
    Console.WriteLine(power(n));
  }
}

// This code is contributed by akashish__
JavaScript
// Function to find power of 2
function power(n) { return Math.pow(2, n); }

// n is the order of the graph
let n = 4;
console.log(power(n));
// contributed by akashish__

Output
16
Comment