Given the number of vertices and the degree of each vertex where vertex numbers are 1, 2, 3,…n. The task is to identify whether it is a graph or a tree. It may be assumed that the graph is Connected.
Examples:
Input : 5
2 3 1 1 1
Output : Tree
Explanation : The input array indicates that
vertex one has degree 2, vertex
two has degree 3, vertices 3, 4
and 5 have degree 1.
1
/ \
2 3
/ \
4 5
Input : 3
2 2 2
Output : Graph
1
/ \
2 - 3
The degree of a vertex is given by the number of edges incident or leaving from it. This can simply be done using the properties of trees like –
- Tree is connected and has no cycles while graphs can have cycles.
- Tree has exactly n-1 edges while there is no such constraint for graph.
- It is given that the input graph is connected. We need at least n-1 edges to connect n nodes.
If we take the sum of all the degrees, each edge will be counted twice. Hence, for a tree with n vertices and n – 1 edges, sum of all degrees should be 2 * (n – 1). Please refer Handshaking Lemma for details. So basically we need to check if sum of all degrees is 2*(n-1) or not.
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
bool check( int degree[], int n)
{
int deg_sum = 0;
for ( int i = 0; i < n; i++)
deg_sum += degree[i];
return (2*(n-1) == deg_sum);
}
int main()
{
int n = 5;
int degree[] = {2, 3, 1, 1, 1};
if (check(degree, n))
cout << "Tree" ;
else
cout << "Graph" ;
return 0;
}
|
Java
class GFG
{
static boolean check( int degree[], int n)
{
int deg_sum = 0 ;
for ( int i = 0 ; i < n; i++)
{
deg_sum += degree[i];
}
return ( 2 * (n - 1 ) == deg_sum);
}
public static void main(String[] args)
{
int n = 5 ;
int degree[] = { 2 , 3 , 1 , 1 , 1 };
if (check(degree, n))
{
System.out.println( "Tree" );
}
else
{
System.out.println( "Graph" );
}
}
}
|
Python
def check(degree, n):
deg_sum = sum (degree)
if ( 2 * (n - 1 ) = = deg_sum):
return True
else :
return False
n = 5
degree = [ 2 , 3 , 1 , 1 , 1 ];
if (check(degree, n)):
print "Tree"
else :
print "Graph"
|
C#
using System;
class GFG
{
static bool check( int [] degree, int n)
{
int deg_sum = 0;
for ( int i = 0; i < n; i++)
{
deg_sum += degree[i];
}
return (2 * (n - 1) == deg_sum);
}
public static void Main()
{
int n = 5;
int [] degree = {2, 3, 1, 1, 1};
if (check(degree, n))
{
Console.WriteLine( "Tree" );
}
else
{
Console.WriteLine( "Graph" );
}
}
}
|
PHP
<?php
function check(& $degree , $n )
{
$deg_sum = 0;
for ( $i = 0; $i < $n ; $i ++)
$deg_sum += $degree [ $i ];
return (2 * ( $n - 1) == $deg_sum );
}
$n = 5;
$degree = array (2, 3, 1, 1, 1);
if (check( $degree , $n ))
echo "Tree" ;
else
echo "Graph" ;
?>
|
Javascript
function check(degree, n)
{
const deg_sum = degree.reduce((a, b) => a + b, 0);
if (2 * (n - 1) === deg_sum) {
return true ;
} else {
return false ;
}
}
const n = 5;
const degree = [2, 3, 1, 1, 1];
if (check(degree, n)) {
console.log( "Tree" );
} else {
console.log( "Graph" );
}
|
Time Complexity:O(N)
Space Complexity:O(1),since no extra space being used.
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
21 Mar, 2023
Like Article
Save Article