Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error in implementation of Erdos-Gallai condition in isgraphical #415

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Syuizen
Copy link

@Syuizen Syuizen commented Jan 10, 2025

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.

Fixes JuliaGraphs#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).
Copy link

codecov bot commented Jan 15, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 97.30%. Comparing base (2ae1c18) to head (c378d9f).

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #415      +/-   ##
==========================================
- Coverage   97.30%   97.30%   -0.01%     
==========================================
  Files         117      117              
  Lines        6948     6943       -5     
==========================================
- Hits         6761     6756       -5     
  Misses        187      187              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@simonschoelly
Copy link
Member

Thank you. I think this is the second bug in this algorithm in a short while. I think we might have mixed up the indices for the summation on the right hand side of the formula.

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
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] isgraphical returns true for a non-graphical sequence
2 participants