Skip to content

Commit c378d9f

Browse files
committed
Fix error in implementation of Erdos-Gallai condition in isgraphical
Fixes #400 Fix `isgraphical` function to correctly handle non-graphical sequences. # Error in current implementation `mindeg` is computed globally at the start using `min(i, sorted_degs[i])` for all indices. However, the Erdös-Gallai condition requires dynamically calculating `min(r, sorted_degs[i])` for vertices after the current index `r`. # What changed * Update the Erdös-Gallai condition check to calculate the sum of the minimum of r and the degrees of the vertices. * Add a test case in `test/connectivity.jl` to verify that `isgraphical` returns false for the sequence [4,2,2,2,0]. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/JuliaGraphs/Graphs.jl/issues/400?shareId=XXXX-XXXX-XXXX-XXXX).
1 parent 2ae1c18 commit c378d9f

File tree

2 files changed

+4
-8
lines changed

2 files changed

+4
-8
lines changed

src/connectivity.jl

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -805,17 +805,12 @@ function isgraphical(degs::AbstractVector{<:Integer})
805805
sorted_degs = sort(degs; rev=true)
806806
# Compute the length of the degree sequence
807807
cur_sum = zero(UInt64)
808-
# Compute the minimum of each degree and the corresponding index
809-
mindeg = Vector{UInt64}(undef, n)
810-
@inbounds for i in 1:n
811-
mindeg[i] = min(i, sorted_degs[i])
812-
end
813808
# Check if the degree sequence satisfies the Erdös-Gallai condition
814-
cum_min = sum(mindeg)
815809
@inbounds for r in 1:(n - 1)
816810
cur_sum += sorted_degs[r]
817-
cum_min -= mindeg[r]
818-
cond = cur_sum <= (r * (r - 1) + cum_min)
811+
# Calculate the sum of the minimum of r and the degrees of the vertices
812+
mid_deg_sum = sum([min(r, sorted_degs[i]) for i in (r + 1):n])
813+
cond = cur_sum <= (r * (r - 1) + mid_deg_sum)
819814
cond || return false
820815
end
821816
return true

test/connectivity.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,7 @@
316316
@test @inferred(isgraphical([2, 2, 2]))
317317
@test @inferred(isgraphical(fill(3, 10)))
318318
@test @inferred(isgraphical(Integer[]))
319+
@test @inferred(!isgraphical([4, 2, 2, 2, 0]))
319320
##@test !@inferred(isgraphical([2]))
320321

321322
# Test simple digraphicality

0 commit comments

Comments
 (0)