1

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

user42700
  • 1,593
  • 8
  • 14
  • Correction: I'm looking for an adjacency matrix of 0's and 1s in Table form with the row column headers described above: – user42700 Jun 01 '19 at 22:44
  • 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. – Henrik Schumacher Jun 01 '19 at 22:46
  • @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. – Szabolcs Jun 02 '19 at 14:21

3 Answers3

2

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.

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
Henrik Schumacher
  • 106,770
  • 7
  • 179
  • 309
1
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

MelaGo
  • 8,586
  • 1
  • 11
  • 24
1
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

kglr
  • 394,356
  • 18
  • 477
  • 896
  • Ran into trouble with TeXForm; it was unable to build the array and then table. – user42700 Jun 02 '19 at 22:22
  • @PRG, it is probably related to this issue. Try using BoxForm`$UseTemplateSlotSequenceForRow = False; before you call TeXForm – kglr Jun 02 '19 at 23:08
  • Still got an error: – user42700 Jun 02 '19 at 23:22
  • \begin{array}{c|cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc } \text{} & \text{S1016} & \text{S1031} & \text{S1047} & \text{S1064} & \text{S1067} & \text{S1084} & \text{S1113} & – user42700 Jun 02 '19 at 23:22
  • @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. – kglr Jun 02 '19 at 23:33
  • wonderful, very creative! ... many thanks ... prg – user42700 Jun 07 '19 at 12:47