Top Banner
Trees- II Analysis of Algorithms 1 Analysis Of Algorithms Trees-II
52

Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II.

Mar 26, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II.

Trees-II

Analysis of Algorithms

1Analysis Of Algorithms Trees-II

ASH
h cd shcd ahs cah sdcs c
Page 2: Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II.

Tree

Analysis Of Algorithms Trees-II 2

Page 3: Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II.

Tree

Motivation for B-Trees …..

We can store an entire data structure in main memoryWhat if we have so much data that it won’t fit?We will have to use disk storage but when this happens our time complexity failsThe problem is that Big-Oh analysis assumes that all operations take roughly equal timeThis is not the case when disk access is involved

Analysis Of Algorithms Trees-II 3

Page 4: Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II.

Tree

….. Motivation…….Assume that a disk spins at 3600 RPMIn 1 minute it makes 3600 revolutions, hence one revolution occurs in 1/60 of a second, or 16.7msOn average what we want is half way round this disk – it will take 8msThis sounds good until you realize that we get 120 disk accesses a second – the same time as 25 million instructionsIn other words, one disk access takes about the same time as 200,000 instructionsIt is worth executing lots of instructions to avoid a disk access

Analysis Of Algorithms Trees-II 4

Page 5: Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II.

Tree

….. Motivation

Assume that we use an Binary tree to store all the details of people in Canada (about 32 million records)We still end up with a very deep tree with lots of different disk accesses; log2 20,000,000 is about 25, so this takes about 0.21 seconds (if there is only one user of the program)We know we can’t improve on the log n for a binary treeBut, the solution is to use more branches and thus less height!As branching increases, depth decreases

Analysis Of Algorithms Trees-II 5

Page 6: Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II.

Analysis Of Algorithms Trees-II 6

A B-tree of order m is a tree where each node may have up to m children in which:

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 the fashion of a search tree all leaves are on the same level all non-leaf nodes except the root have at least m / 2 children the root is either a leaf node, or it has from two to m children a leaf node contains no more than m – 1 keysThe number m should always be odd

Tree

B-Tree

Page 7: Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II.

Analysis Of Algorithms Trees-II 7

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 of order 5 containing 26 items

Page 8: Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II.

Analysis Of Algorithms Trees-II 8

Suppose we start with an empty B-tree and keys arrive in the following order:1 12 8 2 25 6 14 28 17 7 52 16 48 68 3 26 29 53 55 45We want to construct a B-tree of order 5The first four items go into the root:

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

12128811 22

Tree

Constructing a B-Tree …..

Page 9: Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II.

….. Constructing a B-tree …..

Add 25 to the tree

1 1

2 8 2 2

5 6 1

4 2

8 1

7 7 5

2 1

6 4

8 6

8 3 2

6 2

9 5

3 5

5 45

12128811 22 2525

Exceeds Order. Promote middle and split.

Tree

Analysis Of Algorithms Trees-II 9

Page 10: Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II.

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

1 1

2 8 2 2

5 6 1

4 2

8 1

7 7 5

2 1

6 4

8 6

8 3 2

6 2

9 5

3 5

5 45

1212

88

11 22 2525

1212

88

11 22 25256611 22 28281414

Tree

….. Constructing a B-tree …..

Analysis Of Algorithms Trees-II 10

Page 11: Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II.

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

1 1

2 8 2 2

5 6 1

4 2

8 1

7 7 5

2 1

6 4

8 6

8 3 2

6 2

9 5

3 5

5 45

1 1

2 8 2 2

5 6 1

4 2

8 1

7 7 5

2 1

6 4

8 6

8 3 2

6 2

9 5

3 5

5 45

1212

88

22 25256611 22 28281414 28281717

Tree

….. Constructing a B-tree …..

Analysis Of Algorithms Trees-II 11

Page 12: Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II.

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

1 1

2 8 2 2

5 6 1

4 2

8 1

7 7 5

2 1

6 4

8 6

8 3 2

6 2

9 5

3 5

5 45

1212

88

25256611 22 28281414

1717

77 52521616 4848

Tree

….. Constructing a B-tree …..

Analysis Of Algorithms Trees-II 12

Page 13: Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II.

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

1 1

2 8 2 2

5 6 1

4 2

8 1

7 7 5

2 1

6 4

8 6

8 3 2

6 2

9 5

3 5

5 45

88 1717

77662211 161614141212 5252484828282525 6868

Tree

….. Constructing a B-tree …..

Analysis Of Algorithms Trees-II 13

Page 14: Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II.

Adding 3 causes us to split the left most leaf

1 1

2 8 2 2

5 6 1

4 2

8 1

7 7 5

2 1

6 4

8 6

8 3 2

6 2

9 5

3 5

5 45

4848171788

77662211 161614141212 2525 2828 5252 686833 77

Tree

….. Constructing a B-tree …..

Analysis Of Algorithms Trees-II 14

Page 15: Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II.

1 1

2 8 2 2

5 6 1

4 2

8 1

7 7 5

2 1

6 4

8 6

8 3 2

6 2

9 5

3 5

5 45

Add 26, 29, 53, 55 then go into the leaves

484817178833

11 22 66 77 5252 68682525 2828161614141212 2626 2929 5353 5555

Tree

….. Constructing a B-tree …..

Analysis Of Algorithms Trees-II 15

Page 16: Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II.

Add 45 increases the trees level

1 1

2 8 2 2

5 6 1

4 2

8 1

7 7 5

2 1

6 4

8 6

8 3 2

6 2

9 5

3 5

5 45

484817178833

2929282826262525 686855555353525216161414121266 7711 22 4545

Exceeds Order. Promote middle and split.

Exceeds Order. Promote middle and split.

Tree

….. Constructing a B-tree …..

Analysis Of Algorithms Trees-II 16

Page 17: Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II.

Inserting into a B-Tree

Attempt to insert the new key into a leafIf this would result in that leaf becoming too big, split the leaf into two, promoting the middle key to the leaf’s parentIf this would result in the parent becoming too big, split the parent into two, promoting the middle keyThis strategy might have to be repeated all the way to the topIf necessary, the root is split in two and the middle key is promoted to a new root, making the tree one level higher

Tree

Analysis Of Algorithms Trees-II 17

Page 18: Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II.

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

Tree

Analysis Of Algorithms Trees-II 18

Page 19: Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II.

Answer to Exercise

Tree

Analysis Of Algorithms Trees-II 19

Page 20: Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II.

Removal from a B-tree …..

During insertion, the key always goes into a leaf. For deletion we wish to remove from a leaf. There are three possible ways we can do this:1: If the key is already in a leaf node, and removing it doesn’t cause that leaf node to have too few keys, then simply remove the key to be deleted.2: If the key is not in a leaf then it is guaranteed (by the nature of a B-tree) that its predecessor or successor will be in a leaf -- in this case can we delete the key and promote the predecessor or successor key to the non-leaf deleted key’s position.

Tree

Analysis Of Algorithms Trees-II 20

Page 21: Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II.

….. Removal from a B-tree

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: 3: if one of them has more than the min’ number of keys then we can promote one of its keys to the parent and take the parent key into our lacking leaf 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 will have 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

Tree

Analysis Of Algorithms Trees-II 21

Page 22: Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II.

Type #1: Simple leaf deletion

1212 2929 5252

22 77 99 1515 2222 5656 6969 72723131 4343

Delete 2: Since there are enoughkeys in the node, just delete it

Assuming a 5-wayB-Tree, as before...

Tree

Analysis Of Algorithms Trees-II 22

Page 23: Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II.

Type #2: Simple non-leaf deletion

1212 2929 5252

77 99 1515 2222 5656 6969 72723131 4343

Delete 52

Borrow the predecessoror (in this case) successor

5656

Tree

Analysis Of Algorithms Trees-II 23

Page 24: Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II.

Type #4: Too few keys in node and its siblings

1212 2929 5656

77 99 1515 2222 6969 72723131 4343

Delete 72Too few keys!

Join back together

Tree

Analysis Of Algorithms Trees-II 24

Page 25: Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II.

Type #4: Too few keys in node and its siblings

1212 2929

77 99 1515 2222 696956563131 4343

Tree

Analysis Of Algorithms Trees-II 25

Page 26: Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II.

Type #3: Enough siblings

1212 2929

