0

I'm trying to represent and traverse graphs in order to show and control family relationships. I've been able to build the edges to make the following graph (How to draw a tree more beautifully in networkx plus countless others have gotten me this far - my red numbers of course)

family tree graph sample

So I understand this to be a Directed Graph with Parent relationships shown. I want to be able to get some more information using networkx but I haven't been able to work out how to approach it. I'd even take some good doco/tutorials if people have them but I'm trying to solve the following:

  1. I've been able to identify the children who are also parents, can I split these into seperate graphs? e.g. 2,3,6,7,8,9,10,11,12 are one "family", 5,9,15 are another "family", 1,5 is also a family

  2. Can I color or otherwise show the generations. I see 3 generations here (1,2,3 and 4-12 and 13-15) I believe I can use a topological sort to find these orders, however when I do I get 4 nodes, then 8 then 3 instead of 3,9,3

Ewanw
  • 728
  • 11
  • 30

1 Answers1

0

W.r.t. 2): Topological sort will always put a node in the highest possible layer, which is why in your case node 4 is put into in the top layer (presumably). Instead, you can map your y value from the dot layout to a layer. If that is too hacky for you, you can compute the the hierarchical layout explicitly. Both approaches are outlined here.

W.r.t. 1) You seem to define a "family" purely based on parent-child relationships, so all you need to do is compute the predecessors for each node, and then aggregate. Something along the lines of:

families = dict() # maps parents to children
for child in G:
    parents = tuple([predecessor for predecessor, target in nx.predecessor(G, child, cutoff=1).items() if target != []])
    if parents in families:
        families[parents].append(child)
    else: 
        families[parents] = [child]
Paul Brodersen
  • 11,221
  • 21
  • 38