Possible optimizations I could think of:
It turns out that
PropertyList[{g, VertexList[g]}]
can be used as well, and is slightly faster. This form does not seem to be documented.
Another possible optimization is to check if a graph has any custom properties. If not, do no try to retrieve properties for each vertex/edge separately, and save time:
hasCustomProp[g_] := OptionValue[Options[g, Properties], Properties] =!= {}
standardVertexProperties = {
VertexCoordinates,
VertexShape, VertexShapeFunction, VertexSize, VertexStyle,
VertexLabels, VertexLabelStyle,
VertexWeight, VertexCapacity
};
ClearAll[vertexPropertyList]
vertexPropertyList[g_ /; GraphQ[g] && hasCustomProp[g]] := Union @@ PropertyList[{g, VertexList[g]}]
vertexPropertyList[g_ /; GraphQ[g]] := Intersection[PropertyList[g], standardVertexProperties]
This seems a bit risky because it relies on an explicit list of standard property names, and the assumption that PropertyList[g]
will indeed returns these in all cases. Is this true? Can anyone find a counterexample?
Yet another possible optimization is to only try to retrieve custom properties for vertices/edges that have them:
Intersection[
Keys@OptionValue[Options[g, Properties], Properties],
VertexList[g]
]
then use this in PropertyValue[{g,...}]
. The problem with this approach is that when all vertices/edges has a property, it will slow things down. It will speed things up only when some of them do not have it.
Is there anything else I can do to improve performance? Do you see any potential problems with the above approaches?