Change language

Find whether a sequence of degrees can form a simple graph | Havel-Hakimi Algorithm

| |

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 ie isolated. It’s a contradiction.

Approach. One way to test the existence of a simple graph —  Havel-Hakimi algorithm, below:

  • Sort sequence of non-negative integers numbers in ascending order.
  • Remove the first element (say V). Subtract 1 from the following V elements.
  • Repeat 1 and 2 until one of the stop conditions is met.

Stop conditions:

  • All remaining elements are 0 (simple graph exists).
  • Encounter with a negative number after subtraction (simple graph does not exist).
  • Not enough elements for the subtraction step (simple graph does not exist ).

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
// there is a simple graph

bool graphExists (vector " int " & amp; a, int n)

{

// Continue operations until

  // stop conditions met

  while (1)

{

// Sort the list in descending order

< code class = "undefined spaces">  sort (a.begin (), a.end (), greater "& gt; ()); 

 

// Check if all elements are 0

  if (a [0] == 0 and a [a.size () - 1] == 0)

return true

 

// Store the first element in a variable

  // and remove it from the list

int v = a [0] ; 

a.erase (a.begin () + 0); 

 

// Check if there are enough elements

  // are in the list

if (v" a.size ( ))

return false

 

// Subtract the first element from the following v elements

  for ( int i = 0; i "v; i ++)

{

a [i] -; 

 

// Check if the negative element is

  // occur 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 "& lt;  "Yes"

cout "& lt;  "NO" "& lt; endl; 

 

return 0; 

}

 
// This code is provided
// sanjeev2552

python3

# Python implementation of the approach

  
# A function that returns true if
# a simple graph exists

def graphExists (a):

 

# Continue operations until

# stop condition met

while True

 

  # Sort the list in descending order

a = sorted (a, reverse = True )

 

# Check if all elements are 0

if a [ 0 ] = = 0 and a [ < code class = "functions"> len (a) - 1 ] = = 0 :

return True

 

# Store first element in variable

# and remove it from the list

v = a [ 0 ]

a = a [ 1 < code class = "plain">:]

 

# Check if there are enough elements

# are in the list

if v" len (a): 

return False

 

# Subtract the first element from the following elements v

for i in range (v):

  a [i] - = 1

  

  # Check if the negative element is

# occurs after subtraction

if a [i] " 0 :

return False

 

 
# Driver code

a = [ 3 ,  3 , 3 , 3 ]

if (graphExists (a)):

print ( "Yes" )

else :

print ( "No" )

Exit:

 Yes 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically