Skip to content
This repository has been archived by the owner on Jan 30, 2023. It is now read-only.

Commit

Permalink
trac #32805: unify use of weights
Browse files Browse the repository at this point in the history
  • Loading branch information
dcoudert committed Nov 10, 2021
1 parent f716a0b commit 28eec1e
Show file tree
Hide file tree
Showing 2 changed files with 207 additions and 135 deletions.
59 changes: 29 additions & 30 deletions src/sage/graphs/generic_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -4098,11 +4098,12 @@ def eulerian_circuit(self, return_vertices=False, labels=True, path=False):
return edges

def min_spanning_tree(self,
by_weight=False,
weight_function=None,
check_weight=True,
algorithm="Prim_Boost",
starting_vertex=None,
check=False,
by_weight=False):
check=False):
r"""
Return the edges of a minimum spanning tree.

Expand All @@ -4115,6 +4116,9 @@ def min_spanning_tree(self,

INPUT:

- ``by_weight`` -- boolean (default: ``False``); if ``True``, the edges
in the graph are weighted, otherwise all edges have weight 1

- ``weight_function`` -- function (default: ``None``); a function that
takes as input an edge ``(u, v, l)`` and outputs its weight. If not
``None``, ``by_weight`` is automatically set to ``True``. If ``None``
Expand All @@ -4123,8 +4127,8 @@ def min_spanning_tree(self,
be used to transform the label into a weight (note that, if the weight
returned is not convertible to a float, an error is raised)

- ``by_weight`` -- boolean (default: ``False``); if ``True``, the edges
in the graph are weighted, otherwise all edges have weight 1
- ``check_weight`` -- boolean (default: ``True``); whether to check that
the ``weight_function`` outputs a number for each edge.

- ``algorithm`` -- string (default: ``"Prim_Boost"``); the algorithm to
use in computing a minimum spanning tree of ``G``. The following
Expand Down Expand Up @@ -4296,85 +4300,80 @@ def min_spanning_tree(self,
sage: g.min_spanning_tree(algorithm="Prim_Boost")
Traceback (most recent call last):
...
ValueError: could not convert string to float:...
ValueError: the weight function cannot find the weight of...
sage: g.min_spanning_tree(algorithm="Prim_fringe")
Traceback (most recent call last):
...
ValueError: could not convert string to float:...
ValueError: the weight function cannot find the weight of...
sage: g.min_spanning_tree(algorithm="Prim_edge")
Traceback (most recent call last):
...
ValueError: could not convert string to float:...
ValueError: the weight function cannot find the weight of...
sage: g.min_spanning_tree(algorithm="Kruskal")
Traceback (most recent call last):
...
ValueError: could not convert string to float:...
ValueError: the weight function cannot find the weight of...
sage: g.min_spanning_tree(algorithm="Filter_Kruskal")
Traceback (most recent call last):
...
ValueError: could not convert string to float:...
ValueError: the weight function cannot find the weight of...
sage: g.min_spanning_tree(algorithm="Kruskal_Boost")
Traceback (most recent call last):
...
ValueError: could not convert string to float:...
ValueError: the weight function cannot find the weight of...
sage: g.min_spanning_tree(algorithm="NetworkX")
Traceback (most recent call last):
...
ValueError: could not convert string to float:...
ValueError: the weight function cannot find the weight of...
sage: g.min_spanning_tree(algorithm="Boruvka")
Traceback (most recent call last):
...
ValueError: could not convert string to float:...
ValueError: the weight function cannot find the weight of...

sage: g = Graph([(0, 1, 1), (1, 2, [1, 2, 3])], weighted=True)

sage: g.min_spanning_tree(algorithm="Prim_Boost")
Traceback (most recent call last):
...
TypeError: float() argument must be a string or a... number...
ValueError: the weight function cannot find the weight of...
sage: g.min_spanning_tree(algorithm="Prim_fringe")
Traceback (most recent call last):
...
TypeError: float() argument must be a string or a... number...
ValueError: the weight function cannot find the weight of...
sage: g.min_spanning_tree(algorithm="Prim_edge")
Traceback (most recent call last):
...
TypeError: float() argument must be a string or a... number...
ValueError: the weight function cannot find the weight of...
sage: g.min_spanning_tree(algorithm="Kruskal")
Traceback (most recent call last):
...
TypeError: float() argument must be a string or a... number...
ValueError: the weight function cannot find the weight of...
sage: g.min_spanning_tree(algorithm="Filter_Kruskal")
Traceback (most recent call last):
...
TypeError: float() argument must be a string or a... number...
ValueError: the weight function cannot find the weight of...
sage: g.min_spanning_tree(algorithm="Kruskal_Boost")
Traceback (most recent call last):
...
TypeError: float() argument must be a string or a... number...
ValueError: the weight function cannot find the weight of...
sage: g.min_spanning_tree(algorithm="NetworkX")
Traceback (most recent call last):
...
TypeError: float() argument must be a string or a... number...
ValueError: the weight function cannot find the weight of...

sage: graphs.EmptyGraph().min_spanning_tree()
[]
"""
if not self.order():
return []
if weight_function is not None:
by_weight = True

# for weighted graphs
if self.weighted():
by_weight = True

if weight_function is None and by_weight:
def weight_function(e):
return 1 if e[2] is None else e[2]

if not by_weight:
weight_function = lambda e: 1
by_weight, weight_function = self._get_weight_function(by_weight=by_weight,
weight_function=weight_function,
check_weight=check_weight)

wfunction_float = lambda e: float(weight_function(e))

Expand All @@ -4386,13 +4385,13 @@ def weight_function(e):

if algorithm == "Kruskal":
from .spanning_tree import kruskal
return kruskal(g, wfunction=wfunction_float, check=check)
return kruskal(g, weight_function=wfunction_float, check_weight=False, check=check)
if algorithm == "Filter_Kruskal":
from .spanning_tree import filter_kruskal
return filter_kruskal(g, weight_function=wfunction_float, check=check)
return filter_kruskal(g, weight_function=wfunction_float, check_weight=False, check=check)
elif algorithm == "Boruvka":
from .spanning_tree import boruvka
return boruvka(g, wfunction=wfunction_float, check=check)
return boruvka(g, weight_function=wfunction_float, check_weight=False, check=check)
else:
from sage.graphs.base.boost_graph import min_spanning_tree
return min_spanning_tree(g,
Expand Down
Loading

0 comments on commit 28eec1e

Please sign in to comment.