Reputation: 1999
We can obtain the incidence matrix of a digraph using the method incidence_matrix(oriented=None, sparse=True, vertices=None)
in SageMath.
Each row is a vertex, and each column is an edge. The vertices are ordered as obtained by the method
vertices()
, except when parametervertices
is given, and the edges as obtained by the methodedge_iterator()
.
My question is how to specify the vertice order and the edge order?
For example, g
is a weighted digraph with 10 edges:
edges = [('s', 'x', 3), ('s', 'y', 5),
('x', 'y', 2), ('x', 'z', 6),
('y', 'x', 1), ('y', 'z', 4), ('y', 't', 6),
('z', 't', 2),
('t', 's', 3), ('t', 'z', 7)]
g = DiGraph(edges)
I want to arrange the incidence matrix such that the vertices (from top to down) are ordered as s, x, y, z, t
and the edges are ordered (from left to right) as those in edges
(i.e., listing the edges in the alphabetic order s, x, y, z, t
).
Upvotes: 1
Views: 189
Reputation: 4402
A quick look at the code (which you can obtain by doing g.edge_iterator??
) for edge_iterator()
suggests
The iterator returned is over the edges incident with any vertex
given in the parameter "vertices".
if oriented:
for e, (i, j) in enumerate(self.edge_iterator(labels=False)):
if i != j:
m[verts[i],e] = -1
m[verts[j],e] = +1
else:
for e, (i, j) in enumerate(self.edge_iterator(labels=False)):
m[verts[i],e] += 1
m[verts[j],e] += 1
tells me that probably one would have to hack the code for this a bit to change the order.
This seems like a reasonable request to have this customizable, so I've opened Sage issue 27513 for this.
Upvotes: 2