Design and Analysis of Algorithms
Lecture 6
Divide and Conquer Paradigm – V
• The final Algorithm for Polynomial multiplication
• Stable Matching Algorithm
1
Algorithms-II : CS345A
Recap from Lecture 1
2
3
<>
<>
<>
Coefficient
representation
{}
{}
{}
O() time


O( log ) time
O( log ) time
Coefficient
Represent-
ation
Polynomial Evaluation Polynomial Interpolation
Polynomial Evaluation Problem
Given a polynomial of degree less than in coefficient representation,
1. Choose any set of numbers
2. Compute for all .
Assumption:
• is a power of 2.
4
In the last lecture, we discussed a divide and conquer based
algorithm for polynomial evaluation. We were left with a question …
Question
Is it possible to select such that
…
5
¿ 𝑺𝟐
∨¿
𝒏
𝟐
¿ 𝑺
𝟒
∨¿
𝒏
𝟒
¿ 𝑺𝒏
∨¿𝟏
=
Good start !
But how to satisfy the
remaining requirements …
Complex numbers
6
Recalling elementary facts
Complex roots of unity
Definition:
A number is said to be th root of unity
if it satisfies equation.
 Magnitude() = 1
 Argument() = ?
7
1
2 𝜋
𝑛
Complex plane
Unit circle
multiple of
𝑹𝒏=¿
Principal rool of unity
So must lie on the
unit circle. But
where exactly …
Complex roots of unity
What is ? What is ?
8
𝜔4
1
𝜔4
2
𝜔4
3
𝜔4
4
𝜔3
2
𝜔3
1
𝜔3
3
𝑹𝟑 𝑹𝟐
Complex roots of unity
Theorem:
If is odd
If is even
Is it possible to select such that
…
?
9
𝑺
𝟐
=
𝒏
𝟐
𝑺𝟒
=
𝒏
𝟒
𝑺𝒏
=𝟏
𝑺=𝑹𝒏
The Recursion Tree
T() = Time complexity of Evaluating on
= a + T()
10
𝑨
𝑨𝑒𝑣𝑒𝑛 𝑨𝑜𝑑𝑑
degree
points
for
evaluation
() + ()
¿ 𝒏
¿𝒏/𝟐
¿𝒏/𝟒
¿ 𝟏
𝑹𝒏
𝑹 𝒏
𝟒
𝑹𝟏
𝑹 𝒏
𝟐
= O( log )
11
<>
<>
<>
Coefficient
represntation
{}
{}
{}
O() time

O() time

O( log ) time
O( log ) time
?
Done
Interpolation
There is a polynomial
with ’s unknown.
Given the following (point, value) representation of :
Aim: To compute ’s .
12
{}
Attention
• Inspite of the best efforts by the instructor, majority of the class could not
follow the next 5 slides for addressing the problem of interpolation.
• The instructor has now revised these slides.
• Important advice: Please go over these slides very very slowly.
• If you still can’t internalize them, please meet the instructor.
• He will be very grateful to you to know the difficulty you faced.
13
Question : = ?
If ,
Otherwise
14
A Tool
∑
0≤ 𝑗<𝑛
( 𝛽)𝑗
𝜷∈
𝑛
1− 𝛽𝑛
1− 𝛽
0
¿ 1
15
Interpolation
Polynomial Interpolation = Polynomial evaluation
Can you guess the polynomial whose evaluation
will help solve polynomial interpolation ?
Make an obvious guess.
Interpolation
Solution:
Define a polynomial
, where .
Evaluate on .
This will help us extract coefficient of . 
What is ?
16
Can you guess what is relation
between and ?
Interpolation
Solution:
Define a polynomial
17
∑
0≤ 𝑡<𝑛
𝑐𝑡 (𝜔𝑛
𝑗
)
𝑡
𝑪 (𝜔𝑛
𝑗
)
𝑐𝑘(𝜔𝑛
𝑗
)
𝑘
⋅(𝜔𝑛
ℓ
)
𝑗
Focus on only those terms that involve
This expression has terms.
It looks too complex.
Interpolation
Solution:
Define a polynomial
18
∑
0≤ 𝑡<𝑛
𝑐𝑡 (𝜔𝑛
𝑗
)
𝑡
𝑐𝑘(𝜔𝑛
𝑘+ ℓ
)
𝑗
𝑐𝑘 𝜔𝑛
𝑘+ℓ
𝑐𝑘(𝜔𝑛
𝑘+ ℓ
)
𝑛−1
𝑐𝑘
+¿ +…+¿ +…+¿
Tool
for
¿ 0
for
∑
0≤ 𝑡<𝑛
𝑐𝑡 (𝜔𝑛
𝑛 −1
)
𝑡
∑
0≤ 𝑡<𝑛
𝑐𝑡 (𝜔𝑛
0
)
𝑡
Focus on only those terms that involve
∑
0≤ 𝑡<𝑛
𝑐𝑡 (𝜔𝑛
1
)
𝑡
Homework
• Write a neat pseudocode for Interpolation.
19
Optional Programming Assignment
Implement the algorithm.
20
Problem from Lecture 1
A Physics Problem
21
…
: Force exerted on
Aim: Compute for each
22
1 1 1
𝑘𝑞1 𝑞 𝑗
( 𝑗 −1)2
…+
𝑘 𝑞𝑗 −1 𝑞 𝑗
(1)2
+𝑘𝑞2 𝑞 𝑗
( 𝑗 −2)2
−
𝑘𝑞 𝑗+1 𝑞 𝑗
(1)2
…−
𝑘𝑞𝑛−1 𝑞𝑗
(𝑛−1 − 𝑗 )2
−
𝑘 𝑞𝑛 𝑞 𝑗
(𝑛 − 𝑗 )2
𝐹 𝑗=¿
time
in time.
Attempt this problem now 
Stable Marriage Problem
An Algorithm
Stable Marriage Problem
• : set of men
• : set of women
For each man ,
: a preference list of all women
For each woman ,
: a preference list of all men
Aim: To compute a stable marriage.
24
Algorithm for Stable Marriage
Man proposes
God disposes
25
1962, David Gale and Lloyd Shapley
Woman
Stable Marriage Problem
26
Women Men
𝑤1
𝑤2
𝑤3
𝑤𝑛
𝑤𝑖
𝑚1
𝑚2
𝑚3
𝑚𝑗
𝑚𝑛
1
1
1
2
GaleShapley(, )
 ;
While ()
{  Extract_any_Man_from();
 next();
proposes to ;
If Single()
else
{ let  mate();
If( prefers to )
{ rejects ;
goes back to ;
mate()  ;
remove from ();
}
else
{ remove from ();
goes back to ;
}
}
}
Question: Does the algorithm compute stable matching ?
Question: Does the algorithm even terminate ?
Homework:
Ponder over these questions.
27
mate()  ;
in list , precedes )

DEsign and Analysis of ALgorithms slides of lecture 1, 2 and 3

  • 1.
    Design and Analysisof Algorithms Lecture 6 Divide and Conquer Paradigm – V • The final Algorithm for Polynomial multiplication • Stable Matching Algorithm 1 Algorithms-II : CS345A
  • 2.
  • 3.
    3 <> <> <> Coefficient representation {} {} {} O() time   O( log) time O( log ) time Coefficient Represent- ation Polynomial Evaluation Polynomial Interpolation
  • 4.
    Polynomial Evaluation Problem Givena polynomial of degree less than in coefficient representation, 1. Choose any set of numbers 2. Compute for all . Assumption: • is a power of 2. 4 In the last lecture, we discussed a divide and conquer based algorithm for polynomial evaluation. We were left with a question …
  • 5.
    Question Is it possibleto select such that … 5 ¿ 𝑺𝟐 ∨¿ 𝒏 𝟐 ¿ 𝑺 𝟒 ∨¿ 𝒏 𝟒 ¿ 𝑺𝒏 ∨¿𝟏 = Good start ! But how to satisfy the remaining requirements …
  • 6.
  • 7.
    Complex roots ofunity Definition: A number is said to be th root of unity if it satisfies equation.  Magnitude() = 1  Argument() = ? 7 1 2 𝜋 𝑛 Complex plane Unit circle multiple of 𝑹𝒏=¿ Principal rool of unity So must lie on the unit circle. But where exactly …
  • 8.
    Complex roots ofunity What is ? What is ? 8 𝜔4 1 𝜔4 2 𝜔4 3 𝜔4 4 𝜔3 2 𝜔3 1 𝜔3 3 𝑹𝟑 𝑹𝟐
  • 9.
    Complex roots ofunity Theorem: If is odd If is even Is it possible to select such that … ? 9 𝑺 𝟐 = 𝒏 𝟐 𝑺𝟒 = 𝒏 𝟒 𝑺𝒏 =𝟏 𝑺=𝑹𝒏
  • 10.
    The Recursion Tree T()= Time complexity of Evaluating on = a + T() 10 𝑨 𝑨𝑒𝑣𝑒𝑛 𝑨𝑜𝑑𝑑 degree points for evaluation () + () ¿ 𝒏 ¿𝒏/𝟐 ¿𝒏/𝟒 ¿ 𝟏 𝑹𝒏 𝑹 𝒏 𝟒 𝑹𝟏 𝑹 𝒏 𝟐 = O( log )
  • 11.
  • 12.
    Interpolation There is apolynomial with ’s unknown. Given the following (point, value) representation of : Aim: To compute ’s . 12 {}
  • 13.
    Attention • Inspite ofthe best efforts by the instructor, majority of the class could not follow the next 5 slides for addressing the problem of interpolation. • The instructor has now revised these slides. • Important advice: Please go over these slides very very slowly. • If you still can’t internalize them, please meet the instructor. • He will be very grateful to you to know the difficulty you faced. 13
  • 14.
    Question : =? If , Otherwise 14 A Tool ∑ 0≤ 𝑗<𝑛 ( 𝛽)𝑗 𝜷∈ 𝑛 1− 𝛽𝑛 1− 𝛽 0 ¿ 1
  • 15.
    15 Interpolation Polynomial Interpolation =Polynomial evaluation Can you guess the polynomial whose evaluation will help solve polynomial interpolation ? Make an obvious guess.
  • 16.
    Interpolation Solution: Define a polynomial ,where . Evaluate on . This will help us extract coefficient of .  What is ? 16 Can you guess what is relation between and ?
  • 17.
    Interpolation Solution: Define a polynomial 17 ∑ 0≤𝑡<𝑛 𝑐𝑡 (𝜔𝑛 𝑗 ) 𝑡 𝑪 (𝜔𝑛 𝑗 ) 𝑐𝑘(𝜔𝑛 𝑗 ) 𝑘 ⋅(𝜔𝑛 ℓ ) 𝑗 Focus on only those terms that involve This expression has terms. It looks too complex.
  • 18.
    Interpolation Solution: Define a polynomial 18 ∑ 0≤𝑡<𝑛 𝑐𝑡 (𝜔𝑛 𝑗 ) 𝑡 𝑐𝑘(𝜔𝑛 𝑘+ ℓ ) 𝑗 𝑐𝑘 𝜔𝑛 𝑘+ℓ 𝑐𝑘(𝜔𝑛 𝑘+ ℓ ) 𝑛−1 𝑐𝑘 +¿ +…+¿ +…+¿ Tool for ¿ 0 for ∑ 0≤ 𝑡<𝑛 𝑐𝑡 (𝜔𝑛 𝑛 −1 ) 𝑡 ∑ 0≤ 𝑡<𝑛 𝑐𝑡 (𝜔𝑛 0 ) 𝑡 Focus on only those terms that involve ∑ 0≤ 𝑡<𝑛 𝑐𝑡 (𝜔𝑛 1 ) 𝑡
  • 19.
    Homework • Write aneat pseudocode for Interpolation. 19
  • 20.
  • 21.
    Problem from Lecture1 A Physics Problem 21
  • 22.
    … : Force exertedon Aim: Compute for each 22 1 1 1 𝑘𝑞1 𝑞 𝑗 ( 𝑗 −1)2 …+ 𝑘 𝑞𝑗 −1 𝑞 𝑗 (1)2 +𝑘𝑞2 𝑞 𝑗 ( 𝑗 −2)2 − 𝑘𝑞 𝑗+1 𝑞 𝑗 (1)2 …− 𝑘𝑞𝑛−1 𝑞𝑗 (𝑛−1 − 𝑗 )2 − 𝑘 𝑞𝑛 𝑞 𝑗 (𝑛 − 𝑗 )2 𝐹 𝑗=¿ time in time. Attempt this problem now 
  • 23.
  • 24.
    Stable Marriage Problem •: set of men • : set of women For each man , : a preference list of all women For each woman , : a preference list of all men Aim: To compute a stable marriage. 24
  • 25.
    Algorithm for StableMarriage Man proposes God disposes 25 1962, David Gale and Lloyd Shapley Woman
  • 26.
    Stable Marriage Problem 26 WomenMen 𝑤1 𝑤2 𝑤3 𝑤𝑛 𝑤𝑖 𝑚1 𝑚2 𝑚3 𝑚𝑗 𝑚𝑛 1 1 1 2
  • 27.
    GaleShapley(, )  ; While() {  Extract_any_Man_from();  next(); proposes to ; If Single() else { let  mate(); If( prefers to ) { rejects ; goes back to ; mate()  ; remove from (); } else { remove from (); goes back to ; } } } Question: Does the algorithm compute stable matching ? Question: Does the algorithm even terminate ? Homework: Ponder over these questions. 27 mate()  ; in list , precedes )