LEC 7 and 8-5
LEC 7 and 8-5
COMP 222
Lecture 7+8
10/16/2024
Topics covered in coming weeks
• Finding the median
• Matrix multiplication
• Exponentiation
• Quick look at cryptography
Find The smallest/largest element of unsorted
array
Find The smallest/largest element of unsorted
array
1 2 3 4 5 6 7 8 9
4 5 1 3 7 10 11 19 8
Find The smallest/largest element of unsorted
array
Quick Sort : Take 7 as Pivot value and find its position in an array
ARR={4,5,10,11,1,3,19,8,7}
After Partitioning ARR= {4,5,1,3,7,10,11,19,8}
1 2 3 4 5 6 7 8 9
4 5 1 3 7 10 11 19 8
1 2 3 4 5 6 7 8 9
4 5 1 3 7 10 11 19 8
Find The smallest/largest element of unsorted
array
Quick Select: Take 7 as Pivot value and find its position in an
array
ARR={4,5,10,11,1,3,19,8,7}
After Partitioning ARR= {4,5,1,3,7,10,11,19,8}
1 2 3 4 5 6 7 8 9
4 5 1 3 7 10 11 19 8
1 2 3 4 5 6 7 8 9
4 5 1 3 7 10 11 19 8
Find The smallest/largest element of unsorted
array
Quick Select : Take 7 as Pivot value and find its position in an
array
ARR={4,5,10,11,1,3,19,8,7}
After Partitioning ARR= {4,5,1,3,7,10,11,19,8}
1 2 3 4 5 6 7 8 9
4 5 1 3 7 10 11 19 8
1 2 3 4 5 6 7 8 9
4 5 1 3 7 8 10 11 19
Quick Select
Selection Problem
• Suppose we are given a set of N
numbers
• Define the rank of an element to be
one plus the number of the
elements that are smaller.
• Thus, the rank of the element is its
final position if the set is sorted. The
minimum is of rank of 1 and the
maximum is of rank n
Median and Selection
Median
Median
Median and Selection
Median and Selection
Median and Selection
Median and Selection
PARTIONING
Algorithm
Algorithm
Algorithm
Algorithm
Example
Analysis
DIVIDE AND CONQUER
Matrix Multiplication
Basic Matrix Multiplication
N
Time analysis Ci , j ai ,k bk , j
k 1
N N N
Thus T ( N ) c cN 3 O( N 3 )
i 1 j 1 k 1
Basic Matrix Multiplication
N
Time analysis Ci , j ai ,k bk , j
k 1
N N N
Thus T ( N ) c cN 3 O( N 3 )
i 1 j 1 k 1
DIVIDE AND CONQUER
DIVIDE AND CONQUER
DIVIDE AND CONQUER
DIVIDE AND CONQUER
Strassens’s Matrix Multiplication
C11 = P1 + P4 - P5 + P7
= (A11+ A22)(B11+B22) + A22 * (B21 - B11) - (A11 + A12) * B22+
(A12 - A22) * (B21 + B22)
= A11 B11 + A11 B22 + A22 B11 + A22 B22 + A22 B21 – A22 B11 -
A11 B22 -A12 B22 + A12 B21 + A12 B22 – A22 B21 – A22 B22
= A11 B11 + A12 B21
Strassen Algorithm
void matmul(int *A, int *B, int *R, int n) {
if (n == 1) {
(*R) += (*A) * (*B);
} else {
matmul(A, B, R, n/4);
matmul(A, B+(n/4), R+(n/4), n/4);
matmul(A+2*(n/4), B, R+2*(n/4), n/4);
matmul(A+2*(n/4), B+(n/4), R+3*(n/4), n/4);
matmul(A+(n/4), B+2*(n/4), R, n/4);
matmul(A+(n/4), B+3*(n/4), R+(n/4), n/4);
matmul(A+3*(n/4), B+2*(n/4), R+2*(n/4), n/4);
matmul(A+3*(n/4), B+3*(n/4), R+3*(n/4), n/4);
Divide matrices in
}
sub-matrices and
recursively multiply
sub-matrices
Time Analysis
Exponentiation
an How many multiplications
e.g. a27 a26
= a.a26 =a(a(a6 )2 )2
= a.(a13)2 =a(a((a3)2 )2 )2
= a(a.a12)2 =a(a((a.a2)2 )2 )2
a if n=1
an (an/2)2 if n is even
a.an-1 if n is odd
Exponentiation
Function exp(a,n){
if n=1 return a;
if n is even then return[exp(a,n/2)]2
return a * exp(a,n-1)
0 if n=1
T(n) T(n/2)+1 if n is even
T(n-1)+1 if n is odd