I have an $m\times n$ matrix $A$ representing the adjacencies between the nodes of a bipartite graph. Specifically, the nodes are arranged in two partitions, of $m$ and $n$ nodes respectively, such that $A_{ij}\ne 0$ if and only if there is an edge between nodes $i$ and $j$. Moreover, if $A_{ij}\ne 0$, I would like to display the number $A_{ij}$ next to the edge connecting $i,j$. The nodes from the two partitions should have distinct colors, so I can differentiate the partitions.
What I have managed to do thus far is to display the graph, by constructing an $(m+n)\times (m+n)$ block matrix:
$$\left(\begin{array}{cc} 0 & A\\ A^{T} & 0 \end{array}\right)$$
and passing it as argument to AdjacencyGraph
. Explicitly, this is my code so far:
MatToGraph[mat_?MatrixQ] :=
ArrayFlatten[{{0, mat}, {Transpose[mat], 0}}] /. _?(# != 0 &) -> 1 //
AdjacencyGraph
ArrayFlatten
: you can replace thoseConstantArray[0, ...]
expressions with a simple0
for easier typing and better readability. $\endgroup$