How to assign EdgeWeight to a multigraph?

I'm trying to make a program to illustrate the simplify process of a complex resistence network. I create a graph to represent the structure of the network and trying to use EdgeWeight to symbolize the value of resistence. but sometimes I'll need to deal with parallel resistence, so assigning EdgeWeight to them will be a problem.

In simple graphs, one can use:

gg=Graph[{1<->2,2<->3,3<->1},EdgeWeight->{1,2,3}]

to specify the weight of each edge. Then we can use PropertyValue to extract them:

PropertyValue[{gg,#},EdgeWeight]&/@EdgeList@gg

{1,2,3}

But in a non-simple graph:

gg=Graph[{1<->2,1<->2,2<->3},EdgeWeight->{1,2,3}]

It seems that EdgeWeight simply ignored the weight assignment for parallel edges and consider all parallel edges to be the same:

PropertyValue[{gg, #}, EdgeWeight] & /@ EdgeList@gg

{1,1,3}

This put a great barrier on my programming, so how to solve this problem? How to allow multiple EdgeWeight assigned to parallel edges?

• This is a duplicate of mathematica.stackexchange.com/questions/92014/… Essentially, this can't possibly work properly with the current design of Graph. I would consider this a bug. If it is a problem for you, it would be good if you could report it to Wolfram Support. People have suggested it several times that the number of times an issue is reported is taken into account when Wolfram prioritizes it. So please report it, even if Wolfram is aware of the problem. – Szabolcs Aug 18 '16 at 13:56
• @Szabolcs thanks, your comment prevent me from carrying my project on in time. :( not happy about that, but at least it ensures I won't make more frutile tries. – Wjx Aug 18 '16 at 14:06
• I'm not sure what you want specifically, but can't you use symbolic Edgeweights and have replace functions when using the weights? – Feyre Aug 18 '16 at 14:55
• @Feyre no, try the examples and you shall see. ;) – Wjx Aug 18 '16 at 15:06
• @Wjx I think you misunderstand what I mean. I mean to say that you let the EdgeWeight be, say, r1, and when you call the Edgeweights, depending on how it is called let r1 be replaced by either 1 or 2. – Feyre Aug 18 '16 at 15:09

Mathematica's can't properly handle edge properties for multigraphs. The reason is that edges are typically referred to by their end vertices. Therefore there is no way to distinguish between multiple edges connecting the same vertices.

EdgeWeight, EdgeCost, and EdgeCapacity are the only edge properties which can even be stored for a multigraph. These properties are stored as simple lists. The kth element of the list corresponds to the kth edge.

Thus you can work with weighted multigraphs, but you must take care to only set or extract weights in a particular way.

You can extract weights as a vector like this:

wg = Graph[{1 <-> 2, 1 <-> 2, 1 <-> 3}, EdgeWeight -> {3, 4, 5}]

PropertyValue[wg, EdgeWeight]
(* {3, 4, 5} *)

Once these weights were set, you cannot easily change them. SetProperty[wg, EdgeWeight -> ...] or Graph[wg, EdgeWeight -> ...] will produce incorrect results.

You can do this instead:

Graph[VertexList[wg], EdgeList[wg], EdgeWeight -> newWeights]

Whether it's worth the trouble, I do not know. The risk of bumping into bugs is quite high.