Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve test coverage for Steiner Tree & Docs #7348

Merged
merged 7 commits into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions networkx/algorithms/approximation/steinertree.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ def _remove_nonterminal_leaves(G, terminals):


@not_implemented_for("directed")
@nx._dispatchable(edge_attrs="weight", returns_graph=True)
@nx._dispatchable(preserve_all_attrs=True, returns_graph=True)
def steiner_tree(G, terminal_nodes, weight="weight", method=None):
r"""Return an approximation to the minimum Steiner tree of a graph.

Expand Down Expand Up @@ -164,7 +164,7 @@ def steiner_tree(G, terminal_nodes, weight="weight", method=None):
Use the edge attribute specified by this string as the edge weight.
Any edge attribute not present defaults to 1.

method : string, optional (default = 'kou')
method : string, optional (default = 'mehlhorn')
The algorithm to use to approximate the Steiner tree.
Supported options: 'kou', 'mehlhorn'.
Other inputs produce a ValueError.
Expand All @@ -175,6 +175,14 @@ def steiner_tree(G, terminal_nodes, weight="weight", method=None):
Approximation to the minimum steiner tree of `G` induced by
`terminal_nodes` .

Raises
------
NetworkXNotImplemented
If `G` is directed.

ValueError
If the specified `method` is not supported.

Notes
-----
For multigraphs, the edge between two nodes with minimum weight is the
Expand Down
16 changes: 16 additions & 0 deletions networkx/algorithms/approximation/tests/test_steinertree.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,19 @@ def test_steiner_tree_multigraph_weight_attribute(method):
H = nx.approximation.steiner_tree(G, list(G), method=method, weight="distance")
assert len(H.edges) == 2 and H.has_edge(2, 0, key=1)
assert sum(dist for *_, dist in H.edges(data="distance")) == 15


@pytest.mark.parametrize("method", (None, "mehlhorn", "kou"))
def test_steiner_tree_methods(method):
G = nx.star_graph(4)
expected = nx.Graph([(0, 1), (0, 3)])
st = nx.approximation.steiner_tree(G, [1, 3], method=method)
assert nx.utils.edges_equal(st.edges, expected.edges)


def test_steiner_tree_method_invalid():
G = nx.star_graph(4)
with pytest.raises(
ValueError, match="invalid_method is not a valid choice for an algorithm."
):
nx.approximation.steiner_tree(G, terminal_nodes=[1, 3], method="invalid_method")