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.
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) ore not.
This article is contributed by Ayush Khanduri. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
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.
- Construct a graph from given degrees of all vertices
- Finding in and out degrees of all vertices in a graph
- Find K vertices in the graph which are connected to at least one of remaining vertices
- Number of trees whose sum of degrees of all the vertices is L
- Detect cycle in the graph using degrees of nodes of graph
- Maximize the number of uncolored vertices appearing along the path from root vertex and the colored vertices
- Sum of degrees of all nodes of a undirected graph
- Difference Between sum of degrees of odd and even degree nodes in an Undirected Graph
- Queries to check if vertices X and Y are in the same Connected Component of an Undirected Graph
- Check if every vertex triplet in graph contains two vertices connected to third vertex
- Find two disjoint good sets of vertices in a given graph
- Minimize cost to color all the vertices of an Undirected Graph using given operation
- Find if there is a path between two vertices in a directed graph
- Articulation Points (or Cut Vertices) in a Graph
- Largest subset of Graph vertices with edges of 2 or more colors
- Minimum number of edges between two vertices of a Graph
- Calculate number of nodes between two vertices in an acyclic Graph by Disjoint Union method
- Find if there is a path between two vertices in a directed graph | Set 2
- Maximum and minimum isolated vertices in a graph
- Number of Simple Graph with N Vertices and M Edges