Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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

2 Answers 2

up vote 2 down vote accepted

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

share|improve this answer
add comment
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

share|improve this answer
    
The default options are not working. –  becko May 13 at 19:33
    
@becko, are you getting an error message? –  kguler May 13 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. –  becko May 13 at 20:59
    
@becko, could not get the optional color args work properly; so I changed the optional color arguments to required arguments. –  kguler May 13 at 22:00
    
weird; maybe its a bug –  becko May 13 at 22:37
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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