# Plotting bipartite graph from adjacency matrix

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 //

• Here's a trick with ArrayFlatten: you can replace those ConstantArray[0, ...] expressions with a simple 0 for easier typing and better readability. May 13 '14 at 14:32
• @Szabolcs +1 cool! Thanks! I'll edit your fix into the question. May 13 '14 at 14:41

other possible way:

matToGraph[mat_, opts : OptionsPattern[]] :=
Block[{bmat = SparseArray[mat], m, n, eweight, edges},
{m, n} = Dimensions[bmat];
eweight = bmat["NonzeroValues"];
edges = UndirectedEdge[#1, #2 + m] & @@@ bmat["NonzeroPositions"];
Graph[Range[m + n], edges, EdgeWeight -> eweight, opts,
GraphLayout -> "BipartiteEmbedding",
VertexStyle -> {Red, _?(# > m &) -> Blue},
EdgeLabels ->
MapThread[#1 -> Placed[#2, {1/5, {1/2, 1/2}}] &, {edges,
eweight}], EdgeLabelStyle -> Directive[Italic, 18]]
]

testmat = RandomInteger[{0, 3}, {5, 3}];
matToGraph[testmat, VertexSize -> .5,
VertexLabels -> Placed["Name", Center],
VertexLabelStyle -> Directive[White, 18],
EdgeStyle -> Directive[Black, Thick]]


matToGraph[mat_?MatrixQ, col1_, col2_, opts : OptionsPattern[]] :=
With[{am = SparseArray[ArrayFlatten[{{0, mat}, {Transpose[mat], 0}}]],
dims = Dimensions[mat]},
Thread[Range[dims[[1]] + 1, dims[[1]] + dims[[2]]] -> col2]],
VertexSize -> Medium,
EdgeLabels -> Thread[(UndirectedEdge @@@ am["NonzeroPositions"]) ->
(Placed[Style[#, 16, Purple], {1/5, {1/2, 1/2}}] & /@ am["NonzeroValues"])],

testmat = RandomInteger[{0, 2}, {5, 3}];
matToGraph[testmat, Red, Blue,EdgeStyle -> Thick,
VertexLabels -> Placed["Name", {Center, Center}], ImageSize -> 500]


• The default options are not working. May 13 '14 at 19:33
• @becko, are you getting an error message?
– kglr
May 13 '14 at 20:50
• I'm not getting any errors. But if don't explicitly specify the colors, the nodes all show in the default gray colors. To get bipartite red and blue colors, I have to explicitly set those optional arguments. I don't know why this happens. May 13 '14 at 20:59
• @becko, could not get the optional color args work properly; so I changed the optional color arguments to required arguments.
– kglr
May 13 '14 at 22:00
• weird; maybe its a bug May 13 '14 at 22:37