6
$\begingroup$

Why does following command not work in Mathematica 11.2?

Graph[{1 \[DirectedEdge] 1, 1 \[DirectedEdge] 1, 1 \[DirectedEdge] 1},
      EdgeWeight -> {{1, 0}, {0, 1}, {1, 1}}]

From the official docs about EdgeWeight:

The weight $w_i$ can be any expression.

Removing the weights everything looks ok:

enter image description here

Update

The answer by @Carl Woll is good. If I want to display the EdgeWeights, there are also issues. Following does not work:

Block[{Identity}, 
      Graph[{1 \[DirectedEdge] 1, 1 \[DirectedEdge] 1, 1 \[DirectedEdge] 1}, 
       EdgeWeight -> Identity /@ {{1, 0}, {0, 1}, {1, 1} }, EdgeLabels -> "EdgeWeight"]]

There is already a question discussing this: Label multiple edges between same vertices

$\endgroup$
5
$\begingroup$

I think this is worth reporting to support. A workaround is to use a wrapper to prevent EdgeWeight from interpreting a list as an edge specification:

Block[{Identity},
    Graph[
        {1\[DirectedEdge]1, 1\[DirectedEdge]1, 1\[DirectedEdge]1},
        EdgeWeight -> Identity /@ {{1,0},{0,1},{1,1}}
    ]
]

enter image description here

$\endgroup$
  • $\begingroup$ Probably VectorQ is used to check the validity of the weight vector. (It doesn't consider lists of lists to be vectors.) $\endgroup$ – Szabolcs Mar 11 '18 at 16:13
  • $\begingroup$ @Carl Woll. Thanks for your solution. Could you have look at my edited question. Do you know how to solve my second issue as well? $\endgroup$ – Hotschke Mar 12 '18 at 11:01
  • $\begingroup$ @Szabolcs: I have question out of curiosity. If VectorQ is actually used (internally?), could be an easy solution to replace it by ListQ? $\endgroup$ – Hotschke Mar 12 '18 at 14:30
  • $\begingroup$ @Hotschke Please don't change the question to something different. Ask a new question instead. $\endgroup$ – Szabolcs Mar 12 '18 at 14:57
  • $\begingroup$ @Hotschke BTW this is a known limitation/bug of Mathematica: mathematica.stackexchange.com/q/92014/12 It can't handle properties in multigraphs. There is very limited support for EdgeWeight/EdgeCapacty/EdgeCost, but no other properties will work, not even EdgeLabels. I suggest you contact Wolfram Support about this. There are several post on this site describing how to work around the labelling/styling problem: mathematica.stackexchange.com/search?q=edge+label+multigraph $\endgroup$ – Szabolcs Mar 12 '18 at 14:59
2
$\begingroup$

If I want to display the EdgeWeights, there are also issues.

Using Carl Woll's answer to generate a graph with edge weights and using a variant of the accepted answer in the linked q/a:

ClearAll[displayWeightedMultiGraph]
displayWeightedMultiGraph = Module[{i = 1, j, g = #, bcurves,
  labels = PropertyValue[#, EdgeWeight], 
  gccoords = Cases[ToBoxes[#], GraphicsComplexBox[x_, y_, z___] :> x, Infinity][[1]]}, 
  bcurves = Cases[ToBoxes[g], {dir___, ar : Longest[__ArrowBox], ___} :> 
    (## & @@ Thread[{dir, {ar}}]), Infinity] /. 
  {ArrowBox[BezierCurveBox[x_, y___], z___] :> 
     Arrow[BezierCurve[x /. k_Integer :> gccoords[[k]], y], z], 
   ArrowBox[x : {__}, y_] :> Arrow[gccoords[[x]], y]}; 
   SetProperty[g, EdgeShapeFunction -> ({j = i++; Text[labels[[j]], 
     BezierFunction[#, SplineDegree -> 7][0.5]], bcurves[[j]]} &)]] &;

Examples:

g1 = Block[{Identity}, Graph[{1 -> 1, 1 -> 1, 1 -> 1}, 
      EdgeWeight -> Identity /@ {{1, 0}, {0, 1}, {1, 1} }]];
displayWeightedMultiGraph @ g1

enter image description here

g2 = Block[{Identity}, Graph[{1 <-> 2, 1 -> 2, 1 -> 2}, 
      EdgeWeight -> Identity /@ {{0, 0}, {0, 1}, {1, 0}}]];
displayWeightedMultiGraph @ g2

enter image description here

$\endgroup$
  • $\begingroup$ Thank you very much for your answer. This was certainly helpful as well. Unfortunately, I can only mark one as helpful to resolve my issues. You have +1 from me at least. Also I do not know whether a future version of Mathematica will change either the assignment or the display issue as well. Maybe the question itself becomes irrelevant. $\endgroup$ – Hotschke Mar 19 '18 at 10:36
  • $\begingroup$ Could you rewrite your code into a function display_weighted_multigraph[g] so it can be used on several occasions in a notebook? $\endgroup$ – Hotschke Mar 19 '18 at 10:41
  • $\begingroup$ And it does not work for Block[{Identity}, Graph[{1 <-> 2, 1 \[DirectedEdge] 2, 1 \[DirectedEdge] 2}, EdgeWeight -> Identity /@ {{0, 0}, {0, 1}, {1, 0}}]] (containing an undirected edge with label {0,0}). $\endgroup$ – Hotschke Mar 19 '18 at 10:53
  • $\begingroup$ @Hotschke, Thank you for the vote. Carl's post answers your main question. Re the example in your last comment i will post an update if i can find a fix. $\endgroup$ – kglr Mar 19 '18 at 11:09
  • $\begingroup$ @Hotschke, please see the updated version. $\endgroup$ – kglr Mar 19 '18 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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