1 / 25

CSCE 310: Data Structures & Algorithms

CSCE 310: Data Structures & Algorithms. Divide and conquer examples. Divide and conquer examples. Binary search Large integer multiplication Strassen’s matrix multiplication. Sequential Search. Algorithm SequentialSearch(A[0…n-1], K)

jael-horne
Download Presentation

CSCE 310: Data Structures & Algorithms

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. CSCE 310: Data Structures & Algorithms Divide and conquer examples

  2. Divide and conquer examples • Binary search • Large integer multiplication • Strassen’s matrix multiplication

  3. Sequential Search Algorithm SequentialSearch(A[0…n-1], K) //Searches for a given value in a given array by sequential search //Input: An Array A[0…n-1] and a search key K //Output: Returns the index of the first element of A that matches K // or -1 if there are no matching elements i  0 while i < n and A[i]  K do i  i+1 if i < n return i else return -1

  4. Searching algorithm • Searching a key in a sorted list • A[0]  A[1]  …  A[m-1]  A[m]  …  A[n-1]

  5. Binary search K A[0]  …  A[m-1]  A[m]  A[m+1]…  A[n-1] search here if K < A[m] search here if K > A[m]

  6. Binary search (divide and conquer) Algorithm BinarySearch(A[0…n-1], K) //Implements nonrecursive binary search //Input: An Array A[0…n-1] sorted in ascending order and a search key K //Output: An index of an element of A that matches K // or -1 if there are no matching elements left  0; right  n-1 while left  right do middle  (left + right) / 2 if K = A[m] return m else if K < A[m] right  m -1 else left  m + 1 return -1

  7. Search algorithm efficiency • Sequential search: (n) • Binary search: (log2n) • no more than 10 iterations, 10*2 comparisons to find an element in a sorted list of 1000 elements • no more than 20 iterations, 20*2 comparisons for a sorted list of one million!

  8. Improved insertion • Binary search technique can be used to improve the insertion into an ordered array list

  9. - - - - - - Insertion sort

  10. Improved insertion • Binary search technique can be used to improve the insertion into an ordered array list • First, using the binary search to find the location where the new element should be inserted • Second, insert the new element into the array list

  11. BinaryInsertionSort Algorithm BinaryInsertionSort(A[0…n-1]) //Sorts a given array by binary insertion sort //Input: An array A[0…n-1] of n orderable elements //Output: Array A[0…n-1] sorted in nondecreasing order for i  1 to n -1 do v  A[i]; left  0; right  i; while left < right do middle  (left + right) / 2; if v  A[middle] left  middle + 1; else right  middle; for j  i to left + 1 do A[j]  A[j-1]; A[left]  v

  12. In-Class Exercise (4.3.1)

  13. Multiplication of large integers • a , b are both n-digit integers • If we use the brute-force approach to compute c = a * b, what is the time efficiency?

  14. Multiplication of large integers • a = a1a0 • b = b1b0 • c = a * b = (a110n/2 + a0) * (b110n/2 + b0) =(a1 * b1)10n + (a1 * b0 + a0 * b1)10n/2 + (a0 * b0) For instance: a = 123456, b = 117933: Then c = a * b = (123*103+456)*(117*103+933) =(123* 117)106 + (123 * 933 + 456 * 117)103 + (456 * 933)

  15. Multiplication of large integers • a = a1a0 • b = b1b0 • c = a * b = (a110n/2 + a0) * (b110n/2 + b0) =(a1 * b1)10n + (a1 * b0 + a0 * b1)10n/2 + (a0 * b0) =c210n + c110n/2 + c0, where c2 = a1 * b1 isthe product of their first half c0 = a0 * b0 isthe product of their second half c1 = (a1 + a0) * (b1 + b0) – (c2 + c0) is the product of the sum of the a’s halves and the sum of the b’s halves minus the sum of c2 and c0.

  16. Multiplication of large integers • c =c210n + c110n/2 + c0, where c2 = a1 * b1 isthe product of their first half c0 = a0 * b0 isthe product of their second half c1 = (a1 + a0) * (b1 + b0) – (c2 + c0) is the product of the sum of the a’s halves and the sum of the b’s halves minus the sum of c2 and c0 Multiplication of n-digit numbers requires three multiplications of n/2-digit numbers

  17. Multiplication of large integers • M(n) = 3M(n/2) for n>1, M(1) = 1

  18. Multiplication of large integers • M(n) = 3M(n/2) for n>1, M(1) = 1 • M(n)  n1.585

  19. - - - - = - - - Matrix multiplication (brute force] multiplication: (n3) addition: (n3)

  20. A, B: n by n matrices; Aij, Bij: n/2 by n/2 matrices, where i, j  {0, 1} recurrence relations: multiplication: M(n) = ? addition: A(n) = ? Matrix multiplication (divide-conquer recursive algorithm]

  21. Matrix multiplication (divide-conquer] A, B: n by n matrices; Aij, Bij: n/2 by n/2 matrices, where i, j  {0, 1} multiplication: (n3) addition: (n3)

  22. Strassen’s matrix multiplication M1=(A00+A11)*(B00+B11) M2=(A10+A11)*B00 M3=A00*(B01-B11) M4=A11*(B10-B00) M5=(A00+A01)*B11 M6=(A10-A00)*(B00+B01) M7=(A01-A11)*(B10+B11) recurrence relations: multiplication: M(n) = ? addition: A(n) = ?

  23. Strassen’s matrix multiplication M1=(A00+A11)*(B00+B11) M2=(A10+A11)*B00 M3=A00*(B01-B11) M4=A11*(B10-B00) M5=(A00+A01)*B11 M6=(A10-A00)*(B00+B01) M7=(A01-A11)*(B10+B11) M(n) (n2.807) A(n)(n2.807)

  24. In-Class Exercise • Given a sorted array of distinct integers A[1…n], you want to find out whether there is an index i for which A[i] = i. Given a dive-and-conquer algorithm that runs in time O(logn).

  25. Announcement • First midterm • Time: 9:30am-10:20am Feb 27 • Format: open book/open notes exam • Focus: • Chapter 2 • Asymptotic notations • Algorithm analysis • Solving recurrence relations • Chapter 4 • Mergesort, Quicksort

More Related