Presentation is loading. Please wait.

Presentation is loading. Please wait.

UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Lecture 11 Tuesday, 12/4/01 Advanced Data Structures Chapters.

Similar presentations


Presentation on theme: "UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Lecture 11 Tuesday, 12/4/01 Advanced Data Structures Chapters."— Presentation transcript:

1 UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Lecture 11 Tuesday, 12/4/01 Advanced Data Structures Chapters 20-21

2 Relevant Sections of Chapters Ch20 Binomial Heaps You’re responsible for material in this chapter that we discuss in lecture. (Note that this includes all sections.) Ch21 Fibonacci Heaps You’re responsible for material in this chapter that we discuss in lecture. (Note that this includes all sections.) Note that Chapter 22 has been removed. Ch7 HeapSort

3 Overview ä Chapter 7: Heap Review ä Chapter 20: Binomial Heaps ä Chapter 21: Fibonacci Heaps

4 Chapter 7 Heap Review

5 Review of Heap Basics ä Structure: ä Nearly complete binary tree ä Convenient array representation ä HEAP Property: (for MAX HEAP) ä Parent’s label not less than that of each child 16 1410 8793 241 1614108793241 1 2 3 4 5 6 7 8 9 10

6 Operations on a Heap ä HEAPIFY: ä for a given node that is the root of a subtree, if both subtrees of that node are already HEAPs, HEAPIFY enforces the HEAP PROPERTY via “downward swaps” so that the node together with its subtrees form a HEAP ä BUILD-HEAP: ä builds a HEAP from scratch using HEAPIFY ä HEAPSORT: ä sorts an array by first using BUILD-HEAP then repeatedly swapping out root and calling HEAPIFY assuming array representation

7 Operations on a Heap ä HEAPIFY: ä BUILD-HEAP: ä HEAPSORT: assuming array representation Asymptotic worst-case running time is in O(lg n). Asymptotic worst- case running time is in O(n lg n). However this is a loose bound! Time is also in O(n). Asymptotic worst-case running time is in O(n lg n). For a node at height h, time is in O(h). O(n) + O(lgn) nO(lgn) T(n) = T(2n/3) +  (1) is in O( lgn ) using Master Theorem

8 Operations on a Heap ä PRIORITY QUEUE SUPPORT: ä HEAP-INSERT ä adds new leaf to the tree and then “swaps up” to restore HEAP PROPERTY ä HEAP- MAXIMUM ä HEAP PROPERTY guarantees that maximum is at the root of a MAX HEAP ä HEAP- EXTRACT-MAX ä removes the maximum value from the root by swapping it out ä restores HEAP PROPERTY using HEAPIFY assuming array representation Applications: Job Scheduling, Event Scheduling

9 Operations on a Heap ä PRIORITY QUEUE SUPPORT: ä HEAP-INSERT ä HEAP- MAXIMUM ä HEAP- EXTRACT-MAX assuming array representation Asymptotic worst-case running time is in O(lg n). For a node at height h, time is in O(h). Asymptotic worst-case running time is in O(1). Asymptotic worst-case running time is in O(lg n). For a node at height h, time is in O(h). O(1) + O(lgn)

10 Building a Heap using HEAPIFY vs. HEAP-INSERT ä HEAPIFY ä swaps down ä compares parent with both children before each swap 16 1410 8793 241 Asymptotic worst-case running time of BUILD-HEAP using HEAPIFY is in O(n). However, using HEAP-INSERT the time would only be in O(n lg n). ä HEAP-INSERT ä swaps up ä compares parent with one child before each swap number of levels number of nodes in this level maximum number of swaps = length of path from this level down to leaf number of levels number of nodes in this level maximum number of swaps = length of path from this level up to root O(n) as in HEAPIFY

11 Chapter 20 Binomial Heaps

12 Mergeable Heap Operations source: 91.503 textbook Cormen et al. If UNION not needed, binary heap (Ch7) suffices Mergable Heaps supporting fast UNION

13 Mergeable Heap Operations source: 91.503 textbook Cormen et al. Mergable Heaps supporting fast UNION (inefficient SEARCH) search root list use min pointer UNION add to root list; update min pointer orphan, reverse, UNION orphan, consolidate = degree roots swap up if heap property violation swap with root; swap down if heap property violation swap up if heap property violation DECREASE-KEY, EXTRACT-MIN add to root list; update min pointer cascading cut if heap property violation

14 Binomial Tree Definition source: 91.503 textbook Cormen et al. Binomial Heap is a collection of Binomial Trees Binomial Tree B k is an ordered tree defined recursively (children are ordered)

15 Binomial Tree Properties source: 91.503 textbook Cormen et al.

16 Binomial Tree Properties source: 91.503 textbook Cormen et al.

17 Binomial Tree Properties source: 91.503 textbook Cormen et al. Proof: (continued)

18 Binomial Heap Definition source: 91.503 textbook Cormen et al. root degrees increase along root list

19 Binomial Heap Operations: MAKE-HEAP, MINIMUM source: 91.503 textbook Cormen et al. MAKE-HEAP worst-case running time is in  (1) HEAP-MINIMUM worst-case running time is in  (lgn) ( assume no keys with value) Minimum key must be in a root node due to heap-ordering. There are at most roots to check. search root list

20 Binomial Heap Operations: UNION source: 91.503 textbook Cormen et al. Links 2 binomial heaps whose roots have same degree Merge root lists into single linked list sorted by nondecreasing degree Link roots of equal degree until at most one root remains of each degree Pointers into root list: HEAP-UNION worst-case running time is in  (lgn) orphan, consolidate = degree roots

21 Binomial Heap Operations: UNION (continued) source: 91.503 textbook Cormen et al. 3 roots of same degree

22 Binomial Heap Operations: UNION (continued) source: 91.503 textbook Cormen et al.

23 Binomial Heap Operations: UNION (continued) source: 91.503 textbook Cormen et al.

24 Binomial Heap Operations: INSERT source: 91.503 textbook Cormen et al. HEAP-INSERT worst-case running time is in  (lgn) UNION

25 Binomial Heap Operations: EXTRACT-MIN source: 91.503 textbook Cormen et al. HEAP-EXTRACT-MIN worst-case running time is in  (lgn) orphan, reverse, UNION

26 Binomial Heap Operations: DECREASE-KEY source: 91.503 textbook Cormen et al. HEAP-DECREASE-KEY worst-case running time is in  (lgn) If violate heap property, swap up. No change in structure. swap up if heap property violation

27 Binomial Heap Operations: DELETE source: 91.503 textbook Cormen et al. HEAP-DELETE worst-case running time is in  (lgn) DECREASE-KEY, EXTRACT-MIN

28 Chapter 21 Fibonacci Heaps

29 Mergeable Heap Operations source: 91.503 textbook Cormen et al. Mergable Heaps supporting fast UNION (inefficient SEARCH) search root list UNION orphan, reverse, UNION orphan, consolidate = degree roots swap up if heap property violation swap with root; swap down if heap property violation swap up if heap property violation DECREASE-KEY, EXTRACT-MIN use min pointer add to root list; update min pointer orphan, consolidate = degree roots DECREASE-KEY, EXTRACT-MIN add to root list; update min pointer cascading cut if heap property violation O(D(n)) O(D(n))

30 Fibonacci Heap Basics source: 91.503 textbook Cormen et al. For asymptotically fast MST, shortest paths Collection of trees Relaxed structure Lazy: delay work Amortized (potential) cost Circular linked lists Minimum root t(H) = #trees in root list m(H) = #marked nodes Heap Potential Function

31 Potential Method (review) ä Potential Method ä amortized cost can differ across operations (as in accounting method) ä overcharge some operations early in sequence (as in accounting method) ä store overcharge as “potential energy” of data structure as a whole ä (unlike accounting method) ä Let c i be actual cost of ith operation ä Let D i be data structure after applying ith operation  Let  (D i ) be potential associated with D i ä Amortized cost of ith operation: ä Total amortized cost of n operations: ä Must have:to “pay in advance” terms telescope

