copyright © 2007 pearson addison-wesley. all rights reserved. problem reduction dr. m. sakalli...

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

Upload: hannah-foster

Post on 17-Dec-2015

214 views

Category:

Documents


0 download

TRANSCRIPT

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

Copyright © 2007 Pearson Addison-Wesley. All rights reserved.

Problem ReductionProblem Reduction

Dr. M. Sakalli Dr. M. Sakalli Marmara Unv,Marmara Unv,Levitin’s notesLevitin’s notes

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

4-2Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4

Closest-Pair ProblemClosest-Pair ProblemFind the two closest points in a set of Find the two closest points in a set of QQ of of nn2 points in Euclidian sense. 2 points in Euclidian sense.

The distance between points of The distance between points of p1 = (x1, y1) and p2 = (x2, y2), iss √[(x1 - x2)2 + (y1 - y2)2].

Brute-forceBrute-force approach computes the distance between every pair of distinct points approach computes the distance between every pair of distinct points and returns the indexes of the points for which the distance is the smallest, 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, simply it checks (n!/(2!(n-2)!)) points, which is quadratic process, θθ(n2)..

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

4-3Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4

Reducing problem of Closest-Pair and further presorting.Reducing problem of Closest-Pair and further presorting.

A subset A subset PP Q, Q, and X and Y are the array of coordinates, input to at each 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 recursive invocation, each call contains all the points of input subsets, suppose incorporated with sorting. incorporated with sorting.

Allowing the a closest pair function to sort the subset P at every call would Allowing the a closest pair function to sort the subset P at every call would require require f(n) = O(n lg n), and running time would beand running time would be T(n) = 2T(n/2) + O(n lg n), whose solution is to bewhose solution is to be O(n 1g2 n).

Instead providing a presorted input set of Instead providing a presorted input set of PP at every invocation (the same sorted at every invocation (the same sorted PP), reduces the problem to ), reduces the problem to T(n) = 2T(n/2) + O(n), and the solution isand the solution is O(n 1gn).

If |If |PP| | 3, which is the base case, performs the brute-force try all pairs of points 3, which is the base case, performs the brute-force try all pairs of points and returns the closest pair. and returns the closest pair.

If |If |PP| > 3, follows the divide-and-conquer paradigm | > 3, follows the divide-and-conquer paradigm

> Find a vertical line > Find a vertical line ll that bisects the point set that bisects the point set PP into two subsets into two subsets PPL L and and PPRR such such

that |that |PPLL| = || = |PP|/2, ||/2, |PPRR| = |P| /2. For example, the array | = |P| /2. For example, the array XX is divided into arrays is divided into arrays XXLL and and

XXRR, with monotonically increasing order. Similarly, the array , with monotonically increasing order. Similarly, the array YY..

And invoke CP for each subset to find the minimum distance, And invoke CP for each subset to find the minimum distance,

And then merge the results.. And then merge the results..

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

4-4Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4

minmin((dd) = {() = {(dd11, , dd22)})} is not necessarily the smallest distance between is not necessarily the smallest distance between

all the pairs of P. A closer distance can lie on the opposite sides of 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 the line, which is addressed for the points laying closer to the splitting line, within a vertical (horizontal) strip of width 2d. splitting line, within a vertical (horizontal) strip of width 2d.

For every point For every point PP in in PPLL, inspect points in , inspect points in PPRR, that may be closer to , that may be closer to PP

than than dd. There can be no more than 6 such points (because . There can be no more than 6 such points (because dd ≤ ≤ dd2)!2)!

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

4-5Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4

Splitting sorted array, clrs.. Splitting sorted array, clrs..

1 length[YL] length[YR] 02 for i1 to length[Y]3 do if Y[i] PL

4 then length[YL]length[YL]+15 Y[length[YL]]Y[i]6 else length[YR]length[YR]+17 Y[length[YR]]Y[i]

Instead determine the Instead determine the medianmedian of the sorted array from the of the sorted array from the length, from the length, and split. length, from the length, and split.

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

4-6Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4

Problem ReductionProblem Reduction

Transforming a problem into a different problem for which Transforming a problem into a different problem for which an algorithm is already available. an algorithm is already available.

To be of practical value, To be of practical value, the the combined timecombined time of the of the transformationtransformation and and solving the other problem should be solving the other problem should be smallersmaller than solving the problem as given by another than solving the problem as given by another method. method.

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

4-7Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4

Evaluating Polynomial EquationsEvaluating Polynomial Equations

Problem: Find the value of polynomialProblem: Find the value of polynomial

pp((xx) = ) = aannxxnn + + aann-1-1xxnn-1 -1 +… ++… + a a11xx1 1 + + aa0 0

at a point at a point xx = = xx00

Brute-force algorithmBrute-force algorithmpp 0.00.0forfor ii nn downtodownto 0 0 dodo powerpower 1 1

forfor jj 1 1 toto ii do//compute do//compute xxii powerpower powerpower xx pp pp + + aa[[ii] ] powerpowerreturnreturn pp

Efficiency: Efficiency: 00iinn ii = = ΘΘ((nn22) multiplications) multiplications

pp aa[0][0]powerpower 1 1forfor ii 1 1 toto nn dodo

powerpower powerpower x x pp p p + + aa[[ii] ] powerpowerreturnreturn pp

Efficiency: Efficiency: 00iinn ii = = ΘΘ((nn) ) multiplicationsmultiplications

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

4-8Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4

Horner’s RuleHorner’s RuleExample: Example: pp(x) = 2(x) = 2xx44 - - xx33 + 3 + 3xx22 + + xx - 5 = - 5 =

= = xx(2(2xx33 - - xx22 + 3 + 3xx + 1) - 5 = + 1) - 5 =

= = xx((xx(2(2xx22 - - xx + 3) + 1) - 5 = + 3) + 1) - 5 =

= = xx((xx((xx(2(2xx - 1) + 3) + 1) - 5 - 1) + 3) + 1) - 5

Substitution into the last formula leads to a faster algorithm Substitution into the last formula leads to a faster algorithm

Same sequence of computations are obtained by simply arranging the Same sequence of computations are obtained by simply arranging the coefficient in a table and proceeding as follows:coefficient in a table and proceeding as follows:

coefficientscoefficients 22 -1-1 3 3 1 1 -5-5 ie for ie for xx=3=3

Efficiency of Horner: = 2Efficiency of Horner: = 2nn = = ΘΘ((nn))

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

4-9Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4

Horner’s Rule pseudocodeHorner’s Rule pseudocode

Efficiency of Horner’s Rule: # multiplications = # additions = Efficiency of Horner’s Rule: # multiplications = # additions = nn

**********

SSynthetic divisionynthetic division of of of of pp((xx) by () by (x-xx-x00) ) Example: Let Example: Let pp((xx) = 2) = 2xx44 - - xx33 + 3 + 3xx22 + + x x - 5. Find - 5. Find pp((xx):():(xx-3)-3)

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

4-10Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4

Computing Computing aann (revisited) (revisited)

Left-to-right binary exponentiationLeft-to-right binary exponentiation Initialize product accumulatorInitialize product accumulator by 1.by 1.Scan Scan nn’s binary expansion from left to right and do the ’s binary expansion from left to right and do the following: following: If the current binary digit is 0, square the accumulator (S);If the current binary digit is 0, square the accumulator (S);if the binary digit is 1, square the accumulator and multiply it if the binary digit is 1, square the accumulator and multiply it by by a a (SM).(SM).

Example: Compute aExample: Compute a1313. Here, . Here, nn = 13 = 1101 = 13 = 110122. . binary rep. of 13: 1 1binary rep. of 13: 1 1 0 1 0 1

SM SM SM SM S S SM SM accumulator: 1 1accumulator: 1 122**a=aa=a aa22**aa = = aa33 ( (aa33))22 = = aa66 ( (aa66))22**aa= = aa13 13 (computed left-to-right)(computed left-to-right)

Efficiency: (Efficiency: (b-b-1) 1) ≤≤ M( M(nn) ) ≤≤ 2 2((b-b-1) where 1) where b = b = loglog2 2 nn + 1 + 1

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

4-11Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4

Computing Computing aann (cont.) (cont.)

Right-to-left binary exponentiationRight-to-left binary exponentiation

Scan Scan nn’s binary expansion from right to left and compute ’s binary expansion from right to left and compute aann as as the product of terms the product of terms aa2 2 ii corresponding to 1’s in this expansion. corresponding to 1’s in this expansion.

ExampleExample Compute Compute aa13 13 by the right-to-left binary exponentiation. by the right-to-left binary exponentiation. Here, Here, nn = 13 = 1101 = 13 = 110122. .

11 1 1 0 1 0 1 a a88 a a44 a a22 a a : : aa2 2 ii terms terms a a88 * a * a44 * a * a : product : product (computed right-to-left) (computed right-to-left)

Efficiency: same as that of left-to-right binary exponentiationEfficiency: same as that of left-to-right binary exponentiation

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

4-12Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4

Multiplication of large integersMultiplication of large integers To multiply two integers of nTo multiply two integers of n11 and n and n22 digits digits

x * y = (∑x * y = (∑ii x xii22ii)(∑)(∑jjyyjj22jj) = ∑) = ∑ii∑∑jj x xiiyyjj22i+ji+j..

nn11nn22 multiplications, multiplications, θθ((nn22))

Imagine splitting x and y into two very huge digits of k bits each: Imagine splitting x and y into two very huge digits of k bits each:

x = xx = x1122kk + x + x00, y = y, y = y1122kk + y + y00. .

Then Then xy = (xxy = (x1122kk + x + x00)(y)(y1122kk + y + y00) = x) = x11yy11222k 2k + x+ x11yy0022k k + x+ x00yy1122k k + x+ x00yy00. remember 2. remember 2k k is shifting.. is shifting..

Thus, computing a product of n=2k bits is reduced to the problem of four products 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) + on k bits, which gives running time T(n) = 4T(n/2) + θθ(n) = T(n(n) = T(n22). So far no progress ). So far no progress made yet.made yet.

The trick it says (Karatsuba-Ofman algorithm) is to compute the xThe trick it says (Karatsuba-Ofman algorithm) is to compute the x11yy11 and x and x00yy00 in one in one recursive multiplication each, and then compute xrecursive multiplication each, and then compute x11yy00+x+x00yy11 with with z = (xz = (x11-x-x00)(y)(y00-y-y11) = x) = x11yy00 - x - x11yy11 - x - x00yy00 + x + x00yy11 so that x so that x11yy00+x+x00yy11 = z + x = z + x11yy11 + x + x00yy00. .

xy = xxy = x11yy11222k2k + (z + x + (z + x11yy11 + x + x00yy00)2)2kk + x + x00yy00. .

Computing three products recursively, and merging the results, T(n) = 4T(n/2) + Computing three products recursively, and merging the results, T(n) = 4T(n/2) + θθ(n) = T(n(n) = T(nlg3lg3) = T(n) = T(n1.591.59). However, remark the example of 23*14, where ). However, remark the example of 23*14, where 23=223=2..101011+3+3..101000 and 14=1 and 14=1..101011+4+4..101000

In general: c=a*b=cIn general: c=a*b=c22..1010nn+c+c11

..1010n/2n/2+c+c00..101000 where c where c22=a=a11*b*b11, c, c00=a=a00*b*b00, c, c11=(a=(a11+a+a00)*(b)*(b11+b+b00)-)-

(c(c22+c+c00))Z=(aZ=(a11-a-a00)(b)(b00-b-b11)=-(a)=-(a11bb11)+(a)+(a11bb00)+(a)+(a00bb11)-(a)-(a00bb00) ) (a(a11bb00)+(a)+(a00bb11)=(a)=(a11bb11)+(a)+(a00bb00)+Z .)+Z .

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

4-13Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “Introduction to the Design & Analysis of Algorithms,” 2nd ed., Ch. 4

Examples of Solving Problems by ReductionExamples of Solving Problems by Reduction

computing lcm(computing lcm(mm, , nn) via computing gcd() via computing gcd(m, nm, n))

counting number of paths of length counting number of paths of length n n in a graph by raising in a graph by raising the graph’s adjacency matrix to the the graph’s adjacency matrix to the n-n-th powerth power

transforming a maximization problem to a minimization transforming a maximization problem to a minimization problem and vice versa (also, min-heap construction)problem and vice versa (also, min-heap construction)

linear programminglinear programming

reduction to graph problems (e.g., solving puzzles via state-reduction to graph problems (e.g., solving puzzles via state-space graphs) space graphs)