How to force nodes in rows based on weight of incoming edges

Viewed 22

I would like to get a configuration where nodes are placed in rows based on the "weight" of incoming rows. Not sure if "weight" is the right word, but in the following case,

a -> b
b -> c
c -> d

the weight of the incoming edge to d should be 3, because it's 3 edges leading to d from the root. Similarly all nodes that have no incoming edges should be placed at the top. I dont care about the lengths of edges, as long as the rows are correct.

Arbitrarily, I've found that nodes sometimes end up in different rows. E.g., if a node has no incoming edges, it has sometimes ended up in a row after the first.

Is there any way to achieve this?

1 Answers
LOOP over root nodes ( those with no incoming edges )
   Do a Breadth First Search ( BFS ) from root
        Mark each node visited with distance from root
LOOP over nodes
   Add each node to the row where that has its depth == distance from root

If you are looking after the drawing in your own code

LOOP over rows
   Draw the node at the end of its row
LOOP over edges
   Draw edge between src and destination node

If you are using grahviz, then position the nodes using https://graphviz.org/docs/attrs/pos/

Related