Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com

Counting Graphs

Easy
0/40
profile
Contributed by
44 upvotes

Problem statement

Return the number of undirected graphs that can be formed using 'N' vertices, allowing for disconnected components within the graph. Self-edges and multiple edges are not allowed.


For Example:
For N = 2,
Count is 2.
Consider the vertices to be ‘A’ and ‘B’.
These are the possible 2 graphs.  

alt.txt

Detailed explanation ( Input/output format, Notes, Images )
Input format:
The first line contains a number ‘N’.
Output Format:
The only line contains the count of undirected graphs. 
Note:
You don’t need to print anything. It has already been taken care of, just complete the given function.
Sample Input 1:
3
Sample Output 1:
8   
Explanation of sample output 1:
For ‘N’ = 3,
Consider the vertices to be ‘A’, ’B’ and ‘C’.
These are the possible 8 graphs.

alt.txt

Sample Input 2:
5
Sample Output 2:
1024
Constraints:
1 <= ‘N’ <= 8
Time Limit: 1 sec
Hint

Existence of edges in the graph?

Approaches (1)
Combinatorics

Approach:
To determine the total number of edges possible in a graph with 'N' vertices, we can use the combination formula. Each edge requires two vertices so we can choose any two vertices from the 'N' vertices. Therefore, the total number of edges possible, denoted as 'E', is given by the formula E = (N * (N-1)) / 2.

Now, when counting the total number of graphs, we can consider that each edge in the graph may either exist or not exist. This gives us two options for each edge. Since there are 'E' edges, the total number of possible combinations of edge choices is 2^E. This represents all the different possible graphs that can be formed.

By right-shifting 1 'E' times, we essentially multiply 1 by 2 'E' times, which is equivalent to 2^E. This reduces the time complexity from O(log E) due to the power function to O(1). 

Although ‘E’ is relatively small but it is a good practice to use right shift instead of power for calculating a power of 2.


 

Algorithm:

  • declare ‘ans’ as a 32-bit integer
  • declare ‘E’ as a 32-bit integer
  • E = (N * (N-1)) / 2
  • ans = 1 << E
  • return ans
Time Complexity

O(1)
 

We are using right shift operation, which takes O(1) time.

Space Complexity

O(1)
 

We are not using any extra space, thus the constant space complexity.

Code Solution
(100% EXP penalty)
Counting Graphs
All tags
Sort by
Search icon

Interview problems

Approach

To determine the total number of edges possible in a graph with 'N' vertices, we can use the combination formula. Each edge requires two vertices so we can choose any two vertices from the 'N' vertices. Therefore, the total number of edges possible, denoted as 'E', is given by the formula E = (N * (N-1)) / 2.

Now, when counting the total number of graphs, we can consider that each edge in the graph may either exist or not exist. This gives us two options for each edge. Since there are 'E' edges, the total number of possible combinations of edge choices is 2^E. This represents all the different possible graphs that can be formed.

621 views
0 replies
11 upvotes

Interview problems

Easy C++ implementation with Description | TC-O(1) SC-O(1)

// To find the total number of "undirected" graphs by N vertices

// can be acheived by 2^(N(N-1)/2);

 

int countingGraphs(int N)

{

    // Write your code here.

    int power=N*(N-1)/2;

 

    return pow(2,power);

}

836 views
0 replies
1 upvote

Interview problems

Python easy Code

from typing import *

 

def countingGraphs(N: int) -> int:

    # Write your code here

    if N==0 or N==1:

        return N

    return  round(2**(N * (N - 1) / 2))

    return  

    pass

119 views
0 replies
0 upvotes

Interview problems

easy solution in c++

int countingGraphs(int N)

{

    int p=N*(N-1)/2;

    return pow(2,p);// Write your code here.

}

668 views
0 replies
0 upvotes

Interview problems

java

public class Solution {

public static int countingGraphs(int N) {

if (N == 0 || N == 1) {

return N;

}

return (int) Math.round(Math.pow(2, N * (N - 1) / 2));

}

}

 

377 views
0 replies
1 upvote

Interview problems

Works for CPP all test cases

int factorial(int n){

  int fact=1;

  while (n > 0) {

    fact = fact * n;

    n = n - 1;

  }

}

 

int countingGraphs(int N)

 

{

    if (N==1){

        return 1;

    }

    if(N==2){

        return 2;

    }

    return pow(2,factorial(N)/(2*factorial(N-2)));

   

}

578 views
0 replies
2 upvotes

Interview problems

[C++]✅Super Easy Solution | 1 Line | Maths Formula

Number of Graphs = 2^(n*(n-1)/2)

 

int countingGraphs(int N){
    return pow(2,N*(N-1)/2);
}
968 views
0 replies
3 upvotes

Interview problems

simplest question in striver sheet

int powval = N*(N-1)/2;

        return (int)Math.pow(2, powval);

380 views
0 replies
0 upvotes

Interview problems

Easy Java Solution

public class Solution {

    public static int countingGraphs(int N) {

        double pow=N*(N-1)/2;

        double cnt=Math.pow(2, pow);

        return (int)cnt;

    }

}

581 views
2 replies
0 upvotes

Interview problems

N choose 2

number of edges =nC2

 

number of graph = 2^(nC2)

int countingGraphs(int N)
{
   int nC2=N*(N-1)/2;
   return 1<<(nC2);
}
687 views
0 replies
3 upvotes
Full screen
Console