0% found this document useful (0 votes)
44 views29 pages

Daa - CS - F364 - L06 - Fractional Knapsack Problem

The document presents a lecture on the Design & Analysis of Algorithms at BITS Pilani, focusing on polynomial representations, evaluation methods, and the Fast Fourier Transform algorithm. Key topics include polynomial representation forms, Horner's Rule for evaluation, Gaussian Elimination for interpolation, and the Discrete Fourier Transform. The document outlines time complexities for various operations and provides examples to illustrate the concepts discussed.

Uploaded by

jover39002
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)
44 views29 pages

Daa - CS - F364 - L06 - Fractional Knapsack Problem

The document presents a lecture on the Design & Analysis of Algorithms at BITS Pilani, focusing on polynomial representations, evaluation methods, and the Fast Fourier Transform algorithm. Key topics include polynomial representation forms, Horner's Rule for evaluation, Gaussian Elimination for interpolation, and the Discrete Fourier Transform. The document outlines time complexities for various operations and provides examples to illustrate the concepts discussed.

Uploaded by

jover39002
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

BITS Pilani presentation

BITS Pilani Dr. Mukesh Kumar Rohil


Department of Computer Science & Information Systems
Pilani Campus
BITS Pilani
Pilani Campus

Course: Design & Analysis of Algorithms


Lecture 04-05: Polynomial Representations, Evaluating a
Polynomial using Horners’ Rule, Interpolation using Gaussian
Elimination, Discrete Fourier Transform, Fast Fourier Transform
Algorithm
Learning Objectives

• Polynomial Representations
• Evaluating a Polynomial using Horners’ Rule
• Interpolation using Gaussian Elimination
• Discrete Fourier Transform
• Fast Fourier Transform Algorithm

BITS Pilani, Pilani Campus


Polynomial Representation
• We write that
degree(A) = k.
• Any integer strictly
greater than the degree
of a polynomial is a
degree-bound of that
polynomial.
• Therefore, the degree
of a polynomial of
degree-bound n may be
any integer between 0
and (n -1), inclusive.
BITS Pilani, Pilani Campus
Operations on Polynomials

• Division:
C(x) = A(x)/B(x)
• Reciprocal
• C(x) = 1/A(x)

(J-k)
BITS Pilani, Pilani Campus
Representing Polynomials

1. By coefficients form

2. By point values form

BITS Pilani, Pilani Campus


Representing Polygon:
Coefficient Form
• A polynomial f(x) = a0 + a1x + … + an-1xn-1 is represented as
(a0, a1, …, an-1) .

For coefficient form the Time complexity of:


Adding two polynomial = Θ(n)

Multiplying a polynomial by a constant C0 = Θ(n) (∵ C0(a0, a1,


…, an-1) = (C0a0, C0a1, …, C0an-1)

Multiplying two polynomials = Θ(n2) (∵ (a0, a1, …, an-1) x (b0, b1,


…, bn-1) = (a0b0 + a0b1 + a1b0, …., an-1bn-1) using
convolutional algorithms i.e. Iterative Multiply)
BITS Pilani, Pilani Campus
Representing Polynomial:
Point Value Form
• A polynomial f(x) = a0 + a1x + … + an-1xn-1 is represented as
{(x0, f(x0)), (x1, f(x1)), …, (xn-1, f(xn-1))} where all xi‘s are
different.
For Point Value form the Time complexity of:
Adding two polynomial = Θ(n) (Second polynomial also
specified at xi’s)

Multiplying a polynomial by a constant C0 = Θ(n)

Multiplying two polynomials = Θ(n) (∵ multiplication is


represented as {(x0, f1(x0)f2(x0)), (x1, f1(x1)f2(x1)), …, (xn-1,
f1(xn-1)f2(xn-1))})
BITS Pilani, Pilani Campus
Evaluating Polynomials by
Horner’s Rule
a0 + a1x + … + an-1xn-1 • Horner’s rule takes Θ(n)
= a0 + x(a1x + x(a2 + … x(an-2 time.
+ x(an-1)) …)) • Applying Horner’s rule n
times will take Θ(n2)
• Using the Horner’s Rule a time.
polynomial can be
converted from • Example:
coefficient to point-value 6 + 7x + 2x2 = 6 + x(7+x(2))
form in Θ(n2) time.

BITS Pilani, Pilani Campus


Interpolating using Gaussian
Elimination
• Interpolating polynomial a0 - a1 + a2 = 1 ……….(1)
means finding/deriving a0 = 6 ……….(2)
coefficients-form from a0 + a1 + a2 = 15 ……….(3)
the Point-value form of  a0 = 6, a1 = 7 and a2 = 2.
polynomial. Divide Steps:
• Using Gaussian First eliminate a0,
Elimination we can Then eliminate a1,
perform this conversion then
in Θ(n3) time. Conquer Steps:
• Example: find a2
f(x) = {(-1, 1), (0, 6), (1, 15)} = (a0, And then substitute to solve
a1, a2) = a0 + a1x + a2 x2 backwards.)
BITS Pilani, Pilani Campus
Gaussian Elimination: Time
Complexity
Divide Step: • There is only one sub-
• Putting values of xn in problem of size (n-1)
previous equations to  T(n) = T(n-1) + Cn(n-1) +
get new (n-1) equations. bn for n > 1
• Hence, total complexity T(1) = d for n = 1
of divide = (n-1)Cn =
Cn(n-1)
Conquer Step:
Computing xn given the
values of x1, x2, … xn-1
Complexity = bn
BITS Pilani, Pilani Campus
Gaussian Elimination: Time
Complexity
Root Level = 0 T(n) = (Cn2 + (b-c)n) + (C(n-1)2 + (b-
At Level Complexity is c)(n-1)) + (C(n-2)2 + (b-c)(n-2)) + …
0 is Cn2 + (b-c)n + (C(n-i)2 + (b-c)(n-i)) + … + (c22 +
1 is C(n-1)2 + (b-c)(n-1) (b-c).2) + d
2 is C(n-2)2 + (b-c)(n-2) = [Cn(n+1)(2n+1)]/6 – C + (b-
: c)n(n+1)/2 – (b-c) + d
i is C(n-i)2 + (b-c)(n-i)
:
= Θ(n3)
(n-1) is d
Leaf Level = (n-1)

BITS Pilani, Pilani Campus


Discrete Fourier Transform (DFT): Roots
of unity for algebraic structure Ring

• For any positive integer • 3rd roots of unity =


n, the roots of unity −1+𝑖√3 −1−𝑖√3
are the complex U3 = {1, , }
2 2
number solutions (n in • All nth roots of unity =
number) to the 2πk𝑖/n
Un = { 𝑒 | k ∈ {1, 2, …, n}}
equation xn=1.
For roots: Use Euler equation
• If n is even, there are
e2π𝑖 = cos(2π) + 𝑖sin(2π) = 1
two real solutions 1
and -1. (e2π𝑖)k = 1k = 1 (k, a +ve integer)
• If n is odd, there is one xn =1 = e2π𝑖 = e4π𝑖 = … = e2kπ𝑖
real solution i.e. 1. Raise each to power of (1/n).
BITS Pilani, Pilani Campus
Discrete Fourier Transform
(DFT)
• The DFT of the vector (a0, Example:
a1, a2, …, an-1) DFT((6,7,2))=DFT((a0,a1,a2))
[corresponding to the • f(x) = 6 + 7x + 2x2
vector form of f(x)] is the
• Three terms so use U3
vector (𝑓(𝜔𝑛0 ), 𝑓(𝜔𝑛1 ), …,
−1+𝑖√3 −1−𝑖√3
𝑓(𝜔𝑛𝑛−1 )) [corresponding => U3 = {1, , }
2 2
to the point-value form
• Substitute each element
of f(x)] where ωn = e2πi/n =
of U3 in the f(x).
nth root of unity.
• DFT((6,7,2)) =
• Complexity of computing
DFT using Horner’s rule is (15, (3+5√3i)/2, (3-5√3i)/2)
Θ(n2). BITS Pilani, Pilani Campus
Inverse DFT

