franklin university 1 comp 620 algorithm analysis module 3: advanced data structures trees, heap,...

62
Franklin University 1 OMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Upload: marcia-webster

Post on 16-Jan-2016

228 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

1

COMP 620 Algorithm Analysis

Module 3: Advanced Data Structures

Trees, Heap, Binomial Heap, Fibonacci Heap

Page 2: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

2

COMP 620 Algorithm Analysis

Trees• Definition: A tree (which is one type of a data structure) is a

finite set of one or more nodes such that:There is a specially designated node called root.The remaining nodes are divided into n>=0 disjoint sets T1, T2, ..... Tn where each of these sets is a tree. T1, T2, .... Tn are called the subtrees of the root.

• Node - represents an item of information stored in the tree.• Branches - represent the links between the nodes.• Root of the tree or root node - node at the top of the tree, the start of the tree.• Degree of a node - the number of subtrees of the node (i.e., the number of

children from any one node).• Degree of a tree - the maximum degree of any of the nodes in the tree.• Leaf or terminal nodes - nodes with degree 0.

Page 3: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

3

COMP 620 Algorithm Analysis

Tree Terminology (contd.)7. Child/children - the roots of the subtrees of the parent node.

8. Siblings - children of the same parent.

9. Parent - a node that has subtrees (i.e., a node that has children).

10. Level of a node - defined by root = 1, children of root = 2, grandchildren of root = 3, etc. (In some textbooks, the root is defined to be at level 0, children of the root at level 1, etc.)

11. Depth or height of a tree - the maximum level of any node in the tree.

12. Binary trees are trees with no more than two-way branching from each node in the tree. A binary tree is either empty or consists of a root node and two disjoint binary trees called left and right subtrees.

Page 4: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

4

COMP 620 Algorithm Analysis

Tree - ExampleA

L

B

K

O

C

E F

N

M

JIHG

D

Degree of the tree = Height (depth) of the tree =

Terminal nodes of the tree are:

Page 5: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

5

COMP 620 Algorithm Analysis

Binary Trees: Properties• The maximum number of nodes on level i of a binary tree is 2 (i-

1) , where i >= 1.• The maximum number of nodes in a binary tree of depth k is 2k –

1,where k >= 1.• For any non-empty binary tree, T, if n0 represents the number of

leaf nodes and n2 represents the number of nodes with degree 2, then n0 = n2 + 1.

• A full binary tree of depth k is a binary tree of depth k having 2k - 1 nodes, where k >= 1.

• A binary tree with n nodes and depth k is complete if and only if its nodes correspond to the nodes numbered from 1 to n in the full binary tree of depth k, filling in from left to right on each level.

Page 6: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

6

COMP 620 Algorithm Analysis

Full and Complete Binary Trees

C

GFED

B

A

4 5

32

1

Full Binary Tree Complete Binary Tree

Array Representing Above Tree

A B C D E F G0 1 2 3 4 5 6 7

•Leftchild(i) is at 2i if 2i >n no left child

•Rightchild(i) is at 2i + 1 if 2i +1 > n no right child

Page 7: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

7

COMP 620 Algorithm Analysis

Neither Full nor Complete TreesA

D

B

C

E F

G

Array Representation of the Above Tree

A B C D E F G 0 1 2 3 4 5 .. 10 11 .. 23

The array representation of the tree that is neither full nor complete is very wasteful of memory

Page 8: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

8

COMP 620 Algorithm Analysis

Linked Representationtemplate <class BaseData>

Class BtNode

{

public:

BaseData info;

BtNode *leftChild, *rightChild;

};

Page 9: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

9

COMP 620 Algorithm Analysis

Traversing Trees• Inorder traversal1. Traverse the left subtree 2. Visit the root 3. Traverse the right subtreeVoid BinTree::inord( BtNode *rt){

if (rt != NULL){

inord(rt->leftChild);processNode(rt->info);inord(rt->rightChild);

}}

Page 10: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

10

COMP 620 Algorithm Analysis

Traversing Trees• Preorder traversal1. Visit root 2. Traverse the left subtree 3. Traverse the right subtree Void BinTree::preord( BtNode *rt){

if (rt != NULL){

processNode(rt->info);preord(rt->leftChild);preord(rt->rightChild);

}}

Page 11: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

11

COMP 620 Algorithm Analysis

Traversing Trees• Postorder traversal1. Traverse the left subtree

2. Traverse the right subtree

3. Visit the root

• Levelorder traversalEvery node is visited in turn from left to right on every level,

starting at level 1, then level 2, etc., until level n, where n

represents the height (depth) of the tree.

Page 12: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

12

COMP 620 Algorithm Analysis

Binary Search Trees• Binary search tree is of significant importance in Computer

Science.• Has better performance than many other data structures especially

for operations like insertion, deletion, and searching.• Definition:A binary search tree is a binary tree. It may be empty. If

not empty, it satisfies the following properties:Every element has a key, and traditionally no two elements have the same key.Keys in a non-empty left subtree must be smaller than the key in the root of the subtree.Keys in a non-empty right subtree must be larger than the key in the root of the subtree.Left and right subtrees are also binary search trees.

Page 13: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

13

COMP 620 Algorithm Analysis

J

OH

E

A

F

G

I

DB

C

M P

N

L

K

Binary Search Tree

Page 14: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

14

COMP 620 Algorithm Analysis

Binary Search Trees: Operations• Searching a binary search tree:

Begin at the root. If the key of the element to be searched = root key, then the search is successful.If the key of the element to be searched < root key, then search the left subtree.If the key of the element to be searched > root key, then search the right subtree.

• Inserting into a binary search tree:- Search for the key- If the key is not present, locate the parent node- Insert the new node as the left/right child of the parent nodeAll insertions take place at leaf nodes.

Page 15: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

15

COMP 620 Algorithm Analysis

Binary Search Trees: Operations• Deleting from a binary search tree:

The deletion process begins with a search to find the node to be deleted from the binary search tree.

Three possible scenarios exist in the deletion process:

1. Delete a leaf node.

- Make the appropriate pointer in x’s parent a null pointer

2. Delete a node with 1 child.

- Set the appropriate pointer in x’s parent to point to this child

3. Delete a node with 2 children.

- Replace the value stored in the node x by its inorder successor (predecessor) and then delete the successor (predecessor)

Page 16: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

16

COMP 620 Algorithm Analysis

Binary Search Trees: Variations• AVL (or height-balanced) trees

A binary search tree in which the balance factor of each node is 0, 1, or –1, where the balance factor of a node x is defined as the height of the left subtree of x minus the height of x’s right subtree.

• 2-3-4 trees: A tree with the following properties:Each node stores at most 3 data values.Each internal node is a 2-node, 3-node, or a 4-node.All the leaves are on the same level.

Page 17: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

17

COMP 620 Algorithm Analysis

Motivation for AVL Tree

2

1

3

4

5

53

41

2

The height of AVL tree is approximately Log2 n

What is the value of balance of each node?

Page 18: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

18

COMP 620 Algorithm Analysis

2-3-4 Tree

53

27 38 60 70

16 25 33 36 41 46 48 55 59 65 68 73 75 79

Page 19: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

19

COMP 620 Algorithm Analysis

Red Black Trees• Red Black trees

A binary search tree with two kinds of nodes, red and black, which satisfy the following properties:

• Every node is either red or black. Root is black.• Every leaf (NIL) is black.• If a node is red, both its children are black.• Every simple path from node to descendant leaf contains

the same number of black nodes.• A red-black tree with n internal nodes has height at most

2log(n+1).

Page 20: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

20

COMP 620 Algorithm Analysis

11

142

151

8

7

5

RED-BLACK TREE

Page 21: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

21

COMP 620 Algorithm Analysis

B-trees• B-trees of minimum degree t

A tree with the following properties:- Each node stores at most 2t -1 data values.- Every node other than the root must have at least t-1 data values.- n[x] keys in each node x stored in nondecreasing order, so that key1[x] <= key2[x] .. <= key n[x] [x].

- Each internal node contains n[x] + 1 children. X contains n +1 pointers c1[x], c2[x] … c n[x]+1[x].

Page 22: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

22

COMP 620 Algorithm Analysis

B-trees

- The keys keyi [x] separates the ranges of keys stored in each subtree: if ki is any key stored in the subtree with root ci[x] then, then

k1 <= key1[x] <= key2[x] <= ….<= key n[x] [x] <= k n[x]+1

- All the leaves are on the same level.

- Balanced and designed to work efficiently on disks and other direct access secondary storage devices. Many database systems use B-tree.

- the maximum height of a n-key B tree is log t ((n+1)/2)

- insert, delete, and search time on a B-Tree is (log n)

Page 23: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

23

COMP 620 Algorithm AnalysisCopyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Page 24: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

24

COMP 620 Algorithm AnalysisCopyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Page 25: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

25

COMP 620 Algorithm Analysis

B-tree of minimum degree t = 2

M

Q T XD H

Y ZV WN PF GB C J K L

Is 2-3-4 tree a B-tree?

R S

Page 26: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

26

COMP 620 Algorithm AnalysisCopyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Page 27: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

27

COMP 620 Algorithm AnalysisCopyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Page 28: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

28

COMP 620 Algorithm Analysis

B-Tree• Insert

- locate the node where the key should be inserted

- split any full nodes encountered while descending the tree

• Deletion

Deletion is similar to Insertion. Make sure that the tree is still a B-Tree after deletion.

Detailed discussion of deleting from a B-tree, refer to Section 18.3, pages 450-453, of Cormen, Leiserson, and Rivest.

Page 29: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

29

COMP 620 Algorithm Analysis

G M P X

A C D E J K N O R S T U V Y Z

(a) Initial Tree

G M P X

A B C D E J K N O R S T U V Y Z

(b) B Inserted

B-Tree Insertion - Inserts only in leaf node

Page 30: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

30

COMP 620 Algorithm Analysis

B-Tree Insertion - Inserts only in leaf node

G M P T X

A B C D E J K N O U V Y Z Q R S

P

G M T X

A B C D E J K L N O Q R S U V Y Z

(C ) Q Inserted

(d ) L Inserted

Page 31: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

31

COMP 620 Algorithm Analysis

P

C G M T X

A B J K L N O Q R S U V Y Z

B-Tree Insertion - Inserts only in leaf node

(e ) F Inserted

D E F

Page 32: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

32

COMP 620 Algorithm AnalysisCopyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Page 33: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

33

COMP 620 Algorithm AnalysisCopyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Page 34: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

34

COMP 620 Algorithm AnalysisCopyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Page 35: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

35

COMP 620 Algorithm AnalysisCopyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Page 36: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

36

COMP 620 Algorithm AnalysisCopyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Page 37: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

37

COMP 620 Algorithm AnalysisCopyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Page 38: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

38

COMP 620 Algorithm AnalysisCopyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Page 39: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

39

COMP 620 Algorithm AnalysisCopyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.

Page 40: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

40

COMP 620 Algorithm Analysis

Heaps• Heaps data structures are mostly used to support the

following operations (not efficient to use for search):Insert element xReturn min elementDelete minimum elementUnion of two heaps

•  Application:  Dijkstra’s Shortest Path

Prim’s MSTHuffman EncodingHeap Sort

Page 41: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

41

COMP 620 Algorithm Analysis

Binary HeapA heap is a complete binary tree such that the value of the key in the root is greater than the value of the key in each of its children, and that both subtrees are also heaps (a recursive definition).

http://homepages.ius.edu/rwisman/C455/html/notes/Chapter6/HeapSort.htm

Heapsort:(1) Make a heap of the elements to be sorted.(2) Convert the heap into a sorted list. 10

6

3 2

9

5

Page 42: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

42

COMP 620 Algorithm Analysis

Heap SortHeapSort(A)

1 Build_Max_Heap(A)

2 for i ← length[A] downto 2 do

3 exchange A[1] ↔ A[i]

4 heap-size[A] ← heap-size[A] – 1

5 Max_Heapify(A, 1)

Build_Max_Heap(A)

1 heap-size[A] ← length[A]

2 for i ← floor(length[A]/2) downto 1 do

3 Max_Heapify (A, i)

Page 43: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

43

COMP 620 Algorithm Analysis

HEAP SORT

Heapify(A, i)1 l ← LEFT(i)2 r ← RIGHT(i)3 if l ≤ heap-size[A] and A[l] > A[i]4 then largest ← l5 else largest ← i6 if r ≤ heap-size[A] and A[r] > A[largest]7 then largest ← r8 if largest ≠ i9 then exchange A[i] ↔ A[largest]10 Max_Heapify (A, largest)

Page 44: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

44

COMP 620 Algorithm Analysis

HEAPSOperation Linked

List

Binary

Heap

Binomial Heap

Fibonacci

Heap

make-heap 1 1 1 1

insert 1 Log N Log N 1

find-min N 1 Log N 1

delete-min N Log N Log N Log N

decrease-key 1 Log N Log N 1

delete N Log N Log N Log N

union 1 N Log N 1

Page 45: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

45

COMP 620 Algorithm Analysis

BINOMIAL TREE

• Recursive Definition : Bk consists of 2 binomial trees B k-1

that are linked together; the root of the one is the leftmost Child of the root of the other.

B 0 B 2B 3B 1

Page 46: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

46

COMP 620 Algorithm Analysis

Binomial Tree Properties

• Number of nodes = 2 k

• Height of the tree = k

• There are nodes nodes at level i

• The root has degree k and it children are B k-1, B k-2,.. B 0 from left to right.

i

k

Page 47: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

47

COMP 620 Algorithm Analysis

Binomial Heap

• A binomial heap is a set of binomial trees

• Each binomial tree in H is heap-ordered key (x) >= key (parent(x)).

• There never exist 2 or more trees with same degree in the heap.

• Linked list of roots in order of increasing degree

• Binomial tree is stored in a left child-right sibling representation.

Page 48: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

48

COMP 620 Algorithm Analysis

Binomial Heap

5 7

10

3

17

6

4

B 0

B 2

B 1

View

View the operations on binomial heap at the class web site

Head

Sibling

Parent

Child

degree

Key

Roots of the trees connected with singly linked listHead points to the first node in the linked list

Page 49: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

49

COMP 620 Algorithm Analysis

Binomial Heap

MAKE_BINOMIAL_HEAP()allocate(H)head[H] = NIL

Insert(H, x)H’ = MAKE-BINOMIAL-HEAP()set x’s field appropriatelyhead[H’] = xn[H’] = 1

H = BINOMIAL-HEAP-UNION(H, H’]

Page 50: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

50

COMP 620 Algorithm Analysis

Binomial Heap

Binomial-Heap-Minimum(H)y = NIL

x = head[H]

min = inf

while x <> NIL

do if key[x] < min

then min = key[x]

y = x

x = sibling[x]

return y;

Page 51: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

51

COMP 620 Algorithm Analysis

Binomial HeapBINOMIAL-HEAP-UNION (H1, H2)

Merge the root lists of binomial heaps H1 and H2 into a single linked linked list H that is sorted by degree into monotonically increasing order.

Links roots of equal degree until at most one root remains of each degree.

Binomial-Link (y,z)p[y] = zsibling[y] = child[z]child[z] = ydegree [z] = degree[z] + 1

Page 52: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

52

COMP 620 Algorithm Analysis

Binomial Heap

BINOMIAL-HEAP-EXTRACT-MIN(H)find the root x with the minimum key in the root list of H, and remove x from the root list of H

H’ = MAKE-BINOMIAL-HEAP()

reverse the order of the linked list of x’s children and set head[H’] to point to the head of the resulting list

H = BINOMIAL-HEAP-UNION (H, H’)

return (x)

Page 53: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

53

COMP 620 Algorithm Analysis

Fibonacci Heap• A set of min-heap-ordered trees

• Roots of trees are connected with circular doubly linked list. Children of a node are connected with circular doubly linked list.

• Pointer to root of tree with minimum element.Parent

Mark: Newly created nodes are unmarked. This field becomes true if the node has lost a childSince the node became a child

Of another node.Left

RightChild

Key

Degree

Page 54: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

54

COMP 620 Algorithm Analysis

Fibonacci HeapView the operations on Fibonacci heap at the class web site

23 7 3 17 24

18 52 38

4139

4630 26

35

Min[H]

Page 55: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

55

COMP 620 Algorithm Analysis

Fibonacci Heap

Make_heap()allocate(H)min[H] = NILn[H]= 0

Insert(H, x)set x’s field appropriatelyadd x to root list of Hreset min[H] if needed

n[H] = n[H] + 1

Page 56: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

56

COMP 620 Algorithm Analysis

Fibonacci HeapUnion(H1, H2)

H = new heap whose root list contains roots from H1 and H2

Min[H] = min[H1]

If ( min[H1] == NIL) or (min [H2] <> NIL and min[H2] < min[H1])

then min [H] = min[H2]

N[H] = n[H1] + n[H2]

free the objects H1 and H2

return H

Page 57: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

57

COMP 620 Algorithm Analysis

Fibonacci HeapExtract-min (H)z = min[H]

Add z’s children to root list

Remove z from root list

If root list <> {}

then Consolidate H

else min[H] = NIL

n[H] = n[H] - 1

Page 58: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

58

COMP 620 Algorithm Analysis

Fibonacci Heap

Consolidate (H)While 2 trees in H (T1, T2) have the same degree:

if root(T1) < root (T2)

then make T1 child of T2

else make T2 child of T1

for i = 0 to D(n[H] ) // D is max possible trees

if tree T of degree i has root-key < min[H]

then min[H] = T

Page 59: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

59

COMP 620 Algorithm Analysis

• FIB-HEAP-DECREASE-KEY(H, x, k)• 1 if k > key[x]• 2 then error "new key is greater than current key"• 3 key[x] ← k• 4 y ← p[x]• 5 if y ≠ NIL and key[x] < key[y]• 6 then CUT(H, x, y)• 7 CASCADING-CUT(H, y)• 8 if key[x] < key[min[H]]• 9 then min[H] ← x• CUT(H, x, y)• 1 remove x from the child list of y, decrementing degree[y]• 2 add x to the root list of H• 3 p[x] ← NIL• 4 mark[x] ← FALSE• CASCADING-CUT(H, y)• 1 z ← p[y]• 2 if z ≠ NIL• 3 then if mark[y] = FALSE• 4 then mark[y] ← TRUE• 5 else CUT(H, y, z)• 6 CASCADING-CUT(H, z)

Fibonacci Heap Decrease Key

Page 60: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

60

COMP 620 Algorithm Analysis

Fibonacci Heap Decrease Key

Page 61: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

61

COMP 620 Algorithm Analysis

Fibonacci Heap Decrease Key

a. The initial Fibonacci heapb. The node with key 46 has its key decreased to 15. The node

becomes a root, and its parent ( with key 24) which had previously been unmarked, becomes marked.

c-e The node with key 35 has its key decreased to 5. In part (c) , the node now with key 5, becomes a root. Its parent, with key 26, is cut from its parent and made an unmarked root in (d). Another cascading cut occurs, since the node with key 24 is marked as well. This node is cut from its parent and made an unmarked root in part (e). The cascading cuts stop at this point, since the node with key 7 is a root.

Page 62: Franklin University 1 COMP 620 Algorithm Analysis Module 3: Advanced Data Structures Trees, Heap, Binomial Heap, Fibonacci Heap

Franklin University

62

COMP 620 Algorithm Analysis

Fibonacci Heap Delete Node

• FIB-HEAP-DELETE(H, x)

• 1 FIB-HEAP-DECREASE-KEY(H, x, -∞)

• 2 FIB-HEAP-EXTRACT-MIN(H)