0% found this document useful (0 votes)
24 views

Lecture-08 - Advanced Data Structures - 2

The document discusses different types of heaps, including binary heaps, binomial heaps, and Fibonacci heaps. It provides details on the characteristics, structure, operations, and implementations of binary heaps and binomial heaps. Specifically, it describes how binary heaps can be implemented using arrays, and how binomial heaps use a collection of binomial trees connected by a root list. It also explains how to perform operations like insertion, deletion, and merging on these different heap data structures.

Uploaded by

灭霸
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Lecture-08 - Advanced Data Structures - 2

The document discusses different types of heaps, including binary heaps, binomial heaps, and Fibonacci heaps. It provides details on the characteristics, structure, operations, and implementations of binary heaps and binomial heaps. Specifically, it describes how binary heaps can be implemented using arrays, and how binomial heaps use a collection of binomial trees connected by a root list. It also explains how to perform operations like insertion, deletion, and merging on these different heap data structures.

Uploaded by

灭霸
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 56

Algorithmics

CT065-3.5-3

Advanced Data Structures -2


Level 3 – Computing (Software Engineering)
Topic & Structure of Lesson

• Heaps
– Binary Heaps
– Binomial Heaps
– Fibonacci Heaps

Module Code and Module Title Title of Slides


Learning Outcomes

By the end of this lesson you should


be able to:
• Understand the characteristics and
operations of various heaps

Module Code and Module Title Title of Slides


Binary Heap

• Data structure created using a binary tree,


with two additional properties:
– Shape: tree is either complete (all leaves at
same level), or almost complete (leave
nodes filled from left to right and leaves are at
different levels)
– Heap: each parent node is greater than or
equal to it’s children according to some
comparison function that defines the heap

Module Code and Module Title Title of Slides


Binary Heap

• Heap: “Greater than” is not meant in the


numerical sense, but according to the
comparison function that sorts the heap
– Does not affect ordering of sibling nodes.
– Child nodes can be freely interchanged (only
if heap property is not violated)

Module Code and Module Title Title of Slides


Examples

This is legal This is also legal

Min-Heap Max-Heap

Module Code and Module Title Title of Slides


Insertion

• Up-Heap, Bubble/Percolate/Sift-Up
• Worst -> O(log n)
• Average -> O(1)

1. Add value as a leaf (at bottom level)


2. Compare value with parent, stop if order is
correct.
3. If not, swap with parent.
4. Repeat steps 2 – 3 until order is correct.

Module Code and Module Title Title of Slides


Insertion

Module Code and Module Title Title of Slides


Insertion

Module Code and Module Title Title of Slides


Root Deletion

Deletion can only be done on the root.

1. Swap Root with lowest + rightmost leaf node


2. Delete that leaf
3. Compare root value with child nodes, stop if
order is correct
4. If not, swap with child with value that is “greater
than” root
5. Repeat 3 – 4 until order is correct

Module Code and Module Title Title of Slides


Root Deletion

1) Swap 89 with 1
2) Check 89 with 23 and 12. This is a Min-
Heap, so swap with lowest value
Module Code and Module Title Title of Slides
Root Deletion

3) Swap 89 with 19. The Heap is now


properly sorted.

Module Code and Module Title Title of Slides


Binary Heap: Implementation

• Heaps are either complete, or almost complete binary


trees
– Can be stored compactly in arrays
– Pointers not required
• For each node in the ith index
– Parent (i) = floor (i / 2) //the largest integer not greater than x
– Left (i) = 2i + 1
– Right (i) = 2i + 2

Module Code and Module Title Title of Slides


Array Implementation

Parent (1) = floor (1 / 2) = 0th index


Left (1) = 2 * 1 + 1 = 3rd index
Right (1) = 2 * 1 + 2 = 4th index

Module Code and Module Title Title of Slides


Binomial Tree

• Recursive definition:

Module Code and Module Title Title of Slides


Binomial Tree

• Useful properties of order k binomial tree


Bk
– Number of nodes = 2k.
– Height = k.
– Degree of root = k.
– Deleting root yields binomial trees Bk-1, Bk-2,…
B0.
• Proof – by induction on k.

Module Code and Module Title Title of Slides


Binomial Tree

The resulting binomial trees created once the root is deleted


are also binomial trees of order k-1,…0.

Module Code and Module Title Title of Slides


Binomial Heap

• Ordered sequence of binomial trees that satisfy


binomial heap property.
– Each tree is min-heap ordered.
– 0 or 1 binomial trees of order k.

B0 B1 B4
Module Code and Module Title Title of Slides
Binomial Heap
• Properties of N-node binomial heap
– Min key contained in root of B0, B1, …, Bk.
– Contains binomial tree Bi iff bi = 1 where bn b2b1b0 is binary representation of
N.
– At most log2 N + 1 binomial trees.
– Height  log2 N.

N = 19
binary = 10011
# trees = 3
height = 4

B0 B1 B4
Module Code and Module Title Title of Slides
Binomial Heap: Implementation

• Represent trees using left-child, right


sibling pointers.
– 3 links per node (parent, left, right)
– Coding for variable no. of links per node
possible, but painful
• Root of trees connected with singly linked
list.
– Degree of trees strictly decreasing/increasing
from left to right.

Module Code and Module Title Title of Slides


Binomial Heap: Implementation

Module Code and Module Title Title of Slides


Merging

• Create heap H that is union of heaps H'


and H''
– Trivial if H' and H'' are binomial trees of the
same order.
• Connect roots of H' and H''
• Choose smaller key to be root of H
• If H' and H'' are of order k, then the merged heap
H is of order k+1.

Module Code and Module Title Title of Slides


Merging

Binomial Heap

Leftist-power-of-2 Heap
Module Code and Module Title Title of Slides
Merging

• If H' and H'' are binomial heaps consisting of


binomial trees with various orders…

Module Code and Module Title Title of Slides


Merging

• …merge two binomial trees of the same order


one by one. Similar to binary addition.
– Running time = O (log N)

11 + 7 = 18

1111
1011
+ 0111
10010

Module Code and Module Title Title of Slides


Merging

Module Code and Module Title Title of Slides


Merging

Module Code and Module Title Title of Slides


Merging

Module Code and Module Title Title of Slides


Merging

Module Code and Module Title Title of Slides


Merging

Module Code and Module Title Title of Slides


Merging

Module Code and Module Title Title of Slides


Merging

Module Code and Module Title Title of Slides


Merging

1111
1011
+ 0111
10010
H=
B4 B1

Module Code and Module Title Title of Slides


Delete Min

• Delete node with minimum key in binomial


heap H.
– Find root x with min key in root list of H, and
delete
– H'  broken binomial trees
– H  Merge(H', H)
– Running time = O (log N)

Module Code and Module Title Title of Slides


Delete Min

Module Code and Module Title Title of Slides


Decrease Key

• Decrease key of node x in binomial heap H.


– Assume x is in binomial tree Bk.
– Bubble node x up the tree if x is too small.
– Running time = O (log n).

Module Code and Module Title Title of Slides


Delete Node

• Delete node x in binomial heap H.


– Decrease key of x to - .
– Delete min.
– Running time = O (log N).

Module Code and Module Title Title of Slides


Insert Node

• Insert a new node x into binomial heap H.


– H'  MakeHeap(x)
– H  Merge(H', H)

Module Code and Module Title Title of Slides


Fibonacci Heap

• Collection of min-heap ordered trees.


• Structure of heap more flexible than binomial
trees.
– Every element can be in it’s own separate trees, or in
a single tree of depth n.
minimum

Module Code and Module Title Title of Slides


Fibonacci Heap: Implementation

• Represent trees using left-child, right sibling


pointers and circular, doubly linked list.
– Easy to splice off sub-trees
• Roots of all trees linked with circular, doubly
linked list
– Easy to perform node deletion + insertion
• Each node maintains no. of children +
marked/unmarked state.
• Maintain pointer to root with minimum key value

Module Code and Module Title Title of Slides


Find Minimum

• Pointer to minimum value -> finding it is


trivial
– Actual + amortized cost = constant

Module Code and Module Title Title of Slides


Merging

• Concatenate both lists of roots to each


other.
– Actual + amortized cost = constant

Module Code and Module Title Title of Slides


Insert Node

• Create new heap consisting of one node,


then merge.
– Actual + amortized cost = constant

Module Code and Module Title Title of Slides


Delete Minimum

• Remove node with minimum key, then convert


children into roots of new trees.
– Amortized running time -> O(d) = O(log n).

Module Code and Module Title Title of Slides


Delete Minimum

• Need to update pointer to root with minimum key…


– Possibly many roots that also act as single-node trees.
• …therefore, decrease number of roots.
– Link together roots of the same degree.
– Make one of them a child of the other, such that the one with the
smaller key remains the root.
– Repeat until every root has a different degree.
– Use array of length (log N) to keep pointers to each node of
each degree.
– Update array when linking roots.
• At most (log N) roots at the end.
• Amortized running time = O (log N).

Module Code and Module Title Title of Slides


Delete Minimum

N = 8, log n = 3

a[ ] = 6 2 -

1st occurrences of roots with 0 No root of


and 1 degrees respectively degree 2 exists
yet

N = 8, log n = 3

a[ ] = 4 2 -

Only 1 root with 0


degrees exists now

Module Code and Module Title Title of Slides


Delete Minimum

N = 8, log n = 3

a[ ] = 4 7 2

Module Code and Module Title Title of Slides


Delete Minimum

• Check all roots and find minimum value


– Amortized running time = O (log N)
minimum

Overall amortized running time for deleting minimum


key = O (log N).

Module Code and Module Title Title of Slides


Decrease Key

• Decrease key of node x in fibonacci heap H.


• If heap property is violated
– Cut node from parent
– If parent is not root and not marked, mark it.
– If parent is not root and marked, cut parent.
– Repeat process until root is reached or a parent is
marked instead of cut.
– Finally, unmark all roots, and point to minimum.

Module Code and Module Title Title of Slides


Decrease Key

Assuming 4, 7 and 8 are already marked…

Original value of 9 decreased to 0.

Module Code and Module Title Title of Slides


Decrease Key

Cut 0… Then cut 8…

Module Code and Module Title Title of Slides


Decrease Key

Then cut 7…

Root has been reached, stop cutting.

Module Code and Module Title Title of Slides


Decrease Key

Finally, unmark all roots and point to minimum.

minimum

Module Code and Module Title Title of Slides


Delete Node

• Delete node x in binomial heap H.


– Decrease key of x to - .
– Delete min.
– Amortized running time = O (log N).

Module Code and Module Title Title of Slides


Summary

• Heaps
– Binary Heaps
– Binomial Heaps
– Fibonacci Heaps

Module Code and Module Title Title of Slides


Next Lesson

• Shortest Path algorithms


– Prims algorithm
– Kruskals algorithm
– Dijkshtras algorithm

Module Code and Module Title Title of Slides

You might also like