📜  检查给定的顶点度数是否代表图或树

📅  最后修改于: 2022-05-13 01:57:54.266000             🧑  作者: Mango

检查给定的顶点度数是否代表图或树

给定顶点数和每个顶点的度数,其中顶点数为 1、2、3、…n。任务是识别它是图还是树。可以假设图是连通的。例子:

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

顶点的度数由入射或离开它的边数给出。这可以简单地使用树的属性来完成,例如 -

  1. 树是连通的,没有环,而图可以有环。
  2. 树正好有n-1 条边,而图没有这样的约束。
  3. 假设输入图是连通的。我们至少需要 n-1 条边来连接 n 个节点。

如果我们取所有度数的总和,每条边将被计算两次。因此,对于具有n个顶点和n – 1条边的树,所有度数的总和应为2 * (n – 1) 。详情请参考握手引理。所以基本上我们需要检查所有度数的总和是否为 2*(n-1)。

C++
// C++ program to check whether input degree
// sequence is graph or tree
#include
using namespace std;
 
// Function returns true for tree
// false for graph
bool check(int degree[], int n)
{
    // Find sum of all degrees
    int deg_sum = 0;
    for (int i = 0; i < n; i++)
        deg_sum += degree[i];
 
    // Graph is tree if sum is equal to 2(n-1)
    return (2*(n-1) == deg_sum);
}
 
// Driver program to test above function
int main()
{
    int n = 5;
    int degree[] = {2, 3, 1, 1, 1};
 
    if (check(degree, n))
        cout << "Tree";
    else
        cout << "Graph";
 
    return 0;
}


Java
// Java program to check whether input degree
// sequence is graph or tree
class GFG
{
 
    // Function returns true for tree
    // false for graph
    static boolean check(int degree[], int n)
    {
        // Find sum of all degrees
        int deg_sum = 0;
        for (int i = 0; i < n; i++)
        {
            deg_sum += degree[i];
        }
 
        // Graph is tree if sum is equal to 2(n-1)
        return (2 * (n - 1) == deg_sum);
    }
 
    // Driver code
    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");
        }
    }
}
 
 
// This code has been contributed
// by 29AjayKumar


Python
# Python program to check whether input degree
# sequence is graph or tree
def check(degree, n):
     
    # Find sum of all degrees
    deg_sum = sum(degree)
     
    # It is tree if sum is equal to 2(n-1)
    if (2*(n-1) == deg_sum):
        return True
    else:
        return False
     
#main
n = 5
degree = [2, 3, 1, 1, 1];
if (check(degree, n)):
    print "Tree"
else:
    print "Graph"


C#
// C# program to check whether input
// degree sequence is graph or tree
using System;
 
class GFG
{
 
    // Function returns true for tree
    // false for graph
    static bool check(int[] degree, int n)
    {
        // Find sum of all degrees
        int deg_sum = 0;
        for (int i = 0; i < n; i++)
        {
            deg_sum += degree[i];
        }
 
        // Graph is tree if sum is
        // equal to 2(n-1)
        return (2 * (n - 1) == deg_sum);
    }
 
    // Driver code
    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");
        }
    }
}
 
// This code has been contributed
// by Akanksha Rai


PHP


输出:

Tree