Skip to content

Commit 07ae1d7

Browse files
authored
Fix error in implementation of Erdos-Gallai condition in isgraphical (#415)
* Fix error in implementation of Erdos-Gallai condition in isgraphical # 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].
1 parent a6571dd commit 07ae1d7

File tree

2 files changed

+36
-10
lines changed

2 files changed

+36
-10
lines changed

src/connectivity.jl

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -798,24 +798,49 @@ function isgraphical(degs::AbstractVector{<:Integer})
798798
!isempty(degs) || return true
799799
# Check whether the sum of degrees is even
800800
iseven(sum(degs)) || return false
801-
# Check that all degrees are non negative and less than n-1
801+
# Compute the length of the degree sequence
802802
n = length(degs)
803+
# Check that all degrees are non negative and less than n-1
803804
all(0 .<= degs .<= n - 1) || return false
804805
# Sort the degree sequence in non-increasing order
805806
sorted_degs = sort(degs; rev=true)
806-
# Compute the length of the degree sequence
807+
# Initialise a sum variable
807808
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
809+
right_deg_sum = zero(UInt64)
810+
# Initalise a pointer to track the smallest index with degree greater than r
811+
ptr = n
813812
# Check if the degree sequence satisfies the Erdös-Gallai condition
814-
cum_min = sum(mindeg)
815813
@inbounds for r in 1:(n - 1)
816814
cur_sum += sorted_degs[r]
817-
cum_min -= mindeg[r]
818-
cond = cur_sum <= (r * (r - 1) + cum_min)
815+
# Calculate the sum of the minimum of r and the degrees of the vertices
816+
min_idx = r + 1
817+
while ptr >= min_idx
818+
if sorted_degs[ptr] <= r
819+
# left_deg_sum = sum_{ptr+1}^n d_i
820+
right_deg_sum += sorted_degs[ptr]
821+
# move pointer to the 1-slot left
822+
ptr -= 1
823+
else
824+
# the ptr points to the degree greater than r
825+
break
826+
end
827+
end
828+
# calculate min_deg_sum: sum_{r+1}^n min(r, d_i)
829+
if ptr < min_idx
830+
# all required degrees are less than r
831+
# ptr is min_idx - 1
832+
min_deg_sum = right_deg_sum
833+
# prepare for the next iteration
834+
# shift ptr to the right
835+
ptr += 1
836+
# reduce right_deg_sum
837+
right_deg_sum -= sorted_degs[ptr]
838+
else
839+
# d_i with i between ptr and min_idx are greater than r
840+
min_deg_sum = (ptr - r) * r + right_deg_sum
841+
end
842+
# Check the Erdös-Gallai condition
843+
cond = cur_sum <= (r * (r - 1) + min_deg_sum)
819844
cond || return false
820845
end
821846
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)