| 라이브러리 | 스레드 안전성 | 설치 필요 | 특징 |
|---|---|---|---|
heapq | ❌ | ❌ | 가볍고 빠른 Min-Heap |
queue.PriorityQueue | ✅ | ❌ | 스레드 안전 |
sortedcontainers | ❌ | ✅ | 정렬된 리스트 기반 |
heapdict | ❌ | ✅ | 키 기반 힙 구조 지원 |
pqueue | ❌ | ✅ | 다양한 정책 지원 |
heapq 모듈 (표준 라이브러리)설명: 최소 힙(Min-Heap) 기반의 우선순위 큐를 제공합니다.
특징:
heapify, heappush, heappop 함수 제공예제:
import heapq
heap = []
heapq.heappush(heap, (2, "task2"))
heapq.heappush(heap, (1, "task1"))
heapq.heappush(heap, (3, "task3"))
while heap:
priority, task = heapq.heappop(heap)
print(priority, task)
queue.PriorityQueue (표준 라이브러리)설명: thread-safe를 보장하는 우선순위 큐입니다.
특징:
heapq를 사용예제:
from queue import PriorityQueue
pq = PriorityQueue()
pq.put((2, "task2"))
pq.put((1, "task1"))
pq.put((3, "task3"))
while not pq.empty():
priority, task = pq.get()
print(priority, task)
sortedcontainers 라이브러리설명: 정렬된 리스트 기반의 컬렉션을 제공하며, 우선순위 큐로도 사용 가능
설치:
pip install sortedcontainers
예제:
from sortedcontainers import SortedList
pq = SortedList()
pq.add((2, "task2"))
pq.add((1, "task1"))
pq.add((3, "task3"))
while pq:
priority, task = pq.pop(0)
print(priority, task)
heapdict 라이브러리설명: key-value 형태로 사용할 수 있는 우선순위 힙
설치:
pip install heapdict
예제:
from heapdict import heapdict
hd = heapdict()
hd["task1"] = 1
hd["task2"] = 2
hd["task3"] = 3
while hd:
task, priority = hd.popitem()
print(priority, task)
pqueue 라이브러리 (비표준)설명: min, max, fifo 등 다양한 정책을 지원하는 큐
설치:
pip install pqueue
예제:
from pqueue import PriorityQueue
pq = PriorityQueue()
pq.put(2, "task2")
pq.put(1, "task1")
pq.put(3, "task3")
while not pq.empty():
print(pq.get())