Professional Documents
Culture Documents
1
Search Problem
• Input: A set of numbers a1,a2,…,an
and a number x
• Question: Is x one of the numbers ai in the
given set?
Binary Search
• Compare x to the middle element of the
array A[ n 2 ]
?
2
Binary Search Analysis
T (n 2) (1) for n 1
T (n )
(1) for n 1
Merge Sort
1. Divide: Divide the given n-element sequence to
be sorted into two sequences of length n/2
2. Conquer: Sort recursively the two subsequences
using Merge Sort
3. Combine: Merge the two sorted subsequences
to produce the sorted answer
3
Merge
• Input: Sorted arrays K[1..n1], L[1..n2]
• Output: Merged sorted array M[1.. n1+n2]
i 1, j 1
for t 1 to n1 n 2
{ if (i n1 and ( j n 2 or K[i] L[j]) )
then { M[t] K[i], i i 1 }
else { M[t] L[j], j j 1 }
}
4
Merge-Sort
Merge-Sort A[1…n]
If n 1 then
1. Recursively merge-sort A[1 n / 2 ]
and A[ n / 2 1 n ]
2. Merge the two sorted subsequences
Analysis of Merge-Sort
5
Maximum Sum Subarray Problem
• Input: Array A[1…n] of integers (positive and negative)
• Problem: Compute a subarray A[i*…j*] with maximum sum
i.e., if s(i,j) denotes the
j
sum of the elements of a subarray
A[i…j], s (i , j )
k i
A[ k ]
Example: 3 -4 5 -2 -2 6 -3 5 -3 2
max sum = 9
6
Brute force solution - improved
• With a little more care, can improve to (n2):
• Can compute the sums for all subarrays with same left end
in O(n) time compute the sums of all the subarrays
(there are n(n-1)/2 +n subarrays) in time (n2)
for i =1 to n
{ s(i,i)=A[i]
for j=i+1 to n
s(i,j) = s(i,j-1)+A[i,j]
}
7
Divide and Conquer analysis
• Recurrence: T(n) = 2T(n/2) + (n)
• Solution: T(n) = (n logn)
A Ah Al A Ah 2n / 2 Al
B Bh Bl B Bh 2n / 2 Bl
A B ( Ah 2n / 2 Al ) (Bh 2n / 2 Bl )
Ah Bh 2n Ah Bl 2n / 2 Al Bh 2n / 2 Al Bl
8
Multiplication of Big integers
A B ( Ah 2n / 2 Al ) (Bh 2n / 2 Bl )
Ah Bh 2n Ah Bl 2n / 2 Al Bh 2n / 2 Al Bl
( Ah Al )(Bl Bh ) Ah Bl Ah Bh Al Bl Al Bh
A B AhBh 2n [( Ah Al )(Bh Bl ) AhBh Al Bl ] 2n / 2 Al Bl
9
Multiplication of Big integers – Karatsuba’60
Recursive Algorithm MULT(A,B)
Write A = Ah2n/2 +Al and B = Bh2n/2+Bl
Compute a = Ah + Al and b = Bh +Bl
C = MULT(a,b)
Dh = MULT(Ah , Bh )
Dl = MULT(Al , Bl )
Return Dh 2 [C Dh Dl ] 2 Dl
n n/2
Matrix Multiplication
Input: Matrices A [aij ], B [bij ], i , j 1,..., n
Output: C [cij ] A B
C A B
j
col.j
i
c ij = row i
n
cij aik bkj
k 1
10
Standard Matrix Multiplication algorithm
for i = 1 to n
for j = 1 to n
{ cij = 0
for k = 1 to n
cij = cij + aik bkj
}
11
T (n ) 8T (n / 2) (n2 )
Case 1 T (n ) (n3 )
Strassen’s algorithm
• P = (A11 + A22)(B11 + B22)
Q = (A21 + A22)B11
R = A11(B12 - B22)
S = A22(B21 - B11)
T = (A11 + A12)B22
U = (A21 - A11)(B11 + B12)
V = (A12 - A22)(B21 + B22).
• C11 = P + S - T + V
C12 = R + T
C21 = Q + S
C22 = P + R - Q + U
12
Strassen’s algorithm
• Can multiply 2x2 matrices with 7 multiplications,
and 18 additions and subtractions. The method
does not assume commutativity of multiplication
• Method applies to multiplication of 2x2 block
matrices.
• Can be used in divide and conquer scheme with 7
recursive multiplications of n/2 x n/2 submatrices.
T (n ) 7 T (n / 2) (n 2 )
Strassen’s Algorithm
T ( n ) 7 ( n / 2) ( n 2 )
a 7, b 2, f (n ) (n 2 ), nlogb a nlog2 7 n 2.81
Case 1 T (n ) (nlog7 )
13