Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added naive implementation of priority queue #196

Merged
merged 8 commits into from
Mar 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pydatastructs/miscellaneous_data_structures/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

from .queue import (
Queue,
PriorityQueue
)
__all__.extend(queue.__all__)

Expand Down
97 changes: 96 additions & 1 deletion pydatastructs/miscellaneous_data_structures/queue.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
from copy import deepcopy as dc

__all__ = [
'Queue'
'Queue',
'PriorityQueue'
]

class Queue(object):
Expand Down Expand Up @@ -171,3 +172,97 @@ def __len__(self):

def __str__(self):
return str(self.queue)

class PriorityQueue(object):
"""
Represents the concept of priority queue.

Parameters
==========

implementation: str
The implementation which is to be
used for supporting operations
of priority queue.
The following implementations are supported,
'linked_list' -> Linked list implementation.
Optional, by default, 'linked_list' implementation
is used.
comp: function
The comparator to be used while comparing priorities.
Must return a bool object.
By default, `lambda u, v: u > v` is used to compare
priorities i.e., maximum priority elements are extracted
by pop operation.

Examples
========

>>> from pydatastructs import PriorityQueue
>>> pq = PriorityQueue()
>>> pq.push(1, 2)
>>> pq.push(2, 3)
>>> pq.pop()
2
>>> pq2 = PriorityQueue(comp=lambda u, v: u < v)
>>> pq2.push(1, 2)
>>> pq2.push(2, 3)
>>> pq2.pop()
1

References
==========

.. [1] https://en.wikipedia.org/wiki/Priority_queue#Naive_implementations
"""

def __new__(cls, implementation='linked_list', **kwargs):
if implementation == 'linked_list':
return LinkedListPriorityQueue(
kwargs.get("comp", lambda u, v: u > v)
)

def push(self, value, priority):
raise NotImplementedError(
"This is an abstract method.")

def pop(self):
raise NotImplementedError(
"This is an abstract method.")

@property
def is_empty(self):
raise NotImplementedError(
"This is an abstract method.")

class LinkedListPriorityQueue(PriorityQueue):

__slots__ = ['items', 'comp']

def __new__(cls, comp=lambda u, v: u > v):
obj = object.__new__(cls)
obj.items = SinglyLinkedList()
obj.comp = comp
return obj

def push(self, value, priority):
self.items.append(value, priority)

def pop(self):
if self.is_empty:
raise IndexError("Priority queue is empty.")

walk = self.items.head
i, max_i, max_p = 0, 0, walk.data
while walk is not None:
if self.comp(walk.data, max_p):
max_i = i
max_p = walk.data
i += 1
walk = walk.next
pop_val = self.items.extract(max_i)
return pop_val.key

@property
def is_empty(self):
return self.items.size == 0
19 changes: 18 additions & 1 deletion pydatastructs/miscellaneous_data_structures/tests/test_queue.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from pydatastructs.miscellaneous_data_structures import Queue
from pydatastructs.miscellaneous_data_structures.queue import ArrayQueue, LinkedListQueue
from pydatastructs.miscellaneous_data_structures.queue import (
ArrayQueue, LinkedListQueue, PriorityQueue,
LinkedListPriorityQueue)
from pydatastructs.utils.raises_util import raises
from pydatastructs.utils.misc_util import _check_type

Expand Down Expand Up @@ -57,3 +59,18 @@ def test_LinkedListQueue():
q1.popleft()

assert rear.key == q1.popleft().key

def test_PriorityQueue():
pq1 = PriorityQueue(implementation='linked_list')
assert _check_type(pq1, LinkedListPriorityQueue) is True
assert raises(NotImplementedError, lambda: Queue(implementation=''))

def test_LinkedListPriorityQueue():
pq1 = PriorityQueue(implementation='linked_list')
pq1.push(1, 2)
pq1.push(2, 3)
pq1.push(3, 4)
assert pq1.pop() == 3
assert pq1.pop() == 2
assert pq1.pop() == 1
assert raises(IndexError, lambda: pq1.pop())