• The DFT-1 of the vector (y0, y1, y2, …, yn-1) is the vector (a0,
a1, a2, …, an-1) if (y0, y1, y2, …, yn-1) = (𝑓(𝜔𝑛0 ), 𝑓(𝜔𝑛1 ), …,
𝑓(𝜔𝑛𝑛−1 )), where ωn = e2πi/n = nth root of unity.
Example: DFT-1(15, (3+5√3i)/2, (3-5√3i)/2) = (6, 7, 2)

We write the DFT equation as:


[y0 y1 y2 ... yn-1]T = A [a0 a1 a2 ... an-1]T ……. (1)
𝑦0 1 1 1 .. 1 𝑎0
𝑦1 1 𝑤𝑛1 𝑤𝑛2 .. 𝑤𝑛𝑛−1 𝑎1
𝑦2 = 1 2(𝑛−1) 𝑎2
𝑤𝑛2 𝑤𝑛4 .. 𝑤𝑛
: : : : .. : :
𝑦𝑛−1 1 𝑤𝑛𝑛−1
2(𝑛−1)
𝑤𝑛
(𝑛−1)(𝑛−1)
. . 𝑤𝑛 𝑎𝑛−1
BITS Pilani, Pilani Campus
Inverse DFT

We pre-multiply both sides of the equation (1) in the preceding


slide by a matrix B, we get:
B[y0 y1 y2 ... yn-1]T = BA [a0 a1 a2 ... an-1]T= C[a0 a1 a2 ... an-1]T

1 1 1 .. 1
−(𝑛−1)
1 𝑤𝑛−1 𝑤𝑛−2 .. 𝑤𝑛
−2(𝑛−1)
Where, 𝐵 = 1 𝑤𝑛−2 𝑤𝑛−4 .. 𝑤𝑛
: : : .. :
−(𝑛−1) −2(𝑛−1) −(𝑛−1)(𝑛−1)
1 𝑤𝑛 𝑤𝑛 . . 𝑤𝑛
(i, j)th entry of matrix BA (i.e. C) is given by:
(𝑗−𝑖) 2(𝑗−𝑖) (𝑛−1)(𝑗−𝑖)
Cij = 1 + 𝑤𝑛 + 𝑤𝑛 + … + 𝑤𝑛

BITS Pilani, Pilani Campus


Inverse DFT

For i=j, we get B[y0 y1 y2 ... yn-1]T = nI


Cij = 1 + 1 + .. + 1 = n [a0 a1 a2 ... an-1]T=
n[a0 a1 a2 ... an-1]T
Otherwise, we get
 [a0 a1 a2 ... an-1]T
𝑗−𝑖 𝑛
𝑤𝑛 −1
Cij = (𝑗−𝑖) = =(1/n)B[y0 y1 y2 ...yn-1]T
𝑤𝑛 −1
(𝑤𝑛𝑛 ) 𝑗−1 −1 1−1
= DFT-1(y0, y1, y2, ... yn-1)
(𝑗−𝑖) = (𝑗−𝑖) =0
𝑤𝑛 −1 𝑤𝑛 −1
Complexity of computing
1 0 0 0 DFT-1 (Inverse DFT) using
0 1 0 0 Horner’s rule for matrix
C = nI = n
: : ‫ ܆‬: multiplication = Θ(n2).
0 0 0 1 BITS Pilani, Pilani Campus
Inverse DFT: Example

• DFT-1(15, (3+5√3i)/2, (3-5√3i)/2). For n = 3, we have


𝑤3−1 =e(-2*π*i/3) = (-1-√3i)/2, and 𝑤3−2 =(-1+√3i)/2

DFT-1(15, (3+5√3i)/2, (3-5√3i)/2) =

1 1 1 15 18 6
1
(1/3) 1 𝑤3−1 𝑤3−2 (3+5√3i)/2 = 21 = 7
3
1 𝑤3−2 𝑤3−4 (3−5√3i)/2) 6 2

BITS Pilani, Pilani Campus


Fast Fourier Transform

• We separate f(x) into terms having f(𝑤𝑛𝑖 ) = fe((𝑤𝑛𝑖 )2 ) + 𝑤𝑛𝑖


odd and even powers of x as: fo((𝑤𝑛𝑖 )2)
f(x) = fe(x2) + xfo(x2)
= fe((𝑤𝑛2 )i ) + 𝑤𝑛𝑖
Where
fo((𝑤𝑛2 )i)
fe(x) = a0 + a2x + a4x2 + … + an-2x(n/2-1)
𝑖 𝑖
fo(x) = a1 + a3x + a5x2 + … + an-1x(n/2-1) = fe(𝑤𝑛/2 ) + 𝑤𝑛𝑖 fo(𝑤𝑛/2 )
We have,
𝑤𝑛2 =e2(2πi/n) = e2πi/(n/2) = 𝑤𝑛/2 • As i varies from 0 to
𝑛/2
𝑤𝑛 =e2πi/(n/2) = eπi = -1 (n-1), the values of
𝑖
𝑤𝑛/2 repeats at a
distance of n/2.
BITS Pilani, Pilani Campus
Fast Fourier Transform
𝑖+𝑛/2 𝑛/2 𝑖 𝑖
𝑤𝑛/2 = 𝑤𝑛/2 . 𝑤𝑛/2 = 𝑤𝑛/2 ……… 1
𝑖+𝑛/2 𝑛/2
𝑤𝑛 = 𝑤𝑛 . 𝑤𝑛𝑖 = −𝑤𝑛𝑖 ……… 2

Divide Step:
DFT(f(x)) (of size n) = DFT(fe(x)) + DFT(fo(x)) (each of size n/2)

Conquer Step:
Using equations (1) and (2), we write (for 0 ≤ i ≤ (n/2-1))
𝑖 𝑖
f(𝑤𝑛𝑖 ) = fe(𝑤𝑛/2 ) + 𝑤𝑛𝑖 f0(𝑤𝑛/2 )
𝑖+𝑛/2 𝑖 𝑖
f(𝑤𝑛 ) = fe(𝑤𝑛/2 ) - 𝑤𝑛𝑖 f0(𝑤𝑛/2 )

BITS Pilani, Pilani Campus


Fast Fourier Transform: Time
Complexity
• Both Divide and Conquer steps take time linear in input =
Cn
• There are two sub-problems of size n/2. We get the
following recurrence:

T(1) = d
T(n) = 2T(n/2) + Cn for n > 1

BITS Pilani, Pilani Campus


