3. various data structure (2)

Upload: neha-verma

Post on 04-Apr-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/29/2019 3. Various Data Structure (2)

    1/56

    B-TreesBinomial Heap

    Fibonacci Heap

  • 7/29/2019 3. Various Data Structure (2)

    2/56

    B-Trees

  • 7/29/2019 3. Various Data Structure (2)

    3/56

    Motivation for B-Trees

    Index structures for large datasets cannot be stored in main

    memory.

    Storing it on disk requires different approach to efficiency.

    Assuming that a disk spins at 3600 RPM, one revolution

    occurs in 1/60 of a second, or 16.7ms.

    Crudely speaking, one disk access takes about the same time

    as 200,000 instructions.

  • 7/29/2019 3. Various Data Structure (2)

    4/56

    Motivation (cont.)

    Assume that we use an AVL tree to store about 20 million

    records.

    We end up with a very deep binary tree with lots of different

    disk accesses; log2 20,000,000 is about 24, so this takes about0.2 seconds.

    We know we cant improve on the log n lowerbound on

    search for a binary tree.

    But, the solution is to use more branches and thus reduce theheight of the tree!

    As branching increases, depth decreases

  • 7/29/2019 3. Various Data Structure (2)

    5/56

    Definition of a B-tree

    A B-tree of order m is an m-way tree (i.e., a tree where each

    node may have up to m children) in which:

    1. the number of keys in each non-leaf node is one less than the number

    of its children and these keys partition the keys in the children in thefashion of a search tree.

    2. all leaves are on the same level.

    3. all non-leaf nodes except the root have at least m/ 2 children.

    4. the root is either a leaf node, or it has from 2 to m children.

    5. a leaf node contains no more than m1 keys.

    The number m should always be odd

  • 7/29/2019 3. Various Data Structure (2)

    6/56

    An example B-Tree

    51 6242

    6 12

    26

    55 60 7064 9045

    1 2 4 7 8 13 15 18 25

    27 29 46 48 53

    A B-tree oforder 5containing 26 items

    Note that all the leaves are at the same level

  • 7/29/2019 3. Various Data Structure (2)

    7/56

    Inserting into a B-Tree

    Attempt to insert the new key into a leaf:

    If this would result in that leaf becoming too big, split the leaf into two,

    promoting the middle key to the leafs parent.

    If this would result in the parent becoming too big, split the parent into

    two, promoting the middle key.

    This strategy might have to be repeated all the way to the top.

    If necessary, the root is split in two and the middle key is promoted to

    a new root, making the tree one level higher.

  • 7/29/2019 3. Various Data Structure (2)

    8/56

    Suppose we start with an empty B-tree and keys arrive in thefollowing order:1 12 8 2 25 5 14 28 17 7 52 16 48 683 26 29 53 55 45

    We want to construct a B-tree of order 5 The first four items go into the root:

    To put the fifth item in the root would violate condition 5 Therefore, when 25 arrives, pick the middle key to make a

    new root

    Constructing a B-tree

    1 2 8 12

  • 7/29/2019 3. Various Data Structure (2)

    9/56

    Constructing a B-tree (contd.)

    1 2

    8

    12 25

    6, 14, 28 get added to the leaf nodes:

    1 2

    8

    12 146 25 28

  • 7/29/2019 3. Various Data Structure (2)

    10/56

    Constructing a B-tree (contd.)

    Adding 17 to the right leaf node would over-fill it, so we take the

    middle key, promote it (to the root) and split the leaf

    8 17

    12 14 25 281 2 6

    7, 52, 16, 48 get added to the leaf nodes

    8 17

    12 14 25 281 2 6 16 48 527

  • 7/29/2019 3. Various Data Structure (2)

    11/56

    Constructing a B-tree (contd.)

    Adding 68 causes us to split the right most leaf, promoting 48 to the root, and

    adding 3 causes us to split the left most leaf, promoting 3 to the root; 26, 29, 53,

    55 then go into the leaves

    3 8 17 48

    52 53 55 6825 26 28 291 2 6 7 12 14 16

    Adding 45 causes a split of 25 26 28 29

    and promoting 28 to the root then causes the root to split

  • 7/29/2019 3. Various Data Structure (2)

    12/56

    Constructing a B-tree (contd.)

    17

    3 8 28 48

    1 2 6 7 12 14 16 52 53 55 6825 26 29 45

  • 7/29/2019 3. Various Data Structure (2)

    13/56

    Exercise in Inserting a B-Tree

    Insert the following keys to a 5-way B-tree:

    3, 7, 9, 23, 45, 1, 5, 14, 25, 24, 13, 11, 8, 19, 4, 31, 35, 56

  • 7/29/2019 3. Various Data Structure (2)

    14/56

    Removal from a B-tree

    During insertion, the key always goes into a leaf. Fordeletion we wish to remove from a leaf. There are threepossible ways we can do this:

    Case 1: - If the key is already in a leaf node, and removing itdoesnt cause that leaf node to have too few keys(m/ 2 -1),then simply remove the key to be deleted.

    Case 2: - If the key is notin a leaf then it is guaranteed (by thenature of a B-tree) that its predecessor or successor will be ina leaf -- in this case we can delete the key and promote thepredecessor or successor key to the non-leaf deleted keys

    position.

  • 7/29/2019 3. Various Data Structure (2)

    15/56

    Removal from a B-tree (2)

    If (1) or (2) lead to a leaf node containing less than the

    minimum number of keys then we have to look at the siblings

    immediately adjacent to the leaf in question:

    Case 3: if one of them has more than the min. number of keys then wecan promote one of its keys to the parent and take the parent key into

    our lacking leaf

    Case 4: if neither of them has more than the min. number of keys then

    the lacking leaf and one of its neighbours can be combined with their

    shared parent (the opposite of promoting a key) and the new leaf willhave the correct number of keys; if this step leave the parent with too

    few keys then we repeat the process up to the root itself, if required

  • 7/29/2019 3. Various Data Structure (2)

    16/56

    Type #1: Simple leaf deletion

    12 29 52

    2 7 9 15 22 56 69 7231 43

    Delete 2: Since there are enough

    keys in the node, just delete it

    Assuming a 5-way

    B-Tree, as before...

  • 7/29/2019 3. Various Data Structure (2)

    17/56

    Type #2: Simple non-leaf deletion

    12 29 52

    7 9 15 22 56 69 7231 43

    Delete 52

    Borrow the predecessor

    or (in this case) successor

    56

  • 7/29/2019 3. Various Data Structure (2)

    18/56

    Type #4: Too few keys in node andits siblings

    12 29 56

    7 9 15 22 69 7231 43

    Delete 72

    Too few keys!

    Join back together

  • 7/29/2019 3. Various Data Structure (2)

    19/56

    Type #4: Too few keys in node andits siblings

    12 29

    7 9 15 22 695631 43

  • 7/29/2019 3. Various Data Structure (2)

    20/56

    Type #3: Enough siblings

    12 29

    7 9 15 22 695631 43

    Delete 22

    Demote root key and

    promote leaf key

  • 7/29/2019 3. Various Data Structure (2)

    21/56

    Type #3: Enough siblings

    12

    297 9 15

    31

    695643

  • 7/29/2019 3. Various Data Structure (2)

    22/56

    Exercise in Removal from a B-Tree

    Given 5-way B-tree created by these data (last exercise):

    3, 7, 9, 23, 45, 1, 5, 14, 25, 24, 13, 11, 8, 19, 4, 31, 35, 56

    Add these further keys: 2, 6,12

    Delete these keys: 4, 5, 7, 3, 14

  • 7/29/2019 3. Various Data Structure (2)

    23/56

    Analysis of B-Trees

    The maximum number of items in a B-tree of order m and height h:

    root m1

    level 1 m(m1)

    level 2 m2(m1)

    . . .

    level h mh(m1)

    So, the total number of items is

    (1 + m + m2 + m3+ + mh)(m1) =

    [(mh+11)/ (m1)] (m1) =mh+1

    1

    When m = 5 and h = 2 this gives 531 = 124

  • 7/29/2019 3. Various Data Structure (2)

    24/56

    Reasons for using B-Trees

    When searching tables held on disc, the cost of each disctransfer is high but doesn't depend much on the amount ofdata transferred, especially if consecutive items are transferred

    If we use a B-tree of order 101, say, we can transfer each node in onedisc read operation

    A B-tree of order 101 and height 3 can hold 10141 items(approximately 100 million) and any item can be accessed with 3 discreads (assuming we hold the root in memory)

    If we take m = 3, we get a 2-3 tree, in which non-leaf nodeshave two or three children (i.e., one or two keys)

    B-Trees are always balanced (since the leaves are all at the samelevel), so 2-3 trees make a good type of balanced tree

  • 7/29/2019 3. Various Data Structure (2)

    25/56

    Comparing Trees

    Binary trees

    Can become unbalancedand lose their good time complexity (big O)

    AVL trees are strict binary trees that overcome the balance problem

    Heaps remain balanced but onlyprioritise (not order) the keys

    Multi-way trees

    B-Trees can be m-way, they can have any (odd) number of children

    One B-Tree, the 2-3 (or 3-way) B-Tree, approximates a permanentlybalanced binary tree, exchanging the AVL trees balancing operations

    for insertion and (more complex) deletion operations

  • 7/29/2019 3. Various Data Structure (2)

    26/56

    Binomial Heap

  • 7/29/2019 3. Various Data Structure (2)

    27/56

    Binomial Heaps

    A way to implement mergeable heaps.

    Useful in scheduling algorithms and graph algorithms.

  • 7/29/2019 3. Various Data Structure (2)

    28/56

    Definitions: Binomial Heap

    Collection of binomial trees (satisfying some properties).

  • 7/29/2019 3. Various Data Structure (2)

    29/56

    Definitions: Binomial Trees

    The binomial tree B(k) is an ordered tree defined recursively.

    order of children is important.

    1

    32

    1

    23Different Trees

    54 54

    The binomial treeB(k) consists of two binomial treesB(k-1) that

    are linkedtogether

  • 7/29/2019 3. Various Data Structure (2)

    30/56

    ExamplesB0

    B1

    B2

    B3

    B4

    depth # nodes

    0 1

    1 4

    2 6

    3 4

    4 1

  • 7/29/2019 3. Various Data Structure (2)

    31/56

    Another Way to Look at Bk

    Bk-1

    Bk-2B2

    B1B0

    Bk

  • 7/29/2019 3. Various Data Structure (2)

    32/56

    Properties of Binomial Trees

    Properties of binomial tree Bk

    it has 2knodes.

    the height of the tree is k.

    it has exactly Ckinodes at depth ifori= 0, 1,, k

    Cki= n! /(i! (ni) !)

    the root has degree k.

  • 7/29/2019 3. Various Data Structure (2)

    33/56

    Binomial Heaps - Definition

    Binomial heap H is an ordered list of binomial trees that

    satisfies the following properties:

    Each binomial tree is heap-ordered, i.e. for each node its

    key is greater or equal to the key of its parent.

    There is at most one binomial tree in H whose root has

    a given degree.

    Binomial trees are contained in the list in the order of

    increasing degrees.

  • 7/29/2019 3. Various Data Structure (2)

    34/56

    Binomial Heaps - Binomial Trees

    B0 B2B1 B3

  • 7/29/2019 3. Various Data Structure (2)

    35/56

    Binomial Heaps - Binomial Trees

    B4

  • 7/29/2019 3. Various Data Structure (2)

    36/56

    Binomial Heaps - Binomial Trees

    Bk

    Bk-1

    Bk-2

    B2B1

    B0

  • 7/29/2019 3. Various Data Structure (2)

    37/56

    Binomial Heaps - Example

    38

    14 29

    6

    27

    11 17

    8

    18

    12 25

    110

  • 7/29/2019 3. Various Data Structure (2)

    38/56

    Binomial Heaps - Union - Example

    17

    10 44

    6

    50

    48 31

    2937

    318

    24

    23 22

    8

    55

    45 32

    30

    25

    712

    33

    15

    41

    28

    H1 H2

  • 7/29/2019 3. Various Data Structure (2)

    39/56

    Binomial Heaps - Union - Example

    17

    10 44

    6

    50

    48 31

    2937

    318

    24

    23 22

    8

    55

    45 32

    30

    25

    712

    33

    15

    41

    28H1H2

  • 7/29/2019 3. Various Data Structure (2)

    40/56

    Binomial Heaps - Union - Example

    17

    10 44

    6

    50

    48 31

    2937

    3

    18

    24

    23 22

    8

    55

    45 32

    30

    25

    712

    33

    15

    41

    28

    H1H2

  • 7/29/2019 3. Various Data Structure (2)

    41/56

    Binomial Heaps - Union - Example

    17

    10 44

    6

    50

    48 31

    2937

    3

    18

    24

    23 22

    8

    55

    45 32

    3025

    7

    12

    33

    15

    41

    28

    H1H2

  • 7/29/2019 3. Various Data Structure (2)

    42/56

    Binomial Heaps - Union - Example

    17

    10 44

    6

    50

    48 31

    2937

    3

    18

    24

    23 22

    8

    55

    45 32

    3025

    7

    12

    33

    15

    41

    28

    H1H2

    A l

  • 7/29/2019 3. Various Data Structure (2)

    43/56

    Analogy

    37

    3

    50

    31 1748

    441029

    6

    55

    32 2445

    222330

    8

    33

    41

    28

    15

    25

    7

    head[H]

    18

    12

    prev-x x

    12

    33

    41

    28

    15

    25

    7head[H1] 18

    37

    3

    50

    31 1748

    441029

    6

    55

    32 2445

    222330

    8

    head[H2]

    Union

    Like binary addition:

    1 1 1(carries)

    0 0 1 1 1

    1 0 0 1 1

    1 1 0 1 0

    temporarily have three

    trees of this degree

    Bi i l H E Mi

  • 7/29/2019 3. Various Data Structure (2)

    44/56

    Binomial Heaps - ExtractMin -Example

    17

    10 44

    6

    50

    48 31

    2918

    24

    23 22

    8

    55

    45 32

    30

    12

    33

    15

    41

    28

    Bi i l H E t tMi

  • 7/29/2019 3. Various Data Structure (2)

    45/56

    Binomial Heaps - ExtractMin -Example

    17

    10 44

    6

    50

    48 31

    2918

    24

    23 22

    8

    55

    45 32

    30

    12

    33

    15

    41

    28

    Bi i l H E t tMi

  • 7/29/2019 3. Various Data Structure (2)

    46/56

    Binomial Heaps - ExtractMin -Example

    17

    1044

    50

    48 31

    29

    18

    24

    23 22

    8

    55

    45 32

    30

    12

    33

    15

    41

    28

    Bi i l H E t tMi

  • 7/29/2019 3. Various Data Structure (2)

    47/56

    Binomial Heaps - ExtractMin -Example

    17

    1044

    50

    48 31

    29

    18

    24

    23 22

    8

    55

    45 32

    30

    12

    33

    15

    41

    28

  • 7/29/2019 3. Various Data Structure (2)

    48/56

    Binomial Heaps - ExtractMin -

    Example

    17

    1044

    50

    48 31

    29

    18 24

    23 22

    8

    55

    45 32

    3012 33

    15

    41

    28

  • 7/29/2019 3. Various Data Structure (2)

    49/56

    Binomial Heaps - ExtractMin -

    Example

    17

    1044

    50

    48 31

    29

    18 24

    23 22

    8

    55

    45 32

    3012 33

    15

    41

    28

  • 7/29/2019 3. Various Data Structure (2)

    50/56

    Binomial Heaps - ExtractMin -

    Example

    17

    1044

    50

    48 31

    2918 24

    23 22

    8

    55

    45 32

    3012

    33

    15

    41

    28

    Bi i l H D K

  • 7/29/2019 3. Various Data Structure (2)

    51/56

    Binomial Heaps - DecreaseKey -Example

    17

    1044

    50

    48 31

    2918 24

    23 22

    8

    55

    45 32

    3012

    33

    15

    41

    28

    10

    Binomial Heaps DecreaseKe

  • 7/29/2019 3. Various Data Structure (2)

    52/56

    Binomial Heaps - DecreaseKey -Example

    17

    1044

    10

    48 31

    2918 24

    23 22

    8

    55

    45 32

    3012

    33

    15

    41

    28

    Binomial Heaps DecreaseKey

  • 7/29/2019 3. Various Data Structure (2)

    53/56

    Binomial Heaps - DecreaseKey -Example

    17

    1044

    48

    10 31

    2918 24

    23 22

    8

    55

    45 32

    3012

    33

    15

    41

    28

    Binomial Heaps DecreaseKey

  • 7/29/2019 3. Various Data Structure (2)

    54/56

    Binomial Heaps - DecreaseKey -Example

    17

    1044

    48

    29 31

    1018 24

    23 22

    8

    55

    45 32

    3012

    33

    15

    41

    28

    Binomial Heaps DecreaseKey

  • 7/29/2019 3. Various Data Structure (2)

    55/56

    Binomial Heaps - DecreaseKey -Example

    17

    1044

    48

    29 31

    1518 24

    23 22

    8

    55

    45 32

    3012

    33

    10

    41

    28

  • 7/29/2019 3. Various Data Structure (2)

    56/56

    Binomial Heaps - Summary

    Binomial Heaps Heaps

    Min (log n) or(1) (1)

    ExtractMin (log n) (log n)

    DecreaseKey (log n) (log n)

    Union (log n) (n)

    Insert (log n) (log n)

    Delete (log n) (log n)

    MakeEmpty (1) (1)

    IsEmpty (1) (1)