Presentation is loading. Please wait.

Presentation is loading. Please wait.

Binomial Tree B k-1 B0B0 BkBk B0B0 B1B1 B2B2 B3B3 B4B4 Adapted from: Kevin Wayne B k : a binomial tree B k-1 with the addition of a left child with another.

Similar presentations


Presentation on theme: "Binomial Tree B k-1 B0B0 BkBk B0B0 B1B1 B2B2 B3B3 B4B4 Adapted from: Kevin Wayne B k : a binomial tree B k-1 with the addition of a left child with another."— Presentation transcript:

1 Binomial Tree B k-1 B0B0 BkBk B0B0 B1B1 B2B2 B3B3 B4B4 Adapted from: Kevin Wayne B k : a binomial tree B k-1 with the addition of a left child with another binomial tree B k-1

2 Binomial Tree B0B0 B1B1 B2B2 B3B3 B4B4 B1B1 B k-1 BkBk B2B2 B0B0 Number of nodes with respect to k? N(B o ) = 1 N(B k ) = 2 N(B k-1 ) = 2 k

3 Binomial Tree B0B0 B1B1 B2B2 B3B3 B4B4 Height? H(B o ) = 1 H(B k ) = 1 + H(B k-1 ) = k B1B1 B k-1 BkBk B2B2 B0B0

4 Binomial Tree B0B0 B1B1 B2B2 B3B3 B4B4 Degree of root node? k, each time we add another binomial tree B1B1 B k-1 BkBk B2B2 B0B0

5 Binomial Tree B0B0 B1B1 B2B2 B3B3 B4B4 What are the children of the root? k binomial trees: B k-1, B k-2, …, B 0 B1B1 B k-1 BkBk B2B2 B0B0

6 Binomial Tree B4B4 depth 2 depth 3 depth 4 depth 0 depth 1 Why is it called a binomial tree?

7 Binomial Tree B k has nodes at depth i. B4B4 depth 2 depth 3 depth 4 depth 0 depth 1

8 Binomial Heap Binomial heap Vuillemin, 1978. Sequence of binomial trees that satisfy binomial heap property: – each tree is min-heap ordered – top level: full or empty binomial tree of order k – which are empty or full is based on the number of elements B4B4 B0B0 B1B1 55 45 32 30 24 2322 50 483117 44 82910 6 37 318

9 Binomial Heap Like our “set” data structure from last time, except binomial tree heaps instead of arrays B4B4 B0B0 B1B1 55 45 32 30 24 2322 50 483117 44 82910 6 37 318 A 0 : [18] A 1 : [3, 7] A 2 : empty A 3 : empty A 3 : [6, 8, 29, 10, 44, 30, 23, 22, 48, 31, 17, 45, 32, 24, 55] N = 19 # trees = 3 height = 4 binary = 10011

10 Binomial Heap: Properties B4B4 B0B0 B1B1 55 45 32 30 24 2322 50 483117 44 82910 6 37 318 N = 19 # trees = 3 height = 4 binary = 10011 How many heaps? O(log n) – binary number representation

11 Binomial Heap: Properties B4B4 B0B0 B1B1 55 45 32 30 24 2322 50 483117 44 82910 6 37 318 N = 19 # trees = 3 height = 4 binary = 10011 Where is the max/min? Must be one of the roots of the heaps

12 Binomial Heap: Properties B4B4 B0B0 B1B1 55 45 32 30 24 2322 50 483117 44 82910 6 37 318 N = 19 # trees = 3 height = 4 binary = 10011 Runtime of max/min? O(log n)

13 Binomial Heap: Properties B4B4 B0B0 B1B1 55 45 32 30 24 2322 50 483117 44 82910 6 37 318 N = 19 # trees = 3 height = 4 binary = 10011 Height? floor(log 2 n) - largest tree = B log n - height of that tree is log n

14 Binomial Heap: Union How can we merge two binomial tree heaps of the same size (2 k )? – connect roots of H' and H'' – choose smaller key to be root of H H'' 55 45 32 30 24 2322 50 483117 44 82910 6 H' Runtime? O(1)

15 Binomial Heap: Union 55 45 32 30 24 2322 50 483117 44 82910 6 37 318 33 28 15 25 712 How can we combine/merge binomial heaps (i.e. a combination of binomial tree heaps)?

16 Binomial Heap: Union 0011 1001+ 0111 11 1 1 0 1 19 + 7 = 26 55 45 32 30 24 2322 50 483117 44 82910 6 37 318 41 33 28 15 25 712 + Go through each tree size starting at 0 and merge as we go

17 Binomial Heap: Union 55 45 32 30 24 2322 50 483117 44 82910 6 37 318 41 33 28 15 25 712 +

18 Binomial Heap: Union 55 45 32 30 24 2322 50 483117 44 82910 6 37 3 41 33 28 15 25 7 + 12 18 12

19 55 45 32 30 24 2322 50 483117 44 82910 6 37 3 41 33 28 15 25 7 + 12 18 25 37 7 3 18 12 18 12

20 55 45 32 30 24 2322 50 483117 44 82910 6 37 3 41 33 28 15 25 7 12 + 18 25 37 7 3 41 283325 37 157 3 18 12 18 12

21 55 45 32 30 24 2322 50 483117 44 82910 6 37 3 41 33 28 15 25 7 + 18 12 41 283325 37 157 3 12 18 25 37 7 3 41 283325 37 157 3 18 12

22 55 45 32 30 24 2322 50 483117 44 82910 6 37 3 41 33 28 15 25 7 + 18 12 41 283325 37 157 3 12 18 25 37 7 3 41 283325 37 157 3 55 45 32 30 24 2322 50 483117 44 82910 6 18 12

23 Binomial Heap: Union Analogous to binary addition Running time? n Proportional to number of trees in root lists 2 O(log 2 N) n O(log N) 0011 1001+ 0111 11 1 1 0 1 19 + 7 = 26

24 3 37 618 55 45 32 30 24 2322 50 483117 44 82910 H Binomial Heap: Delete Min/Max We can find the min/max in O(log n). How can we extract it? Hint: B k consists of binomial trees: B k-1, B k-2, …, B 0

25 3 37 618 55 45 32 30 24 2322 50 483117 44 82910 H Binomial Heap: Delete Min Delete node with minimum key in binomial heap H. n Find root x with min key in root list of H, and delete n H'  broken binomial trees n H  Union(H', H)

26 3 37 618 55 45 32 30 24 2322 50 483117 44 82910 H Binomial Heap: Delete Min Delete node with minimum key in binomial heap H. n Find root x with min key in root list of H, and delete n H'  broken binomial trees n H  Union(H', H) Running time? O(log N)

27 3 37 618 55 x 32 30 24 2322 50 483117 44 82910 H Binomial Heap: Decrease Key Just call Decrease-Key/Increase-Key of Heap n Suppose x is in binomial tree B k n Bubble node x up the tree if x is too small Running time: O(log N) n Proportional to depth of node x depth = 3

28 Binomial Heap: Delete Delete node x in binomial heap H n Decrease key of x to -  n Delete min Running time: O(log N)

29 Binomial Heap: Insert Insert a new node x into binomial heap H n H'  MakeHeap(x) n H  Union(H', H) Running time. O(log N) 3 37 618 55 45 32 30 24 2322 50 483117 44 82910 H x H'

30 Build-Heap Call insert n times Runtime? Can we get a tighter bound? O(n log n)

31 Build-Heap Call insert n times Consider inserting n numbers n how many times will B 0 be empty? n how many times will we need to merge with B 0 ? n how many times will we need to merge with B 1 ? n how many times will we need to merge with B 2 ? n … n how many times will we need to merge with B log n ? timescost

32 Build-Heap Call insert n times Consider inserting n numbers n how many times will B 0 be empty? n/2O(1) n how many times will we need to merge with B 0 ? n/2O(1) n how many times will we need to merge with B 1 ? n/4O(1) n how many times will we need to merge with B 2 ? n/8O(1) n … n how many times will we need to merge with B log n ? 1O(1) timescost Runtime?Θ(n)

33 Heaps

34 Fibonacci Heaps Similar to binomial heap A Fibonacci heap consists of a sequence of heaps More flexible Heaps do not have to be binomial trees More complicated

35 Heaps Should you always use a Fibonacci heap?

36 Heaps Extract-Max and Delete are O(n) worst case Constants can be large on some of the operations Complicated to implement

37 Heaps Can we do better?


Download ppt "Binomial Tree B k-1 B0B0 BkBk B0B0 B1B1 B2B2 B3B3 B4B4 Adapted from: Kevin Wayne B k : a binomial tree B k-1 with the addition of a left child with another."

Similar presentations


Ads by Google