1
$\begingroup$

Consider the edges

edges={S1040\[DirectedEdge]F283,S1197\[DirectedEdge]F243,S1197\[DirectedEdge]F245,S1863\[DirectedEdge]F243,S1863\[DirectedEdge]F245,S1863\[DirectedEdge]F283,S1863\[DirectedEdge]F244,S1863\[DirectedEdge]F246,S1863\[DirectedEdge]F247,S1863\[DirectedEdge]F280,S1863\[DirectedEdge]F281,S1863\[DirectedEdge]F282,S1863\[DirectedEdge]F284,S2174\[DirectedEdge]F243,S2174\[DirectedEdge]F280,S2174\[DirectedEdge]F281,S2174\[DirectedEdge]F284,S2325\[DirectedEdge]F247,S2340\[DirectedEdge]F245,S2344\[DirectedEdge]F282}

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

$\endgroup$
3
  • $\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$
    – user42700
    Commented Jun 1, 2019 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$ Commented Jun 1, 2019 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
    Commented Jun 2, 2019 at 14:21

3 Answers 3

1
$\begingroup$
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]]]];
TableForm[
 am[[Flatten[Position[v, #] & /@ tov], Flatten[Position[v, #] & /@ fromv]]], 
 TableHeadings -> {tov, fromv}]

enter image description here

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

This requires Szabolcs's package "IGraphM"`.

Needs["IGraphM`"]
G = Graph[edges];
TableForm[
 IGBipartiteIncidenceMatrix[G],
 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}} *)

MatrixForm[
 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.

$\endgroup$
4
  • $\begingroup$ Can you order the columns starting with F243? $\endgroup$
    – user42700
    Commented Jun 1, 2019 at 23:44
  • $\begingroup$ Yes. And you also can, e.g. with Ordering. $\endgroup$ Commented Jun 1, 2019 at 23:48
  • $\begingroup$ wonderful...thank you very much Henrik ... prg $\endgroup$
    – user42700
    Commented Jun 2, 2019 at 0:53
  • 1
    $\begingroup$ @PRG Yes, use the second argument of IGBipartiteIncidenceMatrix to indicate the specific ordering you want. $\endgroup$
    – Szabolcs
    Commented Jun 2, 2019 at 14:22
1
$\begingroup$
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

$\endgroup$
6
  • $\begingroup$ Ran into trouble with TeXForm; it was unable to build the array and then table. $\endgroup$
    – user42700
    Commented Jun 2, 2019 at 22:22
  • $\begingroup$ @PRG, it is probably related to this issue. Try using BoxForm`$UseTemplateSlotSequenceForRow = False; before you call TeXForm $\endgroup$
    – kglr
    Commented Jun 2, 2019 at 23:08
  • $\begingroup$ Still got an error: $\endgroup$
    – user42700
    Commented Jun 2, 2019 at 23:22
  • $\begingroup$ \begin{array}{c|cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc } \text{} & \text{S1016} & \text{S1031} & \text{S1047} & \text{S1064} & \text{S1067} & \text{S1084} & \text{S1113} & $\endgroup$
    – user42700
    Commented Jun 2, 2019 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
    Commented Jun 2, 2019 at 23:33

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.