7:50 AM
@b3m2a1 The InputForm
of Graph
in actually not the fastest way to extract information from a graph.
There's also the problem that there are many different internal representations, and I am not sure that I know all of them.
You can query the representation type using GraphComputation`GraphRepresentation
. Then it's easier to make sense of what you see.
I try to mess with these direct forms as little as possible in IGraph/M because I don't trust them to be stable and I do not know if I missed a few of them (e.g. I wasn't aware of the sparse representation for a while)
That wasn't expected, was it? To get the IncidenceMatrix and process the sparse array? Well, the graph stuff it's a mess ...
This of course ignores properties, but those come with another set of problems ...
Anyway, since you are using MathLink for communication (not LibraryLink), there's a performance limitation anyway. Here's what I know about representations:
The general form is Graph[vertexList, representation, listOfOptions]
. THat listOfOptions is actually a List
If the graph has no vertices, the GraphRepresentation is always NullGraph, and representaton
is {}
A common and fast representation is Simple
, where representation
is {directed, undirected}
. Each of directed
and undirected
is a pattern sparse array describing the directed and undirected edges in a graph (that can be a mixed graph)
This representation doesn't allow self-loops or multi-edges. Are you familiar with pattern arrays? They're a sparse array that only stores positions, not values.
For multi-edges we'd need values.
So the Sparse
representation does that. It's like Simple
but uses normal sparse arrays (not pattern) to describe the directed and undirected adjacency matrices.
Then there's the Incidence
representation that works as above, but uses edge lists based on vertex indices (not vertex names). Example: Graph[{a, b}, {{{1, 2}, {2, 1}}, {{1, 2}}}]
is the same as Graph[{a -> b, b -> a, a <-> b}]
These are the only forms I know about that appear when sending through MathLink.
But there are other forms that may appear as "input forms", including the usual form you'd write to create a graph (with DirectedEdge and UndirectedEdge)
and something that looks like Graph[{a,b}, {{1,2}}, DirectedEdges -> True]
to describe the a->b
graph.