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

Fix output of is_chordal for empty graphs #6563

Merged
merged 2 commits into from
Apr 25, 2023
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
10 changes: 8 additions & 2 deletions networkx/algorithms/chordal.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,17 @@ def is_chordal(G):
search. It returns False when it finds that the separator for any node
is not a clique. Based on the algorithms in [1]_.

Self loops are ignored.

References
----------
.. [1] R. E. Tarjan and M. Yannakakis, Simple linear-time algorithms
to test chordality of graphs, test acyclicity of hypergraphs, and
selectively reduce acyclic hypergraphs, SIAM J. Comput., 13 (1984),
pp. 566–579.
"""
if len(G.nodes) <= 3:
return True
return len(_find_chordality_breaker(G)) == 0


Expand Down Expand Up @@ -130,6 +134,8 @@ def find_induced_nodes(G, s, t, treewidth_bound=sys.maxsize):
The algorithm is inspired by Algorithm 4 in [1]_.
A formal definition of induced node can also be found on that reference.

Self Loops are ignored

References
----------
.. [1] Learning Bounded Treewidth Bayesian Networks.
Expand Down Expand Up @@ -326,9 +332,9 @@ def _find_chordality_breaker(G, s=None, treewidth_bound=sys.maxsize):

If it does find one, it returns (u,v,w) where u,v,w are the three
nodes that together with s are involved in the cycle.

It ignores any self loops.
"""
if nx.number_of_selfloops(G) > 0:
raise nx.NetworkXError("Input graph is not chordal.")
unnumbered = set(G)
if s is None:
s = arbitrary_element(G)
Expand Down
4 changes: 2 additions & 2 deletions networkx/algorithms/tests/test_chordal.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,11 @@ def test_is_chordal(self):
assert not nx.is_chordal(self.non_chordal_G)
assert nx.is_chordal(self.chordal_G)
assert nx.is_chordal(self.connected_chordal_G)
assert nx.is_chordal(nx.Graph())
assert nx.is_chordal(nx.complete_graph(3))
assert nx.is_chordal(nx.cycle_graph(3))
assert not nx.is_chordal(nx.cycle_graph(5))
with pytest.raises(nx.NetworkXError, match="Input graph is not chordal"):
nx.is_chordal(self.self_loop_G)
assert nx.is_chordal(self.self_loop_G)

def test_induced_nodes(self):
G = nx.generators.classic.path_graph(10)
Expand Down