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

Slides:



Advertisements
Similar presentations
Divide-and-conquer: closest pair (Chap.33) Given a set of points, find the closest pair (measured in Euclidean distance) Brute-force method: O(n 2 ). Divide-and-conquer.
Advertisements

Divide-and-Conquer The most-well known algorithm design strategy:
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
1 Divide-and-Conquer The most-well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances 2. Solve smaller instances.
Lectures on Recursive Algorithms1 COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski.
Chapter 6: Transform and Conquer
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 6 Ack : Carola Wenk nad Dr. Thomas Ottmann tutorials.
Divide and Conquer. Recall Complexity Analysis – Comparison of algorithm – Big O Simplification From source code – Recursive.
6-0 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 6 LECTURE.
Advanced Algorithm Design and Analysis (Lecture 10) SW5 fall 2004 Simonas Šaltenis E1-215b
Chapter 4 Divide-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Introduction to Algorithms Rabie A. Ramadan rabieramadan.org 4 Some of the sides are exported from different sources.
Algorithm Design Strategy Divide and Conquer. More examples of Divide and Conquer  Review of Divide & Conquer Concept  More examples  Finding closest.
Chapter 4 Divide-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Chapter 4: Divide and Conquer The Design and Analysis of Algorithms.
Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Chapter 3 Brute Force Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Design and Analysis of Algorithms - Chapter 21 Analysis of Algorithms b Issues: CorrectnessCorrectness Time efficiencyTime efficiency Space efficiencySpace.
5 - 1 § 5 The Divide-and-Conquer Strategy e.g. find the maximum of a set S of n numbers.
Chapter 8 Dynamic Programming Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
CSE 421 Algorithms Richard Anderson Lecture 13 Divide and Conquer.
Design and Analysis of Algorithms - Chapter 41 Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two.
Chapter 6 Transform-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Chapter 11 Limitations of Algorithm Power Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Lecture 6 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Design and Analysis of Algorithms - Chapter 21 Analysis of Algorithms b Issues: CorrectnessCorrectness Time efficiencyTime efficiency Space efficiencySpace.
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Chapter.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 5 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Chapter 6 Transform-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Chapter 6 Transform-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Transform and Conquer This group of techniques solves a problem by a transformation to a simpler/more convenient instance of the same problem (instance.
5-1-1 CSC401 – Analysis of Algorithms Chapter 5--1 The Greedy Method Objectives Introduce the Brute Force method and the Greedy Method Compare the solutions.
Lecture 5 Jianjun Hu Department of Computer Science and Engineering University of South Carolina CSCE350 Algorithms and Data Structure.
Chapter 3 Brute Force. A straightforward approach, usually based directly on the problem’s statement and definitions of the concepts involved Examples:
Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 1 Chapter.
Divide-and-Conquer. Outline Introduction Merge Sort Quick Sort Closest pair of points Large integer multiplication.
CSE 421 Algorithms Lecture 15 Closest Pair, Multiplication.
7.3 Divide-and-Conquer Algorithms and Recurrence Relations If f(n) represents the number of operations required to solve the problem of size n, it follow.
2-0 Copyright © 2007 Pearson Addison-Wesley. All rights reserved. A. Levitin “ Introduction to the Design & Analysis of Algorithms, ” 2 nd ed., Ch. 2 Theoretical.
Chapter 7 Dynamic Programming 7.1 Introduction 7.2 The Longest Common Subsequence Problem 7.3 Matrix Chain Multiplication 7.4 The dynamic Programming Paradigm.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 6 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
A. Levitin “Introduction to the Design & Analysis of Algorithms,” 3rd ed., Ch. 2 ©2012 Pearson Education, Inc. Upper Saddle River, NJ. All Rights Reserved.
Chapter 3 Brute Force Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Chapter 6 Transform-and-Conquer Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Chapter 2 Fundamentals of the Analysis of Algorithm Efficiency Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Advanced Algorithms Analysis and Design
Chapter 4 Divide-and-Conquer
Analysis of algorithms
Chapter 6 Transform-and-Conquer
Divide-and-Conquer The most-well known algorithm design strategy:
Chapter 4 Divide-and-Conquer
Transform and Conquer This group of techniques solves a problem by a transformation to a simpler/more convenient instance [eg sorted] of the same problem.
Transform and Conquer This group of techniques solves a problem by a transformation to a simpler/more convenient instance of the same problem (instance.
Divide-and-Conquer The most-well known algorithm design strategy:
Punya Biswas Lecture 15 Closest Pair, Multiplication
Decrease-and-Conquer
Chapter 3 Brute Force Copyright © 2007 Pearson Addison-Wesley. All rights reserved.
Chapter 6: Transform and Conquer
Divide-and-Conquer The most-well known algorithm design strategy:
Chapter 4 Divide-and-Conquer
Transform and Conquer This group of techniques solves a problem by a transformation to a simpler/more convenient instance of the same problem (instance.
Transform and Conquer Transform and Conquer Transform and Conquer.
Divide-and-Conquer The most-well known algorithm design strategy:
Transform and Conquer Transform and Conquer Transform and Conquer.
CSC 380: Design and Analysis of Algorithms
CSC 380: Design and Analysis of Algorithms
Analysis of algorithms
CSC 380: Design and Analysis of Algorithms
Lecture 15 Closest Pair, Multiplication
Presentation transcript:

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

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 ).

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-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)!

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.

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.

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

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: coefficients ie for x=3 Efficiency of Horner: = 2n = Θ(n)

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)

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 = binary rep. of 13: SM SM S SM accumulator: *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

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 = 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

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= and 14= b In general: c=a*b=c n +c n/2 +c 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.

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)