77 99 1515 2222 696956563131 4343

Delete 22

Demote root key andpromote leaf key

Tree

Analysis Of Algorithms Trees-II 26

Page 27: Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II.

Type #3: Enough siblings

1212

292977 99 1515

3131

696956564343

Tree

Analysis Of Algorithms Trees-II 27

Page 28: Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II.

Exercise in Removal from a B-Tree

Given 5-way B-tree created by these data 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

Tree

Analysis Of Algorithms Trees-II 28

Page 29: Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II.

Answer to Exercise

Tree

Analysis Of Algorithms Trees-II 29

Page 30: Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II.

Analysis of B-Trees

• The maximum number of items in a B-tree of order m and height h:root m – 1level 1 m(m – 1)level 2 m2(m – 1). . .level h mh(m – 1)

• So, the total number of items is(1 + m + m2 + m3 + … + mh)(m – 1) =[(mh+1 – 1)/ (m – 1)] (m – 1) = mmhh+1+1 – 1 – 1

• When m = 5 and h = 2 this gives 53 – 1 = 124

Tree

Analysis Of Algorithms Trees-II 30

Page 31: Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II.

Reasons for using B-Trees

When searching tables held on disc, the cost of each disc transfer is high but doesn't depend much on the amount of data transferred, especially if consecutive items are transferredIf we use a B-tree of order 101, say, we can transfer each node in one disc read operationA B-tree of order 101 and height 3 can hold 1014 – 1 items (approximately 100 million) and any item can be accessed with 3 disc reads (assuming we hold the root in memory)If we take m = 3, we get a 2-3 tree, in which non-leaf nodes have two or three children (i.e., one or two keys)B-Trees are always balanced (since the leaves are all at the same level), so 2-3 trees make a good type of balanced tree

Tree

Analysis Of Algorithms Trees-II 31

Page 32: Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II.

TreeTree

B-Trees:B-Trees: widely used for file systems and databases

Windows: HPFS.Mac: HFS, HFS+.Linux: ReiserFS, XFS, Ext3FS, JFS.Databases: ORACLE, DB2, INGRES, SQL, PostgreSQL

Analysis Of Algorithms Trees-II 32

Page 33: Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II.

Tree

Analysis Of Algorithms Trees-II 33

Page 34: Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II.

Analysis Of Algorithms Trees-II 34

Tree

Binomial Trees …..

The binomial tree Bk is an ordered tree defined recursively.

B0

B1

Bo

Bo

B2

B1

B1

Page 35: Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II.

Analysis Of Algorithms Trees-II 35

Tree

B3

B2

B2

B4

B3

B3

….. Binomial Trees …..

Page 36: Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II.

Tree

Analysis Of Algorithms Trees-II 36

….. Binomial Trees

Properties for tree Bk: There are 2k nodes The height of the tree is k The number of nodes at depth i for i = 0…k is

The root has degree k which is greater than any other node

Page 37: Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II.

Analysis Of Algorithms Trees-II 37

Tree

A binomial heap H is a set of binomials trees that satisfies the following binomial-heap properties:Each binomial tree in H obeys the min-heap property. For any nonnegative integer k, there is at most one binomial tree in H whose root has degree k.Binomial trees will be joined by a linked list of the roots

Binomial Heaps …..

Page 38: Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II.

Tree

Analysis Of Algorithms Trees-II 38

….. Binomial Heaps …..

An n node binomial heap consists of at most Floor(log n) + 1 binomial trees.

Page 39: Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II.

Analysis Of Algorithms Trees-II 39

Tree

9

7 8

6

4

3 2

1B3 3 bits

000

111

110101

100

011001010

4B0 0 bits

4

2

0

1B1 1 bits

4

3 2

1

00

01 10

11B2 2 bits

How many binary bits are needed to count the nodes in any given Binomial Tree? Answer: k for Bk, where k is the degree of the root.

….. Binomial Heaps …..

Page 40: Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II.

Analysis Of Algorithms Trees-II 40

Tree

<1110> 4

2

4

3 2

1

9

8 7

6

4

3 9

1Head

….. Binomial Heaps …..

