cs235102 data structures chapter 9 heap structures

45
CS235102 CS235102 Data Structures Data Structures Chapter 9 Heap Structures Chapter 9 Heap Structures

Upload: norma-wheeler

Post on 20-Jan-2016

248 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CS235102 Data Structures Chapter 9 Heap Structures

CS235102 CS235102 Data StructuresData StructuresChapter 9 Heap StructuresChapter 9 Heap Structures

Page 2: CS235102 Data Structures Chapter 9 Heap Structures

Min-Max HeapMin-Max Heap DeapsDeaps Leftist TreesLeftist Trees Binomial HeapsBinomial Heaps Fibonacci HeapsFibonacci Heaps

Page 3: CS235102 Data Structures Chapter 9 Heap Structures

MIN-MAX Heaps (1/10)MIN-MAX Heaps (1/10)

DefinitionDefinition A A double-ended priority queuedouble-ended priority queue is a data structure that is a data structure that

supports the following operations:supports the following operations: Insert an element with arbitrary keyInsert an element with arbitrary key Delete an element with the largest keyDelete an element with the largest key Delete an element with the smallest keyDelete an element with the smallest key

Min heap or Max heap:Min heap or Max heap: Only Only insertioninsertion and and one of the two deletionone of the two deletion operations are operations are

supportedsupported

Min-Max heap:Min-Max heap: Supports all of the operations just described.Supports all of the operations just described.

Page 4: CS235102 Data Structures Chapter 9 Heap Structures

Definition:Definition: A A mix-maxmix-max heap is a complete binary tree such that if it is n heap is a complete binary tree such that if it is n

ot empty, each element has a field called ot empty, each element has a field called keykey.. Alternating levels of this tree are Alternating levels of this tree are minmin levels and levels and maxmax levels, levels,

respectively.respectively. Let Let xx be any node in a be any node in a minmin--maxmax heap. If heap. If xx is on a is on a minmin ( (mama

xx) level then the element in ) level then the element in xx has the has the minimumminimum ( (maximummaximum) ) key from amongkey from amongall elements in all elements in the subtree with the subtree with root root xx. We call . We call this node a this node a minmin ((maxmax) node.) node.

MIN-MAX Heaps (2/10)MIN-MAX Heaps (2/10)

Page 5: CS235102 Data Structures Chapter 9 Heap Structures

MIN-MAX Heaps (3/10)MIN-MAX Heaps (3/10) Insertion into a min-max heap (at a “max” level)Insertion into a min-max heap (at a “max” level)

If it is If it is smallersmaller//greatergreater than its father (a “min”), then it must than its father (a “min”), then it must bebe smaller smaller//greatergreater than all than all “max” “max”//“min”“min” above. So simply above. So simply check thecheck the “min” “min”//“max”“max” ancestors ancestors

There exists a similar approach at a “min” levelThere exists a similar approach at a “min” level

Page 6: CS235102 Data Structures Chapter 9 Heap Structures

verify_maxverify_max Following the nodes the max node i to the root and insert iFollowing the nodes the max node i to the root and insert i

nto its proper nto its proper placeplace

#define MAX_SIZE 100#define FALSE 0#define TRUE 1#define SWAP(x,y,t) ((t)=(x), (x)=(y), (y)=(t))typedef struct {

int key;/* other fields */}element;element heap[MAX_SIZE];

[1]

[2]

[3]

[4]

[5]

[6]

[7][8

][9]

[10]

[11]

[12]

[13]

item = 80i = 13 grandparent = 3

40

3 0

80

MIN-MAX Heaps (4/10)MIN-MAX Heaps (4/10)

Page 7: CS235102 Data Structures Chapter 9 Heap Structures

min_max_insertmin_max_insert: Insert item into the min-max heap: Insert item into the min-max heap

[1]

[2]

[3][4

][5]

[6]

[7]

[8]

[9]

[10]

[11]

[12]

[13]

[14]

7

70 40

30 9 10 15

45 50 30 20 12

item.key =

*n =

5

12

parent =

13

6

max

min

max

min

10

5

7

80

14

7

40

80

complexity: O(log n)

MIN-MAX Heaps (5/10)MIN-MAX Heaps (5/10)

Page 8: CS235102 Data Structures Chapter 9 Heap Structures

MIN-MAX Heaps (6/10)MIN-MAX Heaps (6/10) Deletion of min elementDeletion of min element

If we wish to delete the element with the smallest key, If we wish to delete the element with the smallest key, then this element is in the root. then this element is in the root.

In general situation, we are to reinsert an element In general situation, we are to reinsert an element itemitem into a min-max-heap, into a min-max-heap, heapheap, whose root is empty., whose root is empty.

We consider the two cases:We consider the two cases:1.1. The root has no childrenThe root has no children

ItemItem is to be inserted into the root. is to be inserted into the root.

2.2. The root has at least one child.The root has at least one child. The The smallest keysmallest key in the min-max-heap is in in the min-max-heap is in one of the children one of the children

or grandchildren of the rootor grandchildren of the root. We determine the node . We determine the node kk has the has the smallest key.smallest key.

The following possibilities need to be considered:The following possibilities need to be considered:

Page 9: CS235102 Data Structures Chapter 9 Heap Structures

MIN-MAX Heaps (7/10)MIN-MAX Heaps (7/10)a)a) item.keyitem.key heapheap[[kk].].keykey

No element in heap with key smaller than item.keyNo element in heap with key smaller than item.key Item may be inserted into the root.Item may be inserted into the root.

b)b) item.keyitem.key heapheap[[kk].].key,key, kk is a child of the root is a child of the root Since Since kk is a max node, it has no descendants with key is a max node, it has no descendants with key

larger than larger than heapheap[[kk].].keykey. Hence, node . Hence, node kk has no descen has no descendants with key larger than dants with key larger than item.keyitem.key..

heapheap[[kk] may be ] may be moved to the moved to the root and root and itemitem inserted into inserted into node node kk..

Page 10: CS235102 Data Structures Chapter 9 Heap Structures

MIN-MAX Heaps MIN-MAX Heaps (8/10)(8/10)c)c) item.keyitem.key heapheap[[kk].].key, key,

k k is a grandchild of the root is a grandchild of the root In this case, In this case, heapheap[[kk] may be moved to the root, now ] may be moved to the root, now heahea

pp[[kk] is seen as presently empty. ] is seen as presently empty. Let Let parentparent be the parent of be the parent of kk.. If If item.keyitem.key heapheap[[parentparent].].keykey, then interchange them. T, then interchange them. T

his ensures that the max node his ensures that the max node parentparent contains the larges contains the largest key in the sub-heap with root t key in the sub-heap with root parentparent..

At this point, we are faced with the problem of inserting At this point, we are faced with the problem of inserting ititemem into the into the sub-heap with sub-heap with root root kk. . Therefore, we Therefore, we repeat the above repeat the above process.process.

Page 11: CS235102 Data Structures Chapter 9 Heap Structures

delete_mindelete_min:: Delete the minimum element from the min-max heapDelete the minimum element from the min-max heap

[1]

[2]

[3][4

][5]

[6]

[7]

[8]

[9]

[10]

[11]

[12]

7

70 40

30 9 10 15

45 50 30 20 12

[0]

7

complexity: O(log n)

parent =

last =k =

i =

temp.key =

x.key =12

*n = 1211

51

5

9

2

5

11

12

Page 12: CS235102 Data Structures Chapter 9 Heap Structures

MIN-MAX Heaps (10/10)MIN-MAX Heaps (10/10) Deletion of max elementDeletion of max element

1.1. Determine the children of the root which are located on Determine the children of the root which are located on max-level, and find the larger one (node) which is the max-level, and find the larger one (node) which is the largest one on the min-max heaplargest one on the min-max heap

2.2. We would consider the node as the root of a max-min We would consider the node as the root of a max-min heapheap

3.3. There exist a There exist a similar similar approach approach (deletion of (deletion of max element) max element) as we as we mentioned mentioned aboveabove

max-min heap

Page 13: CS235102 Data Structures Chapter 9 Heap Structures

Deaps(1/8)Deaps(1/8)

DefinitionDefinition The root contains no elementThe root contains no element The left subtree is a min-heapThe left subtree is a min-heap The right subtree is a max-heapThe right subtree is a max-heap Constraint between the two trees:Constraint between the two trees:

let i be any node in left subtree, j be the corresponlet i be any node in left subtree, j be the corresponding node in the right subtree. ding node in the right subtree.

if j not exists, let j corresponds to parent of iif j not exists, let j corresponds to parent of i i.key <= j.keyi.key <= j.key

Page 14: CS235102 Data Structures Chapter 9 Heap Structures

Deaps(2/8)Deaps(2/8)

i = min_partner(n) =i = min_partner(n) =

j = max_partner(n) =j = max_partner(n) = if j > heapsize j /= 2if j > heapsize j /= 2

1nlog2n 2

1log22 nn

Page 15: CS235102 Data Structures Chapter 9 Heap Structures

Deaps Insert(3/8)Deaps Insert(3/8)public void insert(int x) {public void insert(int x) {

int i;int i;

if (++n == 2) {if (++n == 2) {

deap[2] = x; return; }deap[2] = x; return; }

if (inMaxHeap(n)) {if (inMaxHeap(n)) {

i = minPartner(n);i = minPartner(n);

if (x < deap[i]) {if (x < deap[i]) {

deap[n] = deap[i];deap[n] = deap[i];

minInsert(i, x);minInsert(i, x);

} else maxInsert(n, x);} else maxInsert(n, x);

} else {} else {

i = maxPartner(n);i = maxPartner(n);

if (x > deap[i]) {if (x > deap[i]) {

deap[n] = deap[i];deap[n] = deap[i];

maxInsert(i, x);maxInsert(i, x);

} else minInsert(n, x);} else minInsert(n, x);

}}

}}

Page 16: CS235102 Data Structures Chapter 9 Heap Structures

Deaps(4/8)Deaps(4/8)

Insertion Into A DeapInsertion Into A Deap

Page 17: CS235102 Data Structures Chapter 9 Heap Structures

Deaps(5/8)Deaps(5/8)

Page 18: CS235102 Data Structures Chapter 9 Heap Structures

Deaps(6/8)Deaps(6/8)

Page 19: CS235102 Data Structures Chapter 9 Heap Structures

Deaps delete min(7/8)Deaps delete min(7/8)

public int deleteMin() {public int deleteMin() {

int i, j, key = deap[2], x = int i, j, key = deap[2], x = deap[n--];deap[n--];

// move smaller child to i// move smaller child to i

for (i = 2; 2*i <= n; deap[i] for (i = 2; 2*i <= n; deap[i] = deap[j], i = j) {= deap[j], i = j) {

j = i * 2;j = i * 2;

if (j+1 <= n && (deap[j] if (j+1 <= n && (deap[j] > deap[j+1]) j++;> deap[j+1]) j++;

}}

// try to put x at leaf i// try to put x at leaf i

j = maxPartner(i);j = maxPartner(i);

if (x > deap[j]) {if (x > deap[j]) {

deap[i] = deap[j];deap[i] = deap[j];

maxInsert(j, x);maxInsert(j, x);

} else {} else {

minInsert(i, x);minInsert(i, x);

}}

return key;return key;

}}

Page 20: CS235102 Data Structures Chapter 9 Heap Structures

Deaps(8/8)Deaps(8/8)

Page 21: CS235102 Data Structures Chapter 9 Heap Structures

Leftist Trees(1/7)Leftist Trees(1/7) Support Support combinecombine (two trees to one) (two trees to one)

Page 22: CS235102 Data Structures Chapter 9 Heap Structures

Leftist Trees(2/7)Leftist Trees(2/7) shortest(x) = 0 if x is an external node, otherwiseshortest(x) = 0 if x is an external node, otherwise 1+min(shortest(left(x)),shortest(right(x))}1+min(shortest(left(x)),shortest(right(x))}

Page 23: CS235102 Data Structures Chapter 9 Heap Structures

Leftist Trees(3/7)Leftist Trees(3/7) Definition: Definition: shortest(left(x)) >= shortest(right(x))shortest(left(x)) >= shortest(right(x))

Page 24: CS235102 Data Structures Chapter 9 Heap Structures

Leftist Trees(4/7)Leftist Trees(4/7)

Algorithm for combine(a, b)Algorithm for combine(a, b) assume a.data <= b.dataassume a.data <= b.data if (a.right is null) then make b be right child of if (a.right is null) then make b be right child of

aa else combine (a.right, b)else combine (a.right, b) if shortest (a.right) > shortest (a.left) then exchif shortest (a.right) > shortest (a.left) then exch

angeange

Page 25: CS235102 Data Structures Chapter 9 Heap Structures

Leftist Trees(5/7)Leftist Trees(5/7)

Page 26: CS235102 Data Structures Chapter 9 Heap Structures

Leftist Trees(6/7)Leftist Trees(6/7)

Page 27: CS235102 Data Structures Chapter 9 Heap Structures

Leftist Trees(7/7)Leftist Trees(7/7)

Page 28: CS235102 Data Structures Chapter 9 Heap Structures

Binomial Heaps(1/10)Binomial Heaps(1/10)

Cost Amortization(Cost Amortization( 分期還款分期還款 )) every operation in leftist trees costs O(logn)every operation in leftist trees costs O(logn) actual cost of delete in Binomial Heap could be O(n), actual cost of delete in Binomial Heap could be O(n),

but insert and combine are O(1)but insert and combine are O(1) cost amortization charge some cost of a heavy operatcost amortization charge some cost of a heavy operat

ion to lightweight operationsion to lightweight operations amortized Binomial Heap delete is O(logamortized Binomial Heap delete is O(log22n)n) A tighter bound could be achieved for a sequence of A tighter bound could be achieved for a sequence of

operationsoperations actual cost of any sequence of actual cost of any sequence of ii inserts, inserts, cc combines, combines,

and and dmdm delete in Binomial Heaps is O( delete in Binomial Heaps is O(ii++cc++dmdmloglogii))

Page 29: CS235102 Data Structures Chapter 9 Heap Structures

Binomial Heaps(2/10)Binomial Heaps(2/10) Definition of Binomial HeapDefinition of Binomial Heap

Node: Node: degree, child ,left_link, right_link, data, parentdegree, child ,left_link, right_link, data, parent roots are doubly linkedroots are doubly linked aa points to smallest root points to smallest root

Page 30: CS235102 Data Structures Chapter 9 Heap Structures

Binomial Heaps(3/10)Binomial Heaps(3/10)

Page 31: CS235102 Data Structures Chapter 9 Heap Structures

Binomial Heaps(4/10)Binomial Heaps(4/10)

Insertion Into A Binomial HeapsInsertion Into A Binomial Heaps make a new node into doubly linked circular make a new node into doubly linked circular

list pointed at by alist pointed at by a set a to the root with smallest keyset a to the root with smallest key

Combine two B-heaps Combine two B-heaps aa and and bb combine two doubly linked circular lists to onecombine two doubly linked circular lists to one set set aa to the root with smallest key to the root with smallest key

Page 32: CS235102 Data Structures Chapter 9 Heap Structures

Binomial Heaps(5/10)Binomial Heaps(5/10)

Deletion Of Min ElementDeletion Of Min Element

Page 33: CS235102 Data Structures Chapter 9 Heap Structures

Binomial Heaps(6/10)Binomial Heaps(6/10)

Page 34: CS235102 Data Structures Chapter 9 Heap Structures

Binomial Heaps(7/10)Binomial Heaps(7/10)

Page 35: CS235102 Data Structures Chapter 9 Heap Structures

Binomial Heaps(8/10)Binomial Heaps(8/10)

Page 36: CS235102 Data Structures Chapter 9 Heap Structures

Binomial Heaps(9/10)Binomial Heaps(9/10)

Page 37: CS235102 Data Structures Chapter 9 Heap Structures

Binomial Heaps(10/10)Binomial Heaps(10/10) Trees in B-Heaps is Binomial treeTrees in B-Heaps is Binomial tree

BB00 has exactly one node has exactly one node

BBkk, k > 0, consists of a root with degree k a, k > 0, consists of a root with degree k a

nd whose subtrees are Bnd whose subtrees are B00, B, B11, …, B, …, Bk-1k-1

BBkk has exactly 2 has exactly 2kk nodes nodes

actual cost of a delete is O(logn + actual cost of a delete is O(logn + ss)) ss = number of min-trees in = number of min-trees in a a (original roots - (original roots -

1) and 1) and y y (children of the removed node)(children of the removed node)

Page 38: CS235102 Data Structures Chapter 9 Heap Structures

Fibonacci Heaps(1/8)Fibonacci Heaps(1/8)

DefinitionDefinition deletedelete, delete the element in a specified node, delete the element in a specified node decrease keydecrease key This two operations are followed by cascading This two operations are followed by cascading

cutcut

Page 39: CS235102 Data Structures Chapter 9 Heap Structures

Fibonacci Heaps(2/8)Fibonacci Heaps(2/8)

Deletion From An F-heapDeletion From An F-heap min or not minmin or not min

Page 40: CS235102 Data Structures Chapter 9 Heap Structures

Fibonacci Heaps(3/8)Fibonacci Heaps(3/8) Decrease KeyDecrease Key

if not min, and smaller than parent, then deleteif not min, and smaller than parent, then delete

Page 41: CS235102 Data Structures Chapter 9 Heap Structures

Fibonacci Heap(4/8)Fibonacci Heap(4/8)

To prevent the amortized cost of delete miTo prevent the amortized cost of delete min becomes O(n), each node can have only n becomes O(n), each node can have only one child be deleted.one child be deleted.

If two children of x were deleted, then x mIf two children of x were deleted, then x must be cut and moved to the ring of roots.ust be cut and moved to the ring of roots.

Using a flag (true of false) to indicate whetUsing a flag (true of false) to indicate whether one of x’s child has been cuther one of x’s child has been cut

Page 42: CS235102 Data Structures Chapter 9 Heap Structures

Fibonacci Heaps(5/8)Fibonacci Heaps(5/8) Cascading CutCascading Cut

Page 43: CS235102 Data Structures Chapter 9 Heap Structures

Fibonacci Heaps(6/8)Fibonacci Heaps(6/8)

LemmaLemma the ith child of any node x in a F-Heap has a dthe ith child of any node x in a F-Heap has a d

egree of at least i – 2, except when i=1 the degegree of at least i – 2, except when i=1 the degree is 0ree is 0

CorollaryCorollary Let Sk be the minimum possible number of desLet Sk be the minimum possible number of des

cendants of a node of degree k, then Scendants of a node of degree k, then S00=1, S=1, S11==

2. From the lemma above, we got2. From the lemma above, we got

(2 comes from 1st child(2 comes from 1st child

and root)and root)2 2

0

k

i

ik SS

Page 44: CS235102 Data Structures Chapter 9 Heap Structures

Fibonacci Heaps(7/8)Fibonacci Heaps(7/8)

2F k

2ii2

kF

2 kk FS That’s why the data structure is called FiboThat’s why the data structure is called Fibo

nacci Heapnacci Heap

Page 45: CS235102 Data Structures Chapter 9 Heap Structures

Fibonacci Heaps(8/8)Fibonacci Heaps(8/8)

Application Of F-heapsApplication Of F-heaps