1 / 29

Chapter 6: Transform and Conquer

The Design and Analysis of Algorithms. Chapter 6: Transform and Conquer. Chapter 6. Transform and Conquer Algorithms. Basic Idea Instance simplification Presorting Search with presorting Element uniqueness with presorting Gaussian elimination Representation change Binary Search Trees

pilar
Download Presentation

Chapter 6: Transform and Conquer

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. The Design and Analysis of Algorithms Chapter 6:Transform and Conquer

  2. Chapter 6. Transform and Conquer Algorithms • Basic Idea • Instance simplification • Presorting • Search with presorting • Element uniqueness with presorting • Gaussian elimination • Representation change • Binary Search Trees • Heaps • Horner’s rule for polynomial evaluation • Problem reduction • Conclusion

  3. Basic Idea This group of techniques solves a problem by a transformation • to a simpler/more convenient instance of the same problem (instance simplification) • to a different representation of the same instance (representation change) • to a different problem for which an algorithm is already available (problem reduction) Reduce problem instance to smaller instance of the same problem and extend solution

  4. Examples of Decrease-and-Conquer Algorithms • Decrease by one: • Insertion sort • Graph search algorithms: • DFS • BFS • Topological sorting • Algorithms for generating permutations, subsets

  5. Instance Simplification - Presorting Many problems involving lists are easier when list is sorted. • searching • computing the median (selection problem) • checking if all elements are distinct (element uniqueness) • Topological sorting helps solving some problems for dags. • Presorting is used in many geometric algorithms. • Efficiency of algorithms involving sorting depends on efficiency of sorting.

  6. Search with presorting Problem:Search for a given K in A[0..n-1] Stage 1 Sort the array by an efficient sorting algorithm Stage 2 Apply binary search • Efficiency: Θ(nlog n) + O(log n) = Θ(nlog n) Good or bad? Why do we have our dictionaries, telephone directories, etc. sorted?

  7. Element Uniqueness with Presorting • Presorting-based algorithm Stage 1: sort by efficient sorting algorithm (e.g. mergesort) Stage 2: scan array to check pairs of adjacent elements Efficiency: Θ(nlog n) + O(n) = Θ(nlog n) • Brute force algorithm : Compare all pairs of elements Efficiency: O(n2)

  8. Gaussian Elimination • Given: A system of n linear equations in nunknowns with an arbitrary coefficient matrix. • Transform to: An equivalent system of n linear equations in n unknowns with an upper triangular coefficient matrix. • Solve the latter by substitutions starting with the last equation and moving up to the first one.

  9. Gaussian Elimination a11x1 + a12x2 + … + a1nxn = b1 a21x1 + a22x2 + … + a2nxn = b2 . . . . . an1x1 + an2x2 + … + annxn = bn a11x1+ a12x2 + … + a1nxn = b1 a22x2 + … + a2nx n = b2 . . . . . annxn = bn Efficiency: Θ(n3)

  10. K <K >K Representation Change:Binary Search Trees • Instead of linear representation use tree representation • Arrange keys in a binary tree with the binary search tree property:

  11. Representation Change:Binary Search Trees • Efficiency depends of the tree’s height: log2 nhn-1 • Search, insert and delete: • worst case efficiency: (n) • average case efficiency: (log n)

  12. Representation Change:Binary Search Trees • Advantage: • Inorder traversal produces sorted list • Disadvantage: worst case efficiency is (n) • Several representations have been developed to overcome the disadvantages of BSTs – AVL trees, multi-way search trees - 2-3 trees, 2-3-4 trees, red-black trees.

  13. Representation Change:Heaps Definition A heap is a binary tree with keys at its nodes (one key per node) such that: • It is essentially complete, i.e., all its levels are full except possibly the last level, where only some rightmost keys may be missing • The key at each node is ≥ keys at its children

  14. Representation Change:Heaps a heap not a heap not a heap

  15. Representation Change:Heaps Some Important Properties of a Heap • Given n, there exists a unique binary tree with n nodes that is essentially complete, with h = log2 n • The root contains the largest key • The subtree rooted at any node of a heap is also a heap • A heap can be represented as an array

  16. 1 2 3 4 5 6 9 5 3 1 4 2 9 5 3 1 4 2 Heaps’ Array Representation Store heap’s elements in an array (whose elements indexed, for convenience, 1 to n) in top-down left-to-right order Parental nodes are represented in the first n/2 locations Left child of node j is at 2j Right child of node j is at 2j+1 Parent of node j is at j/2

  17. Heap Construction(bottom-up) • Step 0: Initialize the structure with keys in the order given • Step 1: Starting with the last (rightmost) parental node, fix the heap rooted at it, if it doesn’t satisfy the heap condition: keep exchanging it with its largest child until the heap condition holds • Step 2: Repeat Step 1 for the preceding parental node

  18. Heap Construction(bottom-up)

  19. Heapsort • Stage 1: Construct a heap for a given list of n keys • Stage 2: Repeat operation of root removal n-1 times: • Exchange keys in the root and in the last (rightmost) leaf • Decrease heap size by 1 • If necessary, swap new root with larger child until the heap condition holds

  20. Analysis of Heapsort Stage 1:Build heap for a given list of n keys worst-case C(n) =  2(h-i) 2i = 2(n – log2(n+1) )  (n) (i = 0 to h-1) Note: 2i = nodes at level i

  21. Analysis of Heapsort Stage 2: Repeat operation of root removal n-1 times (fix heap) worst-case C(n) =  2log2i  (n log(n)) i = 1 to h-1 Both worst-case and average-case efficiency: (nlogn)

  22. Priority Queues • A priority queue is the ADT of a set of elements with numerical priorities with the following operations: • delete element with highest priority • insert element with assigned priority • Efficiency : • deleteMin: (1), with (log (n)) for restructuring the heap after the delete operations • insert an element:  (log (n))

  23. Horner’s Rule Given a polynomial of degree n p(x) = anxn + a n-1 x n-1+ … + a1x1 + a0 and a specific value of x, find the value of p at that point.

  24. Two Brute-Force Algorithms Algorithm 1: p 0 for i n down to 0do power 1 for k 1 to i do powerpower * x pp + ai * power return p Algorithm 2: pa0; power 1 for k  1 to n do powerpower * x pp + ak* power return p Efficiency of algorithm 1: (n2) Efficiency of algorithm 2: (n)

  25. Horner’s Rule p(x) = anxn + an-1 xn-1+ … + a1x1 + a0 = x(x(x(…(anx + an-1) + an-2) + …) + a0 Example: p(x) = 2x4 - x3 + 3x2 + x - 5 = = x(2x3 - x2 + 3x + 1) - 5 = = x(x(2x2 - x + 3) + 1) - 5 = = x(x(x(2x - 1) + 3) + 1) - 5

  26. Horner’s Rule Pseudocode Efficiency of Horner’s Rule: # multiplications = # additions = n

  27. Problem Reduction • Transform the problem into a different problem for which an algorithm is already available. • The combined time of the transformation and solving the other problem should be smaller than solving the problem as given by another method.

  28. Examples of Problem Reduction • computing lcm(m, n) via computing gcd(m, n) • counting number of paths of length n in a graph by raising the graph’s adjacency matrix to the n-th power • transforming a maximization problem to a minimization problem and vice versa (also, min-heap construction) • reduction to graph problems (e.g., solving puzzles via state-space graphs)

  29. Conclusion • The Transform-and-Conquer paradigm is a powerful problem-solving strategy. • The key to success is to be able to view the problem from different perspectives

More Related