1 / 42

Algorithms and data structures

Algorithms and data structures. Amortized analysis Binominal trees Binominal heaps Fibonacci heaps. Amortized analysis. Average (expected) cost – requires considering all the input data (with possible distribution) and analysis of execution time

quinney
Download Presentation

Algorithms and data structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Algorithmsand data structures Amortized analysis Binominal trees Binominal heaps Fibonacci heaps

  2. Amortized analysis Average (expected)cost– requires considering all the input data (with possible distribution) and analysis of execution time Amortized analysis – there is considered a required sequence of operations and on that basis could be determined the cost of unit operation Example: generalised stack operations

  3. Stacks – short summary Binary heaps* / binominal h. * / fibonacci h.** • Insertion  (log n) / O(log n) / (1) • Finding the minimal node (1) / O(log n) / (1) • Removing the minimal node (log n) / (log n) / O(log n) • Joining – (n) / O(log n) / (1) • Decreasing element key (log n) / (log n) / (1) • Removing any node (log n) / (log n) / O(log n) * Worst case **Amortized analysis

  4. Binominal trees Definition: Bk-1 B0 Bk-1 Bk B0 B1 B2 B3 B4

  5. Binominal trees – Bk properties . . . . B0 B1 B2 Bk-2 Bk-1 Bk • Number of nodes = 2k • Height = k • There are nodes on the level i • The degree of the root-node is k and is the biggest in the tree k i

  6. Binominal heap 6 1 8 14 29 12 25 11 17 38 18 27 • Each binominal tree in H heap satisfies the minimum-heap property (key in the node is not less than fathers key) • For each d  0 there exist at most on tree with root-degree = d in heap H and trees are ordered with increasing order of the trees • All the trees are represented as left-child right-sibling notion H 10

  7. Binominal heap - definition class BNNODE : key = None degree = 0child = None sibling = Noneparent = None data = None root = None

  8. Binominal heap - Min def MinBNHeap(heap):min = heap current = heapwhile current != None:if current.key < min.key:min = currentcurrent = current.siblingreturn min

  9. Binominal heap - TreeLink 11 12 12 11 19 18 18 19 z’ y z + = Bz Bz By By z y z’ 1 4 1 + = 25 27 4 25 27

  10. Binominal heap - TreeLink def LinkBNTree(newSon, newParent):newSon.parent = newParent newSon.sibling = newParent.childnewParent.child = newSonnewParent.degree = newParent.degree +1

  11. Binominal heap - Union Merge list of trees for both joined heaps (order considering the nodes degrees should be kept) Merge (link) trees while there are trees with duplicated degrees

  12. Binominal heap - Union 3 10 17 28 28 11 45 17 48 3 10 48 11 45 17 3 17 41 27 24 50 25 17 25 41 51 17 50 24 25 51 27 25 27 12 15 18 6 33 8 29 9 30 22 31 35 12 15 18 6 33 8 29 9 30 22 31 35 12 3 12 18 27 18

  13. Binominal heap - Union 11 17 28 45 48 45 48 10 28 11 17 10 17 41 24 25 25 24 41 17 51 50 50 51 12 3 15 6 18 27 33 8 29 9 30 22 31 35 12 3 6 18 27 15 8 29 9 33 30 22 31 35

  14. Union - cases X NextX a a b c b d Bk Bl Bm 1 Skip the tree i.e. X = NextX, NextX = NextX.sibling l > k 2 l = k = m Skip the tree i.e. X = NextX, NextX = NextX.sibling 3 l = k < m, a<=b TreeLink(NextX, X) 4 TreeLink(X, NextX) l = k < m, a>b

  15. Union - implementation def MergeBNHeap(h1, h2): ret = Nonecurrent = Nonewhile h1 != None and h2 != None:if h1.degree <= h2.degree: next, h1 = h1, h1.siblingelse: next, h2 = h2, h2.siblingifret == None: ret, current = next, nextelse: current.sibling, current = next, next if h1 == None: h1,h2=h2,h1ifret == None: return h1else: current.sibling = h1 return ret

  16. Union - implementation def UnionBNHeap(y, z):ret = MergeBNHeap(y, z)if ret == None: return NoneprevX=NoneX=retnextX= ret.sibling while nextX != None:#cases 1..4 return ret

  17. Union – implementation cont. if X.degree != nextX.degree or \ #case 1 (nextX.sibling != None and \ #case 2 nextX.degree == nextX.sibling.degree): prevX, X, nextX = X, nextX, nextX.sibling elifX.key <= nextX.key: #case 3 X.sibling = nextX.sibling nextX.sibling = None LinkBNTree(nextX, X) nextX = X.sibling else: # case 4 if prevX==None: ret = nextX else: prevX.sibling = nextX X.sibling = None LinkBNTree(X, nextX) X, nextX = nextX, nextX.sibling

  18. Binominal heap - Insert def InsertBNHeap(heap, key, data) node= BNNODE()#node.sibling = node.parent = node.child = None#node.degree = 0 node.key = key node.data = data return UnionBNHeap(heap, node)

  19. Binominal heap - ExtractMin 13 13 40 10 19 42 42 10 19 40 37 51 24 50 17 17 51 24 37 50 18 7 8 29 21 30 11 32 32 18 21 29 8 + 32 30 11 32

  20. ExtractMin - Implementation For nimimal node remove the tree that contains the MIN node from heap H Create the new heap H’ from sons of MIN Execute Union on H i H’ def ExtractMinBNHeap(heap):ret = MinBNHeap(heap)usun_ret_z_listy_korzeni_w_heaph1 = odwroc_kolejnosc_synow_ret_i_zwroc_ich_listeheap = UnionBNHeap(heap, h1)ret.sibling= Noneret.child = Nonereturn heap, ret

  21. Binominal heap – Decr. Key 10 3 10 23 3 48 48 23 23 48 3 10 15 15 24 15 15 24 15 24 15 50 50 50 18 4 8 21 40 30 22 31 7 45 32 18 4 55 8 21 40 7 22 31 30 32 55 18 4 7 21 40 8 22 31 30 32 55

  22. Decrease Key - Implementation Change the key of the node W while W is smaller than his parent:exchange content od W and his parent set W = parent of W def DecKeyBNHeap(node, newKey):if newKey > node.key: ERRORnode.key = newKey whilenode.parent!=Noneand node.key<node.parent.key:node.key, node.parent.key = node.parent.key, node.keynode.data,node.parent.data=node.parent.data,node.data node = node.parent

  23. Remove Node - Implementation def RemoveBNHeap(heap, node):tmp = node.keyDecKeyBNHeap(node, MINKEYVALUE)heap,node = ExtractMinBNHeap(heap)node.key = tmpreturn heap,node MINKEYVALUE has to be smallerthanall the keys (we can assume the boundary value or the Min can be determined and used)

  24. Fibonacci Heap Close to binominal heaps but are not so restricted. • Amortized insertion time – O(1) • Amortized min search time– O(1) • Amortized join time – O(1) • Amortized dec key time – O(1) • Amortized ExtractMin (and remove) time O(log n) • Amortized analysis does not require considering of the distribution.

  25. Fibonacci heap • Each binominal tree in H heap satisfy the minimum-heap property (key in the node is not less than fathers key) • Root of H points to Min node • Trees are not ordered • Notion for particular trees: cyclic lists for particular levels and pointer to any child (list joining = O(1)) • Flag for some nodes (flag is set when a node lost any child and has a parent – i.e. is a child itself) • Max degree in the tree D(n) = logf n; f=(1+5) /2 HF 11 3 24 6 17 26 40 31 18 52 38 30 41 35

  26. Joining of cyclic lists prev prev prev prev prev prev prev data data data data data data data next next next next next next next def JoincycleLists(L1, L2):if L1==None: return L2 if L2==None: return L1if L1.next==L1 : L1.prev = L2.prev #L1 miala 1 el. else: L1.next.prev = L2.prev if L2.prev==L2 : L2.next = L1.next #L2 miala 1 el. else: L2.prev.next = L1.next L1.next,L2.prev = L2, L1 L2 L1 L2'

  27. Fibonacci heap - definition class HFNODE: key=None degree=0 child=None left=None right=Noneparent=None marked=Falsedata=NoneHF=Nonesize=0 HF 11 3 24 6 17 26 40 31 18 52 38 30 41 35

  28. Fibonacci heap - Insert 3 3 17 17 38 38 18 18 52 52 31 31 41 30 41 30 • Added node becames a separate tree (thus flag is not set) HF + 11 24 21 6 26 46 35 HF 11 24 6 21 26 46 35

  29. Fibonacci heap - Insert def InsertFibHeap(heap, key, size, data):node= PHFNODE()#node.parent = node.child = NULL#node.marked = falsenode.left = nodenode.right = nodenode.key = keynode.data = dataheap = JoinCycleLists( heap, node )size = size+1if heap.key<node.key: return heap,size else: return node,size

  30. Fibonacci heap - Union def UnionFibHeap (heap1, heap2,size1, size2):heap = JoinCycleLists( heap1, heap1) size = size1 + size2 if heap1.key < heap2.key: return heap1,size else return heap2,size

  31. Fibonacci heap - ExtractMin Move all children of Min to H while there exist duplicated degrees of trees in FH pick two trees with same degree let x->key <= y->key removey from list of trees in H add y do children of x increase deg(x) Consolidate: FibHeapLink:

  32. Fibonacci heap - ExtractMin 0 0 0 0 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 3 17 17 17 17 17 17 38 18 38 18 38 18 18 38 38 18 18 38 52 31 31 31 31 31 31 41 30 30 41 41 30 30 41 30 41 41 30 HF 11 24 6 21 11 24 6 21 52 26 46 26 46 35 35 w,x x 11 24 6 24 6 21 21 52 52 w 26 46 26 46 11 35 35 x 6 52 21 x 6 24 w 21 52 24 11 26 46 w 11 26 46 35 35

  33. Fibonacci heap - ExtractMin 0 0 0 1 1 1 2 2 2 3 3 3 4 4 4 11 11 11 38 38 38 31 31 31 41 41 41 Clear the flag w,x w,x 6 18 52 6 18 21 24 24 21 30 30 17 17 52 26 46 26 46 35 35 HF w,x 6 18 24 21 30 17 52 26 46 35

  34. Fibonacci heap - ExtractMin def ExtractMinFibHeap(heap, size):min = heapif min != None: heap = RemoveFromCycleList(heap, min) for c in all_children_of_min c.parent = None heap = JoinCycleList(heap, min.child) size = size-1 if heap != heap.left: heap = Consolidate(heap) return heap, size, min

  35. Fibonacci heap - Consolidate def Consolidate(heap, size):A = Array(D(size),None)for x in all_nodes_in_heap: d = x.degree; while A[d] != None: y = A[d] if x.key > y.key: x,y = y,x heap = FibHeapLink(heap, x, y) A[d] = None d = d+1 A[d] = x heap = None # merge nodes from A into heap for i in range(0, D(size)): if A[i] != NULL ): A[i].left = A[i] A[i].right = A[i] heap = JoinCycleList(heap, A[i]) if heap.key > A[i].key: heap = A[i]

  36. Fibonacci heap - Consolidate def FibHeapLink(heap, x, y):heap = RemoveNodeFromCycleList( heap, y ) x.child = JoinCycleList( x.child, y) x.degree = x.degree+1 y.mark = Falsereturn heap

  37. Fibonacci heap For heap H until we do: • ExtractMin • Delete Trees in the heap are unoredered valid binominal trees When we perform ExtractMin and Delete maximal degree of a node is D(n) = D(n) = logf n; f=(1+5) /2 and this is amortized time for both operations

  38. Fibonacci heap - DecKey 11 11 11 11 11 11 38 38 38 38 38 38 30 31 31 31 31 31 41 41 41 41 41 41 HF HF 15 6 18 24 21 30 17 26 15 52 35 HF HF 15 6 18 15 4 6 18 24 21 30 17 21 24 30 17 26 52 26 52 4 35 HF 6 18 HF 24 21 30 17 6 18 15 4 26 18 15 4 26 24 6 52 26 46 21 21 24 30 17 30 17 35 52 52

  39. Fibonacci heap - ExtractMin parent = x.parent if parent != None and x.key < parent.key :move_x_to_rootof_heap x.marked = False if parent.marked: repeat_whole_ptocedure_recursively_for_parent else: parent.marked = True

  40. Fibonacci heap - DecKey def DecKeyFibHeap(heap, x, newK):if x.key < newK : ERRORx->key = newK p = x.parentif p != None and x.key < p.key: heap = CutFibHeap(heap, x, p) heap = CascadingCutFibHeap (heap,p)if x.key < heap.key: heap = x;

  41. Fibonacci heap – Cut def CutFibHeap(heap, x, p):RemoveFromCycleList(p.son, x)p.degree = p.degree –1x.left, x.right = x, xheap = JoinCycleList(heap, x)x.parent = Nonex.mark = False def CascadingCutFibHeap(heap, p):z = p.parentif z != None: if not z.marked: z.marked = True else: CutFibHeap(heap, p, z ) CascadingCutFibHeap(heap, z)

  42. Kopce Fibonicciego – DelKey • Decrease key to minimal possible value • Execute ExtractMin def DelKeyFibHeap(heap, x, size):heap = DecKeyFibHeap(heap, x, MINKEYVALUE)heap,size,x = ExtractMinFibHeap(heap, size)return heap,size MINKEYVALUE has to be smallerthanall the keys (we can assume the boundary value or the Min can be determined and used)

More Related