1 / 22

Introduction to Analysis of Algorithms CAS CS 330

Introduction to Analysis of Algorithms CAS CS 330. Lecture 16 Shang-Hua Teng Thanks to Charles E. Leiserson and Silvio Micali of MIT for these slides. Data-structure augmentation. “Methodology”: ( e.g., order-statistics trees ). Choose an underlying data structure ( AVL/ red-black trees ).

lucien
Download Presentation

Introduction to Analysis of Algorithms CAS CS 330

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. Introduction toAnalysis of AlgorithmsCAS CS 330 Lecture 16 Shang-Hua Teng Thanks to Charles E. Leiserson and Silvio Micali of MITfor these slides

  2. Data-structure augmentation “Methodology”: (e.g.,order-statistics trees) • Choose an underlying data structure (AVL/red-black trees). • Determine additional information to be stored in the data structure (subtree sizes) • Make sure this information can be maintained for modifying operations (INSERT, DELETE —rotations). • Develop new dynamic-set operations that use the information (OS-SELECT,…).

  3. Interval trees Goal: To maintain a dynamic set of intervals, such as time intervals. 10 7 11 17 19 5 4 8 15 18 22 23

  4. Interval trees Goal: To maintain a dynamic set of intervals, such as time intervals. 10 = high[i] low[i] = 7 11 17 19 5 4 8 15 18 22 23

  5. Interval trees Goal: To maintain a dynamic set of intervals, such as time intervals. i 10 = high[i] low[i] = 7 11 17 19 5 4 8 15 18 22 23 Query: For a given query interval i, find an interval in the set that overlaps i.

  6. a b m a,b m Following the methodology • Choose an underlying data structure. • Search tree keyed on low (left) endpoint. • Determine additional information to be stored in the data structure. • Store in each node x the largest value m[x] in the subtree rooted at x, as well as the interval int[x] corresponding to the key. int m

  7. high[int[x]] m[left[x]] m[right[x]] m[x] = max Example interval tree 17,19 23 5,11 18 22,23 23 4,8 8 15,18 18

  8. 6,20 30 11,15 30 11,15 30 6,20 0 19 19 14 30 14 Modifying operations • Verify that this information can be maintained for modifying operations. • INSERT: Fix m’s on the way down. • Rotations — Fixup = O(1) time per rotation: 19 Total INSERT time = O(lg n); DELETE similar.

  9. New operations • Develop new dynamic-set operations that use the information. • INTERVAL-SEARCH(i) • x root • whilex¹NIL and i and int[x] don’t overlap • do⊳ • ifleft[x] ¹NIL and low[i] £m[left[x]] • thenx  left[x] • elsex  right[x] • returnx

  10. New operations • Develop new dynamic-set operations that use the information. • (low[i] > high[int[x]] • orlow[int[x]] > high[i]) • INTERVAL-SEARCH(i) • x root • whilex¹NIL and i and int[x] don’t overlap • do⊳ • ifleft[x] ¹NIL and low[i] £m[left[x]] • thenx  left[x] • elsex  right[x] • returnx `

  11. x x root [14,16]and[17,19]don’t overlap Example 1: INTERVAL-SEARCH([14,16]) 17,19 23 5,11 18 22,23 23 4,8 8 15,18 18 14 £ 18  x  left[x]

  12. Example 1: INTERVAL-SEARCH([14,16]) 17,19 23 5,11 18 22,23 23 x 4,8 8 15,18 18 [14,16]and[5,11]don’t overlap 14 > 8 x  right[x]

  13. Example 1: INTERVAL-SEARCH([14,16]) 17,19 23 5,11 18 22,23 23 4,8 8 15,18 18 x [14,16]and[15,18]overlap return[15,18]

  14. x x root [12,14]and[17,19]don’t overlap Example 2: INTERVAL-SEARCH([12,14]) 17,19 23 5,11 18 22,23 23 4,8 8 15,18 18 12 £ 18 x  left[x]

  15. Example 2: INTERVAL-SEARCH([12,14]) 17,19 23 5,11 18 22,23 23 x 4,8 8 15,18 18 [12,14]and[5,11]don’t overlap 12 > 8 x  right[x]

  16. Example 2: INTERVAL-SEARCH([12,14]) 17,19 23 5,11 18 22,23 23 4,8 8 15,18 18 x [12,14]and[15,18]don’t overlap 12 > 10 x  right[x]

  17. Example 2: INTERVAL-SEARCH([12,14]) 17,19 23 5,11 18 22,23 23 4,8 8 15,18 18 x x = NIL no interval that overlaps[12,14]exists

  18. List alloverlapping intervals: • Search, list, delete, repeat. • Insert them all again at the end. Time = O(k lg n), where k is the total number of overlapping intervals. This is an output-sensitive bound. Best algorithm to date: O(k + lg n). Analysis Time = O(h) = O(lg n), since INTERVAL-SEARCH does constant work at each level as it follows a simple path down the tree.

  19. Correctness? • INTERVAL-SEARCH(i) • x root • whilex¹NIL and i and int[x] don’t overlap • ifleft[x] ¹NIL and low[i] £m[left[x]] • thenx  left[x] • elsex  right[x] • returnx

  20. Correctness Theorem. Let L be the set of intervals in the left subtree of node x, and let R be the set of intervals in x’s right subtree. • If the search goes right, then • { iÎL : i overlaps i } = . • If the search goes left, then • {iÎL : i overlaps i } =  • {iÎR : ioverlapsi } = . In other words, it’s always safe to take only 1 of the 2 children: we’ll either find something, or nothing was to be found.

  21. i L high( j) =m[left[x]] low(i) Correctness proof • Proof. Suppose first that the search goes right. • Ifleft[x] = NIL, then we’re done, since L = . • Otherwise, the code dictates that we must have low[i] > m[left[x]]. The value m[left[x]] corresponds to the right endpoint of some interval jÎL, and no other interval in L can have a larger right endpoint than high( j). • Therefore, {iÎL : i overlaps i } = .

  22. L Proof (continued) Suppose that the search goes left, and assume that {iÎL : i overlaps i } = . • Then, the code dictates that low[i] £m[left[x]] = high[ j]for some jÎ L. • Since jÎ L, it does not overlap i, and hence high[i] < low[ j]. • But, the binary-search-tree property implies that for all iÎR, we have low[ j] £low[i]. • But then {iÎR : i overlaps i } = . i j i

More Related