0% found this document useful (0 votes)
14 views41 pages

LEC 7 and 8-5

Uploaded by

tanveer457928
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views41 pages

LEC 7 and 8-5

Uploaded by

tanveer457928
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 41

Design and Analysis of Algorithm

COMP 222
Lecture 7+8

Dr. Huma Ayub


[email protected]

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

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
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

Suppose we want to multiply two matrices of size N


x N: for example A x B = C.

C11 = a11b11 + a12b21 2x2 matrix multiplication can be


accomplished in 8 multiplication.(23) and 4
C12 = a11b12 + a12b22 addition

C21 = a21b11 + a22b21


Multiplication O(N3) and addition O(N2)
C22 = a21b12 + a22b22
Basic Matrix Multiplication
void matrix_mult (){
for (i = 1; i <= N; i++) { algorithm
for (j = 1; j <= N; j++) {
for (k = 1; k <= N; k++)
compute Ci,j;
}
}}

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

Suppose we want to multiply two matrices of size N


x N: for example A x B = C.

C11 = a11b11 + a12b21


C12 = a11b12 + a12b22
C21 = a21b11 + a22b21
2x2 matrix multiplication can be
C22 = a21b12 + a22b22 accomplished in 8 multiplication.(2log28 =23)
Basic Matrix Multiplication
void matrix_mult (){
for (i = 1; i <= N; i++) { algorithm
for (j = 1; j <= N; j++) {
for (k = 1; k <= N; k++)
compute Ci,j;
}
}}

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

• Strassen showed that 2x2 matrix multiplication can be accomplished


in 7 multiplication and 18 additions or subtractions. .(2log27 =22.807)

• This reduce can be done by Divide and Conquer Approach.


Strassens’s Matrix Multiplication

P1 = (A11+ A22)(B11+B22) C11 = P1 + P4 - P5 + P7


P2 = (A21 + A22) * B11 C12 = P3 + P5
P3 = A11 * (B12 - B22) C21 = P2 + P4
P4 = A22 * (B21 - B11) C22 = P1 + P3 - P2 + P6
P5 = (A11 + A12) * B22
P6 = (A21 - A11) * (B11 + B12)
P7 = (A12 - A22) * (B21 + B22)
Comparison

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

You might also like