Fast Fourier Transform: Time
Complexity
Root level = 0 • Divide + Conquer steps takes Cn (linear
At level: time).
• Recursive step has two sub-problems of
0 it is Cn. size n/2.
1 it is 2(Cn/2).
log 𝑛 −1
: T(n) = σ𝑖=0 𝐶𝑛 + dn = Cnlog2(n) +
i it is 2i(Cn/2i) dn
: = ϴ(nlog2(n))
Leaf level it is
d.2log2(n) = dn
Leaf level=(log2n – 1)
BITS Pilani, Pilani Campus
Recursive FFT & Inverse FFT
S# Statement
Recursive_FFT(a) // a is an array • For Computing DFT using
1. N = a.length // n is a power of 2 Recursive_FFT:
2. If n == 1
3. return a
Line# Change to be made
4. wn = e2πi/n 4 wn = e(-2πi/n)
5. w=1
14 Return (y/n) but in sub-
6. A(0) = (a0, a2, …, an-2)
7. A(1) = (a1, a3, …, an-1)
problems (return y)
8. y(0) = recursive_FFT(A(0) )
9. y(1) = recursive_FFT(A(1) ) • For Inverse FFT (FFT-1):
10. For k=0 to (n/2 – 1)
11. yk = yk(0) + wyk(1)
Line#4: wn = e-2πin
12. yk+(n/2) = yk(0) - wyk(1) Line#14: return (1/n)
13. w=wwn
recursive_FFT-1(a)
14. Return y; BITS Pilani, Pilani Campus
Polynomial Multiplication
using FFT
Example: Given f1(x) = x + 2, and f2(x) = 2x + 3. Multiply.
1: Pad each polynomial with zeros so that number of terms
is the nearest power of 2. (Four terms in this example).
f1(x) = (2, 1, 0, 0), and f2(x) = (3,2,0,0)
2: Find DTF(f1(x)) and DFT(f2(x)). (Four terms in each.)
3: Find product of DFTs of two polynomials. The product is
obtained by multiplying corresponding entries of DFTs.
4: Find DFT-1(DTF(f1(x)).DTF(f2(x))). (i.e. DFT-1 of the
product computed in step 3 above).
5: The result of step 4 is the product of polynomials in the
coefficient form.

BITS Pilani, Pilani Campus


Polynomial Multiplication
using FFT
Example: Given f1(x) = x + 2, and f2(x) = 2x + 3. Multiply.
1: f1(x) = (2, 1, 0, 0), and f2(x) = (3,2,0,0)
2: DTF(f1(x)) = (3, 2+i, 1, 2-i) and DFT(f2(x)) = (5, 3+2i, 1, 3-2i).
3: P_DFT = DTF(f1(x)). DTF(f2(x)) =
(3x5, (2+i)x(3+2i), 1x1, (2-i)(3-2i)) = (15, 4+7i, 1, 4 – 7i)
4: DFT-1(PDFT) = (6, 7, 2, 0)
5: f(x) = (6, 7, 2, 0) => f(x) = 6 + 7x + 2x2

Verify: f(x) = (x+2)(2x+3) = 2x2 + 4x + 3x +6 = 6 + 7x + 2x2

BITS Pilani, Pilani Campus


Divide and Conquer Graph of
f1(x)
(2,1,0,0)

(2,0) (1,0)

(2) (0) (1) (0)


w2 = -1
(2+0, 2- 0) = (2, 2) (1+0, 1- 0) = (1, 1)
w4 = i
(2+1, 2+i, 2 – 1, 2 - i)
= (3, 2+i, 1, 2 – i)

BITS Pilani, Pilani Campus


Divide and Conquer Graph of
f2(x)
(3,2,0,0)

(3,0) (2,0)

(3) (0) (2) (0)


w2 = -1
(3+0, 3- 0) = (3, 3) (2+0, 2- 0) = (2, 2)
w4 = i
(3+2, 3+2i, 3 – 2, 3 - 2i)
= (5, 3+2i, 1, 3 – 2i)

BITS Pilani, Pilani Campus


Divide and Conquer Graph of
DFT-1(f1(x) f2(x))
(15, 4+7i, 1, 4-7i)

(15,1) (4+7i, 4-7i)

(15) (1) (4+7i) (4-7i)


w2-1= -1

((4+7i) + (4-7i),
(15+1, 15- 0) =
(4+7i) - (4-7i)) =
(16, 15)
(8, 14i)
W4-1 = -i
(1/4)(16+8, 14+14, 16– 8, 14 - 14)
= (6, 7, 2, 0)
BITS Pilani, Pilani Campus
Thanks ….

Any questions please …..

Thanking you

You might also like