Skip to content

Instantly share code, notes, and snippets.

@marcinwyszynski
Created January 12, 2012 12:47
Show Gist options
  • Save marcinwyszynski/1600309 to your computer and use it in GitHub Desktop.
Save marcinwyszynski/1600309 to your computer and use it in GitHub Desktop.
A simple PriorityQueue in Python
class PriorityQueue(object):
def __init__(self):
self.store = {}
def enq(self, element, weight):
if weight in self.store:
self.store[weight].append(element)
else:
self.store[weight] = [element]
def deq(self):
if len(self.store.keys()) == 0:
return None
max_weight = sorted(self.store.keys())[-1]
element = self.store[max_weight].pop(0)
if len(self.store[max_weight]) == 0:
del self.store[max_weight]
return element
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment