Create the edges out of a list of nodes when their levels in the network are known

26 Views Asked by At

I have a list of nodes and a list of their levels in a network and I am having trouble to create the edges. Assume the following example where the lists are series:

names = ['George', 'John', 'Leda', 'Theo']
levels = [0, 1, 1, 2]

The idea is that George has John and Leda beneath him, whereas Theo is beneath both John and Leda and connected with them.

In other words, my graph will be a DiGraph() which should be created by the list of edges that are required. So the output I am trying to achieve is:

[('George', 'John'),
 ('George', 'Leda'),
 ('John', 'Theo')
 ('Leda', 'Theo')]

I have not gone very far myself:

end = len(levels)
edges = []

for i in np.arange(start=end-1, stop=-1, step=-1):
    if levels[i]-levels[i-1] == 1:
        edge = (names[i-1], names[i])
        edges.append(edge)

The above piece of code skips the part where the two names are in the same level. I would, therefore, appreciate some help to either complete this piece of code or to be shown a more pythonic way to think of this.

In case it is necessary, I intend to create the network with graphviz.Digraph() in order to achieve a graph where the tabs have the shape of the record and add some more info in there that exist in other parts of the initial dataframe. But I am open to suggestions for another library too as long as the the final output has orthogonal shapes and can fit long names and more information in more than one lines.

0

There are 0 best solutions below