I think I have understood a particular situation as described below, but I lack the theoretical knowledge to conduct a proof and I couldn't find any source that mentions it. If my understanding is correct, I can save half the space on my adjacency matrix, if it isn't I'm likely to have pretty weird bugs. So I'd like to be sure, and I'd appreciate if someone with a more solid background could review my reasoning.
Say I represent a DAG of n vertices in an n * n adjacency matrix such that the entry i,j
is 1
if there is an edge from vertex i
to vertex j
, 0
otherwise. Because the graph is directed and acyclic, it follows that, if i,j = 1
, then j,i = 0
. If I now sort the nodes in the matrix such that the topological level of the node at in is equal to or greater than the node at in-1, then it seems to me that half of the adjacency matrix will always only contain 0
s, as it is the case in the following example:
V 1 V 2 from V 1 2 3 4 5 6 7 8 / \ / \ / \ / \ to V 1 0 0 0 0 0 0 0 0 / \ / \ 2 0 0 0 0 0 0 0 0 e1/ e2\ e3/ e4\ 3 1 0 0 0 0 0 0 0 / \ / \ 4 1 1 0 0 0 0 0 0 V 3 V 4 V 5 5 0 1 0 0 0 0 0 0 /|\ / 6 0 0 0 1 0 0 0 0 / | \ / 7 0 0 0 1 0 0 0 0 / | \ / 8 0 0 0 1 1 0 0 0 e5/ e6| e7\ e8/ / | \ / V 6 V 7 V 8
Maybe I'm just right, but is there a formal way to check this?
Let adj[i][j]
be the adjacency entry from node i
to node j
and you've sorted it such that for all nodes i < j
, node i
is higher up the topological hierarchy than node j
.
Let's assume for a moment that your assumption was incorrect: that we have a counter-example for which adj[i][j] == 1
for i > j
(i.e., a one in the upper-right half of your matrix representation). This implies that there must be a cycle containing i
and j
, since our sorting guarantees that node j
is higher up than node i
yet we have adj[i][j] == 1
implies we can "climb" up the hierarchy. This is a contradiction, since we know we have a DAG. Therefore we have proven that your assumption is correct.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With