I am trying to use arbitrary edge properties in Graph
s, 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
.