'Walk back until first branch found in networkx graph
Given a directed graph, I want to start at a leaf node, and walk back (breadth first) until I find the first node with more than one successor (or a degree greater than 1).
I can do it with this code, but nx.ancestors() involves reading in all the nodes that have a path to the leaf node, which can be very large.
for ancestor in reversed(list(nx.ancestors(DG, leaf_node))):
if DG.out_degree(ancestor) > 1: # branching point reached
# print("found")
Is there a way I can do this faster, potentially going back one node at a time and checking for degree? Thanks!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|