Created
June 15, 2015 18:51
-
-
Save akellehe/443344a42b6217057105 to your computer and use it in GitHub Desktop.
Get descendants from a tree using a queue.
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 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