How do I rearrange an adjacency matrix of an acyclic digraph so its non-zero elements are above the diagonal?

388 Views Asked by At

Any graph can be represented by an adjacency matrix. The matrix for an acyclic digraph can be represented as a matrix with all its non-zero elements above the diagonal.

However, if I were to take an arbitrary, acyclic digraph I am unlikely to get the adjacency matrix in this form (unless I am lucky). I would generally need to re-label the nodes to achieve this.

My question is: given an adjacency matrix for an acyclic digraph, which algorithm rearranges it so that all the non-zero elements are above the diagonal?

1

There are 1 best solutions below

3
On

You want to sort the vertices so that whenever there is an edge $i \to j$, $i$ comes before $j$. One way to do that is to count the number of vertices reachable from each vertex, and sort in decreasing order of that. If $A$ is the original adjacency matrix ($n \times n$), then $j$ is reachable from $i$ iff $((I+A)^{k})_{ij} > 0$ where $k \ge n-1$. So compute $I+A$, square it $\lceil \log_2(n-1) \rceil$ times, and count the number of nonzero entries in each row.