How do you use a binary heap to implement a priority queue?

Vilx-

Despite the fact that I've got a BS in Computer Science (and this was covered in the university), I've never been able to understand the relationship between binary heaps and priority queues. It just... doesn't click. I completely understand what a binary heap is and I know how to implement one in an array. I also know what a priority queue is. But how do the two of them fit together?

A quick google query shows a lot of articles like this one. Which kind of explains it, yet I'm left more questions:

  1. A priority queue needs to ensure that if two items with the same priority are added, then they will be removed in the same order that they were added. How does a binary heap ensure this? (In fact, if I'm not mistaken, then the border case where ALL the items have the same priority would produce a heap which would violate this rule).
  2. What happens when you remove the root from the heap, then put in the last element in place of the root, and then need to swap it with one of the children - but both children have the same value. How do you pick which one to swap with? (Holding in mind the FIFO rule of same-priority items)

What am I missing here?

templatetypedef

A priority queue is an abstract data type like "stack" or "associative array." There can are many different ways to implement a priority queue - you can use binary heaps, binomial heaps, Fibonacci heaps, pairing heaps, etc.

I don't believe that there is any intrinsic requirement that a priority queue be "stable" and require that elements with equal priorities be dequeued in the same relative order in which they were added, much in the same way that there's no instrinsic requirement that a sorting algorithm be stable (though many are). It's a nice property to have, but usually it's not guaranteed. This is why, for example, a standard heapsort isn't a stable sort.

Fortunately, it's not too hard to adjust binary heaps to be stable. Keep a counter associated with the binary heap. Whenever you add an element to the heap, tag it with the current value of the counter and increment the counter. When doing heap operations, use the counter as a tiebreaker to determine which value is smaller if two values compare equal. Essentially, this changes the comparison to a lexicographical comparison first on the real value and then on the insertion order.

Hope this helps!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do you use a binary heap to implement a priority queue?

From Dev

Advantages of a Binary Heap for a Priority Queue?

From Java

How do you implement a Stack and a Queue in JavaScript?

From Dev

How to implement regular queue using priority queue?

From Dev

How do you order objects in a priority_queue in C++?

From Dev

Use a linked list to implement a Priority Queue

From Dev

Priority queue and heap

From Dev

How do I hide the heap methods from the user when writing a priority queue?

From Dev

How to implement a priority queue using two queues

From Dev

How to implement a stack using a priority queue?

From Dev

How to implement a priority queue using two queues

From Dev

How to implement priority queue with unordered linkedlist

From Dev

How to implement a priority queue using SQS(Amazon simple queue service)

From Dev

How do I implement a priority queue with explicit links (using a triply linked datastructure)?

From Dev

How do I implement in Java a priority queue of a Tuple that sorts using both fields with different types?

From Dev

Difference between priority queue and a heap

From Dev

Implementing a priority queue with a min heap

From Dev

Finding the Minimum in a Priority Queue (heap)

From Dev

How to use the priority queue STL for objects?

From Java

Is this logic possible? (concerns: binary min heap, priority queue, insert at index 0 but root is at index 1)

From Dev

How to implement a priority queue using the field of a class in java

From Dev

Implement a priority queue using stack

From Dev

Implement a priority queue using stack

From Dev

how to set priority in priority queue

From Dev

Java Min Heap Priority Queue Implementation

From Dev

Allocating data for the priority_queue on heap

From Dev

Templated Priority Queue inheriting from templated Heap

From Dev

Python Heap Priority Queue sift_up()

From Dev

Creating a STL min heap priority queue?

Related Related

  1. 1

    How do you use a binary heap to implement a priority queue?

  2. 2

    Advantages of a Binary Heap for a Priority Queue?

  3. 3

    How do you implement a Stack and a Queue in JavaScript?

  4. 4

    How to implement regular queue using priority queue?

  5. 5

    How do you order objects in a priority_queue in C++?

  6. 6

    Use a linked list to implement a Priority Queue

  7. 7

    Priority queue and heap

  8. 8

    How do I hide the heap methods from the user when writing a priority queue?

  9. 9

    How to implement a priority queue using two queues

  10. 10

    How to implement a stack using a priority queue?

  11. 11

    How to implement a priority queue using two queues

  12. 12

    How to implement priority queue with unordered linkedlist

  13. 13

    How to implement a priority queue using SQS(Amazon simple queue service)

  14. 14

    How do I implement a priority queue with explicit links (using a triply linked datastructure)?

  15. 15

    How do I implement in Java a priority queue of a Tuple that sorts using both fields with different types?

  16. 16

    Difference between priority queue and a heap

  17. 17

    Implementing a priority queue with a min heap

  18. 18

    Finding the Minimum in a Priority Queue (heap)

  19. 19

    How to use the priority queue STL for objects?

  20. 20

    Is this logic possible? (concerns: binary min heap, priority queue, insert at index 0 but root is at index 1)

  21. 21

    How to implement a priority queue using the field of a class in java

  22. 22

    Implement a priority queue using stack

  23. 23

    Implement a priority queue using stack

  24. 24

    how to set priority in priority queue

  25. 25

    Java Min Heap Priority Queue Implementation

  26. 26

    Allocating data for the priority_queue on heap

  27. 27

    Templated Priority Queue inheriting from templated Heap

  28. 28

    Python Heap Priority Queue sift_up()

  29. 29

    Creating a STL min heap priority queue?

HotTag

Archive