Skip to main content

Indexed Heap Queue (Priority Queue)

Project description

Python Indexed Heap Queue

A Python implementation of an indexed priority queue using a min heap data structure. This data structure maintains a mapping between keys and their priorities while providing efficient O(log n) access to the minimum priority element.

Key Features

  • Fast O(log n) operations for insertion, deletion, and priority updates
  • Direct key-based access to priorities in O(1) time
  • Type-safe implementation with full generic type support
  • Dictionary-like interface implementing the Python Mapping protocol
  • Memory efficient with O(n) space complexity

Requirements

  • Python 3.9+
  • No external dependencies

Installation

Install using pip:

pip install indexed-heapq

Basic Usage

from indexed_heapq import IndexedHeapQueue

# Create an empty queue
queue = IndexedHeapQueue()

# Add some items
queue['task1'] = 3
queue['task2'] = 1
queue['task3'] = 2

# Get the highest priority item (lowest number)
next_task, priority = queue.peek()  # ('task2', 1)

# Remove and return the highest priority item
next_task, priority = queue.pop()   # ('task2', 1)

# Update a priority
queue['task1'] = 0  # Move task1 to the front

# Access a priority directly
priority = queue['task3']  # 2

# Remove an item
del queue['task3']

# Check if an item exists
if 'task1' in queue:
    print("Task1 is in the queue")

Performance

Operation Time Complexity
getitem O(1)
peek() O(1)
pop() O(log n)
insert O(log n)
update O(log n)
delete O(log n)

Type Hints

The class is fully generic and type-safe:

from indexed_heap_queue import IndexedHeapQueue

# With explicit type hints
queue: IndexedHeapQueue[str, float] = IndexedHeapQueue()
queue['task1'] = 1.5

# Or let type inference work
queue = IndexedHeapQueue({ 'task1': 1.5 })
queue['task2'] = 1

Common Use Cases

  • Task schedulers with priority management
  • Graph algorithms (Eager Dijkstra's shortest path, Eager Prim's MST)
  • Event scheduling systems
  • Any application requiring a priority queue with key-based access

License

This project is licensed under the MIT License - see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

indexed_heapq-0.1.0.tar.gz (7.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

indexed_heapq-0.1.0-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file indexed_heapq-0.1.0.tar.gz.

File metadata

  • Download URL: indexed_heapq-0.1.0.tar.gz
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.9.21

File hashes

Hashes for indexed_heapq-0.1.0.tar.gz
Algorithm Hash digest
SHA256 894ebebc6f428e09cf5a763d1abf88985ba9c17cad8212aaca9dfcd5465f6cdb
MD5 4a395cfe8386aca187b84f43f8f37962
BLAKE2b-256 0f65182683da24ef54722278b5996169b487057591f6a24e37d50e4bab096f7a

See more details on using hashes here.

File details

Details for the file indexed_heapq-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: indexed_heapq-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.9.21

File hashes

Hashes for indexed_heapq-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 095a64282c0d7b64a7165c83cfb1ac948f211aa0fe6b1b6be1553fc9836fec44
MD5 95348189232e08c01f73c81a63ccc4cb
BLAKE2b-256 d5a768491d2dda37638726bb7033953cdc604fcf59785a48f33470c70edd368d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page