python heap queue algorithm heapq

Excerpt from the official document: https: //docs.python.org/zh-cn/3.7/library/heapq.html

 

This module provides an implementation of the heap queue algorithm, also known as priority queuing algorithm.

Heap is a binary tree, the value of each of its parent node will only be smaller or larger than all children. It uses arrays to implement: counting from zero, for all K , has `` heap [k] <= heap [2 * k + 1] `` and . For comparison, there is no element is considered to be infinite. Heap the most interesting features is the smallest element is always at the root: .   heap[k] <= heap[2*k+2] heap[0] 

Implement this API and textbook heap algorithms are not the same, that the two aspects: (a) we use index-based scratch. This makes the index relationship between the node and its child nodes less intuitive, but since Python uses zero-based index, so this is more appropriate. (B) Our pop method returns the smallest element, but not the greatest (this is called "minimum heap" in textbooks; and "max heap" is more common in textbooks, because it is more suitable for in situ sequencing).

Based on these two aspects, the heap seen as a native Python list no surprise: represents the smallest element, while maintaining the heap invariant! heap[0]  heap.sort() 

To create a stack, you can use the list to initialized , or you can pass a function to convert to a list piles. []  heapify() 

It defines the following functions:

heapq. heappush ( heap,   item )

The item value added heap , the heap remains invariant.    

heapq. heappop ( heap )

Pop-up and return heap smallest element, maintaining the heap invariant. If the stack is empty, throw . Use , you can access only the smallest element not pop it.   IndexError  heap[0] 

heapq. heappushpop ( heap,   item )

The item into the stack, and then return to the pop heap smallest elements. The combined operating ratio called first and then call up and running more efficiently.     heappush()  heappop() 

heapq. heapify ( x )

The List X convert piles, in situ, the linear time.  

heapq. heapreplace ( heap , item)  

Pop-up and return heap smallest one while pushing into new Item . Heap of the same size. If the stack is empty initiated .    IndexError

This one-step operation than adding more efficient, and more suitably in the use of fixed-size stack. pop / push composition always return an element from the stack and replace it with Item . heappop()  heappush()  

The returned value may be higher than the added item greater. If you do not hope so, consider switching . Its push / pop will return a combination of two values is the smaller, the larger the value left in the stack.   heappushpop()

The module also provides three general functions heap-based functions.

heapq. merge ( *iterables,   key=None,   reverse=False )

将多个已排序的输入合并为一个已排序的输出(例如,合并来自多个日志文件的带时间戳的条目)。 返回已排序值的 iterator

类似于 sorted(itertools.chain(*iterables)) 但返回一个可迭代对象,不会一次性地将数据全部放入内存,并假定每个输入流都是已排序的(从小到大)。

具有两个可选参数,它们都必须指定为关键字参数。

key 指定带有单个参数的 key function,用于从每个输入元素中提取比较键。 默认值为 None (直接比较元素)。

reverse 为一个布尔值。 如果设为 True,则输入元素将按比较结果逆序进行合并。 要达成与 sorted(itertools.chain(*iterables), reverse=True) 类似的行为,所有可迭代对象必须是已从大到小排序的。

在 3.5 版更改: 添加了可选的 key  reverse 形参。

heapq. nlargest ( n,   iterable,   key=None )

 iterable 所定义的数据集中返回前 n 个最大的元素。 如果提供了 key 则其应指定一个单参数的函数,用于从 that is used to extract a comparison key from each element in iterable 的每个元素中提取比较键 (例如 key=str.lower)。 等价于: sorted(iterable, key=key, reverse=True)[:n]

heapq. nsmallest ( n,   iterable,   key=None )

 iterable 所定义的数据集中返回前 n 个最小元素组成的列表。 如果提供了 key 则其应指定一个单参数的函数,用于从 iterable 的每个元素中提取比较键 (例如 key=str.lower)。 等价于: sorted(iterable, key=key)[:n]

后两个函数在 n 值较小时性能最好。 对于更大的值,使用 sorted() 函数会更有效率。 此外,当 n==1 时,使用内置的 min()  max() 函数会更有效率。 如果需要重复使用这些函数,请考虑将可迭代对象转为真正的堆。

基本示例

堆排序 可以通过将所有值推入堆中然后每次弹出一个最小值项来实现。

>>>
>>> def heapsort(iterable):
...     h = []
...     for value in iterable:
...         heappush(h, value)
...     return [heappop(h) for i in range(len(h))]
...
>>> heapsort([1, 3, 5, 7, 9, 2, 4, 6, 8, 0])
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

这类似于 sorted(iterable),但与 sorted() 不同的是这个实现是不稳定的。

Heap elements can tuple. This applies to the comparison value (for example, the task priority) were assigned where the main track record:

>>>
>>> h = []
>>> heappush(h, (5, 'write code'))
>>> heappush(h, (7, 'release product'))
>>> heappush(h, (1, 'write spec'))
>>> heappush(h, (3, 'create tests'))
>>> heappop(h)
(1, 'write spec')

Guess you like

Origin www.cnblogs.com/xiximayou/p/12411690.html