Professional Documents
Culture Documents
Lec3 dnc2 v1 Light 1up
Lec3 dnc2 v1 Light 1up
Trevor Brown
https://student.cs.uwaterloo.ca/~cs341
trevor.brown@uwaterloo.ca
1
PROBLEM: NON-DOMINATED POINTS
• A point dominates
everything to the southwest So, I am a
non-dominated point
+𝑦
No other point
dominates me
+𝑥
2
MORE FORMALLY
+𝑦
3
BRUTE FORCE ALGORITHM
4
+𝑦
+𝑥
5
PROBLEM DECOMPOSITION
+𝑦
7
1
4
3 9
5 8
14
6 11
12
2 10 16
13 15 +𝑥
6
PROBLEM DECOMPOSITION
+𝑦 𝑺𝟏
7
1 𝑺𝟐
4
3 9
5 8
14
6 11
12
2 10 16
13 15 +𝑥
7
PROBLEM DECOMPOSITION
+𝑦 +𝑦
7
1
4
3 9
5 8
13
6 11
2 10 16
+𝑥 12 14 +𝑥
𝑺𝟏 𝑺𝟐
8
PROBLEM DECOMPOSITION
+𝑦 +𝑦
7
1
4
3 9
5 8
13
6 11
2 10 16
+𝑥 12 14 +𝑥
𝑺𝟏 𝑺𝟐
9
COMBINING TO GET NON-DOMINATED POINTS
• Let 𝑄1 , 𝑄2 , … , 𝑄𝑘 be the non-dominated points in 𝑺𝟏
• Let 𝑅1 , 𝑅2 , … , 𝑅𝑚 be the non-dominated points in 𝑺𝟐
Just need to find rightmost 𝑸𝒊 that
is not dominated
(that has 𝑦-coordinate ≥ 𝑅1 . 𝑦)
𝑆1 𝑆2
10
11
Θ(𝑛 log 𝑛) Running time complexity?
(unit cost model)
𝑇(𝑛)
Θ(1)
Assume 𝑛 = 2𝑗 for simplicity
Θ(1) or Θ 𝑛 𝑛
𝑇 𝑛 = 2𝑇 +Θ 𝑛
2
Θ(𝑛)
13
MULTIPRECISION MULTIPLICATION
• Input: two 𝒌-bit positive integers X and Y
• With binary representations:
𝑋 = [𝑋[𝑘 − 1], … , 𝑋[0]]
𝑌 = [𝑌[𝑘 − 1], … , 𝑌[0]]
• Output: The 2𝑘-bit positive integer 𝑍 = 𝑋𝑌
• With binary representation: 𝑍 = [𝑍[2𝑘 − 1], … , 𝑍[0]]
14
BRUTE FORCE ALGORITHM
𝑿[𝟑] 𝑿[𝟎]
𝑿 1 0 1 0 𝒀 1 0 1 1
1 0 1 0
• One row per digit of 𝑌
times 1 0 1 1 • For each row
0 0 0 0 1 0 1 0
copy the 𝑘 bits of 𝑋
0 0 0 1 0 1 0 0 • Add the 𝑘 rows together
0 0 0 0 0 0 0 0
0 1 0 1 0 0 0 0 • Θ(𝑘) binary additions
of Θ(𝑘) bit numbers
𝒁 0 1 1 0 1 1 1 0
• Total runtime is
𝚯 𝒌𝟐 bit operations
15
A DIVIDE-AND-CONQUER APPROACH
𝑿 1 0 1 0 𝒀 1 0 1 1
1 0 1 1
𝑿𝑳 1 0 𝑿𝑹 1 0 𝒀𝑳 𝒀𝑹
k/2 bit
shift!
𝟐𝒌/𝟐 𝑿𝑳 1 0 0 0 𝟐𝒌/𝟐 𝒀𝑳 1 0 0 0
+𝑿𝑹 1 0 +𝒀𝑹 1 1
=𝑿 1 0 1 0 =𝒀 1 0 1 1
16
EXPRESSING 𝒌-BIT MULT. AS 𝒌/𝟐-BIT MULT.
17
Runtime?
(bit complexity model)
Recall: 𝑋𝑌 = 2𝑘 𝑋𝐿 𝑌𝐿 + 2𝑘/2 𝑋𝐿 𝑌𝑅 + 𝑋𝑅 𝑌𝐿 + 𝑋𝑅 𝑌𝑅
18
Θ(1) • Assume 𝑘 = 2𝑗 for ease
𝑘
• 𝑇 𝑘 = 4𝑇 + Θ(𝑘)
Θ(log 𝑘) or Θ 𝑘 2
• Master theorem says
𝑇 𝑘 ∈ Θ(𝑘 log2 4 ) = Θ(𝑘 2 )
𝑘
4𝑇
2 Same complexity as brute force!
Θ(𝑘)
https://www.wired.com/story/mathematicians-
discover-the-perfect-way-to-multiply/
20
KARATSUBA’S ALGORITHM
• Let’s optimize from four subproblems to three
Recall: 𝑋𝑌 = 2𝑘 𝑋𝐿 𝑌𝐿 + 2𝑘/2 𝑿𝑳 𝒀𝑹 + 𝑿𝑹 𝒀𝑳 + 𝑋𝑅 𝑌𝑅
• Idea: compute 𝑿𝑳 𝒀𝑹 + 𝑿𝑹 𝒀𝑳 with only one multiplication
• Note 𝑿𝑳 𝒀𝑹 + 𝑿𝑹 𝒀𝑳 appears in (𝑿𝑳 + 𝑿𝑹 )(𝒀𝑳 + 𝒀𝑹 )
• 𝑿𝑳 + 𝑿𝑹 𝒀𝑳 + 𝒀𝑹 = 𝑿𝑳 𝒀𝑳 + 𝑿𝑳 𝒀𝑹 + 𝑿𝑹 𝒀𝑳 + 𝑿𝑹 𝒀𝑹
• Let 𝑋𝑇 = 𝑋𝐿 + 𝑋𝑅 and 𝑌𝑇 = 𝑌𝐿 + 𝑌𝑅
• Then 𝑿𝑳 𝒀𝑹 + 𝑿𝑹 𝒀𝑳 = 𝑿𝑻 𝒀𝑻 − 𝑿𝑳 𝒀𝑳 − 𝑿𝑹 𝒀𝑹
• And the other two terms 𝑿𝑳 𝒀𝑳 and 𝑿𝑹 𝒀𝑹 are already in 𝑿𝒀
• So 𝑋𝑌 = 2𝑘 𝑋𝐿 𝑌𝐿 + 2𝑘/2 𝑋𝑇 𝑌𝑇 − 𝑋𝐿 𝑌𝐿 − 𝑋𝑅 𝑌𝑅 + 𝑋𝑅 𝑌𝑅 Only three unique
multiplications!
21
Θ(1) Running time complexity?
𝑘
• 𝑇 𝑘 = 3𝑇 + Θ(𝑘)
2
Θ(𝑘) *
• Assume 𝑘 = 2𝑗 for ease
• Master theorem:
• 𝑎 = 3, 𝑏 = 2, 𝑦 = 1
𝑘
3𝑇
2 • 𝑥 = log 𝑏 𝑎 = log 2 3
• 𝑇 𝑘 ∈ Θ 𝑘 log2 3
• ≈ Θ 𝑘1.58
Θ(𝑘)
From general experience the mathematics of basic things at the end always
turns out to be elegant.”
24
And Harvey and van der Hoeven achieved O(n log n) in November 2020!
[https://hal.archives-ouvertes.fr/hal-02070778/document]
Their method is a refinement of the major work that came before them. It
splits up digits, uses an improved version of the fast Fourier transform, and
takes advantage of other advances made over the past 40 years.
Unfortunately, simple complexity doesn’t
always mean simple algorithm…
= 𝐴𝑖𝑘 𝐵𝑘𝑗
𝑘=1
• Running time (unit cost)?
26
ATTEMPTING A BETTER SOLUTION
• What if we first partition the matrix into sub-matrices
• Then divide and conquer on the sub-matrices
• Example of partitioning: 4x4 matrix into four 2x2 matrices
27
MULTIPLYING PARTITIONED MATRICES
𝑎11 𝑎12 𝑏11 𝑏12
𝑎 𝑏 𝑎21 𝑎22 𝑏21 𝑏22
Let A = = 𝑐 𝑐12
𝑐 𝑑 11 𝑑11 𝑑12
𝑐21 𝑐22 𝑑21 𝑑22
28
IDENTIFYING SUBPROBLEMS TO SOLVE
𝑎 𝑏 𝑒 𝑓 𝑎 𝑏 𝑒 𝑓
𝐶 = 𝐴𝐵 = 𝐶 = 𝐴𝐵 =
𝑐 𝑑 𝑔 ℎ 𝑐 𝑑 𝑔 ℎ
𝑎𝑒 + 𝑏𝑔 𝑎𝑓 + 𝑏ℎ 𝑎𝑒 + 𝑏𝑔 𝑎𝑓 + 𝑏ℎ
= =
𝑐𝑒 + 𝑑𝑔 𝑐𝑓 + 𝑑ℎ 𝑐𝑒 + 𝑑𝑔 𝑐𝑓 + 𝑑ℎ
𝑎 𝑏 𝑒 𝑓 𝑎 𝑏 𝑒 𝑓
𝐶 = 𝐴𝐵 = 𝐶 = 𝐴𝐵 =
𝑐 𝑑 𝑔 ℎ 𝑐 𝑑 𝑔 ℎ
𝑎𝑒 + 𝑏𝑔 𝑎𝑓 + 𝑏ℎ 𝑎𝑒 + 𝑏𝑔 𝑎𝑓 + 𝑏ℎ
= =
𝑐𝑒 + 𝑑𝑔 𝑐𝑓 + 𝑑ℎ 𝑐𝑒 + 𝑑𝑔 𝑐𝑓 + 𝑑ℎ
30
Time complexity (unit cost)?
Θ(1) 𝑛
• 𝑇 𝑛 = 8𝑇 + Θ(𝑛2 )
2
Θ(𝑛2 )
31
STRASSEN FAST MATRIX MULTIPLICATION ALGORITHM
𝑎 𝑏 𝑒 𝑓 𝑎𝑒 + 𝑏𝑔 𝑎𝑓 + 𝑏ℎ 𝒓 𝒔
𝐴𝐵 = = =𝑪=
𝑐 𝑑 𝑔 ℎ 𝑐𝑒 + 𝑑𝑔 𝑐𝑓 + 𝑑ℎ 𝒕 𝒖
Key idea: get rid of one multiplication!
Define
32
STRASSEN FAST MATRIX MULTIPLICATION ALGORITHM
𝑎 𝑏 𝑒 𝑓 𝑎𝑒 + 𝑏𝑔 𝑎𝑓 + 𝑏ℎ 𝒓 𝒔
𝐴𝐵 = = =𝑪=
𝑐 𝑑 𝑔 ℎ 𝑐𝑒 + 𝑑𝑔 𝑐𝑓 + 𝑑ℎ 𝒕 𝒖
Define
Claim
33
Source: https://www.computer.org/csdl/journal/td/2002/11/l1105/13rRUxAASVu
34
35
Θ(1) Running time complexity?
𝑛
• 𝑇 𝑛 = 7𝑇 + Θ(𝑛2 )
Θ(1) or Θ 𝑛2 2
• Master theorem
𝑛
7𝑇
2 • 𝑎 = 7, 𝑏 = 2, 𝑦 = 2
• 𝑥 = log 2 7
• 𝑥 > 𝑦 so 𝑇 𝑛 ∈ Θ 𝑛 𝑥
• 𝑇 𝑛 ∈ 𝚯 𝒏𝐥𝐨𝐠𝟐 𝟕 ≈ Θ 𝑛2.81
Θ(𝑛2 )
36
How much better is How much better is
Θ(𝑛2.81 ) than Θ(𝑛3 )? Θ(𝑛2.376 ) than Θ(𝑛3 )?
37