Download presentation
Presentation is loading. Please wait.
Published bySuzanna Lambert Modified over 6 years ago
1
Lecture 6 More Divide-Conquer and Paradigm #4 Data Structure.
Talk at U of Maryland Lecture 6 More Divide-Conquer and Paradigm #4 Data Structure. Today, we do more “divide-and-conquer”. And, we do Algorithm design paradigm # 4: invent or augment a data structure.
2
More divide and conquer: powering a number
Problem: Compute an , where n is an integer. Naïve algorithm Θ(n). Divide-and-conquer: an = an/2 an/ if n is even. = a(n-1)/2 a(n-1)/2 a if n is odd Note, another kind of divide and conquer. Time complexity: T(n) = T(n/2) + Θ(1) = Θ(logn)
3
Back to Fibonacci numbers
Recursion: not careful would give exponential time algorithm. Plain linear bottom up computation: Θ(n). Can we do better? Theorem: Fn+1 Fn Fn Fn I will prove this in class. Then you can compute this similar to powering of an integer in Θ(logn) steps. n
4
VLSI tree layout Problem: Embed a complete binary tree with n leaves in a grid using minimal area. W(n) H(n) H(n) = Θ(lg n) W(n) = Θ(n) Area = Θ(n lg n)
5
How do we improve this embedding?
If we wish to have an O(n) solution for the area, perhaps we wish to have √n for L(n) and W(n). What recursion scheme would get us there? The Master theorem Case 1 says if b=4, a=2, then log4 2 =1/2. So if we have something like T(n) = 2T(n/4) + o(√n) we would get O(√n) solution for L(n) and W(n).
6
H-tree embedding scheme
L(n/4) L(n) L(n) = 2L(n/4) + Θ(1) = Θ(√n) Area = Θ(n)
7
Paradigm #4 Use a data structure.
8
Talk at U of Maryland Example 1. Heapsort We invent a data structure (the heap) to support sorting.
9
Talk at U of Maryland Example 2. Subrange sum We want to maintain an array of integers a[1..n], and support the following operations: A. Increment a[i] by b; that is, set a[i] += b. B. Subrange sum: compute the sum of the elements a[c], a[c+1], ..., a[d]. Augment array as follows: a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] a[1..2] a[3..4] a[5..6] a[7..8] a[1..4] a[5..8] a[1..8] Operations A and B are both in O(logn) time.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.