-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
[BUG]: method pop
in PrimMST.js not working properly
#1298
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
Comments
I am working on correcting the bug, refactoring the method |
The priority queue should not be a part of Please have a look - it should be possible to merge the PQ used & implemented in |
I couldn't merge the two implementations because the PQ we use has keys in addition to the priority value to be able to track the nodes of the graph, which none of the already existing implementations have. |
* ref: KeyPriorityQueue in separate file #1298 * feat: add tests for KeyPriorityQueue #1298 * fix: _shiftDown refactored and corrected #1298 * fix: use KeyPriorityQueue in PrimMST #1298 * feat: add test for PrimMST #1298 * fix: format files #1298 * fix: minor coding style changes * fix: use map for keys and priorities #1298
Description
In
Graphs/PrimMST.js
, the method_shiftDown
of classPriorityQueue
does not give the correct output when a node must be shifted to a left-child leaf without a right-child "brother", which results in a wrong behavior of methodpop
.Expected Behavior
An example of a tree that produces the bug is given by the following array of nodes denoted by their priority:
queue = [0, 1, 2]
ie
If we call method
pop
on this queue, we expect to get the new tree:queue = [1, 2]
ie
Actual Behavior
In the case described above, we will get as output a tree represented by:
queue = [2, 1]
ie
This violates the properties of the heap (we have 2 > 1 but a parent node should never have a bigger value than any of its children).
Steps to reproduce (if applicable)
export { PriorityQueue }
PrimMST.test.js
containing the following codenpm test -- PrimMST.test.js
Additional information
This is due to the fact that in
_shiftDown
, the children's priorities are computed under a singletry {} catch {}
statement, thus the priority of the left child is set toInfinity
if the right child does not exist.The text was updated successfully, but these errors were encountered: