在之前寫最短路徑,和哈夫曼編碼的時候都用到了優先順序佇列優化,用的是c++中的包,priority_queue。
那在python中會以什麼形式存在呢。
class
array
(object):
def__init__
(self, maxsize=
none):
self.elem =
[none
]* maxsize
self.length = maxsize
def__setitem__
(self, key, value)
: self.elem[key]
= value
def__len__
(self)
:return self.length
def__getitem__
(self, item)
:return self.elem[item]
def__iter__
(self)
:for i in self.elem:
yield i
class
maxheap
(object):
def__init__
(self, maxsize=
none):
self.maxsize = maxsize
self.elem = array(maxsize)
self.count =
0def
__len__
(self)
:return self.count
defadd(self, value)
:if self.count >= self.maxsize:
raise exception(
'full'
) self.elem[self.count]
= value
self.count +=
1 self.siftup(self.count -1)
defsiftup
(self, ndx)
: index = ndx
while index >0:
parent =
(index -1)
//2if self.elem[parent]
< self.elem[index]
: self.elem[parent]
, self.elem[index]
= self.elem[index]
, self.elem[parent]
index = parent
defextract
(self)
:if self.count <=0:
raise exception(
"empty"
) value = self.elem[0]
self.count -=
1 self.elem[0]
= self.elem[self.count]
self.siftdown(0)
return value
defsiftdown
(self, ndx)
: parent = ndx
while parent *2+
1< self.count:
child = parent *2+
1if child != self.count -
1and self.elem[child]
< self.elem[child +1]
: child = child +
1if self.elem[parent]
> self.elem[child]
:break
else
: self.elem[parent]
, self.elem[child]
= self.elem[child]
, self.elem[parent]
parent = child
class
priorityqueue
(object):
def__init__
(self, maxsize)
: self.maxsize = maxsize
self.maxheap = maxheap(maxsize)
defpush
(self, priority, value)
:# 注意這裡把這個tuple push進去,python比較tuple從第乙個開始比較這樣就很巧妙的實現了按照優先順序排序
entry =
(priority, value)
self.maxheap.add(entry)
defpop
(self, with_priority=
false):
entry = self.maxheap.extract(
)if with_priority:
return entry
else
:return entry[1]
defis_empty
(self)
:return
len(self.maxheap)==0
deftest_priority_queue()
: size =
5 pq = priorityqueue(size)
pq.push(5,
'purple'
) pq.push(0,
'white'
) pq.push(3,
'orange'
) pq.push(1,
'black'
) res =
while
not pq.is_empty():
))assert res ==
['purple'
,'orange'
,'black'
,'white'
]test_priority_queue(
)
按最小優先順序出隊的順序的優先順序佇列:
import heapq
class
priorityqueue
(object):
def__init__
(self, maxsize)
: self.maxsize = maxsize
self.minheap =
defpush
(self, priority, value)
: entry =
(priority, value)
defpop
(self, with_priority =
false):
if with_priority:
return entry
else
:return entry[1]
defis_empty
(self)
:return
len(self.minheap)==0
deftest_priority_queue()
: size =
5 pq = priorityqueue(size)
pq.push(5,
'purple'
)# priority, value
pq.push(0,
'white'
) pq.push(3,
'orange'
) pq.push(1,
'black'
) res =
while
not pq.is_empty():
))print
(res)
test_priority_queue(
)
python優先順序佇列 python 優先順序佇列
簡介 優先順序佇列是基於堆的,關於堆的時候可以參考文章堆,優先順序佇列就是入隊時,會分配乙個優先順序,之後出隊時,根據優先順序出列。如,入隊時 4,a 6,r 3 d 則出隊順序 6,r 4,a 3 d 優先順序佇列的python實現 class priorityqueue object def i...
python優先順序佇列
class priorityqueue data dict def init self pass 入隊 如果物件沒有優先等級,則新增到隊尾,如果對空,則預設等級為1 如果物件有優先等級,如果有同等級的則新增到對應等級末尾 def push self,obj,key none if key none ...
佇列 優先順序佇列
優先順序佇列的隊尾是不需要改變的,永遠在低下標處。當佇列增加資料時,隊頭的位置就是資料項的大小減去1.public class priorityq 插入 public void insert long item else quearray j 1 item nitem 刪除 public long ...