forked from AniketChaudhri/Google-Maps-0.5
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPriority_Queue.py
58 lines (42 loc) · 1.4 KB
/
Priority_Queue.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
'''
Priority Queue is self implemented in Python 'Queue' library.
But we faced some errors using that module.
Hence we made this file refering to Python Queue Documentation
Code written by Adarsh Anand'''
import heapq
class AStarQueue(object):
def __init__(self):
self.myheap = []
def show(self):
return self.myheap
def push(self, priority, distance, node):
heapq.heappush(self.myheap, (priority, distance, node))
def pop(self):
priority, distance, node = heapq.heappop(self.myheap)
return priority, distance, node
# Create a priority queue
class PriorityQueue(object):
def __init__(self):
self.myheap = []
def show(self):
return self.myheap
def push(self, priority, node):
heapq.heappush(self.myheap, (priority, node))
def pop(self):
priority, node = heapq.heappop(self.myheap)
return priority, node
# Create a priority queue that doesn't add duplicate nodes
class PrioritySet(object):
def __init__(self):
self.myheap = []
self.myset = set()
def show(self):
return self.myheap
def push(self, priority, node):
if not node in self.myset:
heapq.heappush(self.myheap, (priority, node))
self.myset.add(node)
def pop(self):
priority, node = heapq.heappop(self.myheap)
self.myset.remove(node)
return priority, node