Priority queues

40
PRIORITY QUEUES Michael Tsai 2010/12/28

description

Priority queues. Michael Tsai 2010/12/28. Outline. Dynamic hashing Priority Queue 的種類 DEPQ 的用途 Leftist Tree Binomial Heaps. Dynamic hashing. 觀察 : 當 n/b 比較大以後 , O(1) 就開始崩壞 ( 往 O(n) 方向移動 ) 應變 : 所以要隨時觀察 n/b, 當它大過某一個 threshold 時就把 hash table 變大 觀察 : 把 hash table 變大的時候 , - PowerPoint PPT Presentation

Transcript of Priority queues

Page 1: Priority queues

PRIORITY QUEUESMichael Tsai2010/12/28

Page 2: Priority queues

Outline• Dynamic hashing• Priority Queue 的種類• DEPQ 的用途• Leftist Tree• Binomial Heaps

Page 3: Priority queues

Dynamic hashing• 觀察 : 當 n/b 比較大以後 , O(1) 就開始崩壞 ( 往 O(n) 方向移動 )

• 應變 : 所以要隨時觀察 n/b, 當它大過某一個 threshold 時就把hash table 變大• 觀察 : 把 hash table 變大的時候 , • 需要把小 hash table 的東西通通倒出來 , • 算出每一個 pair 在大 hash table 的位置• 然後重新放進大 hash table

• 有個可憐鬼做 insert 正好碰到應該 hash table rebuild 的時候 , 他就會等非常非常久 . T_T

Page 4: Priority queues

Dynamic hashing• 目標 : 重建的時候 , 不要一次把所以重建的事情都做完• 或許 , 留一些之後慢慢做 ?• 每個 operation 的時間都要合理• 又叫做 extendible hashing

Page 5: Priority queues

例子k h(k)A0 100 000A1 100 001B0 101 000B1 101 001C1 110 001C2 110 010C3 110 011C5 110 101

h(k,i)=bits 0-i of h(k)Example:h(A0,1)=0h(A1,3)=001=1h(B1,4)=1001=9

Page 6: Priority queues

Dynamic hashing using directories

A0, B0A1, B1C2C3

00011011 k h(k)

A0 100 000A1 100 001B0 101 000B1 101 001C1 110 001C2 110 010C3 110 011C5 110 101

Insert C5

directory depth=number of bits of the index of the hash table

h(C5, 2)=01=1

C5, overflowA0, B0A1, B1C2C3

000001010011

100101110111

C5

we increase d by 1until not all h(k,d) of the keys in the cell are the same動腦時間 : 如果原本的要加入 C1 呢 ? 如果第二步驟後加入 A4 呢 ? 答案 : p. 412-413

Page 7: Priority queues

Dynamic hashing using directories

• 為什麼比較快 ?

• 只需要處理 overflow 的櫃子• 如果把 directory 放在記憶體 , 而櫃子資料放在硬碟• 則• search 只需要讀一次硬碟• insert 最多需要讀一次硬碟 ( 讀資料 , 發現 overflow 了 ), 寫兩次硬碟 ( 寫兩個新的櫃子 )• 當要把 hash table 變兩倍大時 , 不需要碰硬碟 ( 只有改directory)

Page 8: Priority queues

Directoryless Dynamic hashing• 假設 hash table 很大 , 但是我們不想一開始就整個開來用 (initialization 會花很大 )• 用兩個變數來控制的 hash table 大小 : r, q• hash table 開啟的地方為 0, 之間

r=2, q=2

0~q-1 及之間使用 h(k,r+1)

q~ 之間使用 h(k,r)

Page 9: Priority queues

Directoryless Dynamic hashing• 每次輸入的時候 , 如果現在這個櫃子滿了• 則開一個新的櫃子 : • 原本 q 櫃子裡面的東西用• h(k,r+1) 分到 q 和兩櫃子裡• 注意有可能還是沒有解決問題• 多出來的暫時用 chain 掛在櫃子下面

k h(k)A0 100 000A1 100 001B4 101 100B5 101 101C1 110 001C2 110 010C3 110 011C5 110 101

B4, A0A1, B5C2C3

00011011

r=2, q=0

insert C5, full

A0A1, B5C2C3B4

000011011

r=2, q=1

C5

100

問 : 再加入 C1 呢 ? ( 課本 p 415)

Page 10: Priority queues

複習 : Priority queue的 operations• A. 看 ( 找到 )queue 裡面 priority 最小 ( 或最大 ) 的element• B. 插入一個 element 到 queue 裡面• C. 拿掉 (delete)queue 裡面 priority 最小 ( 或最大 ) 的element• 之前用 max or min heap• A=O(1)• B=O(log n)• C=O(log n)

Page 11: Priority queues

其他 priority queue 的種類• Meldable priority queue: • 除了原本三個 operation• 還支援一個 operation 可以把兩個 priority queue 合併

• 有什麼用途 ?

• Double-ended: 可以拿最大也可以拿最小的 ( 原本的為 single-ended)• 支援下面的 operations• A. 看 ( 找到 )queue 裡面 priority 最小的 element• B. 看 ( 找到 )queue 裡面 priority 最大的 element• C. 插入一個 element 到 queue 裡面• D. 拿掉 (delete)queue 裡面 priority 最小的 element• E. 拿掉 (delete)queue 裡面 priority 最大的 element

Page 12: Priority queues

DEPQ 的用途 (1)• DEPQ=Double-Ended Priority Queue

• 用來 implement network buffer

網路卡驅動程式的 buffer

PCMAN Mozilla Firefox 網路印表機驅動程式…….World of Warcraft uTorrent

狀況二 : 網路目前idle, 必須選出下一個要傳到網路上的封包

狀況一 : 網路 busy, 可是 buffer 已經滿了 , 必須丟掉東西DEPQ

!!

網路實體線路取出 priority 最低的封包丟掉 取出 priority 最高的封包傳出

Page 13: Priority queues

DEPQ的用途 (2): External sorting• External sorting 的適用時機 : 無法全部丟入主記憶體 ( 部分要放在慢速儲存媒體 )• 使用 DEPQ 來 implement quick sort 的變種

DEPQ

待 sorting 的數字們1. 從待 sorting 的數字中不斷拿數字放入 DEPQ 直到放滿主記憶體為止2. 每次再從待 sorting 的數字們中取一個新的數字 A. 假設此時 DEPQ中具 max/min priority 的數字分別為 B 與 S. 2-a. 若 A>B, 則將 A 放到右邊一堆2-b. 若 A<S, 則將 A 放到左邊一堆

2-c. 若 , 則將 DEPQ 中 B 拿出丟到右邊 ( 或者將 DEPQ中 S 拿出丟到左邊 ), 並將 A 放入 DEPQ

左邊一堆 : 都比 DEPQ 中的數字小 右邊一堆 : 都比 DEPQ 中的數字大

Page 14: Priority queues

DEPQ的用途 (2): External sorting

DEPQ

左邊一堆 : 都比中間一堆的數字小 右邊一堆 : 都比中間一堆的數字大

3. 將待 sorting 之數字堆拿空以後 , 剩下 DEPQ 的數字 , 依序拿出 priority 最高的 , 放入中間一堆中間一堆 : 從原本 DEPQ 放出來的

4. 中間一堆已排好 , 遞迴呼叫繼續將左邊一堆及右邊一堆排好 .

Page 15: Priority queues

Meldable Priority Queue: Leftist tree

• Heap: 要花多少時間結合兩個 heap?• 把兩個 heap 都當作沒有排過直接重新建一個大 heap• 用從一個 array 建成一個 heap 的方法 ( 課本 section 7.6 heapsort) 需要 O(n)

• 目標 : • meld, insert, delete=O(log n)• find=O(1)

Page 16: Priority queues

Extended Binary Tree

A

B C

D E F

Binary Tree Extended Binary Tree

A

B C

D E F

對應

: internal nodes : external nodes

Page 17: Priority queues

Leftist tree 的一些定義• Shortest(x): 從某個 extended tree 的 node走到任一個 external node 的距離• 可以用遞迴定義 :• Shortest(x)=• a. 0, if x 是 external node• b. 1+ min{shortest(leftChild(x)), shortest(rightChild(x))}, if x 是 internal node

• 定義 : A leftist tree is a binary tree such that if it is not empty, then shortest(leftChild(x))shortest(rightChild(x)), for every internal node x.

Page 18: Priority queues

例子 : 這是 leftist tree嗎 ?

A

B C

D E F1

2

2

1

1

1

shortest(leftChild(C))=0shortest(rightChild(C))=1

Page 19: Priority queues

一些性質• 假設 r 為 leftist tree 的 root, n 為 internal nodes 個數• 1. • 2. 從 root 到任何一 external node 的路徑中最右邊的一條 , 為最短 . 其長度為 shortest(r)

• 為什麼 ?• 1. shortest(r)層內沒有 external nodes. • 2. 由定義得知左邊的路徑一定比右邊的路徑長 .

Page 20: Priority queues

Min leftist tree• Min leftist tree: 一個所有某 node 的 key值永遠比它的小孩 key值小的 leftist tree

11

7

2

80

50

13

2

1

1

1

1

1

12

9

5

10

8

20

1

1

1

1

1

2

18

115

1

Page 21: Priority queues

Insert & Delete by Melding• Insert(a, tree b): 創造一個只有 a 的 min leftist tree, 然後 meld(a,b).

• Delete(tree b): Meld(leftChild(root(b)), rightChild(root(b)))

atree b

Meld(a,b)

rleft child’s tree right child’s tree

remove root node

Meld(leftChild(root), rightChild(root))

Page 22: Priority queues

Melding process - Example

11

7

2

80

50

13

2

1

1

1

1

1

12

9

5

10

8

20

1

1

1

1

1

2

18

115

1

2 is smaller than 5

add to the new tree

11

7

2

13

Step 1: 把兩棵樹合併並確定 parent 的 key 大於 children 的 key

Page 23: Priority queues

Melding process - Example

80

50

1

1 12

9

5

10

8

20

1

1

1

1

1

2

18

115

1

5 is smaller than 50

add to the new tree

11

7

2

13

12

9

5

20

18

Step 1: 把兩棵樹合併並確定 parent 的 key 大於 children 的 key

Page 24: Priority queues

Melding process - Example

80

50

1

1 10

81

1

15

1

8 is smaller than 50

add to the new tree

11

7

2

13

12

9

5

20

18

10

8

15

Step 1: 把兩棵樹合併並確定 parent 的 key 大於 children 的 key

Page 25: Priority queues

Melding process - Example

80

50

1

1

add to the new tree

11

7

2

13

12

9

5

20

18

10

8

15 8

0

50

Step 1: 把兩棵樹合併並確定 parent 的 key 大於 children 的 key

Page 26: Priority queues

Melding process - ExampleStep 1: 把兩棵樹合併並確定 parent 的 key 大於 children 的 key

11

7

2

13

12

9

5

20

18

10

8

15 8

0

50

Page 27: Priority queues

Melding process - ExampleStep 2: 沿著剛剛右邊一條路線的 root, 確定所有的 shortest(left)shortest(right)

11

7

2

13

12

9

5

20

18

10

8

15 8

0

50

1

1ok

1

1

2ok

11

2

1

not ok

Page 28: Priority queues

Melding process - ExampleStep 2: 沿著剛剛右邊一條路線的 root, 確定所有的 shortest(left)shortest(right)

11

7

2

13

5

10

8

15 8

0

50

1

1

1

1

2 1

12

9

20

181

1

2

2

1

1

1

not ok

Page 29: Priority queues

Melding process - ExampleStep 2: 沿著剛剛右邊一條路線的 root, 確定所有的 shortest(left)shortest(right)

11

7

2

13

5

10

8

15 8

0

50

1

1

1

1

2 1

12

9

20

181

1

2

2

1

1

1

2

Page 30: Priority queues

那要花多少時間呢 ?• Meld operation = O(??)

• 兩個 operation 都只要花走右邊路徑長度的時間• 右邊路徑長度都是最短的• Worst case: balanced binary tree• 所以所花時間為 O(log n)

Page 31: Priority queues

變形 : Weight-based leftist tree• w(x): node 的 weight• w(x)= 以 x 當作 root 的 subtree 裡面 internal node 的數目• 如果 x 是 external node, 則 w(x)=0

• w(x) 遞迴定義 :• w(x)=1+w(leftChild(x))+w(rightChild(x))

• Weight-based Leftist Tree定義 : • 對每一個 internal node x, w(leftChild(x))w(rightChild(x)). • 其中又可以有 Max or min weight-based leftist tree• (parent key值大於 children key值 )

Page 32: Priority queues

Weight-based leftist tree• 定理 : 對weight-based leftist tree 的任一 internal node x, rightmost(x) 為其至任一 external node 的路徑中最右邊一條的長度 . 則 rightmost(x)• 證明 ( 使用歸納法 )請見課本 p.431

• meld, insert, delete 的方法如同一般 leftist tree• 好處 : 第二步驟不用再使用獨立一個 pass即可完成

Page 33: Priority queues

B-Heap (Binomial Heap)• 目標 : • 1. 支援 insert, delete (min), and meld 的動作• 2. “平均來說” 單一的 operation 可能可以到達 O(1) 或O(log n)

• 跟課本講得不太一樣• 參考資料 :• http://en.wikipedia.org/wiki/Binomial_heap• 很讚的動畫 applet• http://www.cse.yorku.ca/~aaw/Sotirios/BinomialHeap.html

Page 34: Priority queues

Binomial Tree長什麼樣子

• binomial tree 的定義 :• 1. binomial tree of order 0 只有單一 node• 2. binomial tree of order k 一個 root, 自己的 children為 order 為 k-1, k-2, …, 0 的 binomial tree 們• node 數目為 n,

𝐵2𝐵1𝐵0 𝐵3

Page 35: Priority queues

表示方法

Siblings: Linked List, ordered by tree degree

min

Page 36: Priority queues

Binomial Heap長什麼樣子• 是 binomial tree組成的 forest• 1. 每個 tree 的任何一 parent node 的 key 大 ( 小 )於child node 的 key• 2. 每個 order 的 binomial tree 只能在這個 forest 裡面有一顆或零棵

• 條件 2. 使得 n 個 node 的 binomial heap 最多只有 +1棵樹

𝐵1𝐵0𝐵3

Page 37: Priority queues

Merge 兩棵 order 一樣的樹• 看 root key誰比較小• 小的當新的樹的 root• 大的接在新的樹的 root 下面

2 4

min

2

4

min

Page 38: Priority queues

Merge 兩個 heap (Meld)• 假設要 merge p 和 q 兩個 heap• p 和 q各有 order 0-K 的 binomial tree• 則開始看每個 order ( 假設是 i)• 如果 p 和 q 中只有一個有 order i 的• 則直接丟到 merged 的 heap• 否則的話 , 就把兩個 tree 合併• 合併後的 tree, 還有可能跟後面order 比較大的 tree 再合併• 黑板舉另外一個例子• O(log n)

5

8

4𝐵1𝐵0

2𝐵0

3

6 7

9

𝐵2

p

q

order 0: 都有

2

4合併的 :

order 1: 只有一個 , 但 merge 過後的也有

5

8

order 2: 只有一個 , 但 merge 過後的也有

3

6 7

9

Page 39: Priority queues

Insert & Find minimum• Insert:• 使用 merge 來 implement• 當作 merge 原本的 heap 和一個 order 0 的 tree( 只有一個node) 就好• O(log n)• “平均來講”可以達到 O(1)!

• Find minimum:• 把所有樹的 root key 都檢查一遍找到最小的• O(log n)• 或 O(1) 如果有一個 pointer 每次都指到最小的• ( 把找的動作在其他 operation 做了 )

Page 40: Priority queues

Delete minimum

假設這個是最小的

拿掉之後 , 底下的 sub tree 也是 binomial heap

除了有動到的 tree 之外 , 其他的 sub tree 也是一個binomial heap

Merge

O(log n)