-
-
Notifications
You must be signed in to change notification settings - Fork 434
Optimize Havel-Hakimi Algorithm with Max-Heap for Improved Efficiency #2735
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
base: main
Are you sure you want to change the base?
Optimize Havel-Hakimi Algorithm with Max-Heap for Improved Efficiency #2735
Conversation
Optimize degree sequence realization by avoiding repeated sorting using qsort()
Added a git fetch --tags step to the workflow after the checkout process. This ensures that the build process can access version tags, preventing issues with missing or shallow clone history during version detecti
…sorting Extended Description: Replaced the repeated sorting operations with a max-heap implementation. This improves the efficiency of the Havel-Hakimi degree sequence realization by avoiding redundant sorting steps. The max-heap handles vertex selection efficiently, reducing the overall complexity and improving performance
| with: | ||
| fetch-depth: 0 | ||
|
|
||
| - name: Fetch tags |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is the reasoning for this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sir, actually I added this to fetch all tags, but later realized the repo has no tags, so it's unnecessary.
|
|
||
|
|
||
| // Choose vertices in the order of their IDs. | ||
| #include <queue> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason this is included here instead of at the top of the file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sir, actually I added it here while fixing an issue, but yeah, it makes more sense to have it at the top. I'll move it.
|
|
||
| // Choose vertices in the order of their IDs. | ||
| #include <queue> | ||
| #include <vector> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
vector is already included
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sir, my bad! It’s my first time, so I’m still learning and made this mistake. But I’ll be more careful next time and won’t repeat it. I will remove the duplicate.
| ++it, ++k) { | ||
| if (--(it->degree) < 0) { | ||
| goto fail; | ||
| std::vector<VertexDeg> remainingNodes; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The vector should either be initialized of size degree or call reserve to prevent unnecessary reallocations.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sir, thanks for pointing it out! I’m still learning and really appreciate the guidance. I will make sure to initialize the vector properly next time
|
|
||
| typedef std::list<vd_pair> vlist; | ||
| vlist vertices; | ||
| typedef std::pair<igraph_integer_t, igraph_integer_t> VertexDeg; // (degree, vertex) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is the structure an std::pair now? A type for this is already defined and was used in the line you removed. A struct with names is preferable over an std::pair. You should also use using over typedef when possible in C++.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sir, I changed it to std::pair while refactoring, but yeah, you’re right. A struct with proper names is better. I’ll fix it. Thanks for the help and guidance.
| if (vd.degree == 0) { | ||
| continue; | ||
| if (degree > maxHeap.size()) { | ||
| return IGRAPH_EINVAL; // Return error code if the sequence is invalid |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment is redundant as the code clearly says the same thing. The comment here should rather say why the sequence is invalid.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will update the comment to explain why the sequence is invalid instead of just stating the obvious.
| // Connect the current vertex to its neighbors | ||
| for (igraph_integer_t i = 0; i < degree; ++i) { | ||
| if (maxHeap.empty()) { | ||
| return IGRAPH_EINVAL; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A comment explaining why it is invalid at this step would be useful for fresh eyes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will add a clear comment explaining why it’s invalid at this step to make it easier for fresh eyes.
| } | ||
|
|
||
|
|
||
| int main(void) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are all the tests deleted?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since it is my first time, I made the mistake of deleting the tests unintentionally. I am learning from it and will be more careful moving forward.
|
This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 14 days if no further activity occurs. Thank you for your contributions. |
This PR introduces an optimization to the Havel-Hakimi degree sequence realization algorithm by replacing redundant sorting operations with a max-heap implementation.