Message Boards Message Boards

2
|
6117 Views
|
2 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Performance: How to retrieve all edge/vertex properties for a graph?

Posted 7 years ago

Cross posted on StackExchange.


Hello everyone,

How can I retrieve the complete list of vertex (or edge) property names for a graph? I am looking for a way that is first robust (must work on any graph), then efficient (even for large graphs), and hopefully relatively simple. It should return even those property names which are assigned only to some of the vertices (or edges).

Any suggestions?

Unfortunately, the naïve method is very slow:

In[27]:= g = ExampleData[{"NetworkGraph", "CondensedMatterCollaborations2005"}];

In[28]:= Union @@ (PropertyList[{g, #}] & /@ VertexList[g]) // AbsoluteTiming
Out[28]= {0.39492, {"Name", VertexCoordinates, VertexShape, VertexShapeFunction, VertexSize, VertexStyle}}

In[29]:= Union @@ (PropertyList[{g, #}] & /@  EdgeList[g]) // AbsoluteTiming
Out[29]= {1.11729, {EdgeShapeFunction, EdgeStyle, EdgeWeight}}
POSTED BY: Szabolcs Horvát
2 Replies

I am still looking for answers to this, and I set a bounty for it on StackExchange. If there is a faster way, I will use it in IGraph/M (but I am not getting my hopes up).

POSTED BY: Szabolcs Horvát

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?

POSTED BY: Szabolcs Horvát
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract