Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
98 views
in Technique[技术] by (71.8m points)

python - Networkx get ancestors with depth

Is there anything within NetworkX that will combine the functionality of ancestors and provide the depth of each object?

I want to get a dictionary of all ancestors in NetworkX, and get the depth from the source for each of those. Right now the function returns a set, but I'd like to know how deep each item is in the dependencies of an object.

My source is a sql table that has the input and output of a pipeline, which I then turn into a directed graph. Example:

input   output
b       a
c       a
x       b
y       b
z       b
w       b
l       c
m       c

and after running ancestors I have the set of everything that is in the chain, but I need a way to also be able to say that x is a level 3 or depth 3 object.

I've been looking through the various functions like traversal, but nothing jumps out at me as providing this functionality. Do I need to create my own loop to recreate ancestors with depth at this point?

question from:https://stackoverflow.com/questions/65910085/networkx-get-ancestors-with-depth

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I'll assume you've done the preprocessing, and have a graph pipe, and you know the root node a.

single_source_shortest_path_length(path, a)

This returns a dict of nodes and path lengths. The depth is that path length plus 1.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...