[go: up one dir, main page]

0% found this document useful (0 votes)
16 views10 pages

Divide and Conquer3-1

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1/ 10

Lecture 4

Divide and Conquer


Selection sort
• Idea:
Find the smallest element in the array
Exchange it with the element in the first position
Find the second smallest element and exchange it with the element in the second position
Continue until the array is sorted

2
Algorithm

SELECTION-SORT(A)
n ← length[A]
for j ← 1 to n - 1
do smallest ← j
for i ← j + 1 to n
do if A[i] < A[smallest]
then smallest ← i
exchange A[j] ↔ A[smallest]

3
Analysis of Selection sort
Selection Sort(A) Cost Times

n ← length[A] C1 1
for j ← 1 to n - 1 C2 n
do smallest ← j C3 (n-1)


n 1
for i ← j + 1 to n C4 (n  j  1)
j 1

do if A[i] < A[smallest] C5 


n 1
(n  j )
j 1

then smallest ← i C6

n 1
j 1
(n  j )
exchange A[j] ↔ A[smallest] C7 (n-1)

4
Strassen’s matrix multiplication
The problem:

n  n
Multiply two matrices A and B, each of size

     
 A   B   C 
     
  nn   nn   nn

5
The conventional method

c00 c01 a00 a01 b00 b01


3 for loops
= *
c10 c11 a10 a11 b10 b11

a00 * b00 + a01 * b10 a00 * b01 + a01 * b11


=
a10 * b00 + a11 * b10 a10 * b01 + a11 * b11

n
C ij   Aik  Bkj
k 1 Time complexity =O(N3)

6
The divide and conquer approach

ae+bg af+bh a b e f
= *
ce+dg cf+dh c d g h 8 multiplications
C A B

• A,B and C are square matrices of size N  N  4 additions


• a, b, c, d are sub-matrices of A, of size N / 2  N / 2
• e, f, g, h are sub-matrices of B, of size N / 2  N / 2

T(N) = 8T(N/2) + O(N2) = O(N3)

7
Strassen’s Matrix Multiplication
c00 c01 a00 a01 b00 b01
= *
c10 c11 a10 a11 b10 b11
7 multiplications
m1+ m4- m5 + m7 m3 + m5
=
m2 + m4 m1+ m3 - m2 + m6

6 additions
m1 = (a00 + a11) * (b00 + b11)
4 subtractions
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)
8
Analysis of Strassen’s method

T(n) = 7T(n/2) + O(n2) = O(n3)

T(n) = 7log 2n = nlog 27 ≈ n2.807

T(n) = n2.807 < n3

Strassen’s matrix multiplication has better asymptotic efficiency but is not used
in practice.

9
Reasons

The constants used in Strassen’s method are high and for a


typical application conventional method works better.

The submatrices in recursion take extra space.

Because of the limited precision of computer arithmetic on


noninteger values, larger errors accumulate in Strassen’s
algorithm than in conventional method

10

You might also like