Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions src/connectivity.jl
Original file line number Diff line number Diff line change
Expand Up @@ -805,17 +805,12 @@ function isgraphical(degs::AbstractVector{<:Integer})
sorted_degs = sort(degs; rev=true)
# Compute the length of the degree sequence
cur_sum = zero(UInt64)
# Compute the minimum of each degree and the corresponding index
mindeg = Vector{UInt64}(undef, n)
@inbounds for i in 1:n
mindeg[i] = min(i, sorted_degs[i])
end
# Check if the degree sequence satisfies the Erdös-Gallai condition
cum_min = sum(mindeg)
@inbounds for r in 1:(n - 1)
cur_sum += sorted_degs[r]
cum_min -= mindeg[r]
cond = cur_sum <= (r * (r - 1) + cum_min)
# Calculate the sum of the minimum of r and the degrees of the vertices
mid_deg_sum = sum([min(r, sorted_degs[i]) for i in (r + 1):n])
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doing it this way makes the algorithm run in O(n^2) time. I think we might be able to avoid this by using the fact that we only need to consider in each iteration those degrees that are smaller than r. And for the other m degrees we can just add m * r to the sum. Using the factor that the degrees are sorted we can slide from the right hand side.

And we should be able to avoid a vector for each iteration.

Do you feel up for it? Otherwise I will give it a try.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the comment, then we need a pointer to track the index. I will make a revision over the weekend, along with some benchmark results.

cond = cur_sum <= (r * (r - 1) + mid_deg_sum)
cond || return false
end
return true
Expand Down
1 change: 1 addition & 0 deletions test/connectivity.jl
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@
@test @inferred(isgraphical([2, 2, 2]))
@test @inferred(isgraphical(fill(3, 10)))
@test @inferred(isgraphical(Integer[]))
@test @inferred(!isgraphical([4, 2, 2, 2, 0]))
##@test !@inferred(isgraphical([2]))

# Test simple digraphicality
Expand Down
Loading