Design a brute-force algorithm for multiplying two polynomials, both are expressed as functions of x, and show that its time efficiency of O(n^2).
suppose polynomials are presented as array
Poly1[] = {a0,a1,..... an-2,
an-1}
Ploy2[] = {b0,b1,..... bn-2,
bn-1}
The first input array represents a0 +
a1x1 + a2x2 +....+
an-1xn-1
The second array represents b0 +
b1x^1 + b2x2 +....+
bn-1xn-1
Algorithm to multiply both polynomial poly1 and poly2
solution is to consider every term of first polynomial and multiply
it with every term of second polynomial.
multiply(Poly1[0..n-1], Poly2[0..n-1])
step 1) Create a result array output[] of size 2*n-1.
step 2) Initialize all entries in output[] as 0.
step 3) Traverse array Poly1[] and do following for every element
poly1[i]
Traverse array Poly2[] and do following for every element
Poly2[j]
output[i+j] = output[i+j] + Poly1[i] * Poly2[j]
step 4) Return output[].
Time complexity = O(n^2)
Get Answers For Free
Most questions answered within 1 hours.