Created
December 21, 2009 19:05
-
-
Save jodell/261156 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
## | |
# Given a key/value list of nodes and their children, this returns the reversed, | |
# breadth-first search, the driving need of which is to find the build order of | |
# applying seed tables and their dependencies. | |
# | |
# - jodell 20080530 | |
# | |
def reverse_bfs(list, nodes = []) | |
return nodes unless nodes.any? { |v| not list[v].nil? } | |
return reverse_bfs( | |
list.reject { |k, v| not (list.invert.keys.flatten - [nil]).include?(k) }, | |
list.invert.keys.flatten - [nil] | |
) | nodes | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment