Priority Queue--Data Structure

(一)

Main operations of a queue:

    PushBack(e); PopFront().


Priority queue is a generalization of a queue where each element is assigned a priority and elements come out in order by priority.

Main operations of Priority Queue:

    Insert(job): Add a job.

    ExtractMax(): To process a data with the highest priority.

    Remove(it): Removes an element pointer by an iteratorit

    GetMax(): Returns an element with maximum priority (without changing set of elements).

    ChangePriority(it,p): changes the priority of an element pointed by it top.

Algorithms that use Priority Queue:

    Dijkstra's : finding a shortest path in a graph

    Prim's : constructing a minimum spanning tree of a graph 

    Huffman's : constructing an optimum prefix-free encoding of a string

    Heap sort: sorting a given sequeue


Summary:

                                  Insert       ExtractMax

unsorted array/list       O(1)          O(n)

sorted array/list           O(n)          O(1)

Binary heap                O(logn)     O(logn)


(二) Binary Heap

Binary min-heap, Binary max-heap

Operations:

    GetMax(): Return the root value. (Runtime: O(1))

    Insert() : Attach a new node to any leaf, then sift up.

    ExtractMax(): Replace the root with any leaf, then sift down. (Runtime: O(tree height))

    ChangePriority(): Change a value of node, then sift up/down. (Runtime: O(tree height))

    Remove(): change the priority of the element to infinity, let it sift up then extract the maximum. (Runtime: O(tree height))


Complete Binary Tree:

A binary tree is complete if all its levels arefilled except possibly the last one which isfilled from left to right. 

Advantages: 

  Low height:  at most O(logn)

  Store as Array


What do we pay for these advantages?

    We need to keep the tree complete.

Which binary heap operations modifythe shape of the tree?

    Only Insert and ExtractMax (Remove changes the shape by calling ExtractMax). 

    To insert an element as a leaf in the leftmost vacant position in the last level and let it sift up.

    To extract the maximum value, replace the root by the last leaf and let it sift down.


Pseudocode:

Parent(i)

return ⌊i/2

LeftChild(i)

return 2i

RightChild(i)

return 2i + 1

SiftUp(i)

while i > 1 and H[Parent(i)] < H[i]:

  swap H[Parent(i)] and H[i]
  i ← Parent(i)

SiftDown(i)

maxIndex ← i
l ← LeftChild(i)
if l ≤ size and H[l] > H[maxIndex]:

  maxIndex ← l
  r ← RightChild(i)
if r ≤ size and H[r] > H[maxIndex]:

  maxIndex ← r

if i ̸= maxIndex:

  swap H[i] and H[maxIndex]

  SiftDown(maxIndex )

Insert(p)

if size = maxSize: return ERROR

size ← size + 1

H[size] ← p

SiftUp(size )

ExtractMax()

result ← H[1]

H[1] ← H[size]

size ← size − 1

SiftDown(1)

return result

Remove(i)

H[i] ← ∞

SiftUp(i )

ExtractMax()

ChangePriority(i, p)

oldp ← H[i]

H[i] ← p
if p > oldp:

   SiftUp(i )

else:

   SiftDown(i )


Summary:

The resulting implement is:

 Fast: all operations work in time O(logn)(GetMax work in O(1)).

 Space efficient: store as an array of priority; parent-child connections are not stored,but are computed on the fly.

 Easy to implemented: all operations are implemented in just a few lines of code.



Heap sort: Runtime: nlogn

HeapSort(A[1. . . n])

create an empty priority queue

for i from 1 to n:

  Insert(A[i ])
for i from n downto 1:

  A[i] ← ExtractMax()


Turn an array into a heap:

BuildHeap(A[1. . . n])

size ← n
for i from ⌊n/2⌋ downto 1:

  SiftDown(i )


In-place Heap Sort:

HeapSort(A[1. . . n])

BuildHeap(A)  {size = n}

repeat (n − 1) times:

   swap A[1] and A[size]

   size ← size − 1

   SiftDown(1)

 




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值