heapq of python
heap
https://stackoverflow.com/questions/19979518/what-is-pythons-heapq-module
堆不是二叉樹。
堆以list方式儲存。
堆不同於sorted list。
堆在插入和刪除比sorted list更加高效。
搜尋還是sorted list高效。
Quoting Wikipedia:
Heaps are commonly implemented with an array. Any binary tree can be stored in an array, but because a binary heap is always a complete binary tree, it can be stored compactly. No space is required for pointers; instead, the parent and children of each node can be found by arithmetic on array indices.
This image below should help you to feel the difference between tree and list representation of the heap and (note, that this is a max heap, which is the inverse of the usual min-heap!):
In general, heap data structure is different from a sorted list in that it sacrifices some information about whether any particular element is bigger or smaller than any other. Heap only can tell, that this particular element is less, than it's parent and bigger, than it's children. The less information a data structure stores, the less time/memory it takes to modify it. Compare the complexity of some operations between a heap and a sorted array:
Heap Sorted array Average Worst case Average Worst case Space O(n) O(n) O(n) O(n) Search O(n) O(n) O(log n) O(log n) Insert O(1) O(log n) O(n) O(n) Delete O(log n) O(log n) O(n) O(n)
heapq
https://docs.python.org/3.5/library/heapq.html#
此模組實現的優先佇列演算法。
即在一個排隊佇列中, 不是按照先來先服務的順序,
而是按照節點在佇列中的權重,動態叫號。
潛臺詞是, 佇列中的節點,是動態改變的, 就是在任意時間:
(1)已排隊節點,可以退出排隊。
(2)新來節點,可以理解加入排隊。
對於順序列表, 其實實現動態效能,要時間代價太大。
This module provides an implementation of the heap queue algorithm, also known as the priority queue algorithm.
Heaps are binary trees for which every parent node has a value less than or equal to any of its children. This implementation uses arrays for which
heap[k] <= heap[2*k+1]
andheap[k] <= heap[2*k+2]
for all k, counting elements from zero. For the sake of comparison, non-existing elements are considered to be infinite. The interesting property of a heap is that its smallest element is always the root,heap[0]
.
>>> 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]
DEMO
# Python code to demonstrate working of # heapify(), heappush() and heappop() # importing "heapq" to implement heap queue import heapq # initializing list li = [5, 7, 9, 1, 3] # using heapify to convert list into heap heapq.heapify(li) # printing created heap print ("The created heap is : ",end="") print (list(li)) # using heappush() to push elements into heap # pushes 4 heapq.heappush(li,4) # printing modified heap print ("The modified heap after push is : ",end="") print (list(li)) # using heappop() to pop smallest element print ("The popped and smallest element is : ",end="") print (heapq.heappop(li))
應用
https://www.geeksforgeeks.org/applications-priority-queue/
(1)最短路演算法
(2)素數演算法
(3)資料壓縮
(4)A星搜尋
(5)堆排序
(6)系統負載均衡,中斷處理。 程序排程,磁碟排程。
Dijkstra’s Shortest Path Algorithm using priority queue: When the graph is stored in the form of adjacency list or matrix, priority queue can be used to extract minimum efficiently when implementing Dijkstra’s algorithm.
Prim’s algorithm: It is used to implement Prim’s Algorithm to store keys of nodes and extract minimum key node at every step.
Data compression : It is used in Huffman codes which is used to compresses data.
Artificial Intelligence : A* Search Algorithm : The A* search algorithm finds the shortest path between two vertices of a weighted graph, trying out the most promising routes first. The priority queue (also known as the fringe) is used to keep track of unexplored routes, the one for which a lower bound on the total path length is smallest is given highest priority.
Heap Sort : Heap sort is typically implemented using Heap which is an implementation of Priority Queue.
Operating systems: It is also use in Operating System for load balancing (load balancing on server), interrupt handling.