summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNavya Agarwal <82928853+navyagarwal@users.noreply.github.com>2023-04-26 01:00:57 +0530
committerGitHub <noreply@github.com>2023-04-25 12:30:57 -0700
commit8982556f31c74ac58540f01ec21ebcd1507b9a6f (patch)
tree84e47e6567444be567718f9680e1459537a26bc1
parentfb67743e868be82f8fef6983f343f0e239b032b0 (diff)
downloadnetworkx-8982556f31c74ac58540f01ec21ebcd1507b9a6f.tar.gz
Fix output of is_chordal for empty graphs (#6563)
* Fix for is_chordal for empty graphs * Handle self loops case
-rw-r--r--networkx/algorithms/chordal.py10
-rw-r--r--networkx/algorithms/tests/test_chordal.py4
2 files changed, 10 insertions, 4 deletions
diff --git a/networkx/algorithms/chordal.py b/networkx/algorithms/chordal.py
index 6ff8b046..444bfbc6 100644
--- a/networkx/algorithms/chordal.py
+++ b/networkx/algorithms/chordal.py
@@ -73,6 +73,8 @@ 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
@@ -80,6 +82,8 @@ def is_chordal(G):
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
@@ -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.
@@ -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)
diff --git a/networkx/algorithms/tests/test_chordal.py b/networkx/algorithms/tests/test_chordal.py
index 132698a9..148b22f2 100644
--- a/networkx/algorithms/tests/test_chordal.py
+++ b/networkx/algorithms/tests/test_chordal.py
@@ -60,11 +60,11 @@ class TestMCS:
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)