Message Boards Message Boards


Label duplicate edges differently?

Posted 1 year ago
5 Replies
0 Total Likes

I have a Graph with multiple edges between two vertices. Of course, I want to label them differently. (Or, more generally, give them different properties such as weight or style).

Unfortunately, WL seems to treat multiple edges just like one edge. They get all the same labels. I cannot use EdgeLabels->{(from->to)->lbl, ...} since this doesn't distinguish between multiple edges. But even if I use Labeled[from->to,lbl] during the definition of the graph's edges, multiple edges do not get different labels. See the following labels with two edges 1->2. hey get the same label, namely the first set.

Labeled[1 \[DirectedEdge] 2, "First 1\[DirectedEdge]2"], 
1 \[DirectedEdge] 3, 
Labeled[1 \[DirectedEdge] 2, "Second 1\[DirectedEdge]2"], 
3 \[DirectedEdge] 2, 
3 \[DirectedEdge] 1}
, VertexLabels -> {1 -> "#1", 2 -> "#2", 3 -> "#3"}
, EdgeLabels -> "Name", ImageSize -> Small]

enter image description here

5 Replies

This is actively being developed and will show up in version 12.1. At the moment the same edges can not be distinguished unfortunately. @Szabolcs Horvát might know a workaround…

Note: Not a Wolfram employee but I only know from what I've seen in the live streams…

Posted 1 year ago

I will try to insert additional artificial vertices into each of those multiple edges. This will of course change some properties of the graph, but I need it for visualization and some path calculations only. So this might be a workaround.

  Labeled[1 \[DirectedEdge] 21, "First 1\[DirectedEdge]2"], 
  21 \[DirectedEdge] 2,
  1 \[DirectedEdge] 3,
  Labeled[1 \[DirectedEdge] 22, "Second 1\[DirectedEdge]2"], 
  22 \[DirectedEdge] 2,
  3 \[DirectedEdge] 2, 3 \[DirectedEdge] 1}
 , VertexLabels -> {1 -> "#1", 2 -> "#2", 3 -> "#3"}
 , EdgeLabels -> "Name", ImageSize -> Medium]

enter image description here

This is a real problem. Still not fixed in Mathematica 12.2. Renders Graph[ ] nearly useless for my application. I hope they fix it soon!

Posted 15 days ago

Hi Erik,

V12.1 added EdgeTaggedGraph. Does it address your use case? If not, can you please provide examples of what you are trying to achieve.

Thank you, Rohit. Yes, that does address my issue. For the benefit of any future readers of this discussion, I will give a basic example:

Graph[{Labeled[1 -> 1, left], Style[1 -> 2, Blue], 
  Style[2 -> 2, Gray], Labeled[1 -> 1, right], Style[1 -> 2, Green]}]

yields a self-inconsistent diagram: for the purposes of drawing the lines, the repeated edges are treated as distinct, while for the purposes of style and labeling they are treated as identical.

EdgeTaggedGraph[{Labeled[1 -> 1, left], Style[1 -> 2, Blue], 
  Style[2 -> 2, Gray], Labeled[1 -> 1, right], Style[1 -> 2, Green]}]

treats the repeated edges consistently as being distinct entities.

I am thankful that the EdgeTaggedGraph workaround is already present, but I remain troubled that Graph is internally inconsistent about whether the object is treated as a multigraph or not. It would have been more consistent to not introduce a new function (EdgeTaggedGraph) but rather to have an option to Graph, e.g. Multigraph->True or Multigraph->False or Multigraph->Automatic that would treat repeated edges in a consist way. But that's water under the bridge.

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract