18

Bug persisting through 12.0


Unsupported Functionality (loosely, a bug)

Mathematica (as of 11.0.1 for Mac OS X x86 (64-bit) (September 21, 2016)) does not support multi-graph where edge properties vary between two nodes that share multiple edges. Although this is not recognised officially by Wolfram with a bug reference number they could provide, it is a failure to fully provide multi-graph support.

While the following yields False the feature remains unsupported:

g1 = Graph[{1, 2, 3, 4, 5}, {1 <-> 2, 1 <-> 2, 2 <-> 3, 4 <-> 4, 
    5 <-> 1, 2 <-> 4},
   EdgeWeight -> Range[6]];
PropertyValue[{g1, #}, EdgeWeight] & /@ EdgeList[g1] == Range[6]
(*FALSE*)

Original Issue

Today I was working with a large multi-graph with properties on the edges when I discovered that properties aren't assigned as expected.

My preferred graph construction method is to specify properties before creating a graph, using the following undocumented syntax:

Graph[{vertices},{edges)]

The following minimal example shows my problem, the two edges 1<->2 have been given different values of the Property "foo" - but that's not what PropertyValue reports:

seGraph1 = 
 Graph[{1, 2, 3, 4}, {Property[1 <-> 2, "foo" -> 1], 
   Property[1 <-> 2, "foo" -> 3], Property[2 <-> 3, "foo" -> 5], 
   Property[3 <-> 4, "foo" -> 6]}]
seGraph2 = 
 Graph[{1, 2, 3, 4}, {Property[1 <-> 2, "foo" -> 30], 
   Property[1 <-> 2, "foo" -> 10], Property[2 <-> 3, "foo" -> 5], 
   Property[3 <-> 4, "foo" -> 6]}];
PropertyValue[{seGraph1, #}, "foo"] & /@ EdgeList[seGraph1]
PropertyValue[{seGraph2, #}, "foo"] & /@ EdgeList[seGraph2]
(*{3,3,5,6}*)
(*10,10,5,6}*)

Not one to stick to undocumented naughtiness, I also tried adding properties after the creation of the Graph. But the same problem:

seGraph3 = Graph[{1 <-> 2, 1 <-> 2, 2 <-> 3, 3 <-> 4}];
MapThread[(PropertyValue[{seGraph3, #1}, "foo"] = #2) &, {EdgeList[
    seGraph3], {1, 3, 5, 6}}];
PropertyValue[{seGraph3, #}, "foo"] & /@ EdgeList[seGraph3]
(*{3, 3, 5, 6}*)

Question

Is there a way to build a multigraph with different PropertyValues for identical edges?

Note: I'm on 10.1 for at least a few more days, but I'd happily have a solution that will work for 10.2

Szabolcs
  • 234,956
  • 30
  • 623
  • 1,263
Charlotte Hadley
  • 2,364
  • 19
  • 17
  • This problem has come up a number of times. I don't think it's possible to do it. Personally I think that this is glaring design error, yet another example of the problems the graph-related functionality has. – Szabolcs Aug 20 '15 at 19:19
  • @Szabolcs oh shoot! I've dealt with some problems with properties before and looked briefly for similar on mma.SE but hoped the multigraph functionality was new enough they might've made sure it worked. – Charlotte Hadley Aug 20 '15 at 19:21
  • 2
    I'm not very happy with how the graph-related stuff is being developed and about WRI's responses to complaints. There are an unusually high amount of bugs in this area, bugs tend to go unfixed for a long time, and sometimes they even claim that obvious problems are not bugs. There's also the thing when deleting edges or vertices from a weighted graph (or graph with properties) tends to corrupt the graph. – Szabolcs Aug 20 '15 at 19:25
  • @Szabolcs deleting vertices/edges! I remember that horribleness. At WTC 2014 there were some discussions about having a GraphData object but I think Charles Pooh essentially said it wasn't going to happen.. – Charlotte Hadley Aug 20 '15 at 19:34
  • 1
    Related: http://mathematica.stackexchange.com/questions/74125/color-edges-of-a-multigraph-when-there-are-parallel-edges-using-graph-and-edge – Sjoerd C. de Vries Aug 20 '15 at 21:02
  • @Szabolcs I just got 10.3 and still found this. Is it acceptable to tag this as a bug or as an unsupported feature? – Charlotte Hadley Jan 26 '16 at 10:47
  • I guess it makes sense to tag as a bug. – Szabolcs Jan 26 '16 at 11:12
  • 1
    I'm still on 10.3 and kinda scuppered by the lack of multigraph support (really whats the point in including a MultigraphQ function if you truly cannot handle multigraphs!). Can anyone tell me whether 10.4 manages any better? – Quantum_Oli May 17 '16 at 11:37
  • @Quantum_Oli I'm on 10.4.1 now and the test I wrote above still fails, so multigraph are unsupported. What are you trying to do? Could you bridge the gap with https://github.com/szhorvat/IGraphR ? – Charlotte Hadley May 17 '16 at 11:40
  • I'm optimising a route through a network where edges have a cost but also a time associated with them and therefore have multiple edges between vertices with different costs and times (and maybe other properties). I've not heard of IGraphR before but I have used @Szabolcs excellent MaTeX package extensively. Thanks for checking 10.4.1 for me, I'll look into IGraphR. Can you say briefly what it does? – Quantum_Oli May 17 '16 at 11:47
  • Hahaha! Ok, I see IGraphR is a nice interface for talking to igraph. Given I was already thinking of moving some of my MMA to python for this project (I don't know of a nice way of parsing html in MMA and I've loved the python BeautifulSoup library before), maybe I should just go full python! – Quantum_Oli May 17 '16 at 11:50
  • @Quantum_Oli maybe full python is the best way to go, I've been let down on multigraph support in Mma and moved to R (-flavoured igraph) for that myself. 'Course there is https://github.com/erocarrera/pythonika if you wanted to look into using Python alongside the Mma notebook. – Charlotte Hadley May 17 '16 at 12:56
  • @Quantum_Oli I stopped working on IGraphR (consider it replaced by IGraph/M) but as I remember it won't really help with the multigraph-can't-have-edge-properties problem because edge weights automatically read from the EdgeWeight property and transferred to igraph. The same is true for IGraph/M. – Szabolcs May 17 '16 at 13:36
  • @Quantum_Oli What you might be able to do (for path finding) is merge multi-edges into a single one with the appropriate weight. But since you're looking at train connections which also have a time (not only a ticket cost), it might not work for this. – Szabolcs May 17 '16 at 13:37
  • @Szabolcs it’s been 5 years now and Wolfram’s leaning heavily into graph with the Wolfram Physics project... is this still not fixed? – Charlotte Hadley Feb 11 '21 at 18:58
  • @CharlieJoeyHadley It's fixed now, although IMO still in a fragile way. Edges now can have "tags" which distinguish them. The problem is that it is not enforced that all edges should have tags. Thus it is very easy to accidentally create multi-edges which are not distinguishable. – Szabolcs Feb 11 '21 at 21:31
  • 1
    @CharlieJoeyHadley In your original example, change Graph to EdgeTaggedGraph, which will fix it. – Szabolcs Feb 11 '21 at 21:33

1 Answers1

5

As confirmed by @Szabolcs back in 2021 edges can now have tags which allows us to distinguish them.

Use EdgeTaggedGraph to make use of this feature.

g1 = EdgeTaggedGraph[{1, 2, 3, 4, 5}, {1 <-> 2, 1 <-> 2, 2 <-> 3, 4 <-> 4, 
    5 <-> 1, 2 <-> 4},
   EdgeWeight -> Range[6]];
PropertyValue[{g1, #}, EdgeWeight] & /@ EdgeList[g1] == Range[6]
(*TRUE*)
Charlotte Hadley
  • 2,364
  • 19
  • 17