Skip to content

Instantly share code, notes, and snippets.

@akellehe
Created June 15, 2015 18:51
Show Gist options
  • Save akellehe/443344a42b6217057105 to your computer and use it in GitHub Desktop.
Save akellehe/443344a42b6217057105 to your computer and use it in GitHub Desktop.
Get descendants from a tree using a queue.
def get_descendants(node, A):
unexplored = deque([node])
descendants = set()
while unexplored:
node = unexplored.popleft()
descendants.add(node)
children = A.getrow(node).indices
for child in children:
unexplored.append(child)
return descendants
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment