Created
November 9, 2019 08:37
-
-
Save woodRock/fb447c3491333094c2b04d86bd31ea86 to your computer and use it in GitHub Desktop.
This algorithm is a ruby implementation of the Havel-Hakimi Algorithm.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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