# Expressing an Edge List as a 0-1 Table with Headers

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.

• Correction: I'm looking for an adjacency matrix of 0's and 1s in Table form with the row column headers described above: – PRG Jun 1 '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 1 '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 2 '19 at 14:21

edges2 = edges /. DirectedEdge -> UndirectedEdge;
v = VertexList[Graph[edges2]];
fromv = Sort[Union[edges2[[All, 1]]]];
tov = Sort[Union[edges2[[All, 2]]]];
TableForm[
am[[Flatten[Position[v, #] & /@ tov], Flatten[Position[v, #] & /@ fromv]]],


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

This requires Szabolcs's package "IGraphM".

Needs["IGraphM"]
G = Graph[edges];
TableForm[
IGBipartiteIncidenceMatrix[G],
]


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


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.

• Can you order the columns starting with F243? – PRG Jun 1 '19 at 23:44
• Yes. And you also can, e.g. with Ordering. – Henrik Schumacher Jun 1 '19 at 23:48
• wonderful...thank you very much Henrik ... prg – PRG Jun 2 '19 at 0:53
• @PRG Yes, use the second argument of IGBipartiteIncidenceMatrix to indicate the specific ordering you want. – Szabolcs Jun 2 '19 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}}]


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