Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2007 Pearson Addison-Wesley. All rights reserved. Problem Reduction Dr. M. Sakalli Marmara Unv, Levitin ’ s notes.

Similar presentations


Presentation on theme: "Copyright © 2007 Pearson Addison-Wesley. All rights reserved. Problem Reduction Dr. M. Sakalli Marmara Unv, Levitin ’ s notes."— Presentation transcript:

1 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. Problem Reduction Dr. M. Sakalli Marmara Unv, Levitin ’ s notes

2 4-1 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 4 Closest-Pair Problem Find the two closest points in a set of Q of n  2 points in Euclidian sense. The distance between points of s The distance between points of p 1 = (x 1, y 1 ) and p 2 = (x 2, y 2 ), is √[(x 1 - x 2 ) 2 + (y 1 - y 2 ) 2 ]. Brute-force approach computes the distance between every pair of distinct points and returns the indexes of the points for which the distance is the smallest, simply it checks (n!/(2!(n-2)!)) points, which is quadratic process, θ. Brute-force approach computes the distance between every pair of distinct points and returns the indexes of the points for which the distance is the smallest, simply it checks (n!/(2!(n-2)!)) points, which is quadratic process, θ(n 2 ).

3 4-2 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 4 Reducing problem of Closest-Pair and further presorting. A subset P  Q, and X and Y are the array of coordinates, input to at each recursive invocation, each call contains all the points of input subsets, suppose incorporated with sorting. Allowing the a closest pair function to sort the subset P at every call would require and running time would bewhose solution is to be Allowing the a closest pair function to sort the subset P at every call would require f(n) = O(n lg n), and running time would be T(n) = 2T(n/2) + O(n lg n), whose solution is to be O(n 1g 2 n). Instead providing a presorted input set of P at every invocation (the same sorted P), reduces the problem to and the solution is Instead providing a presorted input set of P at every invocation (the same sorted P), reduces the problem to T(n) = 2T(n/2) + O(n), and the solution is O(n 1gn). If |P|  3, which is the base case, performs the brute-force try all pairs of points and returns the closest pair. If |P| > 3, follows the divide-and-conquer paradigm > Find a vertical line l that bisects the point set P into two subsets P L and P R such that |P L | = |P|/2, |P R | = |P| /2. For example, the array X is divided into arrays X L and X R, with monotonically increasing order. Similarly, the array Y.  And invoke CP for each subset to find the minimum distance,  And then merge the results..

4 4-3 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 4 min(d) = {(d 1, d 2 )} is not necessarily the smallest distance between all the pairs of P. A closer distance can lie on the opposite sides of the line, which is addressed for the points laying closer to the splitting line, within a vertical (horizontal) strip of width 2d. For every point P in P L, inspect points in P R, that may be closer to P than d. There can be no more than 6 such points (because d ≤ d2)!

5 4-4 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 4 Splitting sorted array, clrs.. 1 length[Y L ]  length[Y R ]  0 2 for i  1 to length[Y] 3 do if Y[i]  P L 4 then length[Y L ]  length[Y L ]+1 5 Y[length[Y L ]]  Y[i] 6 else length[Y R ]  length[Y R ]+1 7 Y[length[Y R ]]  Y[i] Instead determine the median of the sorted array from the length, from the length, and split.

6 4-5 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 4 Problem Reduction Transforming a problem into a different problem for which an algorithm is already available. To be of practical value, the combined time of the transformation and solving the other problem should be smaller than solving the problem as given by another method.

7 4-6 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 4 Evaluating Polynomial Equations Problem: Find the value of polynomial p(x) = a n x n + a n-1 x n-1 +… + a 1 x 1 + a 0 p(x) = a n x n + a n-1 x n-1 +… + a 1 x 1 + a 0 at a point x = x 0 at a point x = x 0 Brute-force algorithm p  0.0 for i  n downto 0 do power  1 power  1 for j  1 to i do//compute x i for j  1 to i do//compute x i power  power  x power  power  x p  p + a[i]  power p  p + a[i]  power returnp return pEfficiency:  0  i  n i = Θ(n 2 ) multiplications p  a[0] power  1 for i  1 to n do power  power  x power  power  x p  p + a[i]  power p  p + a[i]  power return p Efficiency:  0  i  n i = Θ(n) multiplications

8 4-7 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 4 Horner’s Rule Example: p(x) = 2x 4 - x 3 + 3x 2 + x - 5 = = x(2x 3 - x 2 + 3x + 1) - 5 = = x(2x 3 - x 2 + 3x + 1) - 5 = = x(x(2x 2 - x + 3) + 1) - 5 = = x(x(2x 2 - x + 3) + 1) - 5 = = x(x(x(2x - 1) + 3) + 1) - 5 = x(x(x(2x - 1) + 3) + 1) - 5 Substitution into the last formula leads to a faster algorithm Same sequence of computations are obtained by simply arranging the coefficient in a table and proceeding as follows: coefficients2-1 3 1-5ie for x=3 Efficiency of Horner: = 2n = Θ(n)

9 4-8 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 4 Horner’s Rule pseudocode Efficiency of Horner’s Rule: # multiplications = # additions = n ***** Synthetic division of of p(x) by (x-x 0 ) Example: Let p(x) = 2x 4 - x 3 + 3x 2 + x - 5. Find p(x):(x-3)

10 4-9 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 4 Computing a n (revisited) Left-to-right binary exponentiation Initialize product accumulator by 1. Scan n’s binary expansion from left to right and do the following: If the current binary digit is 0, square the accumulator (S); if the binary digit is 1, square the accumulator and multiply it by a (SM). Example: Compute a 13. Here, n = 13 = 1101 2. binary rep. of 13: 1 1 0 1 SM SM S SM accumulator: 1 1 2 *a=a a 2 *a = a 3 (a 3 ) 2 = a 6 (a 6 ) 2 *a= a 13 (computed left-to-right) Efficiency: (b-1) ≤ M(n) ≤ 2(b-1) where b =  log 2 n  + 1

11 4-10 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 4 Computing a n (cont.) Right-to-left binary exponentiation Scan n’s binary expansion from right to left and compute a n as the product of terms a 2 i corresponding to 1’s in this expansion. Example Compute a 13 by the right-to-left binary exponentiation. Here, n = 13 = 1101 2. 1 1 0 1 a 8 a 4 a 2 a : a 2 i terms a 8 * a 4 * a : product (computed right-to-left) Efficiency: same as that of left-to-right binary exponentiation

12 4-11 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 4 Multiplication of large integers b To multiply two integers of n 1 and n 2 digits  x * y = (∑ i x i 2 i )(∑ j y j 2 j ) = ∑ i ∑ j x i y j 2 i+j. n 1 n 2 multiplications, θ(n 2 ) b Imagine splitting x and y into two very huge digits of k bits each: x = x 1 2 k + x 0, y = y 1 2 k + y 0. Then  xy = (x 1 2 k + x 0 )(y 1 2 k + y 0 ) = x 1 y 1 2 2k + x 1 y 0 2 k + x 0 y 1 2 k + x 0 y 0. remember 2 k is shifting.. b Thus, computing a product of n=2k bits is reduced to the problem of four products on k bits, which gives running time T(n) = 4T(n/2) + θ(n) = T(n 2 ). So far no progress made yet. b The trick it says (Karatsuba-Ofman algorithm) is to compute the x 1 y 1 and x 0 y 0 in one recursive multiplication each, and then compute x 1 y 0 +x 0 y 1 with  z = (x 1 -x 0 )(y 0 -y 1 ) = x 1 y 0 - x 1 y 1 - x 0 y 0 + x 0 y 1 so that x 1 y 0 +x 0 y 1 = z + x 1 y 1 + x 0 y 0.  xy = x 1 y 1 2 2k + (z + x 1 y 1 + x 0 y 0 )2 k + x 0 y 0. b Computing three products recursively, and merging the results, T(n) = 4T(n/2) + θ(n) = T(n lg3 ) = T(n 1.59 ). However, remark the example of 23*14, where 23=2. 10 1 +3. 10 0 and 14=1. 10 1 +4. 10 0 b In general: c=a*b=c 2. 10 n +c 1. 10 n/2 +c 0. 10 0 where c 2 =a 1 *b 1, c 0 =a 0 *b 0, c 1 =(a 1 +a 0 )*(b 1 +b 0 )-(c 2 +c 0 ) Z=(a 1 -a 0 )(b 0 -b 1 )=-(a 1 b 1 )+(a 1 b 0 )+(a 0 b 1 )-(a 0 b 0 )  (a 1 b 0 )+(a 0 b 1 )=(a 1 b 1 )+(a 0 b 0 )+Z.

13 4-12 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 4 Examples of Solving Problems by Reduction b computing lcm(m, n) via computing gcd(m, n) b counting number of paths of length n in a graph by raising the graph’s adjacency matrix to the n-th power b transforming a maximization problem to a minimization problem and vice versa (also, min-heap construction) b linear programming b reduction to graph problems (e.g., solving puzzles via state- space graphs)


Download ppt "Copyright © 2007 Pearson Addison-Wesley. All rights reserved. Problem Reduction Dr. M. Sakalli Marmara Unv, Levitin ’ s notes."

Similar presentations


Ads by Google