Skip to content

Commit 10c885b

Browse files
committed
Refactor 'is_chordal' (and helpers) for clarity
Short-circuit one-liners (e.g., 'A || do B') are expanded into 'if' blocks (similarly, 'if !A; do B; end'), and the cardinality closure in '_max_cardinality_vertex' is inlined as a lambda function.
1 parent 7acd67c commit 10c885b

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

src/chordality.jl

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,14 @@ function is_chordal end
5252

5353
@traitfn function is_chordal(g::AG::(!IsDirected)) where {AG<:AbstractGraph}
5454
# The `AbstractGraph` interface does not support parallel edges, so no need to check
55-
has_self_loops(g) && throw(ArgumentError("Graph must not have self-loops"))
55+
if has_self_loops(g)
56+
throw(ArgumentError("Graph must not have self-loops"))
57+
end
5658

5759
# Every graph of order `< 4` has no cycles of length `≥ 4` and thus is trivially chordal
58-
nv(g) < 4 && return true
60+
if nv(g) < 4
61+
return true
62+
end
5963

6064
unnumbered = Set(vertices(g))
6165
start_vertex = pop!(unnumbered) # The search can start from any arbitrary vertex
@@ -72,8 +76,9 @@ function is_chordal end
7276
push!(numbered, v)
7377
subsequent_neighbors = filter(in(numbered), collect(neighbors(g, v)))
7478

75-
# A complete subgraph is also called a "clique," hence the naming here
76-
_induces_clique(subsequent_neighbors, g) || return false
79+
if !_induces_clique(subsequent_neighbors, g)
80+
return false
81+
end
7782
end
7883

7984
#= A perfect elimination ordering is an "if and only if" condition for chordality, so if
@@ -84,13 +89,14 @@ end
8489
function _max_cardinality_vertex(
8590
g::AbstractGraph{T}, unnumbered::Set{T}, numbered::Set{T}
8691
) where {T}
87-
cardinality(v::T) = count(in(numbered), neighbors(g, v))
88-
return argmax(cardinality, unnumbered)
92+
return argmax(v -> count(in(numbered), neighbors(g, v)), unnumbered)
8993
end
9094

9195
function _induces_clique(vertex_subset::Vector{T}, g::AbstractGraph{T}) where {T}
9296
for (i, u) in enumerate(vertex_subset), v in Iterators.drop(vertex_subset, i)
93-
has_edge(g, u, v) || return false
97+
if !has_edge(g, u, v)
98+
return false
99+
end
94100
end
95101

96102
return true

0 commit comments

Comments
 (0)