1
$\begingroup$

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}]
    }]

enter image description here

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"?

$\endgroup$
2
  • $\begingroup$ 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 $\endgroup$
    – Jason B.
    Commented Dec 20, 2016 at 20:16
  • $\begingroup$ This is related, if not a duplicate. $\endgroup$
    – Jason B.
    Commented Dec 20, 2016 at 20:17

2 Answers 2

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

Mathematica graphics

WeightedAdjacencyMatrix[g2] // Normal

Mathematica graphics

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"]

Mathematica graphics

f[g, "PipeWidth"]

Mathematica graphics

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

Mathematica graphics

$\endgroup$
1
$\begingroup$

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

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

Mathematica graphics

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.