Ask Your Question

Why the function graphs() can't generate graphs?

asked 2014-09-18 18:34:45 -0500

danny gravatar image

updated 2015-01-17 15:25:54 -0500

FrédéricC gravatar image
sage: L = list(graphs(5, lambda G: max( >= 2))
sage: len(L)
sage: L = list(graphs(5, lambda G: max( < 2))
sage: len(L)
sage: L = list(graphs(5))
sage: len(L)
edit retag flag offensive close merge delete

2 answers

Sort by » oldest newest most voted

answered 2014-09-18 21:48:13 -0500

tmonteil gravatar image

updated 2014-09-18 22:08:00 -0500

The graphs with a given property (provided by the lambda function) are constructed by recurrence (see the augment parameter in the doc). The problem you are facing is that having maximal degree at least two is not preserved by removing an edge. In particular, the graph on 5 vertices with no edges does not have this property, hence no larger graph can be constructed from this one, which explains why you do not get any graph in your first construction.

A possible workaround is to actually filter among all graphs of size 5:

sage: L = [G for G in graphs(5) if max( >= 2]
sage: len(L)

Of course, it is not speed-efficient in general since you have to look at all graphs. In your case it is not a problem since almost all graphs have your property.

Another workaround is to notice that the condition max( >= 2 for H=G is equivalent to the condition min( <= 2 for H=G.complement(), and this latter condition is hereditary:

sage: M = [G.complement() for G in graphs(5, lambda G: min( <= 2)]
sage: len(M)

Of course, you will not see much speed difference in this case, but it can matter for classes of graphs with smaller density among all graphs:

sage: %timeit L = [G for G in graphs(7) if max( >= 5]
1 loops, best of 3: 11.7 s per loop
sage: %timeit M = [G.complement() for G in graphs(7, lambda G: min( <= 1)]
1 loops, best of 3: 5.84 s per loop
edit flag offensive delete link more


Great explanation - I figured it was something along these lines but was too lazy to go more in depth.

kcrisman gravatar imagekcrisman ( 2014-09-19 07:38:22 -0500 )edit

Thanks for your excellent answer, tmonteil. Another question, whether for the same reason, the function graphs.nauty_geng() also cannot generate graphs with maximum degree at least k and n vertices?

danny gravatar imagedanny ( 2014-09-19 10:26:54 -0500 )edit

See also the following question

tmonteil gravatar imagetmonteil ( 2016-05-12 11:19:08 -0500 )edit

answered 2014-09-18 21:10:47 -0500

kcrisman gravatar image

I think you need to give that function the property keyword... but also look at the documentation.

- ``property`` -- (default: ``lambda x: True``) any property to be
      tested on graphs before generation, but note that in general the
      graphs produced are not the same as those produced by using the
      property function to filter a list of graphs produced by using
      the ``lambda x: True`` default. The generation process assumes
      the property has certain characteristics set by the ``augment``
      argument, and only in the case of inherited properties such that
      all subgraphs of the relevant kind (for ``augment='edges'`` or
      ``augment='vertices'``) of a graph with the property also
      possess the property will there be no missing graphs.  (The
      ``property`` argument is ignored if ``degree_sequence`` is

That's a large caveat as to what sort of functions can be put in there, so perhaps this is also part of the problem.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2014-09-18 18:34:45 -0500

Seen: 126 times

Last updated: Sep 19 '14