Questions tagged [heapq]

A Python module that implements the heap queue algorithm. Use only when the question is specific to this heap implementation, as opposed to heap mechanics in general.

Module documentation: heapq

Alternative, using a different interface: queue.PriorityQueue

See also the language-agnostic tag.

90 questions
1
vote
2 answers

Python heappush vs simple append - what is the difference?

From https://www.tutorialspoint.com/heap-queue-or-heapq-in-python: heappush – This function adds an element to the heap without altering the current heap. If the current heap is not altered, why don't we use the append() list method? Is the list…
Robin Andrews
  • 3,514
  • 11
  • 43
  • 111
1
vote
4 answers

Does heapq.heappush() compare on int AND string without been specified for?

I was checking out this solution to a question on leetcode.com def topKFrequent(self, words, k): count = collections.Counter(words) heap = [(-freq, word) for word, freq in count.items()] heapq.heapify(heap) return…
Saturnian
  • 1,686
  • 6
  • 39
  • 65
1
vote
1 answer

heapq membership test and replacement

For the example from the official heapq: >>> heap = [] >>> data = [(1, 'J'), (4, 'N'), (3, 'H'), (2, 'O')] >>> for item in data: ... heappush(heap, item) ... >>> while heap: ... print(heappop(heap)[1]) J O H N I want to further implement an…
user3015347
  • 503
  • 3
  • 12
1
vote
1 answer

What types of heap elements can be used with heapq module in python?

In the documentation, it mentions it could be tuples. But could it be lists? If yes, then is the priority decided by the first element of the list by default? Because in the Priority Queue Implementation Notes in the documentation, they have used…
moon
  • 33
  • 3
1
vote
1 answer

How to find the values of a parent node in a Huffman tree using Python

I did not write this code. I am trying to draw a Huffman Tree out of this but I want the values of the parent nodes and put them in a list with its binary code. How can I achieve this? import heapq from collections import defaultdict def…
RyanPython
  • 412
  • 1
  • 5
  • 14
1
vote
2 answers

why need call _siftdown at the end of the func _siftup in python moudle heapq

I am reading the source code of heapq, and found that the _siftup func called _siftdown at last. I think it is a bit redundant. I want to to known that is this really necessary? I did a test bwtween the two condition, but I did not find any…
ppd0705
  • 11
  • 3
1
vote
1 answer

TypeError between two instances of Vertices (nodes) in Dijkstra's SPF algorithm

I am currently working on solving a train schedule optimization problem as part of my studies. In this problem, a utility function has to be maximized which increases in the number of (critical) stations visited, and decreases in the amount of…
1
vote
2 answers

How to efficiently pop all elements with the smallest key in heapq?

I am working on a simulation experiment and trying to make my code as efficient as possible. In one part, I have a min heap priority queue that I have implemented using heapq module. Throughout the simulation, I have to pop all elements with the…
Joe
  • 79
  • 3
1
vote
1 answer

Heapq in Python3 not working with tuples since it's not pop with the expectation order

Everyone says if you push tuple into heapq, it will take the first argument as the comparison factor. But it's not! I'm curious what's wrong in my code? for task_name, counter in tasks_counter.items(): heappush(tasks_q, (-int(counter),…
newBike
  • 14,385
  • 29
  • 109
  • 192
0
votes
1 answer

Sorting heapq with second element in lexicographical order is failing

I have a list of tuples: [(5, 'i love'), (2, 'island'), (5, 'iz')]. I want to perform a two-step sorting process to extract a certain number of tuples (let's say n, which is 3 in this example) from the list: Sort based on the first element: Sort…
0
votes
0 answers

Python class with multi custom compare functions used in priorityqueues

I have a class person and it has three custom compare functions (compare_by_name / age / height), and i need three priorityQueue(Or Heapq) to save objects seperately using different compare functions? What should I do. from queue import…
MeetYyj
  • 1
  • 2
0
votes
1 answer

Why is the time complexity of heapq.merge higher than that of heapq.heapify?

Merging k sorted lists containing a total of n elements using heapq.merge is supposed to have a complexity of O(n * logk). This is not directly listed in the documentation but can be inferred from the Wikipedia article mentioning the direct k-way…
Nick
  • 2,924
  • 4
  • 36
  • 43
0
votes
1 answer

Is python's heapq.heapify() faster on a list that is close to a heap?

Like the title says, I would like to know if python's heapq.heapify() will work faster on a list that is close to a heap or does it do the entire operation element by element on every list? I'm debating on how often to use heapify().
0
votes
0 answers

customized heapq behaviour erratic

Solving leetcode # 23 "Merge K Sorted Linked lists" (you can read more about it here) I've stumbled upon some baffling behaviour from heapq: I needed a more customized comparator for the list nodes to ensure ones with higher following values in the…
0
votes
0 answers

Why I only getting one neighbor in Dijkstra path?

CODE: def prueba(graph: 'nx.classes.graph.Graph', start: str, end: str): def cost(u, v): return graph.get_edge_data(u,v).get('weight') prev = {} dist = {} Q = [] pq.heapify(Q) visited = set() for v in list(nx.nodes(graph)): …
Alex
  • 3
  • 1