Docsity

Divide and Conquer Method - Lecture Slides | COT 5405, Study notes of Algorithms and Programming

Partial preview of the text

Download Divide and Conquer Method - Lecture Slides | COT 5405 and more Study notes Algorithms and Programming in PDF only on Docsity!

ANALYSIS OF ALGORITHMS

Sanjay Ranka

September 13, 2002

Divide and Conquer Method

At each level of recursion:

  1. Divide the problem into a number of subproblems.
  2. Conquer the problems by solving them recursively,

or, for small subproblems, solve in a straightforward

manner.

  1. Combine the solutions to the subproblem into the

solution of the original problem.

Merge Sort

Maximum Subsequence Problem: Algorithm

Step 1. Set M AXSU M = 0.

Step 2. Calculate s(i, j) =

∑j

k=i

a[k], and update the

M AXSU M by s(i, j) if s(i, j) > M AXSU M ; for

1 ≤ i ≤ j ≤ n.

Step 3. M AXSU M is the answer.

Analysis: In s(i, j) we sum (j − i + 1) terms.

Therefore the complexity of the algorithm is:

n ∑

i=

n ∑

j=i

j ∑

k=i

n ∑

i=

n ∑

j=i

(j − i + 1)

n ∑

i=

(n + i + 1)(n − i + 2)

n ∑

i=

{(n

2

  • 3n + 2) − (2n + 3)i + i

2

}

n

3

  • 3n

2

  • 2n

= O(n

3

Note that s(i, j) =

∑j

k=i

a[k] = s(i, j − 1) + a[j].

Step 4. Find M AXSU M =

max(M AXSU M (L), M AXSU M (R)).

Flaw - What happens if the maximum sum is due to

terms in the middle of of the sequence as in the

following example?

Here M AXSU M (L) is 6 due to sum of first three

terms, M AXSU M (R) is 8 due to sum of 6th and 7th

terms, and M AXSU M is 11 due to sum of terms 1

through 7.

calculate

BORDER(i, L) = BORDER(i + 1, L) + a[i].

Step 4b. If BORDER(i, L) > M AXSU M B

(L) then

M AXSU M

B

(L) = BORDER(i, L).

Step 5. As step 4 except for the right half of the sequence.

Step 6. Finally,

M AXSU M =

max(M AXSU M (L), M AXSU M (R),

M AXSU M

B

(L) + M AXSU M

B

(R)).

Analysis : If T (n) denotes the total number of basic

operations of addition, then T (n) = 2T (n/2) + n. Here

we have ignored the costs of managing of the stacks,

assignments etc. The term n represents the cost of

adding sums from the border to the edges. Solution of

this equation, with T (1) = 1 gives T (n) = n lg n + n.

Alternatively, let

U = 2

n

U 1

+ U

0

and V = 2

n

V 1

+ V

0

where U 1

= (u 2 n− 1

u 2 n− 2

... u n

2

and

U

0

= (u n− 1

u n− 2

... u 0

2

Likewise, V 1

= (v 2 n− 1

v 2 n− 2

... v n

2

and

V

0

= (v n− 1

u n− 2

... v 0

2

. Then,

U × V = 2

2 n

U 1

V

1

n

(U 1

V

0

+ U

0

V

1

) + U

0

V

0

Apparently there is no saving, this multiplication also

requires execution time Θ(n

2

).

Multiplication: Divide and Conquer

U ×V = (

2 n

n

)U 1

V

1

n

(U 1

−U

0

)(V

0

−V

1

n

+1)U 0

V

0

which requires only three multiplications and some

extra addition. If T (n) denotes time to multiply two

binary integers of size n each, then

T (2n) = 3T (n) + cn, and T (1) = 1,

where cn denotes the cost associated with additions and

shifting of binary integers etc.

Matrix multiplication

Let C = A × B = (c ij

) be the product of two n × n

matrices A and B, where

A =

                

a 11

a 12

... a 1 n

a 21

a 22

... a 2 n

a n 1

a n 2

... a nn

                

and B =

                

b 11

b 12

... b 1 n

b 21

b 22

... b 2 n

b n 1

b n 2

... b nn

                

  • We calculate n

2

terms, c ij

for

i = 1, 2 ,... , n, j = 1, 2 ,... , n.

  • Each c ij

requires n scalar multiplications.

  • Therefore, total complexity of this matrix

multiplication is of the order n

3

.

Suppose n = 2

k

for some integer value of k. Let

C =

           

r

. s

t

. u

           

= A×B =

           

a

. b

c

. d

           

×

           

e

. g

f

. h

           

where r, s, t, u are all

n

2

×

n

2

matrices.

  • In this way of obtaining C, by first partitioning A and

B in submatrices, we calculate 4 matrices

r, s, t, and u.

Matrix Multiplication: Strassen’s Algorithm

P

1

= a(g − h) = ag − ah

P

2

= (a + b)h = ah + bh

P

3

= (c + d)e = ce + de

P

4

= d(f − e) = df − de

P

5

= (a + d)(e + h) = ae + ah + de + dh

P

6

= (b − d)(f + h) = bf + bh − df − dh

and

P

7

= (a − c)(e + g) = ae + ag − ce − cg

Then, we can obtain r, s, t, and u as follows:

r = P 5

+ P

4

− P

2

+ P

6

s = P 1

+ P

2

t = P 3

+ P

4

u = P 5

+ P

1

− P

3

− P

7

For example,

P

5

+ P

4

− P

2

+ P

6

= (ae + ah + de + dh) + (df − de)

  • −(ah + bh) + (bf + bh − df − dh)

= ae + bf

= r