@@ -7885,25 +7885,34 @@ def longest_path(self, s=None, t=None, use_edge_labels=False, algorithm="MILP",
7885
7885
argument is set to ``None`` by default, which means that no constraint
7886
7886
is set upon the first vertex in the path.
7887
7887
7888
+ This parameter can only be used when ``algorithm`` is ``"MILP"``.
7889
+
7888
7890
- ``t`` -- a vertex (default: ``None``); forces the destination of the
7889
7891
path (the method then returns the longest path ending at ``t``). The
7890
7892
argument is set to ``None`` by default, which means that no constraint
7891
7893
is set upon the last vertex in the path.
7892
7894
7895
+ This parameter can only be used when ``algorithm`` is ``"MILP"``.
7896
+
7893
7897
- ``use_edge_labels`` -- boolean (default: ``False``); whether to
7894
7898
compute a path with maximum weight where the weight of an edge is
7895
7899
defined by its label (a label set to ``None`` or ``{}`` being
7896
7900
considered as a weight of `1`), or to compute a path with the longest
7897
7901
possible number of edges (i.e., edge weights are set to 1)
7898
7902
7903
+ This parameter can only be used when ``algorithm`` is ``"MILP"``.
7904
+
7899
7905
- ``algorithm`` -- string (default: ``"MILP"``); the algorithm to use
7900
- among ``"MILP"`` and ``"backtrack"``. Two remarks on this respect :
7906
+ among ``"MILP"``, ``"backtrack"`` and ``"heuristic"`` :
7901
7907
7902
- * While the MILP formulation returns an exact answer, the backtrack
7903
- algorithm is a randomized heuristic.
7908
+ * ``"MILP"`` returns an exact answer.
7904
7909
7905
- * As the backtrack algorithm does not support edge weighting, setting
7906
- ``use_edge_labels=True`` will force the use of the MILP algorithm.
7910
+ * ``"backtrack"`` is renamed ``"heuristic"`` (:issue:`36574`).
7911
+
7912
+ * ``"heuristic"`` is a randomized heuristic for finding a long path in
7913
+ an unweighted (di)graph. This heuristic does not take into account
7914
+ parameters ``s``, ``t`` and ``use_edge_labels``. An error is raised
7915
+ if these parameters are set.
7907
7916
7908
7917
- ``solver`` -- string (default: ``None``); specify a Mixed Integer
7909
7918
Linear Programming (MILP) solver to be used. If set to ``None``, the
@@ -7948,7 +7957,7 @@ def longest_path(self, s=None, t=None, use_edge_labels=False, algorithm="MILP",
7948
7957
The heuristic totally agrees::
7949
7958
7950
7959
sage: g = graphs.PetersenGraph()
7951
- sage: p = g.longest_path(algorithm="backtrack ").edges(sort=True, labels=False)
7960
+ sage: p = g.longest_path(algorithm="heuristic ").edges(sort=True, labels=False)
7952
7961
sage: len(p)
7953
7962
9
7954
7963
@@ -7970,13 +7979,13 @@ def longest_path(self, s=None, t=None, use_edge_labels=False, algorithm="MILP",
7970
7979
7971
7980
TESTS:
7972
7981
7973
- The argument ``algorithm`` must be either ``'backtrack'`` or
7974
- ``'MILP'``::
7982
+ The argument ``algorithm`` must be either ``'backtrack'``,
7983
+ ``'heuristic'`` or ``' MILP'``::
7975
7984
7976
7985
sage: graphs.PetersenGraph().longest_path(algorithm="abc")
7977
7986
Traceback (most recent call last):
7978
7987
...
7979
- ValueError: algorithm must be either 'backtrack' or 'MILP'
7988
+ ValueError: algorithm must be either 'backtrack', 'heuristic' or 'MILP'
7980
7989
7981
7990
Disconnected graphs not weighted::
7982
7991
@@ -8049,13 +8058,43 @@ def longest_path(self, s=None, t=None, use_edge_labels=False, algorithm="MILP",
8049
8058
sage: H = {(0, 3), (2, 0), (3, 4)}
8050
8059
sage: H == {x for x in G.longest_path().edge_iterator(labels=False)} # needs sage.numerical.mip
8051
8060
True
8061
+
8062
+ :issue:`36574`::
8063
+
8064
+ sage: G = graphs.PathGraph(3)
8065
+ sage: P = G.longest_path(algorithm='backtrack')
8066
+ doctest:...: DeprecationWarning: algorithm 'backtrack' is deprecated.
8067
+ Use algorithm 'heuristic' instead.
8068
+ See https://github.com/sagemath/sage/issues/36574 for details.
8069
+ sage: G.longest_path(algorithm='heuristic', s=0)
8070
+ Traceback (most recent call last):
8071
+ ...
8072
+ ValueError: parameters s, t, and use_edge_labels can not be used in
8073
+ combination with algorithm 'heuristic'
8074
+ sage: G.longest_path(algorithm='heuristic', t=2)
8075
+ Traceback (most recent call last):
8076
+ ...
8077
+ ValueError: parameters s, t, and use_edge_labels can not be used in
8078
+ combination with algorithm 'heuristic'
8079
+ sage: G.longest_path(algorithm='heuristic', use_edge_labels=True)
8080
+ Traceback (most recent call last):
8081
+ ...
8082
+ ValueError: parameters s, t, and use_edge_labels can not be used in
8083
+ combination with algorithm 'heuristic'
8052
8084
"""
8053
8085
self._scream_if_not_simple()
8054
8086
8055
- if use_edge_labels:
8056
- algorithm = "MILP"
8057
- if algorithm not in ("backtrack", "MILP"):
8058
- raise ValueError("algorithm must be either 'backtrack' or 'MILP'")
8087
+ if algorithm not in ("backtrack", "heuristic", "MILP"):
8088
+ raise ValueError("algorithm must be either 'backtrack', 'heuristic' or 'MILP'")
8089
+ if algorithm == "backtrack":
8090
+ from sage.misc.superseded import deprecation
8091
+ deprecation(36574, "algorithm 'backtrack' is deprecated. "
8092
+ "Use algorithm 'heuristic' instead.")
8093
+ algorithm = 'heuristic'
8094
+ if algorithm == 'heuristic':
8095
+ if s is not None or t is not None or use_edge_labels:
8096
+ raise ValueError("parameters s, t, and use_edge_labels can not "
8097
+ "be used in combination with algorithm 'heuristic'")
8059
8098
8060
8099
# Quick improvement
8061
8100
if not self.is_connected():
@@ -8100,8 +8139,8 @@ def longest_path(self, s=None, t=None, use_edge_labels=False, algorithm="MILP",
8100
8139
from sage.graphs.graph import Graph
8101
8140
return [0, Graph()] if use_edge_labels else Graph()
8102
8141
8103
- # Calling the backtrack heuristic if asked
8104
- if algorithm == "backtrack ":
8142
+ # Calling the heuristic if asked
8143
+ if algorithm == "heuristic ":
8105
8144
from sage.graphs.generic_graph_pyx import find_hamiltonian as fh
8106
8145
x = fh(self, find_path=True)[1]
8107
8146
return self.subgraph(vertices=x, edges=list(zip(x[:-1], x[1:])))
0 commit comments