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

Optimisation: Use heapify in MutableBinaryHeap #712

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

rushabh-v
Copy link
Contributor

@rushabh-v rushabh-v commented Dec 10, 2020

Fixes: #639

Note: I was a little confused by the issue-description, so not sure if the changes I made are at the correct place.

@rushabh-v rushabh-v changed the title Optimisation: Use heapify instead of iteratively inserting elements Optimisation: Use heapify in MutableBinaryHeap Dec 10, 2020
@oxinabox
Copy link
Member

oxinabox commented Dec 10, 2020

@AquaIndigo can you review?

@oxinabox
Copy link
Member

CI fails so i suspect this is not correct

@rushabh-v
Copy link
Contributor Author

Turned out I haven't updated nodemap after calling heapify. I am wondering if we can do that efficiently (without using extra space).

Or does the issue intend to use heapify at some other place @AquaIndigo? (That was the confusion that I mentioned here)

@Indigo2233
Copy link

Turned out I haven't updated nodemap after calling heapify. I am wondering if we can do that efficiently (without using extra space).

Or does the issue intend to use heapify at some other place @AquaIndigo? (That was the confusion that I mentioned here)

I think that heapify! could help.

Comment on lines +58 to +59
vs = [4, 1, 3, 2, 16, 9, 10, 14, 8, 7]
vs2 = collect(enumerate(vs))
Copy link
Contributor Author

@rushabh-v rushabh-v Dec 11, 2020

Choose a reason for hiding this comment

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

The new implementation was overwriting vs. So, the tests were failing because of updated vs (The value of vs was not as expected because it was updated during some other test). So, I updated it to use separate vs and vs2 for each test (the values are still the same).

Copy link
Member

Choose a reason for hiding this comment

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

It is not OK for MutableBinaryMinHeap(vs) to mutate vs.
We should add a copy to the constructor if that is the case.

(we can consider later adding a MutableBinaryMinHeap! or something that does do so. but lets not worry about it in this PR)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

list_values related tests (i.e., this one) started failing after adding copy in the constructor. the reason for that is because nodemap is not updated as I mentioned here. Those tests were not failing before because vs was being mutated.

I am trying to think if there is an efficient way to update nodemap after the heapify! call.

It might be that not being able to update nodemap efficiently (with O(n) implementation) is the reason they implemented it in O(nlogn) in the first place.

h = MutableBinaryMaxHeap(vs)

@test length(h) == 10
@test !isempty(h)
@test first(h) == 16
@test isequal(list_values(h), vs)
@test isequal(heap_values(h), [16, 14, 10, 8, 7, 3, 9, 1, 4, 2])
@test isequal(heap_values(h), [16, 14, 10, 8, 7, 9, 3, 2, 4, 1])
Copy link
Contributor Author

@rushabh-v rushabh-v Dec 11, 2020

Choose a reason for hiding this comment

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

I think this is just due to the difference in the implementation of heapify and the previous method _heap_bubble_up. Otherwise, there is no problem IMO, so I updated the test. (Let me know if that's not the case)

Copy link
Member

Choose a reason for hiding this comment

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

Do these both follow the heap rule about being larger than children?
(I don't know how to read heaps when written in array form TBH)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, they both are valid heaps. I also confirmed it using this function here.

Copy link
Member

Choose a reason for hiding this comment

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

seems fine then

@rushabh-v
Copy link
Contributor Author

I ran the tests locally, it was all green.

@@ -121,6 +126,7 @@ end
end

@testset "hmin / push! / pop!" begin
vs = [4, 1, 3, 2, 16, 9, 10, 14, 8, 7]
Copy link
Member

Choose a reason for hiding this comment

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

this is a good change anyway. Testsets hould be indipendent.
Though maybe we move this down to after ss is declared?

@oxinabox
Copy link
Member

oxinabox commented Dec 29, 2020

Sorry for taking so long to review this.
I lost track of it

It may also need rebasing to get out new CI setup to work

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.

BinaryHeap constructor performs differently from heapify
3 participants