Skip to content
NotesAlgorithm

算法设计与分析课程笔记,覆盖分治、图算法、最短路与复杂度基础。

Divide and Conquer and Running Time analysis

Karatsuba Integer Multiplication

对于两个 n 位整数 xy,若做最朴素的乘法,则需要进行O(n2)次elemwise的乘法运算。 对齐进行分治优化(Divide and Conquer),第一步我们可以将它们写成:

x=x110k+x0y=y110k+y0

其中 k=n/2。那么它们的乘积为:

xy=(x110k+x0)(y110k+y0)=x1y1102k+(x1y0+x0y1)10k+x0y0

注意到在分治之后的式子中,如果继续朴素地计算x1y1,x0y1,x1y0,x0y0,那么总的乘法次数为4T(n/2)+O(n),根据主定理,其时间复杂度仍然是O(n2),并没有得到优化。

我们注意到在结果中,我们只需要(x0y1+x1y0)的整体值,而分别计算x0y1x1y0是多余的,产生了多余的信息。因此,我们可以通过计算(x1+x0)(y1+y0)x1y1x0y0来得到(x0y1+x1y0)的值。这样,我们只需要进行3次乘法运算,就可以得到(x0y1+x1y0)的值。总的乘法次数为3T(n/2)+O(n),根据主定理,其时间复杂度为 O(nlog23)

Divide it !

There are some better algorithms

Better Integer Multiplication Algorithms

The last three algorithms are based on FFT

Matrix Multiplication Optimization

Matrix Multiplication optimization 该算法在信息论上可被证明是时间复杂度最低的分治算法

Time Complexity Analysis

what are the unit-time operation for the computer?

Word RAM model: 能放在RAM中的数据,都可以视为unit-time word

  • Read/write a word
  • Arithmetic operations on words
  • Comparison of words

Big O Notation

if T(n)=O(n2), 就是说T(n)<=Cn2 for some constant C and all n>=n0

Big Ω Notation

if T(n)=Ω(n2), 就是说T(n)>=Cn2 for some constant C and all n>=n0

Big Θ Notation

if T(n)=Θ(n2), 就是说T(n)=O(n2) and T(n)=Ω(n2)

[!TIHINKING] 是否存在两个关于n的函数f和g使得 f(n)O(g(n)),且 f(n)Ω(g(n))? 只要f交错震荡穿过g即可

little o notation

T(n)=o(n2) means that , for all C, there exists n >n_0 such that T(n)<Cn2

little ω notation

T(n)=ω(n2) means that , for all C, there exists n >n_0 such that T(n)>Cn2

Divide and Conquer

Merge sort as an example T(n)=2T(n/2)+O(n)