# Label graphs based on custom properties

Is it possible to make (Weighted)AdjacencyMatrix from a graph whose elements have custom properties?

Consider this graph:

g = Graph[{
Property[DirectedEdge[0, 1], {"WaterFlow" -> x, "PipeWidth" -> 10.0}],
Property[DirectedEdge[1, 2], {"WaterFlow" -> x + y, "PipeWidth" -> 8.5}],
Property[DirectedEdge[2, 1], {"WaterFlow" -> y, "PipeWidth" -> 8.5}],
Property[DirectedEdge[2, 3], {"WaterFlow" -> x, "PipeWidth" -> 10.0}]
}]


How do I set the option EdgeLabels for Graph so that all the edges are labeled according to a given custom property, say "WaterFlow", or "PipeWidth"?

• I know this is a hack, but the support for custom graph properties could be better. If you define edges to be your list, then Graph[edges /. Rule["WaterFlow", x_] :> Sequence[Rule["WaterFlow", x], Rule[EdgeLabels, x]]] works Commented Dec 20, 2016 at 20:16
• This is related, if not a duplicate. Commented Dec 20, 2016 at 20:17

g2 = SetProperty[g,
{VertexShapeFunction -> "Name",
EdgeWeight -> (# -> PropertyValue[{g, #}, "WaterFlow"] & /@ EdgeList[g]),
EdgeLabels -> "EdgeWeight"}]


WeightedAdjacencyMatrix[g2] // Normal


More generally, define a function to identify the EdgeWeights with a property:

ClearAll[f]
f = With[{g = #, prop = #2},
SetProperty[g, {VertexShapeFunction -> "Name",
EdgeWeight -> (# -> PropertyValue[{g, #}, prop] & /@ EdgeList[g]),
EdgeLabels -> "EdgeWeight"}]] &;

f[g, "WaterFlow"]


f[g, "PipeWidth"]


WeightedAdjacencyMatrix[f[g, "PipeWidth"]] // Normal


As the coming function of IGEdgeMap and IGEdgeProp in prerelease version IGraph/M, the life become easier

IGEdgeMap[# &, EdgeLabels -> IGEdgeProp[#], g] & /@ {"WaterFlow", "PipeWidth"}