Presentation is loading. Please wait.

Presentation is loading. Please wait.

Design and Analysis of Algorithms - Chapter 61 Transform and Conquer Solve problem by transforming into: b a more convenient instance of the same problem.

Similar presentations


Presentation on theme: "Design and Analysis of Algorithms - Chapter 61 Transform and Conquer Solve problem by transforming into: b a more convenient instance of the same problem."— Presentation transcript:

1 Design and Analysis of Algorithms - Chapter 61 Transform and Conquer Solve problem by transforming into: b a more convenient instance of the same problem (instance implification) presortingpresorting Gaussian eliminationGaussian elimination b a different representation of the same instance (representation change) balanced search treesbalanced search trees heaps and heapsortheaps and heapsort polynomial evaluation by Horner’s rulepolynomial evaluation by Horner’s rule Fast Fourier TransformFast Fourier Transform b a different problem altogether (problem reduction) reductions to graph problemsreductions to graph problems linear programminglinear programming

2 Design and Analysis of Algorithms - Chapter 62 Instance simplification - Presorting Solve instance of problem by transforming into another simpler/easier instance of the same problem Presorting: Many problems involving lists are easier when list is sorted. b searching b computing the median (selection problem) b computing the mode b finding repeated elements

3 Design and Analysis of Algorithms - Chapter 63 Selection Problem Find the k th smallest element in A[1],…A[n]. Special cases: minimum: k = 1minimum: k = 1 maximum: k = nmaximum: k = n median: k = n/2median: k = n/2 b Presorting-based algorithm sort listsort list return A[k]return A[k] b Partition-based algorithm (Variable decrease & conquer): pivot/split at A[s] using partitioning algorithm from quicksortpivot/split at A[s] using partitioning algorithm from quicksort if s=k return A[s]if s=k return A[s] else if s<k repeat with sublist A[s+1],…A[n].else if s<k repeat with sublist A[s+1],…A[n]. else if s>k repeat with sublist A[1],…A[s-1].else if s>k repeat with sublist A[1],…A[s-1].

4 Design and Analysis of Algorithms - Chapter 64 Notes on Selection Problem b Presorting-based algorithm: Ω(nlgn) + Θ(1) = Ω(nlgn) b Partition-based algorithm (Variable decrease & conquer): worst case: T(n) =T(n-1) + (n+1) -> Θ(n 2 )worst case: T(n) =T(n-1) + (n+1) -> Θ(n 2 ) best case: Θ(n)best case: Θ(n) average case: T(n) =T(n/2) + (n+1) -> Θ(n)average case: T(n) =T(n/2) + (n+1) -> Θ(n) Bonus: also identifies the k smallest elements (not just the k th )Bonus: also identifies the k smallest elements (not just the k th ) b Special cases max, min: better, simpler linear algorithm (brute force) b Conclusion: Presorting does not help in this case.

5 Design and Analysis of Algorithms - Chapter 65 Finding repeated elements b Presorting-based algorithm: use mergesort (optimal): Θ(nlgn)use mergesort (optimal): Θ(nlgn) scan array to find repeated adjacent elements: Θ(n)scan array to find repeated adjacent elements: Θ(n) b Brute force algorithm: Θ(n 2 ) b Conclusion: Presorting yields significant improvement b Similar improvement for mode b What about searching? Θ(nlgn)

6 Design and Analysis of Algorithms - Chapter 66 Taxonomy of Searching Algorithms b Elementary searching algorithms sequential searchsequential search binary searchbinary search binary tree searchbinary tree search b Balanced tree searching AVL treesAVL trees red-black treesred-black trees multiway balanced trees (2-3 trees, 2-3-4 trees, B trees)multiway balanced trees (2-3 trees, 2-3-4 trees, B trees) b Hashing separate chainingseparate chaining open addressingopen addressing

7 Design and Analysis of Algorithms - Chapter 67 Balanced trees: AVL trees b For every node, difference in height between left and right subtree is at most 1 b AVL property is maintained through rotations, each time the tree becomes unbalanced b lg n ≤ h ≤ 1.4404 lg (n + 2) - 1.3277 average: 1.01 lg n + 0.1 for large n b Disadvantage: needs extra storage for maintaining node balance b A similar idea: red-black trees (height of subtrees is allowed to differ by up to a factor of 2)

8 Design and Analysis of Algorithms - Chapter 68 AVL tree rotations b Small examples: 1, 2, 31, 2, 3 3, 2, 13, 2, 1 1, 3, 21, 3, 2 3, 1, 23, 1, 2 b Larger example: 4, 5, 7, 2, 1, 3, 6 b See figures 6.4, 6.5 for general cases of rotations;

9 Design and Analysis of Algorithms - Chapter 69 General case: single R-rotation

10 Design and Analysis of Algorithms - Chapter 610 Double LR-rotation

11 Design and Analysis of Algorithms - Chapter 611 Balance factor b Algorithm maintains balance factor for each node. For example:

12 Design and Analysis of Algorithms - Chapter 612 Heapsort Definition: A heap is a binary tree with the following conditions: b it is essentially complete: b The key at each node is ≥ keys at its children

13 Design and Analysis of Algorithms - Chapter 613 Definition implies: b Given n, there exists a unique binary tree with n nodes that is essentially complete, with h= lg n b The root has the largest key b The subtree rooted at any node of a heap is also a heap

14 Design and Analysis of Algorithms - Chapter 614 Heapsort Algorithm: 1. Build heap 2. Remove root –exchange with last (rightmost) leaf 3. Fix up heap (excluding last leaf) Repeat 2, 3 until heap contains just one node.

15 Design and Analysis of Algorithms - Chapter 615 Heap construction b Insert elements in the order given breadth-first in a binary tree b Starting with the last (rightmost) parental node, fix the heap rooted at it, if it does not satisfy the heap condition: 1.exchange it with its largest child 2.fix the subtree rooted at it (now in the child’s position) Example: 2 3 6 7 5 9

16 Design and Analysis of Algorithms - Chapter 616 Root deletion The root of a heap can be deleted and the heap fixed up as follows: b exchange the root with the last leaf b compare the new root (formerly the leaf) with each of its children and, if one of them is larger than the root, exchange it with the larger of the two. b continue the comparison/exchange with the children of the new root until it reaches a level of the tree where it is larger than both its children

17 Design and Analysis of Algorithms - Chapter 617 Representation b Use an array to store breadth-first traversal of heap tree: b Example: b Left child of node j is at 2j b Right child of node j is at 2j+1 b Parent of node j is at j /2 b Parental nodes are represented in the first n /2 locations 9 1 53 42 1 2 3 4 5 6 9 5 3 1 4 2

18 Design and Analysis of Algorithms - Chapter 618 Bottom-up heap construction algorithm

19 Design and Analysis of Algorithms - Chapter 619 Analysis of Heapsort See algorithm HeapBottomUp in section 6.4 b Fix heap with “problem” at height j: 2j comparisons b For subtree rooted at level i it does 2(h-i) comparisons b Total for heap construction phase: Σ 2(h-i) 2 i = 2 ( n – lg (n + 1)) = Θ(n) i=0 h-1 # nodes at level i

20 Design and Analysis of Algorithms - Chapter 620 Analysis of Heapsort (continued) Recall algorithm: 1.Build heap 2.Remove root –exchange with last (rightmost) leaf 3.Fix up heap (excluding last leaf) Repeat 2, 3 until heap contains just one node. Θ(n)Θ(n) Θ(log n) n – 1 times Total: Total: Θ(n) + Θ( n log n) = Θ(n log n) Note: Note: this is the worst case. Average case also Θ(n log n).

21 Design and Analysis of Algorithms - Chapter 621 Priority queues b A priority queue is the ADT of an ordered set with the operations: find element with highest priorityfind element with highest priority delete element with highest prioritydelete element with highest priority insert element with assigned priorityinsert element with assigned priority b Heaps are very good for implementing priority queues

22 Design and Analysis of Algorithms - Chapter 622 Insertion of a new element b Insert element at last position in heap. b Compare with its parent and if it violates heap condition exchange them exchange them b Continue comparing the new element with nodes up the tree until the heap condition is satisfied Example:Efficiency:

23 Design and Analysis of Algorithms - Chapter 623 Bottom-up vs. Top-down heap construction b Top down: Heaps can be constructed by successively inserting elements into an (initially) empty heap b Bottom-up: Put everything in and then fix it b Which one is better?


Download ppt "Design and Analysis of Algorithms - Chapter 61 Transform and Conquer Solve problem by transforming into: b a more convenient instance of the same problem."

Similar presentations


Ads by Google