I am trying to use arbitrary edge properties in Graphs, and I am looking for easy ways to work with them.
In many cases it would be convenient to be able to set edge properties in a single go, specifying them as a list, where each element corresponds to the respective element in EdgeList. Is this possible? Is there an easy way?
There are a few standard edge properties which apply to edges only (thus the system knows that they are not vertex properties) and which can be set this way. EdgeWeight, EdgeCapacity and EdgeCost are such properties.
pg = SetProperty[g, EdgeWeight -> {2, 3}];
PropertyValue[{pg, 1 <-> 2}, EdgeWeight]
(* 2 *)
This won't work with arbitrary properties because the system doesn't know that they refer to edges.
pg = SetProperty[g, "foo" -> {2, 3}];
Options[pg, Properties]
(* {Properties -> {"GraphProperties" -> {"foo" -> {2, 3}}}} *)
It's set as a graph property, not as an edge property.
This won't work either:
pg = SetProperty[g, "foo" -> {1 <-> 2 -> 2, 2 <-> 3 -> 3}];
Options[pg, Properties]
(* {Properties -> {"GraphProperties" -> {"foo" -> {1 <-> 2 -> 2, 2 <-> 3 -> 3}}}} *)
This will work, but it's very cumbersome and probably slow even if automated:
pg = SetProperty[{g, 1 <-> 2}, "foo" -> 2];
pg = SetProperty[{pg, 2 <-> 3}, "foo" -> 3];
Options[pg, Properties]
(* {Properties -> {2 <-> 3 -> {"foo" -> 3}, 1 <-> 2 -> {"foo" -> 2}}} *)
Then the question is: is there a better way to set arbitrary edge properties in one go for all edges? Is there a better way to get arbitrary edge properties?
Why do I think that there may be a better way?
One hint is that WeightedAdjacencyMatrix has an undocumented (but used elsewhere in the documentation) option EdgeWeights which generates the adjacency matrix based on an arbitrary edge property.
WeightedAdjacencyMatrix[pg] // Normal
(* {{0, 1, 0}, {1, 0, 1}, {0, 1, 0} *)
WeightedAdjacencyMatrix[pg, EdgeWeight -> "foo"] // Normal
(* {{0, 2, 0}, {2, 0, 3}, {0, 3, 0}} *)
So I'm hopeful that there's a way to manage edge properties more easily.
Part of this question is mapping one edge property into another. Say, I import a GML file which has an edge attribute "Capacity", and I want to easily transfer this into Mathematica's standard EdgeCapacity.
"foo", then this will destroy it, no? Trying to work around that might get messy. – Szabolcs Oct 02 '15 at 13:32Propertiesoption on the graph object. Then getting the property is best done with WeightedAdjacencyMatrix in such cases? – Szabolcs Oct 02 '15 at 15:43EdgeWeightoption inWegithedAdjacencyMatrix. Is there an equivalent forWeightedAdjacencyGraph? I.e. is there a direct way to fill theEdgeCapacity(not theEdgeWeight) from a matrix ? – Szabolcs Mar 09 '17 at 10:25Infinitybut it would sometimes be useful to be able to use0or0.. Do you think it would be useful to request these features from Wolfram Support? Is there any chance that they might get added? – Szabolcs Mar 09 '17 at 15:38VertexWeight. What about getting? Is there anything better than what I showed in the question? – Szabolcs Apr 14 '17 at 16:56WegithedAdjacencyMatrixmethod to get edge properties: it is much faster thanPropertyValue, but it only works if"foo"is set for every single edge. Is there a fast method that also works when some edges do not have the property set? – Szabolcs Apr 14 '17 at 18:17