This drawing shows a tree of parent-child relationships. It is directed, without cycles. A child can have multiple parents.

The corresponding array of arrays in Perl is:
(
[A C],
[B C],
[D F G],
[C E D],
[E J X I],
[I J]
)
The first element in each sub-array is the parent of the rest, and the number of sub-arrays is the number of nodes who have at least one child.
Problem
I want to assign a number to each node which tells which level it is on in the graph. The level should also tell whether two nodes are independent, by which I mean they are not in direct parent-child relation. The answer to this specific example should (among many other answers) be:
[A B C D E F G X I J]
[1 1 2 3 3 4 4 4 4 5]
I solution can be implemented in any language, but Perl is preferred.
Still, non of the suggested solutions seems to work for this array:
(
[ qw( Z A )],
[ qw( B D E ) ],
[ qw( A B C ) ],
[ qw( G A E )],
[ qw( L B E )]
)
as does
(
[ qw/ M A / ],
[ qw/ N A X / ],
[ qw/ A B C / ],
[ qw/ B D E / ],
[ qw/ C F G / ],
[ qw/ F G / ]
[ qw/ X C / ]
)
Finally, I think I have solved the problem of finding correct levels, using Borodin’s and ikegami’s solutions (thanks guys, highly appreiciate your efforts):
Output: