VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-if-a-degree-sequence-can-form-a-simple-graph-havel-hakimi-algorithm/

⇱ Find if a degree sequence can form a simple graph | Havel-Hakimi Algorithm - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Find if a degree sequence can form a simple graph | Havel-Hakimi Algorithm

Last Updated : 24 Jun, 2024

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: 

  • 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:


Output
Yes

Time Complexity: O()
Auxiliary Space: O(1)

Comment
Article Tags:
Article Tags: