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()

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:

- Sort the sequence of non-negative integers in non-increasing order.
- Delete the first element(say V). Subtract 1 from the next V elements.
- Repeat 1 and 2 until one of the stopping conditions is met.

Stopping conditions:

- All the elements remaining are equal to 0 (Simple graph exists).
- Negative number encounter after subtraction (No simple graph exists).
- Not enough elements remaining for the subtraction step (No simple graph exists).

Below is the implementation of the above approach:

## C++

`// C++ implementation of the approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function that returns true if ` `// a simple graph exists ` `bool` `graphExists(vector<` `int` `> &a, ` `int` `n) ` `{ ` ` ` `// Keep performing the operations until one ` ` ` `// of the stopping condition is met ` ` ` `while` `(1) ` ` ` `{ ` ` ` `// Sort the list in non-decreasing order ` ` ` `sort(a.begin(), a.end(), greater<>()); ` ` ` ` ` `// Check if all the elements are equal to 0 ` ` ` `if` `(a[0] == 0) ` ` ` `return` `true` `; ` ` ` ` ` `// Store the first element in a variable ` ` ` `// and delete it from the list ` ` ` `int` `v = a[0]; ` ` ` `a.erase(a.begin() + 0); ` ` ` ` ` `// Check if enough elements ` ` ` `// are present in the list ` ` ` `if` `(v > a.size()) ` ` ` `return` `false` `; ` ` ` ` ` `// Subtract first element from next v elements ` ` ` `for` `(` `int` `i = 0; i < v; i++) ` ` ` `{ ` ` ` `a[i]--; ` ` ` ` ` `// Check if negative element is ` ` ` `// encountered after subtraction ` ` ` `if` `(a[i] < 0) ` ` ` `return` `false` `; ` ` ` `} ` ` ` `} ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `vector<` `int` `> a = {3, 3, 3, 3}; ` ` ` `int` `n = a.size(); ` ` ` ` ` `graphExists(a, n) ? cout << ` `"Yes"` `: ` ` ` `cout << ` `"NO"` `<< endl; ` ` ` ` ` `return` `0; ` `} ` ` ` `// This code is contributed by ` `// sanjeev2552 ` |

*chevron_right*

*filter_none*

## Python3

`# Python implementation of the approach ` ` ` `# Function that returns true if ` `# a simple graph exists ` `def` `graphExists(a): ` ` ` ` ` `# Keep performing the operations until one ` ` ` `# of the stopping condition is met ` ` ` `while` `True` `: ` ` ` ` ` `# Sort the list in non-decreasing order ` ` ` `a ` `=` `sorted` `(a, reverse ` `=` `True` `) ` ` ` ` ` `# Check if all the elements are equal to 0 ` ` ` `if` `a[` `0` `]` `=` `=` `0` `and` `a[` `len` `(a)` `-` `1` `]` `=` `=` `0` `: ` ` ` `return` `True` ` ` ` ` `# Store the first element in a variable ` ` ` `# and delete it from the list ` ` ` `v ` `=` `a[` `0` `] ` ` ` `a ` `=` `a[` `1` `:] ` ` ` ` ` `# Check if enough elements ` ` ` `# are present in the list ` ` ` `if` `v>` `len` `(a): ` ` ` `return` `False` ` ` ` ` `# Subtract first element from next v elements ` ` ` `for` `i ` `in` `range` `(v): ` ` ` `a[i]` `-` `=` `1` ` ` ` ` `# Check if negative element is ` ` ` `# encountered after subtraction ` ` ` `if` `a[i]<` `0` `: ` ` ` `return` `False` ` ` ` ` `# Driver code ` `a ` `=` `[` `3` `, ` `3` `, ` `3` `, ` `3` `] ` `if` `(graphExists(a)): ` ` ` `print` `(` `"Yes"` `) ` `else` `: ` ` ` `print` `(` `"No"` `) ` |

*chevron_right*

*filter_none*

**Output:**

Yes

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.

## Recommended Posts:

- Find the Degree of a Particular vertex in a Graph
- Find any simple cycle in an undirected unweighted Graph
- Degree of a Cycle Graph
- Print the degree of every node from the given Prufer sequence
- Nodes with prime degree in an undirected Graph
- Print the node with the maximum degree in the prufer sequence
- Difference Between sum of degrees of odd and even degree nodes in an Undirected Graph
- Spanning Tree With Maximum Degree (Using Kruskal's Algorithm)
- Number of Simple Graph with N Vertices and M Edges
- Python | Simple registration form using Tkinter
- Bellman Ford Algorithm (Simple Implementation)
- Minimum Cost of Simple Path between two nodes in a Directed and Weighted Graph
- Kruskal's Algorithm (Simple Implementation for Adjacency Matrix)
- Prim's Algorithm (Simple Implementation for Adjacency Matrix Representation)
- Hierholzer's Algorithm for directed graph
- Graph Coloring | Set 2 (Greedy Algorithm)
- Arrange numbers to form a valid sequence
- Welsh Powell Graph colouring Algorithm
- Hypothesis (language regularity) and algorithm (L-graph to NFA) in TOC
- Why Prim’s and Kruskal's MST algorithm fails for Directed Graph?

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.