Ticket #11693 (closed defect: fixed)

Opened 21 months ago

Last modified 12 months ago

Edges are doubled when creating Graphs with multiedges=True

Reported by: iandrus Owned by: iandrus
Priority: major Milestone: sage-5.1
Component: graph theory Keywords:
Cc: Work issues:
Report Upstream: N/A Reviewers: Nathann Cohen
Authors: Ivan Andrus Merged in: sage-5.1.beta1
Dependencies: Stopgaps:

Description

sage: Graph([[3,3]],multiedges=False)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)

/Users/gvol/<ipython console> in <module>()

/Users/gvol/vcs/cur-sage/local/lib/python2.6/site-packages/sage/graphs/graph.py in __init__(self, data, pos, loops, format, boundary, weighted, implementation, sparse, vertex_labels, **kwds)
   1137                     if multiedges is False:
   1138                         # from sage.misc.prandom import choice

-> 1139                         raise ValueError("Non-multigraph input dict has multiple edges (%s,%s)"%(u, choice([v for v in data[u] if data[u].count(v) > 1])))
   1140                     if multiedges is None: multiedges = True
   1141             if multiedges is None: multiedges = False

ValueError: Non-multigraph input dict has multiple edges (3,3)

Attachments

trac_11693-loops-not-multiedges.patch Download (1.0 KB) - added by iandrus 21 months ago.

Change History

comment:1 Changed 21 months ago by iandrus

  • Status changed from new to needs_review
  • Authors set to Ivan Andrus

Changed 21 months ago by iandrus

comment:2 Changed 21 months ago by iandrus

  • Summary changed from Loops are treated as multiedges when creating graph from list to Edges are doubled when creating Graphs with multiedges=True

The edge is being added twice which clearly causes a problem. It gets taken care of in the case that multiedges=False, or if the edges are labelled, though I'm still not sure why the edges are added twice there as well.

comment:3 follow-up: ↓ 4 Changed 21 months ago by ncohen

  • Status changed from needs_review to needs_info

What about just testing "v == u" immediately before "data[v].append(u)" ?

The problem with multiedges is that the problem is not well defined. What should be done when the input is :

  • (1, 2), (2, 1)
  • (1, 2), (1, 2)
  • (1, 2, None), (1, 2, None)
  • (1, 2, 'a'), (2, 1, 'a')
  • (1, 2, 'a'), (2, 1, 'b')

And the same with dictionaries...

Nathann

comment:4 in reply to: ↑ 3 Changed 14 months ago by iandrus

Replying to ncohen:

What about just testing "v == u" immediately before "data[v].append(u)" ?

That would solve the loop case, but then there is no way to specify only one edge in a multiedge graph. Right now if you add (1,2) as an edge there will be two edges between 1 and 2. If you add (1,2),(2,1) there will be four and so on. Try the examples from the doctest in the patch and you'll see what I mean.

The problem with multiedges is that the problem is not well defined. What should be done when the input is :

  • (1, 2), (2, 1)
  • (1, 2), (1, 2)
  • (1, 2, None), (1, 2, None)
  • (1, 2, 'a'), (2, 1, 'a')
  • (1, 2, 'a'), (2, 1, 'b')

I think these should all add two edges with the possible exceptions of those which have the same label i.e. (1, 2, None), (1, 2, None) and (1, 2, 'a'), (2, 1, 'a').

And the same with dictionaries...

This is a little more tricky, but the second should definitely add 2 edges, and I suppose the first should add 2 as well, though it's fuzzier to me.

I agree some of this is perhaps non-intuitive and I'm open to be convinced, but I think adding twice as many edges is not good. :-)

comment:5 Changed 14 months ago by ncohen

  • Status changed from needs_info to needs_review

Well, I agree that this patch should be merged, but I really think that these parameters are a mess. If I were to use it myself I think I would use add_edge instead of using the constructor as it looks mostly unreliable (surprising behaviour on some inputs) :-/

The patch is good to go, though ! :-)

Nathann

comment:6 Changed 14 months ago by ncohen

  • Status changed from needs_review to positive_review

comment:7 Changed 13 months ago by jdemeyer

  • Milestone changed from sage-5.0 to sage-5.1

comment:8 Changed 13 months ago by jdemeyer

  • Reviewers set to Nathann Cohen

comment:9 Changed 12 months ago by jdemeyer

  • Status changed from positive_review to closed
  • Resolution set to fixed
  • Merged in set to sage-5.1.beta1
Note: See TracTickets for help on using tickets.