Skip to content

Instantly share code, notes, and snippets.

@jodell
Created December 21, 2009 19:05
Show Gist options
  • Save jodell/261156 to your computer and use it in GitHub Desktop.
Save jodell/261156 to your computer and use it in GitHub Desktop.
##
# 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