Divide and Conquer Algorithm - Lecture Notes | CSE 830, Study notes of Computer Science

Material Type: Notes; Professor: Torng; Class: Design & Theory of Algorithms; Subject: Computer Science & Engineering; University: Michigan State University; Term: Unknown 1989;

Typology: Study notes

Pre 2010

Uploaded on 07/23/2009

koofers-user-fnb-1
koofers-user-fnb-1 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lecture 6
Divide-and-Conquer Algorithms
Quicksort Algorithm and Multiplication/Exponentiation Problems
Quicksort Algorithm
Choose a partition element
Partition list of elements into smaller and larger lists
Recurse on smaller and larger lists
Running time analysis
T(n) = T(smaller) + T(larger) + (n)
Best-case: T(smaller) and T(larger) are both T(n/2)
T(n) = 2 T(n/2) + (n)
T(1) = 1
Apply master theorem to see result as (n log n)
Worst-case: T(smaller) or T(larger) = T(n-1) [other is T(0)]
T(n) = T(n-1) + (n)
Applying inhomogeneous equation approach, we get root=1 with mult. 3
This leads to an (n2) complexity
This can also be observed by unrolling the recurrence
Average-case analysis?
Assumptions:
All items are distinct
Number of comparisons needed to partition array is n-1
All permutations are equally likely
t(n) = n-1 + 1/n[i=1 to n t(i-1) + t(n-i)] for n >= 1
t(0) = 0
i=1 to n t(n-i) = t(n-1) + t(n-2) + … + t(0) = i=1 to n t(i-1)
t(n) = n-1 + 2/n k=0 to n-1 t(k)
Constructive induction or clever manipulation leads to (n log n) result
Can we modify how quicksort chooses its partition element to guarantee that we get (n log n)
behavior in the worst case?
pf3
pf4

Partial preview of the text

Download Divide and Conquer Algorithm - Lecture Notes | CSE 830 and more Study notes Computer Science in PDF only on Docsity!

Lecture 6 Divide-and-Conquer Algorithms Quicksort Algorithm and Multiplication/Exponentiation Problems Quicksort Algorithm Choose a partition element Partition list of elements into smaller and larger lists Recurse on smaller and larger lists Running time analysis T(n) = T(smaller) + T(larger) + (n) Best-case: T(smaller) and T(larger) are both T(n/2) T(n) = 2 T(n/2) + (n) T(1) = 1 Apply master theorem to see result as (n log n) Worst-case: T(smaller) or T(larger) = T(n-1) [other is T(0)] T(n) = T(n-1) + (n) Applying inhomogeneous equation approach, we get root=1 with mult. 3 This leads to an (n^2 ) complexity This can also be observed by unrolling the recurrence Average-case analysis? Assumptions: All items are distinct Number of comparisons needed to partition array is n- All permutations are equally likely t(n) = n-1 + 1/n[i=1 to n t(i-1) + t(n-i)] for n >= 1 t(0) = 0 i=1 to n t(n-i) = t(n-1) + t(n-2) + … + t(0) = i=1 to n t(i-1) t(n) = n-1 + 2/n k=0 to n-1 t(k) Constructive induction or clever manipulation leads to (n log n) result Can we modify how quicksort chooses its partition element to guarantee that we get (n log n) behavior in the worst case?

Matrix Multiplication How many operations are needed to multiply two 2 by 2 matrices? Traditional approach 8 (two-number) multiplications and 4 additions Strassen’s approach (1969) 7 (two-number) multiplications and 15 additions/subtractions (See problem 7.22) How does this change things for multiplying n by n matrices? Lets assume that n is a power of 2. Traditional approach n^3 two-number multiplications n^3 – n^2 two-number additions Complexity is (n^3 ) Divide-and-conquer approach T(n) = 7 T(n/2) + 18 matrix additions of matrices of dimension n/ = 7 T(n/2) + 18 (n^2 /4)

  • each matrix addition subtraction involves n^2 /4 (two number) ops Applying the master method, we get that the complexity is (nlg 7) Can we do better than 7 multiplications for a 2 by 2 matrix? No, Hopcroft and Kerr have shown that 7 multiplications are necessary Comparison n = 70 Direct multiplication: 70^3 = 343, Strassen’s method: 70lg 7^ ~ 150, Current best known matrix multiplication is O(n2.376) by Coppersmith and Winograd Hidden constants make this impractical Conjecture: Two n by n matrices can be multiplied in time O(n2 +^ ) 0 <  < 1 Matrix multiplication can sometimes be used in graph algorithms as a fundamental step so theoretical improvements in the efficiency of this operation can lead theoretical improvements in those algorithms
  • edges in a graph can be represented as a matrix Homework
  • For a k by k multiplication, how many two number multiplications can be done while improving on Strassen’s algorithm
  • What effect does the number of additions have on the final complexity of the algorithm?

What is the time required by this approach? T(m,n) <= 0 if n = 1 <= T(m,n/2) + M(mn/2, mn/2) if n is even <= T(m,n-1) + M(m, (n-1)m) otherwise Combining both cases together: T(m,n) <= T(m, floor(n/2)) + M(m floor(n/2), m floor(n/2)) + M(m, (n-1)m) Problem 7.29: This evaluates to O(mana) assuming M(q,s) = (sqa-1) Direct multiplication: a = 2 D-and-C multiplication: a = lg 3 Lower bound argument: afloor(n/2)afloor(n/2)^ : M((m-1)n/2, (m-1)n/2) = (((m-1)(n/2))a) = (mana) Good summary table on bottom of pg 246 It is interesting to note that the D-and-C exponentiation algorithm gains little unless the D-and-C multiplication algorithm is also used. Iterative version of this algorithm is given on pg. 247. Exponentiation with modular arithmetic xy mod z = [(x mod z) times (y mod z)] mod z xy^ mod z = (x mod z)y^ mod z With modular arithmetic, all multiplications can be treated as essentially unit cost operations Thus, we see that D-and-C modular exponentiation requires only (log n) multiplications The direct approach requires n-1 such multiplications Application to public-key cryptography: Alice is sending a message m to Bob Bob does the following first Chooses two 100-digit prime numbers p and q Computes z = pq Computes  = (p-1)(q-1) Chooses n uniformly at random from 1 to z- Computes the unique number s between 1 and z-1 such that ns mod  = 1 Problem 7.31 describe how to compute s If no such s can be found, then must choose another n Key property of n is that n must have no common factors with  Bob no longer needs (p, q, ) for any more computations Bob publishes z and n but keeps s (and p, q, ) secret Alice encrypts her message m to Bob as follows: Assume m can be represented as a number between 0 and z- Alice compute c = mn^ mod z Bob decrypts message m as follows Bob computes cs^ mod z = (mn^ mod z)s^ mod z = (mn)s^ mod z = (mns) mod z = m because of the following result from number theory mx^ mod z = m if 0 <= m <= z-1 AND x mod  = 1 What does an eavesdropper who intercepts c and has n and z do? Needs to compute the nth root of c mod z; no efficient solution is known Best known approach Factor z into p and q, Compute  as (p-1)(q-1), compute s, then proceed as Bob did No efficient algorithm is known for factorization