Consider the edges


How can I create an adjacency matrix from this graph in table form where the rows are the Fxxx nodes and the columns are the Syyyy nodes and both rows and columns are in increasing order. For example, row 1 is F243, row 2 is F244, etc. Likewise column 1 is S1040, column 2 is S1197, etc.

enter image description here

  • $\begingroup$ Correction: I'm looking for an adjacency matrix of 0's and 1s in Table form with the row column headers described above: $\endgroup$ – PRG Jun 1 at 22:44
  • $\begingroup$ Ah, then it is good luck that nobody wasted their time with this... You should edit your question in order to address this change of aim. $\endgroup$ – Henrik Schumacher Jun 1 at 22:46
  • $\begingroup$ @HenrikSchumacher The term "incidence matrix" is used in several ways. It could indicate the edge/vertex incidence, which is what IncidenceMatrix is for. It could indicate the adjacency of nodes from the two partitions of a bipartite graph. This is what is asked here, but IncidenceMatrix doesn't do this. $\endgroup$ – Szabolcs Jun 2 at 14:21
edges2 = edges /. DirectedEdge -> UndirectedEdge;
v = VertexList[Graph[edges2]];
am = Normal[AdjacencyMatrix[Graph[edges2]]];
fromv = Sort[Union[edges2[[All, 1]]]];
tov = Sort[Union[edges2[[All, 2]]]];
 am[[Flatten[Position[v, #] & /@ tov], Flatten[Position[v, #] & /@ fromv]]], 
 TableHeadings -> {tov, fromv}]

enter image description here

  • $\begingroup$ what is "v" in the above code for TableForm? $\endgroup$ – PRG Jun 2 at 0:10
  • $\begingroup$ @PRG My bad, forgot to copy that line. Fixed it in the answer. $\endgroup$ – MelaGo Jun 2 at 0:48
  • $\begingroup$ Fantastic!! ... Many thanks MelaGo $\endgroup$ – PRG Jun 2 at 0:53

This requires Szabolcs's package "IGraphM"`.

G = Graph[edges];
 TableHeadings -> IGBipartitePartitions[G]

enter image description here

This code relies on IGBipartiteIncidenceMatrix using the same ordering and partitioning that is returned by IGBipartitePartitions.

It is possible to specify your own partitions (and ordering) in IGBipartiteIncidenceMatrix:

parts = Sort /@ IGBipartitePartitions[g, F243]
(* {{F243, F244, F245, F246, F247, F280, F281, F282, F283, F284}, 
    {S1040, S1197, S1863, S2174, S2325, S2340, S2344}} *)

 IGBipartiteIncidenceMatrix[g, parts],
 TableHeadings -> parts

enter image description here

Here we used the second argument of IGBipartitePartitions to indicate that the first partition should be the one containing F243. Then we sorted the vertices in each partition. Finally, we used the partition specification with IGBipartiteIncidenceMatrix.

  • $\begingroup$ Can you order the columns starting with F243? $\endgroup$ – PRG Jun 1 at 23:44
  • $\begingroup$ Yes. And you also can, e.g. with Ordering. $\endgroup$ – Henrik Schumacher Jun 1 at 23:48
  • $\begingroup$ wonderful...thank you very much Henrik ... prg $\endgroup$ – PRG Jun 2 at 0:53
  • 1
    $\begingroup$ @PRG Yes, use the second argument of IGBipartiteIncidenceMatrix to indicate the specific ordering you want. $\endgroup$ – Szabolcs Jun 2 at 14:22
v = Transpose[List @@@ edges];
{sources, sinks} = Union /@ v;
indices = Flatten[MapIndexed[# -> #2[[1]] &, #] & /@ {sources, sinks}];
sa = Transpose @ SparseArray[Transpose[v /. indices ] -> 1];
grid = Prepend[Join[List /@ sinks, sa, 2], Prepend[sources, ""]];
Grid[grid, Dividers -> {{2 -> True}, {2 -> True}}] 

enter image description here

  • $\begingroup$ Ran into trouble with TeXForm; it was unable to build the array and then table. $\endgroup$ – PRG Jun 2 at 22:22
  • $\begingroup$ @PRG, it is probably related to this issue. Try using BoxForm`$UseTemplateSlotSequenceForRow = False; before you call TeXForm $\endgroup$ – kglr Jun 2 at 23:08
  • $\begingroup$ Still got an error: $\endgroup$ – PRG Jun 2 at 23:22
  • $\begingroup$ \begin{array}{c|cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc } \text{} & \text{S1016} & \text{S1031} & \text{S1047} & \text{S1064} & \text{S1067} & \text{S1084} & \text{S1113} & $\endgroup$ – PRG Jun 2 at 23:22
  • $\begingroup$ @PRG, that is what TeXForm produces. I use TeXForm only to generate an expression for posing on this page (as an alternative to positing an image of the output produced by Grid[...]). I deleted that part to avoid possible confusion. $\endgroup$ – kglr Jun 2 at 23:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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