Skip to content

Instantly share code, notes, and snippets.

@woodRock
Created November 9, 2019 08:37
Show Gist options
  • Save woodRock/fb447c3491333094c2b04d86bd31ea86 to your computer and use it in GitHub Desktop.
Save woodRock/fb447c3491333094c2b04d86bd31ea86 to your computer and use it in GitHub Desktop.
This algorithm is a ruby implementation of the Havel-Hakimi Algorithm.
def hakimi n,list
list = list.sort.reverse
return true if list.empty? or list[0] == 0
list.delete(0)
n = list.shift
return false if (n > list.length)
(0..n-1).each {|i| list[i] = (list[i]-1)}
return hakimi(n, list)
end
list = [5, 3, 0, 2, 6, 2, 0, 7, 2, 5]
puts hakimi(list.length, list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment