1 / 13

7.3 Divide-and-Conquer Algorithms and Recurrence Relations

An overview of divide-and-conquer algorithms and recurrence relations, with examples such as binary search, merge sort, and fast multiplication of integers. Includes the Master Theorem and estimation of complexity.

juanah
Download Presentation

7.3 Divide-and-Conquer Algorithms and Recurrence Relations

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. 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 that f satisfies the recurrence relation f(n) = af(n/b) + g(n) this is called a divide-and-conquer algorithms and recurrence relations • Example 1:Binary Search We introduced a binary search algorithm in section 3.1 .

  2. The binary search • Algorithm 3: the binary search algorithm Procedure binary search (x: integer, a1, a2, …,an: increasing integers) i :=1 { i is left endpoint of search interval} j :=n { j is right endpoint of search interval} While i < j begin m := (i + j) / 2 if x > am then i := m+1 else j := m end If x = ai then location := i else location :=0 {location is the subscript of the term equal to x, or 0 if x is not found}

  3. Divide-and-Conquer Algorithms and Recurrence Relations • Example 3: Merge Sort • Algorithm 9A Recursive Merge Sort. Procedure mergesort(L = a1, . . . , an) If n > 1 then m := n/ 2 L1 := a1, a2, . . . ,am L2 := am+1, am+2, . . ., an L := merge (mergesort(L1), mergesort(L2)) {L is now sorted into elements in nondecreasing order}

  4. Divide-and-Conquer Algorithms and Recurrence Relations • Example 4: Fast Multiplication of Integers • There are more efficient algorithms than the conventional algorithm (described in section 3.6) for multiplying integers. • Suppose that a and b are integers with binary expansions of length 2n. • Let a = (a2n-1 a2n-2 . . . a1 a0)2 ,b = (a2n-1 a2n-2 . . . a1 a0)2 • Let a = 2nA1 + A0 , b = 2nB1 + B0 A1= (a2n-1 . . . an+1 an)2 , A0= (an-1 . . . a1 a0)2, B1 = (b2n-1 . . . bn+1 bn)2 , B0= (bn-1 . . . b1 b0)2 , • the algorithm for fast multiplication of integers is based on the fact that ab can be rewritten as • ab= (22n+2n) A1 B1 + 2n(A1 - A0) (B0 - B1 ) +(2n +1)A0B0 .

  5. Divide-and-Conquer Algorithms and Recurrence Relations • This shows that if f(n) is the total number of bit operations needed to multiply two n-bit integers, thenf(2n) = 3f(n) + Cn • This reasoning behind this equation is as follows. • The three multiplications of n-bit integers are carried out using 3f(n)-bit operations.

  6. Divide-and-Conquer Algorithms and Recurrence Relations • Theorem 1: let f be an increasing function that satisfies the recurrence relation f(n)= af(n/b) + c • Whenever n is divisible by b, where a≧1 ,b is an integer greater than 1, and c is a positive real number . Then f(n) is O(nlogb a) if a >1 , O(log n) if a =1 • when n=bk,where k is a positive integer, f(n) = C1nlogba +C2= C1ak+C2 where C1= f(1)+c/(a-1) and C2 = -c/(a-1).

  7. Divide-and-Conquer Algorithms and Recurrence Relations • Example 6: Let f(n) = 5f(n/2) + 3 and f(1) =7. Find f(2k), where k is a positive integer. Also , estimate f(n) if f is an increase function. • Example 7: Estimate the number of comparisons used by a binary search. f(n) = f(n/2) + 2. • Example 8: Estimate the number of comparisons used to locate the maximum and minimum elements in a sequence using the algorithm given in example 2. f(n) = 2f(n/2) + 2.

  8. Divide-and-Conquer Algorithms and Recurrence Relations • Theorem 2: Master Theorem Let f be an increasing function that satisfies the recurrence relation f(n)= af(n/b) + cnd Whenever n=bk, where k is a positive integer, a≧1, b is an integer greater than 1, and c and d are real numbers with c positive and d nonnegative. Then f(n) is O(nd) if a < bd, O(nd log n) if a= bd , O(n logba) if a > bd.

  9. Divide-and-Conquer Algorithms and Recurrence Relations • Example 9: Complexity of Merge Sort In Example 3 we explained that the number of comparisons usedby the merge sort to sort a list of n elements is less than M(n), where M(n)=2 M(n/2) +n . By the Master Theorem (Theorem 2) we find that M(n) is O(n logn) , which agrees with the estimate found in section 4.4. • Example 10: Estimate the number of bit operations needed to multiply two n-bit integers using the fast multiplication algorithm described in Example 4.

  10. Divide-and-Conquer Algorithms and Recurrence Relations • Example 11 : Estimate the number of multiplications and additions required to multiply two n x n matrices using the matrix multiplication algorithm referred to in example 5. The function for the number of multiplications and additions is f(n)=7f(n/2)+(15/4)n2.

  11. Divide-and-Conquer Algorithms and Recurrence Relations • Example 12: The Closest-Pair Problem Consider the problem of determining the closest pair of points in a set of n points (x1,y2),. . ., (xn,yn) in the plane, where the distance between two points (xi,yi) and (xj,yj) is the usual Euclidean distance [ (xi -xj)2+ (yi - yj)2 ]0.5. • This problem arises in many applications such as determining the closest pair of airplanes in the air space at a particular altitude being managed by an air traffic controller. • How can this closest pair of points be found in an efficient way? (FIGURE 1 )

  12. Divide-and-Conquer Algorithms and Recurrence Relations FIGURE 1 The Recursive Step of the Algorithm for Solving the Closest-Pair Problem.

  13. Divide-and-Conquer Algorithms and Recurrence Relations FIGURE 2 Showing That There Are at Most Seven Other Points to Consider for Each Point in the Strip.

More Related