6

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

Hotschke
  • 707
  • 3
  • 11

2 Answers2

6

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

Carl Woll
  • 130,679
  • 6
  • 243
  • 355
  • Probably VectorQ is used to check the validity of the weight vector. (It doesn't consider lists of lists to be vectors.) – Szabolcs Mar 11 '18 at 16:13
  • @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? – Hotschke Mar 12 '18 at 11:01
  • @Szabolcs: I have question out of curiosity. If VectorQ is actually used (internally?), could be an easy solution to replace it by ListQ? – Hotschke Mar 12 '18 at 14:30
  • @Hotschke Please don't change the question to something different. Ask a new question instead. – Szabolcs Mar 12 '18 at 14:57
  • @Hotschke BTW this is a known limitation/bug of Mathematica: https://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: https://mathematica.stackexchange.com/search?q=edge+label+multigraph – Szabolcs Mar 12 '18 at 14:59
  • @Szabolcs: Thanks for your response and pointing out current limitations in Mathematica. My experience with Wolfram support is that it will take at least several months (or years) until Mathematica is fixed or extended. I can understand this for open-source projects but not for a software as expensive as Mathematica. I consider the support and the lack of completeness of existing functionality a weakness of Mathematica. They add constantly so many new features, but the already existing ones are not as flexible as one would expect. – Hotschke Mar 13 '18 at 10:19
  • @Szabolcs Regarding my change/extension of the question: I think using {0,1} or a={1,0} should make no difference. Using a vector-valued function felt close enough to the current question. But I can understand that you want to keep the scope of questions as narrow as possible. I will move it to a follow up question with reference to this one. I hope this is acceptable. – Hotschke Mar 13 '18 at 10:27
  • Also the referenced issue about limitation/bug of Mathematica from 2 1/2 years ago confirms my experience that I should not get my hopes up that this will be fixed in the near future. – Hotschke Mar 13 '18 at 10:29
  • @Hotschke If no one asks for it to be fixed, it won't be fixed. It has been suggested multiple times by WRI employees that the frequency of reporting may be considered when prioritizing a bug. – Szabolcs Mar 13 '18 at 10:43
  • @Szabolcs. I certainly agree with you. However, I use right now a Home Edition of Mathematica with no subscription model. That means I have to pay for new releases. This makes it unattractive for me to report bugs because these kind of fixes will be added only to new versions not 11.2. And also the phrase 'may be considered when prioritizing a bug' sounds very motivating writing a bug report. I actually have reported something else to Wolfram support: it got fixed two versions later. That doesn't sound too bad. But when you are working on something, it is. – Hotschke Mar 13 '18 at 11:08
  • @Hotschke You are not the only person affected by these problems. – Szabolcs Mar 13 '18 at 11:13
  • @Carl Woll: When I right-click on the graphical output and want to Save Graph As..., I get only an error message: Set::write: Tag Inherited in Inherited[State] is Protected. The question 132009 seems to be related. Do you know how to resolve this? – Hotschke Apr 24 '18 at 05:42
2

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

kglr
  • 394,356
  • 18
  • 477
  • 896
  • 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. – Hotschke Mar 19 '18 at 10:36
  • Could you rewrite your code into a function display_weighted_multigraph[g] so it can be used on several occasions in a notebook? – Hotschke Mar 19 '18 at 10:41
  • 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}). – Hotschke Mar 19 '18 at 10:53
  • @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. – kglr Mar 19 '18 at 11:09
  • @Hotschke, please see the updated version. – kglr Mar 19 '18 at 14:30
  • Thank you! A small issue concerning the order is present: the undirected edge should have the label {0,0}. compare EdgeList[g] and PropertyValue[g, EdgeWeight]. I think the index should match. – Hotschke Mar 19 '18 at 14:49
  • In "11.2.0 for Microsoft Windows (64-bit) (September 11, 2017)", the code when copied into a fresh notebook and executed fails with the error "Symbol LineScaledCoordinate appears in multiple contexts". However, the second time that the notebook is run, it works fine. Moving Needs["GraphUtilities\"];before the definition ofdisplayWeightedMultiGraph` solves this problem. – bbgodfrey Mar 19 '18 at 17:24
  • @bbgodfrey, thank you. Made the change you suggested. – kglr Mar 19 '18 at 20:36
  • @Hotschke, good catch. I will update if I can find a fix. – kglr Mar 19 '18 at 20:44
  • @kglr Your last update introduced a problem: BezierFunction[#, SplineDegree -> 7] says SplineDegree is not an option for BezierFunction. – Hotschke Apr 09 '18 at 16:45
  • @kglr the old version has also a problem if I add vertex labels with VertexLabels -> "Name" – Hotschke Apr 09 '18 at 16:46
  • @kglr and hopefully finally: there are also problems with following larger graph g = Block[{Identity}, Graph[ { 1 <-> 2, 1 <-> 3, 1 <-> 4, 1 <-> 6, 2 <-> 3, 3 <-> 4, 4 <-> 5, 4 <-> 6, 5 <-> 6, 1 -> 5, 2 -> 5, 2 -> 4, 5 \[DirectedEdge] 3, 6 \[DirectedEdge] 3, 6 \[DirectedEdge] 2 }, EdgeWeight -> Identity /@ { {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 1}, {0, 1}, {0, 1}, {1, 0}, {1, 0}, {1, 0} }]]. I already try to decipher your code, but unfortunately my Mathematica knowledge is still quite limited and struggle to fit every piece of information together. – Hotschke Apr 09 '18 at 16:50
  • If you use VertexLabels -> "Name", Mathematica does not use GraphicsComplexBox anymore. Try for yourself ToBoxes[Block[{Identity}, Graph[{1 -> 1, 1 -> 1, 1 -> 1}, VertexLabels -> "Name", EdgeWeight -> Identity /@ {{1, 0}, {0, 1}, {1, 1}}]]]. This means this answer as it stands does not work anymore. – Hotschke Apr 13 '18 at 12:52