Repository: sagemath/sage

Issue:
ZeroDivisionError in graph.maximum_average_degree for the empty graph
### Is there an existing issue for this?

- [X] I have searched the existing issues for a bug report that matches the one I want to file, without success.

### Did you read the documentation and troubleshoot guide?

- [X] I have read the documentation and troubleshoot guide

### Environment

```markdown
- **OS**: macOS
- **Sage Version**: 10.0.beta9
```


### Steps To Reproduce

```py
sage: G = Graph()
sage: G.maximum_average_degree()
```

### Expected Behavior

```py
0
```


### Actual Behavior

```py
---------------------------------------------------------------------------
ZeroDivisionError                         Traceback (most recent call last)
Cell In [2], line 1
----> 1 G.maximum_average_degree()

File ~/sage/src/sage/graphs/graph.py:4706, in Graph.maximum_average_degree(self, value_only, solver, verbose)
   4696 p.solve(log=verbose)
   4698 # Paying attention to numerical error :
   4699 # The zero values could be something like 0.000000000001
   4700 # so I can not write l > 0
   (...)
   4704 # setting the minimum to 1/(10 * size of the whole graph )
   4705 # should be safe :-)
-> 4706 m = 1/(10 * Integer(g.order()))
   4707 d_val = p.get_values(d)
   4708 g_mad = g.subgraph(v for v, l in d_val.items() if l > m)

File ~/sage/src/sage/rings/integer.pyx:2034, in sage.rings.integer.Integer.__truediv__()
   2032         return y
   2033 
-> 2034     return coercion_model.bin_op(left, right, operator.truediv)
   2035 
   2036 cpdef _div_(self, right):

File ~/sage/src/sage/structure/coerce.pyx:1204, in sage.structure.coerce.CoercionModel.bin_op()
   1202     self._record_exception()
   1203 else:
-> 1204     return PyObject_CallObject(op, xy)
   1205 
   1206 if op is mul:

File ~/sage/src/sage/rings/integer.pyx:2019, in sage.rings.integer.Integer.__truediv__()
   2017 if type(left) is type(right):
   2018     if mpz_sgn((<Integer>right).value) == 0:
-> 2019         raise ZeroDivisionError("rational division by zero")
   2020     x = <Rational> Rational.__new__(Rational)
   2021     mpq_div_zz(x.value, (<Integer>left).value, (<Integer>right).value)

ZeroDivisionError: rational division by zero
```

### Additional Information

Needs to check corner cases


Discussion:

Resolving patch:
diff --git a/src/sage/graphs/graph.py b/src/sage/graphs/graph.py
--- a/src/sage/graphs/graph.py
+++ b/src/sage/graphs/graph.py
@@ -4673,9 +4673,30 @@ def maximum_average_degree(self, value_only=True, solver=None, verbose=0):
             sage: mad_g = g.maximum_average_degree(value_only=False)
             sage: g.is_isomorphic(mad_g)
             True
+
+        TESTS:
+
+        Check corner cases::
+
+            sage: Graph().maximum_average_degree(value_only=True)
+            0
+            sage: Graph().maximum_average_degree(value_only=False)
+            Graph on 0 vertices
+            sage: Graph(1).maximum_average_degree(value_only=True)
+            0
+            sage: Graph(1).maximum_average_degree(value_only=False)
+            Graph on 1 vertex
+            sage: Graph(2).maximum_average_degree(value_only=True)
+            0
+            sage: Graph(2).maximum_average_degree(value_only=False)
+            Graph on 1 vertex
         """
         self._scream_if_not_simple()
         g = self
+        if not g:
+            return ZZ.zero() if value_only else g.parent()()
+        elif not g.size():
+            return ZZ.zero() if value_only else g.parent()([[next(g.vertex_iterator())], []])
         from sage.numerical.mip import MixedIntegerLinearProgram
 
         p = MixedIntegerLinearProgram(maximization=True, solver=solver)


Test patch: