Tan Van - Binomial Heap Presentation

download Tan Van - Binomial Heap Presentation

of 30

Transcript of Tan Van - Binomial Heap Presentation

  • 8/3/2019 Tan Van - Binomial Heap Presentation

    1/30

    Binomial Heap

    CS566

    Tan Van

  • 8/3/2019 Tan Van - Binomial Heap Presentation

    2/30

    Binomial Heap History

    Binomial heap was introduced in 1978 by Jean Vuillemin

    Jean Vuillemin is a professor in mathematics and computer science.

  • 8/3/2019 Tan Van - Binomial Heap Presentation

    3/30

    Binomial Tree A binomial heap is a collection of binomial trees.

    Binomial tree Bk is an ordered tree defined recursively. The binomial treeB0 has one node. The binomial tree Bk consists of two binomial trees Bk-1and they are connected such that the root of one tree is the leftmost child ofthe other.

    Binomial tree properties:

    Bk has 2^k nodes Bk has height k

    There are exactly ( ik) nodes at depth i for i=0, 1, 2,,k.

    The root has degree k which is greater than other node in the tree.Each of the roots child is the root of a subtree Bi.

  • 8/3/2019 Tan Van - Binomial Heap Presentation

    4/30

    Binomial Tree Example

    7

    B0 B1

    5

    8

    B2

    10

    15

    4

    6

    6

    7

    23

    11

    15

    10

    20

    25

    21

    23

    32

    14

    4

    7

    5

    9

    4

    3

    6

    33

    11

    31

    34

    22

    B3

    B4

  • 8/3/2019 Tan Van - Binomial Heap Presentation

    5/30

    Binomial Heap Properties

    Each binomial tree is heap-order: key of a node is greater or equal to thekey of its parent. The root has the smallest key in the tree.

    There is at most one binomial tree whose root has a given degree. Thismeans that there are at most |_log n_| +1 trees in a binomial heap.

    The binomial trees in the binomial heap are arranged in increasing order ofdegree

    Example:

    5 1

    1210

    15

    head[H]

    7

    2

    1310

    15

    3

    1210

    16

  • 8/3/2019 Tan Van - Binomial Heap Presentation

    6/30

    Binomial Heap Implementation

    Each node has the following fields:

    p: parent

    child: leftmost child

    sibling

    Degree Key

    Roots of the trees are connected using linked list.

  • 8/3/2019 Tan Van - Binomial Heap Presentation

    7/30

    Binomial Heap Implementation

    2

    0

    NIL

    NIL

    head[H]

    2

    1

    2

    NIL

    NIL

    12

    0

    NIL NIL

    head[H] 1

    1210

    15

    10

    1

    15

    0

    NIL NIL

    a) c)keydegree

    child sibling

    p

    b)

  • 8/3/2019 Tan Van - Binomial Heap Presentation

    8/30

    Binomial Heap Operations

    Create heap

    Find minimum key

    Unit two binomial heap

    Insert a node

    Extract minimum node Decrease a key

    Delete a node

  • 8/3/2019 Tan Van - Binomial Heap Presentation

    9/30

    Create A New Binomial Heap

    The operation simply creates a new pointer and sets it to NIL.

    Pseudocode:

    Binomial-Heap-Create()

    1 head[H]

  • 8/3/2019 Tan Van - Binomial Heap Presentation

    10/30

    Find Minimum Key Since the binomial heap is a min-heap-order, the minimum key of each

    binomial tree must be at the root. This operation checks all the roots to

    find the minimum key. Pseudocode: this implementation assumes that there are no keys with

    value

    Binomial-Heap-Minimum(H)

    1 y

  • 8/3/2019 Tan Van - Binomial Heap Presentation

    11/30

    Find Minimum Key Example

    5 1

    1210

    15

    head[H]

    7

    2 5 1

    1210

    15

    head[H]

    7

    2

    5 1

    1210

    15

    head[H]

    7

    2 5 1

    1210

    15

    head[H]

    7

    2

    a) b)

    c) d)

  • 8/3/2019 Tan Van - Binomial Heap Presentation

    12/30

    Unite Two Binomial Heaps

    This operation consists of the following steps

    Merge two binomial heaps. The resulting heap has the roots in increasing orderof degree

    For each tree in the binomial heap H, if it has the same order with another tree,link the two trees together such that the resulting tree obeys min-heap-order.

  • 8/3/2019 Tan Van - Binomial Heap Presentation

    13/30

    Unite Two Binomial Heaps

    11 1

    1210

    15

    head[H1]

    20

    24head[H2]

    9

    3a)

    11 1

    1210

    15

    head[H1]

    20

    2 3 4

    9

    b)

  • 8/3/2019 Tan Van - Binomial Heap Presentation

    14/30

    Unite Two Binomial Heaps

    11

    1

    1210

    15

    head[H1]

    20

    2

    3

    4

    9

    c)

    11

    1

    1210

    15

    20

    2

    3 4

    9

    head[H1]d)

  • 8/3/2019 Tan Van - Binomial Heap Presentation

    15/30

    Unite Two Binomial HeapsBinomial-Heap-Union(H1,H2)1 H

  • 8/3/2019 Tan Van - Binomial Heap Presentation

    16/30

    Unite Two Binomial Heaps

    Pseudocode:Binomial-Link(y,z)

    1 p[y]

  • 8/3/2019 Tan Van - Binomial Heap Presentation

    17/30

    Unite Two Binomial Heaps

    Binomial-Heap-Merge

    P H.Head; P1 H1.Head; P2 H2.Head

    while P1 not NIL and P2 not NIL do

    if P1.degree < P2.degree then

    P.sibling P1; P1 P1.sibling

    else

    P.sibling P2; P2 P2.sibling

    Run time: The running time is O (log n)

    The total number of combined roots is at most |_logH1_| + |_logH2_| +2.

    Binomial-Heap-Merge is O (log n) + the while loop is O (log n). Thus, thetotal time is O(log n).

  • 8/3/2019 Tan Van - Binomial Heap Presentation

    18/30

    Insert New Node

    Create a new heap H and set head[H] to the new node.

    Unite the new heap H with the existing heap H.

    Pseudocode:

    Binomial-Heap-Insert(H,x)

    1 H

  • 8/3/2019 Tan Van - Binomial Heap Presentation

    19/30

    Insert New Node Example

    1

    1210

    15

    5head[H]head[H]

    5New node:

    1

    1210

    15

    head[H] 5

  • 8/3/2019 Tan Van - Binomial Heap Presentation

    20/30

    Extract Node With Minimum Key

    This operation is started by finding and removing the node x with minimum

    key from the binomial heap H. Create a new binomial heap H and set tothe list of xs children in the reverse order. Unite H and H to get the

    resulting binomial heap.

    Pseudocode

    Binomial-Heap-Extract-Min(H)

    1 find the root x with the minimum key in the root list of H,

    and remove x from the root list of H.

    2 H

  • 8/3/2019 Tan Van - Binomial Heap Presentation

    21/30

    Extract Minimum Key Example

    5 1

    1210

    15

    head[H]

    7

    2

    1210

    15

    3

    1210

    15

    5 1

    1210

    15

    head[H]

    7

    2

    1210

    15

    3

    1210

    15

  • 8/3/2019 Tan Van - Binomial Heap Presentation

    22/30

    Extract Minimum Key Example

    5 12 10

    15

    head[H]

    7

    2

    1210

    15

    2

    1210

    15

    head[H]

    5

    7

    2

    1210

    15

    2

    1210

    15

    head[H]

    10

    15

    12

  • 8/3/2019 Tan Van - Binomial Heap Presentation

    23/30

    Decreasing a key

    The current key is replaced with a new key. To maintain the min-heap

    property, it is then compared to the key of the parent. If its parents key isgreater then the key and data will be exchanged. This process continuesuntil the new key is greater than the parents key or the new key is in theroot.

    Pseudocode:Binomial-Heap-Decrease-Key(H,x,k)

    1 if k > key[x]2 then error new key is greater than current key

    3 key[x]

  • 8/3/2019 Tan Van - Binomial Heap Presentation

    24/30

    Decreasing a key

    Execution time: This procedure takes O(log n) since the maximum depth ofx is |_log n_|.

    Example:

    5 2

    1210

    15

    head[H]5 2

    1210

    1

    head[H]

    5 2

    121

    10

    head[H] 5 1

    122

    10

    head[H]

  • 8/3/2019 Tan Van - Binomial Heap Presentation

    25/30

    Delete a Node

    With assumption that there is no node in H has a key of -.

    The key of deleting node is first decreased to -.

    This node is then deleted using extracting min procedure.

    Pseudocode: (from book)

    Binomial-Heap-Delete(H,x)

    1 Binomial-Heap-Decrease-Key(H,x,-)

    2 Binomial-Heap-Extract-Min(H)

    Run time: O(log n) since the run time of both Binomial-Heap-Decrease-Keyand Binomial-Heap-Extract-Min procedures are in order of O(log n).

  • 8/3/2019 Tan Van - Binomial Heap Presentation

    26/30

    Delete a Node Example

    5 2

    1210

    15

    head[H]5 2

    12-

    15

    head[H]

    5 -

    122

    15

    head[H] 5

    12 2

    15

    head[H]

    head[H]

    a) b)

    c) d)

  • 8/3/2019 Tan Van - Binomial Heap Presentation

    27/30

    Delete a Node Example

    5 12 2

    15

    head[H]5

    12

    2

    15

    head[H]

    5

    12

    2

    15

    head[H]

    e) f)

    g)

  • 8/3/2019 Tan Van - Binomial Heap Presentation

    28/30

    Compare With Binary Heap

    Procedure Binomial Heap Binary Heap

    Make-Heap O (1) O (1)

    Insert O (log n) O (log n)

    Minimum O (log n) O (1)

    Extract-Min O (log n) O (log n)

    Union O (log n) O (n)

    Decrease-Key O (log n) O (log n)

    Delete O (log n) O (log n)

  • 8/3/2019 Tan Van - Binomial Heap Presentation

    29/30

    Applications

    The binomial heap is used in an event-driven simulator.

    It is also used in the applications that need to find the minimum-spanning-tree such as electronic circuit design.

  • 8/3/2019 Tan Van - Binomial Heap Presentation

    30/30

    References

    Thomas H. Cormen, Charles E. Leiserson, Ronald L. Revest, and CliffordStein, Introduction To Algorithms, McGraw-Hill Higher Education, secondedition, 2001