How to use queue.PriorityQueue as maxheap
Asked Answered
P

5

6

How to use queue.PriorityQueue as maxheap?

The default implementation of queue.PriorityQueue is minheap, in the documentation also there is no mention whether this can be used or not for maxheap.

Passmore answered 19/12, 2016 at 8:54 Comment(3)
try read this linkReisch
@Minji heap push is not there in the mentioned linkPassmore
One easy way would be to invert the priority. That is, if your item's priority is 2, change it to -2.Wittgenstein
R
6

PriorityQueue by default only support minheaps.

One way to implement max_heaps with it, could be,

from queue import PriorityQueue

# Max Heap
class MaxHeapElement:
    def __init__(self, x):
        self.x = x

    def __lt__(self, other):
        return self.x > other.x

    def __str__(self):
        return str(self.x)


max_heap = PriorityQueue()

max_heap.put(MaxHeapElement(10))
max_heap.put(MaxHeapElement(20))
max_heap.put(MaxHeapElement(15))
max_heap.put(MaxHeapElement(12))
max_heap.put(MaxHeapElement(27))

while not max_heap.empty():
    print(max_heap.get())
Raffo answered 10/8, 2018 at 3:48 Comment(0)
A
2

Based on the comments, the simplest way to get maxHeap is to insert negative of the element.

from queue import PriorityQueue

max_heap = PriorityQueue()

max_heap.put(MaxHeapElement(-10))
max_heap.put(MaxHeapElement(-20))
max_heap.put(MaxHeapElement(-15))
max_heap.put(MaxHeapElement(-12))
max_heap.put(MaxHeapElement(-27))

while not max_heap.empty():
    print(-1*max_heap.get())
Annis answered 24/8, 2018 at 1:44 Comment(0)
B
2

Let's say you have a list:

k = [3, 2, 6, 4, 9]

Now, let's say you want to print out the max element first (or any other element with the maximum priority).

Then the logic is to reverse the priority by multiplying it with -1, then use the PriorityQueue class object which supports the min priority queue for making it a max priority queue.

For example:

import queue

k = [3, 2, 6, 4, 9]
q = queue.PriorityQueue()
for idx in range(len(k)):
    # We are putting a tuple to queue - (priority, value)
    q.put((-1 * k[idx], idx))

# To print the max priority element, just call the get()
# get() will return tuple, so you need to extract the 2nd element
print(q.get()[1]
Bonni answered 12/6, 2020 at 16:37 Comment(0)
P
1

To adhere to the (priority, value) structure for an element in a priority queue, the wrapper class can be modified as below:

class MaxHeapElement:    
   def __init__(self, priority, value):
       self.priority = priority
       self.value = value

   def __lt__(self, other):
       return self.priority > other.priority

   def __str__(self):
       return f"{self.priority}, {self.value}"
Protractile answered 30/7, 2023 at 21:42 Comment(0)
I
0

Invert the value of the keys and use heapq. For example, turn 1000.0 into -1000.0 and 5.0 into -5.0.

from heapq import heappop, heappush, heapify

heap = []
heapify(heap)

heappush(heap, -1 * 1000)
heappush(heap, -1 * 5)
-heappop(heap) # return 1000
-heappop(heap) # return 5
Inocenciainoculable answered 4/6, 2021 at 9:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.