Skip to content

Bug in PriorityQueue::remove() #28

@michalsrb

Description

@michalsrb

The PriorityQueue::remove() does not work correctly in some cases.

Here is minimal reproducer:

use std::collections::hash_map::RandomState;

use priority_queue::PriorityQueue;

fn main() {
    let mut queue = PriorityQueue::<i32, i32, RandomState>::default();

    for i in 0..7 {
        queue.push(i, i);
    }

    queue.remove(&0);

    let mut last_priority = *queue.peek().unwrap().1;
    while let Some((i, priority)) = queue.pop() {
        dbg!(priority);
        assert!(last_priority >= priority);
        last_priority = priority;
    }
}

This will pop elements in order 6, 5, 3, 4, which is clearly not correct.

I have partially debugged it and I think the map, heap and qp are updated correctly and remain consistent, but the actual algorithm for removing element from heap is not correct. It seems that the remove() function assumes that heapify() will perform up-heapify or down-heapify as needed, but it in fact only performs down-heapify. So in the case when up-heapify should have happened, the "heap" vector is no longer an actual heap.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions