-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Minor touchups to the beamsearch module (#7059)
* MAINT: Rm unnecessary import. * DOC: Add note about relative performance w/ bfs_edges. * DOC: Simplify docstring example. * MAINT: Minor test refactor/parametrization. * DOC: Fixup note. * Fix typo.
- Loading branch information
Showing
2 changed files
with
31 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,24 @@ | ||
"""Unit tests for the beam search functions.""" | ||
import pytest | ||
|
||
import networkx as nx | ||
|
||
|
||
def identity(x): | ||
return x | ||
|
||
|
||
class TestBeamSearch: | ||
"""Unit tests for the beam search function.""" | ||
|
||
def test_narrow(self): | ||
"""Tests that a narrow beam width may cause an incomplete search.""" | ||
# In this search, we enqueue only the neighbor 3 at the first | ||
# step, then only the neighbor 2 at the second step. Once at | ||
# node 2, the search chooses node 3, since it has a higher value | ||
# that node 1, but node 3 has already been visited, so the | ||
# search terminates. | ||
G = nx.cycle_graph(4) | ||
edges = nx.bfs_beam_edges(G, 0, identity, width=1) | ||
assert list(edges) == [(0, 3), (3, 2)] | ||
|
||
def test_wide(self): | ||
G = nx.cycle_graph(4) | ||
edges = nx.bfs_beam_edges(G, 0, identity, width=2) | ||
assert list(edges) == [(0, 3), (0, 1), (3, 2)] | ||
|
||
def test_width_none(self): | ||
G = nx.cycle_graph(4) | ||
edges = nx.bfs_beam_edges(G, 0, identity, width=None) | ||
assert list(edges) == [(0, 3), (0, 1), (3, 2)] | ||
def test_narrow(): | ||
"""Tests that a narrow beam width may cause an incomplete search.""" | ||
# In this search, we enqueue only the neighbor 3 at the first | ||
# step, then only the neighbor 2 at the second step. Once at | ||
# node 2, the search chooses node 3, since it has a higher value | ||
# than node 1, but node 3 has already been visited, so the | ||
# search terminates. | ||
G = nx.cycle_graph(4) | ||
edges = nx.bfs_beam_edges(G, source=0, value=lambda n: n, width=1) | ||
assert list(edges) == [(0, 3), (3, 2)] | ||
|
||
|
||
@pytest.mark.parametrize("width", (2, None)) | ||
def test_wide(width): | ||
"""All nodes are searched when `width` is None or >= max degree""" | ||
G = nx.cycle_graph(4) | ||
edges = nx.bfs_beam_edges(G, source=0, value=lambda n: n, width=width) | ||
assert list(edges) == [(0, 3), (0, 1), (3, 2)] |