Skip to content

Commit 79a5d07

Browse files
author
Release Manager
committed
sagemathgh-40014: set correct default algorithm for the diameter of (un)weighted graphs Fixes sagemath#40013. We now ensure that the default algorithm is `'DHV'` for weighted graphs and `'iFUB'` for unweighted graphs. ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> - [x] The title is concise and informative. - [x] The description explains in detail what this PR is about. - [x] I have linked a relevant issue or discussion. - [x] I have created tests covering the changes. - [x] I have updated the documentation and checked the documentation preview. ### ⌛ Dependencies <!-- List all open PRs that this PR logically depends on. For example, --> <!-- - sagemath#12345: short description why this is a dependency --> <!-- - sagemath#34567: ... --> URL: sagemath#40014 Reported by: David Coudert Reviewer(s): Frédéric Chapoton
2 parents 24768ca + 9111558 commit 79a5d07

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

src/sage/graphs/graph.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5022,6 +5022,9 @@ def diameter(self, by_weight=False, algorithm=None, weight_function=None,
50225022
The diameter is defined to be the maximum distance between two vertices.
50235023
It is infinite if the graph is not connected.
50245024
5025+
The default algorithm is ``'iFUB'`` [CGILM2010]_ for unweighted graphs
5026+
and ``'DHV'`` [Dragan2018]_ for weighted graphs.
5027+
50255028
For more information and examples on how to use input variables, see
50265029
:meth:`~GenericGraph.shortest_paths` and
50275030
:meth:`~Graph.eccentricity`
@@ -5112,6 +5115,12 @@ def diameter(self, by_weight=False, algorithm=None, weight_function=None,
51125115
Traceback (most recent call last):
51135116
...
51145117
ValueError: algorithm 'iFUB' does not work on weighted graphs
5118+
5119+
Check that :issue:`40013` is fixed::
5120+
5121+
sage: G = graphs.PetersenGraph()
5122+
sage: G.diameter(by_weight=True)
5123+
2.0
51155124
"""
51165125
if not self.order():
51175126
raise ValueError("diameter is not defined for the empty graph")
@@ -5124,10 +5133,7 @@ def diameter(self, by_weight=False, algorithm=None, weight_function=None,
51245133
weight_function = None
51255134

51265135
if algorithm is None:
5127-
if by_weight:
5128-
algorithm = 'iFUB'
5129-
else:
5130-
algorithm = 'DHV'
5136+
algorithm = 'DHV' if by_weight else 'iFUB'
51315137
elif algorithm == 'BFS':
51325138
algorithm = 'standard'
51335139

0 commit comments

Comments
 (0)