250 likes | 442 Views
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)
E N D
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) //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
Searching algorithm • Searching a key in a sorted list • A[0] A[1] … A[m-1] A[m] … A[n-1]
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]
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
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!
Improved insertion • Binary search technique can be used to improve the insertion into an ordered array list
- - - - - - Insertion sort
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
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
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?
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)
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.
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
Multiplication of large integers • M(n) = 3M(n/2) for n>1, M(1) = 1
Multiplication of large integers • M(n) = 3M(n/2) for n>1, M(1) = 1 • M(n) n1.585
- - - - = - - - Matrix multiplication (brute force] multiplication: (n3) addition: (n3)
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]
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)
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) = ?
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)
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).
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