Suppose that we are given two polynomials of degree n 1 as

Suppose that we are given two polynomials of degree n = 1 as their arrays of coefficients, a_0..a_n-1 and b_0, ..b_n-1, so that a(x) = sigma^i = n - 1 _i = 0 a_i x^i and b(x) = sigma^i = n - 1 _i = 0 b_i x^i. Further, suppose that we want to compute the description of a a(x)b(x), their product. For all parts, you are not required to prove correctness of your algorithms, but are required to give a time analysis in O form. Give the straight-forward, iterative algorithm for this problem. Give a simple divide-and-conquer algorithm for this problem. Use the Karatsuba method to give an improved divide-and-conquer algorithm for this problem.

Solution

a)O(log n)n

RECURSIVE-IT(a)

n->length[a] n is a power of 2

if n=1

then return a

wn<-e 2pii/n

w<-1

a[0]<-(a0,a2,---an-2)

a[1]<-(a1,a3,…,an-1)

y[0]<-RECURSIVE-IT(a[0])

y[1]<-RECURSIVE-IT(a[1]

for k<-0 to n/2-1

do yk<-yk[0]+wyk[1]

yk+(n/2)<-yk[0]-wyk[1]

w<-wwn

return y y is assumed to be column vector.

b)

A divide-and-conquer algorithm for integer multiplication.

function multiply(x, y)

Input: Positive integers x and y, in binary

Output: Their product n = max(size of x, size of y)

if n = 1: return xy

xL, xR = leftmost n/2, rightmost n/2 bits of x

yL, yR = leftmost n/2, rightmost n/2 bits of y

P1 = multiply(xL, yL)

P2 = multiply(xR, yR)

P3 = multiply(xL + xR, yL + yR)

return P1 × 2 n + (P3 P1 P2) × 2 n/2 + P2

This algorithm takes O(n^2) time

c)

def karat sub a ( x , y ) :

i f x < 100 or y < 1 0 0: return xy

# c a l c u l a t e s t h e s i z e o f the numbers in base 10

  m = max( len ( str ( x ) ) , len ( str ( y ) ) ) // 2

# s p l i t   t h e d i g i t s e q u e n c e s ab o u t t h e mi ddle

cu t = pow( 1 0 , m)

a , b = x // cut , x % cu t

c , d = y // cut , y % cu t

# d i v i d e and conquer

z0 = k a r a t sub a ( a , c )

z1 = k a r a t sub a ( ( a + b ) , ( c + d ) )

z2 = k a r a t sub a ( b , d )

return z0 pow( 1 0 , 2m) + ( z1 z0 z2 ) pow( 1 0 , m) + z2

 Suppose that we are given two polynomials of degree n = 1 as their arrays of coefficients, a_0..a_n-1 and b_0, ..b_n-1, so that a(x) = sigma^i = n - 1 _i = 0 a
 Suppose that we are given two polynomials of degree n = 1 as their arrays of coefficients, a_0..a_n-1 and b_0, ..b_n-1, so that a(x) = sigma^i = n - 1 _i = 0 a

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site