32 Unordered Binomial Tree Properties source: 91.503 textbook Cormen et al. DIFFERENCE DECREASE-KEY Fibonacci Heap operation may violate Unordered Binomial Tree properties.

33 Fibonacci Heap Operations: INSERT source: 91.503 textbook Cormen et al. Increase in potential = Amortized cost is in O(1) Actual cost is in O(1) add to root list; update min pointer Heap Potential Function t (H) = #trees in root list m(H) = #marked nodes

34 Fibonacci Heap Operations: UNION source: 91.503 textbook Cormen et al. add to root list; update min pointer Heap Potential Function t (H) = #trees in root list m(H) = #marked nodes

35 Fibonacci Heap Operations: EXTRACT-MIN source: 91.503 textbook Cormen et al. disassemble Consolidate wherever possible: Link roots of = degree until at most one root remains of each degree Process one tree at a time, starting with new “min”. Next, update “min” First, disassemble old min tree. orphan, consolidate = degree roots

36 Fibonacci Heap Operations: EXTRACT-MIN Consolidation source: 91.503 textbook Cormen et al. Degree 0 merge: Combine 7 with 23 Degree 1 merge: (keep going) Combine 7/23 with 17/30 Degree 2 merge: (keep going) Combine 7… with 24…

37 Fibonacci Heap Operations: EXTRACT-MIN Consolidation source: 91.503 textbook Cormen et al.

38 Fibonacci Heap Operations: EXTRACT-MIN Pseudocode source: 91.503 textbook Cormen et al.

39 Fibonacci Heap Operations: EXTRACT-MIN Pseudocode source: 91.503 textbook Cormen et al. may do multiple degree merges

40 Fibonacci Heap Operations: EXTRACT-MIN Analysis source: 91.503 textbook Cormen et al. Amortized cost is in O(lgn) D(n) = upper bound on maximum degree of any node in an n-node Fibonacci heap (shown in Section 21.3 to be in O(lgn)) O(D(n)) Heap Potential Function t (H) = #trees in root list m(H) = #marked nodes Actual Work: >= O(D(n)) since at most D(n) children of minimum node When CONSOLIDATE is called, size of root list <= D(n) + t(H) - 1 Work in CONSOLIDATE’s for loop in O(D(n) + t(H)) due to tree linking in each iteration Total Actual Cost is in O(D(n) + t(H))

41 Fibonacci Heap Operations: DECREASE-KEY PseudoCode source: 91.503 textbook Cormen et al. cascading cut if heap property violation

42 Fibonacci Heap Operations: DECREASE-KEY Analysis source: 91.503 textbook Cormen et al. Heap Potential Function t (H) = #trees in root list m(H) = #marked nodes As soon as 2nd child of x is lost, cut x from parent, making it a new root. Actual Work: Dominated by cost of CASCADING-CUT Assume CASCADING-CUT called recursively c times Total Actual Cost is in O(c) Amortized cost is in  (1) Change in potential is at most:: t(H) to start with, c-1 added from cuts, + tree rooted at x c-1 unmarked by cascading cuts + at most 1 from last call to CASCADING-CUT

43 Fibonacci Heap Operations: DELETE source: 91.503 textbook Cormen et al. Amortized cost is in  (lgn) DECREASE-KEY, EXTRACT-MIN O(D(n))

44 Bounding the Maximum Degree source: 91.503 textbook Cormen et al. EXTRACT-MIN (& DELETE) O(lgn) bounds require D(n) in O(lgn) Show: Cutting node when it loses 2nd child Link only occurs in EXTRACT-MIN, DELETE

45 Bounding the Maximum Degree source: 91.503 textbook Cormen et al.

46 Bounding the Maximum Degree source: 91.503 textbook Cormen et al. Exercise 2.2-8

47 Bounding the Maximum Degree source: 91.503 textbook Cormen et al. Thus, EXTRACT-MIN (& DELETE) have O(lgn) time bounds.


Download ppt "UMass Lowell Computer Science 91.503 Analysis of Algorithms Prof. Karen Daniels Fall, 2001 Lecture 11 Tuesday, 12/4/01 Advanced Data Structures Chapters."

Similar presentations


Ads by Google