![]() |
VOOZH | about |
Given a sequence of non-negative integers arr[], the task is to check if there exists a simple graph corresponding to this degree sequence. Note that a simple graph is a graph with no self-loops and parallel edges.
Examples:
Input: arr[] = {3, 3, 3, 3}
Output: Yes
This is actually a complete graph(K4)Input: arr[] = {3, 2, 1, 0}
Output: No
A vertex has degree n-1 so it's connected to all the other n-1 vertices.
But another vertex has degree 0 i.e. isolated. It's a contradiction.
Approach: One way to check the existence of a simple graph is by Havel-Hakimi algorithm given below:
Stopping conditions:
Below is the implementation of the above approach:
Yes
Time Complexity: O()
Auxiliary Space: O(1)