Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II."— Presentation transcript:

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

2 Tree Analysis Of Algorithms Trees-II2

3 Tree Motivation for B-Trees ….. We can store an entire data structure in main memory What if we have so much data that it wont fit? We will have to use disk storage but when this happens our time complexity fails The problem is that Big-Oh analysis assumes that all operations take roughly equal time This is not the case when disk access is involved Analysis Of Algorithms Trees-II3

4 Tree ….. Motivation……. Assume that a disk spins at 3600 RPM In 1 minute it makes 3600 revolutions, hence one revolution occurs in 1/60 of a second, or 16.7ms On average what we want is half way round this disk – it will take 8ms This sounds good until you realize that we get 120 disk accesses a second – the same time as 25 million instructions In other words, one disk access takes about the same time as 200,000 instructions It is worth executing lots of instructions to avoid a disk access Analysis Of Algorithms Trees-II4

5 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 cant improve on the log n for a binary tree But, the solution is to use more branches and thus less height! As branching increases, depth decreases Analysis Of Algorithms Trees-II5

6 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 keys The number m should always be odd Tree B-Tree

7 Analysis Of Algorithms Trees-II7 Tree 516242 612 26 5560 706490 45 1247813151825 2729464853 A B-tree of order 5 containing 26 items

8 Analysis Of Algorithms Trees-II8 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 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 12812 Tree Constructing a B-Tree …..

9 ….. Constructing a B-tree ….. Add 25 to the tree 1 12 82 25 6 142817 7 52164868 3 2629535545 1281225 Exceeds Order. Promote middle and split. Tree Analysis Of Algorithms Trees-II9

10 6, 14, 28 get added to the leaf nodes: 1 12 82 25 6 142817 7 52164868 3 2629535545 12 8 1225 12 8 1225 612 28 14 Tree ….. Constructing a B-tree ….. Analysis Of Algorithms Trees-II10

11 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 12 82 25 6 142817 7 52164868 3 2629535545 12 8 2 25 61 2 28 14 28 17 Tree ….. Constructing a B-tree ….. Analysis Of Algorithms Trees-II11

12 7, 52, 16, 48 get added to the leaf nodes 1 12 82 25 6 142817 7 52164868 3 2629535545 12 8 25 6 1 2 28 14 17 7 52 16 48 Tree ….. Constructing a B-tree ….. Analysis Of Algorithms Trees-II12

13 Adding 68 causes us to split the right most leaf, promoting 48 to the root 1 12 82 25 6 142817 7 52164868 3 2629535545 817 7 6 2 1 16 14 12 52 48 28 2568 Tree ….. Constructing a B-tree ….. Analysis Of Algorithms Trees-II13

14 Adding 3 causes us to split the left most leaf 1 12 82 25 6 142817 7 52164868 3 2629535545 48 17 8 7 6 2 1 16 14 12 2528 5268 3 7 Tree ….. Constructing a B-tree ….. Analysis Of Algorithms Trees-II14

15 1 12 82 25 6 142817 7 52164868 3 2629535545 Add 26, 29, 53, 55 then go into the leaves 48 17 8 3 1267 52682528 16 14 12 26 29 53 55 Tree ….. Constructing a B-tree ….. Analysis Of Algorithms Trees-II15

16 Add 45 increases the trees level 1 12 82 25 6 142817 7 52164868 3 2629535545 48 17 8 3 29 28 26 25 68 55 53 52 16 14 12 67 12 45 Exceeds Order. Promote middle and split. Tree ….. Constructing a B-tree ….. Analysis Of Algorithms Trees-II16

17 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 Tree Analysis Of Algorithms Trees-II17

18 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-II18

19 Answer to Exercise Tree Analysis Of Algorithms Trees-II19

20 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 doesnt 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 keys position. Tree Analysis Of Algorithms Trees-II20

21 ….. 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-II21

22 Type #1: Simple leaf deletion 122952 27915225669723143 Delete 2: Since there are enough keys in the node, just delete it Assuming a 5-way B-Tree, as before... Tree Analysis Of Algorithms Trees-II22

23 Type #2: Simple non-leaf deletion 122952 7915225669723143 Delete 52 Borrow the predecessor or (in this case) successor 56 Tree Analysis Of Algorithms Trees-II23

24 Type #4: Too few keys in node and its siblings 122956 79152269723143 Delete 72 Too few keys! Join back together Tree Analysis Of Algorithms Trees-II24

25 Type #4: Too few keys in node and its siblings 1229 79152269563143 Tree Analysis Of Algorithms Trees-II25

26 Type #3: Enough siblings 1229 79152269563143 Delete 22 Demote root key and promote leaf key Tree Analysis Of Algorithms Trees-II26

27 Type #3: Enough siblings 12 297915 31 695643 Tree Analysis Of Algorithms Trees-II27

28 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-II28

29 Answer to Exercise Tree Analysis Of Algorithms Trees-II29

30 Analysis of B-Trees The maximum number of items in a B-tree of order m and height h: rootm – 1 level 1m(m – 1) level 2m 2 (m – 1)... level hm h (m – 1) m h+1 – 1 So, the total number of items is (1 + m + m 2 + m 3 + … + m h )(m – 1) = [(m h+1 – 1)/ (m – 1)] (m – 1) = m h+1 – 1 When m = 5 and h = 2 this gives 5 3 – 1 = 124 Tree Analysis Of Algorithms Trees-II30

31 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 transferred If we use a B-tree of order 101, say, we can transfer each node in one disc read operation A 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-II31

32 Tree 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-II32

33 Tree Analysis Of Algorithms Trees-II33

34 Analysis Of Algorithms Trees-II34 Tree Binomial Trees ….. The binomial tree B k is an ordered tree defined recursively. B0B0 B1B1 Bo B2B2 B1

35 Analysis Of Algorithms Trees-II35 Tree B3B3 B2 B4B4 B3 ….. Binomial Trees …..

36 Tree Analysis Of Algorithms Trees-II36 ….. Binomial Trees Properties for tree B k : There are 2 k 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

37 Analysis Of Algorithms Trees-II37 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 …..

38 Tree Analysis Of Algorithms Trees-II38 ….. Binomial Heaps ….. An n node binomial heap consists of at most Floor(log n) + 1 binomial trees.

39 Analysis Of Algorithms Trees-II39 Tree 9 78 6 4 32 1 B 3 3 bits 000 111 110 101 100 011 001 010 4 B 0 0 bits 4 2 0 1 B 1 1 bits 4 32 1 00 01 10 11 B 2 2 bits How many binary bits are needed to count the nodes in any given Binomial Tree? Answer: k for B k, where k is the degree of the root. ….. Binomial Heaps …..

40 Analysis Of Algorithms Trees-II40 Tree 4 2 4 32 1 9 87 6 4 39 1 Head ….. Binomial Heaps ….. There are 14 nodes, which is 1110 in binary. This also can be written as, 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!

41 Analysis Of Algorithms Trees-II41 Tree Node Representation ….. Binomial Heaps …..

42 Analysis Of Algorithms Trees-II42 Tree ….. Binomial Heaps ….. Binomial Min-Heap Walk across roots, find minimum O(log n) since at most log n + 1 trees 4 2 4 32 1 9 87 6 6 49 3 Head

43 Analysis Of Algorithms Trees-II43 Tree ….. Binomial Heaps ….. Binomial-Link(y,z) 1.p[y] z 2.sibling[y] child[z] 3.child[z] y 4.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. z y z y z yy z (1) Runtime

44 Analysis Of Algorithms Trees-II44 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 degree Walk 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)

45 Analysis Of Algorithms Trees-II45 Tree ….. Binomial Heaps ….. Starting with the following two binomial heaps: 69 5819 18 93 8060 Merge root lists, but now we have two trees of same degree 53 3263 2 69 5819 18 93 80 60 53 3263 2 Combine trees of same degree using binomial link, make smaller key the root of the combined tree 53 3263 2 69 5819 18 93 80 60

46 Analysis Of Algorithms Trees-II46 Tree ….. Binomial Heaps ….. 12 715 28 33 41 25 37 3 6 441029 8 17 31 48 22 23 32 24 45 55 18 30 50 head[H 1 ]head[H 2 ] Merge Heap H1 and H2

47 Analysis Of Algorithms Trees-II47 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 heap H Make-Binomial-Heap() p[x] NIL child[x] NIL sibling[x] NIL degree[x] 0 head[H] x H Binomial-Heap-Union(H, H) Runtime: O(log n)

48 Analysis Of Algorithms Trees-II48 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 roots extraction Reversal of roots, and combining into new heap H Runtime: Θ(log n)

49 Analysis Of Algorithms Trees-II49 Tree ….. Binomial Heaps ….. 13 10 1 251216 6 18 23 26 29 14 17 38 11 27 41 8 42 head[H] 37 28 77 Extract-Min(H)

50 Analysis Of Algorithms Trees-II50 Tree ….. Binomial Heaps ….. Heap Decrease Key Same as decrease-key for a binary heap Move the element upward, swapping values, until we reach a position where the value is key of its parent Runtime: Θ(log n) Heap Delete Key Set key of node to delete to –infinity and extract it: Decrease-key(H, p, -) Extract-min(H) Runtime: Θ(log n)

51 Analysis Of Algorithms Trees-II51 Tree 25 6 29 14 17 38 11 27 8 12 18 41 37 head[H] 16 23 26 42 13 10 28 77 ….. Binomial Heaps Decrease key 26 to 7

52 End Trees-II 52Analysis Of Algorithms Trees-II


Download ppt "Trees-II Analysis of Algorithms 1Analysis Of Algorithms Trees-II."

Similar presentations


Ads by Google