2
$\begingroup$

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
$\endgroup$
2
  • 2
    $\begingroup$ Here's a trick with ArrayFlatten: you can replace those ConstantArray[0, ...] expressions with a simple 0 for easier typing and better readability. $\endgroup$
    – Szabolcs
    May 13, 2014 at 14:32
  • $\begingroup$ @Szabolcs +1 cool! Thanks! I'll edit your fix into the question. $\endgroup$
    – a06e
    May 13, 2014 at 14:41

2 Answers 2

3
$\begingroup$

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

enter image description here

$\endgroup$
0
2
$\begingroup$
matToGraph[mat_?MatrixQ, col1_, col2_, opts : OptionsPattern[]] := 
   With[{am = SparseArray[ArrayFlatten[{{0, mat}, {Transpose[mat], 0}}]], 
        dims = Dimensions[mat]}, 
   AdjacencyGraph[Unitize@am, GraphLayout -> "BipartiteEmbedding",
   VertexStyle ->Join[Thread[Range[dims[[1]]] -> col1], 
                      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"])], 
  FilterRules[{opts}, Options[AdjacencyGraph]]]]

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

enter image description here

$\endgroup$
6
  • $\begingroup$ The default options are not working. $\endgroup$
    – a06e
    May 13, 2014 at 19:33
  • $\begingroup$ @becko, are you getting an error message? $\endgroup$
    – kglr
    May 13, 2014 at 20:50
  • $\begingroup$ 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. $\endgroup$
    – a06e
    May 13, 2014 at 20:59
  • $\begingroup$ @becko, could not get the optional color args work properly; so I changed the optional color arguments to required arguments. $\endgroup$
    – kglr
    May 13, 2014 at 22:00
  • $\begingroup$ weird; maybe its a bug $\endgroup$
    – a06e
    May 13, 2014 at 22:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.