There are 14 nodes, which is 1110 in binary. This also can be written as <1110>, which means there is no B0, one B1, one B2 and one B3. There is a corresponding set of trees for a heap of any size!

Page 41: Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II.

Analysis Of Algorithms Trees-II 41

Tree

Node Representation

….. Binomial Heaps …..

Page 42: Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II.

Analysis Of Algorithms Trees-II 42

Tree

….. Binomial Heaps …..

Binomial Min-HeapWalk across roots, find minimum O(log n) since at most log n + 1 trees

4

2

4

3 2

1

9

8 7

6

6

4 9

3Head

Page 43: Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II.

Analysis Of Algorithms Trees-II 43

Tree

….. Binomial Heaps …..

Binomial-Link(y,z)1. p[y] z2. sibling[y] child[z]3. child[z] y4. degree[z] degree[z] + 1

Link binomial trees with the same degree. Note that z, the second argument to BL(), becomes the parent, and y becomes the child.

zy

z

y

z

y

y

z

(1) Runtime

Page 44: Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II.

Analysis Of Algorithms Trees-II 44

Tree

….. Binomial Heaps …..Binomial-Heap-Union(H1, H2)

H Binomial-Heap-Merge(H1, H2)This merges the root lists of H1 and H2 in increasing order of root degreeWalk across the merged root list, merging binomial trees of equal degree. If there are three such trees in a row only merge the last two together (to maintain property of increasing order of root degree as we walk the roots)

Runtime: Merge time plus Walk Time: O(log n)

Page 45: Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II.

Analysis Of Algorithms Trees-II 45

Tree

….. Binomial Heaps …..

Starting with the following two binomial heaps:

69

58 19

18

93

8060

Merge root lists, but now we have two trees of same degree

53

32 63

2

69

58 19

18

93

8060

53

32 63

2

Combine trees of same degree using binomial link, make smaller key the root of the combined tree

53

32 63

2

69

58 19

1893

8060

Page 46: Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II.

Analysis Of Algorithms Trees-II 46

Tree

….. Binomial Heaps …..

12 7 15

28 33

41

25 37

3 6

4410298

1731482223

32 2445

55

18

30

50

head[H1] head[H2]

Merge Heap H1 and H2

Page 47: Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II.

Analysis Of Algorithms Trees-II 47

Tree

….. Binomial Heaps …..Binomial-Heap-Insert(H)

To insert a new node, simple create a new Binomial Heap with one node (the one to insert) and then Union it with the heapH’ Make-Binomial-Heap()p[x] NILchild[x] NILsibling[x] NILdegree[x] 0head[H’] xH Binomial-Heap-Union(H, H’)

Runtime: O(log n)

Page 48: Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II.

Analysis Of Algorithms Trees-II 48

Tree

….. Binomial Heaps …..

Binomial-Heap-Extract-Min(H)With a min-heap, the root has the least value in the heap.Notice that if we remove the root from the figure below, we are left with four heaps, and they are in decreasing order of degree. So to extract the min we create a root list of the children of the node being extracted, but do so in reverse order. Then call Binomial-Heap-Union(..)Part of original heap showing

binomial tree with minimal root.

Broken into separate Binomial Trees after root’s extraction

Reversal of roots, and

combining into new heap

H

Runtime: Θ(log n)

Page 49: Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II.

Analysis Of Algorithms Trees-II 49

Tree

….. Binomial Heaps …..

13

10 1

2512166

1823262914

17 3811

27

41

8

42

head[H] 37

28

77

Extract-Min(H)

Page 50: Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II.

Analysis Of Algorithms Trees-II 50

Tree

….. Binomial Heaps …..Heap Decrease Key

Same as decrease-key for a binary heapMove the element upward, swapping values, until we reach a position where the value is key of its parent

Runtime: Θ(log n)

Heap Delete KeySet key of node to delete to –infinity and extract it:

Decrease-key(H, p, -∞)Extract-min(H)

Runtime: Θ(log n)

Page 51: Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II.

Analysis Of Algorithms Trees-II 51

Tree

25 6

2914

17 3811

27

8

12

18

41

37

head[H]

16

2326

42

13

10

28

77

….. Binomial Heaps

Decrease key 26 to 7

Page 52: Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II.

52Analysis Of Algorithms Trees-II