引入
课件用的是英文,因此复制过来的部分大部分保留英文。而我英文不好,表达不清意思,因此自己写的部分大部分还是沿用中文。
Introduction
In computer science, an algorithm is any well-defined computational procedure that takes some value(s) as input and produces some value(s) as output.
A particular input of a problem is an instance of that problem.
A data structure is a way to store and organize data in order to facilitate access and modifications.
Pseudocode
We'll typically describe algorithms as procedures written in a pseudocode(伪代码).
- Independent of specific c languages, but uses structural conventions of a normal programming language (like C, Java, C++)
- Intended for human reading rather than machine reading (omit nonessential details and easier to understand)
Some conventions:
- Give a valid name for the pseudocode procedure, specify the input and output variables' names (as well as the types) at the beginning.
- Use proper Indentation for every statement in a block structure.
- For a flow control statements use if-else. Always end an if statement with an end-if. Both if, else and end-if should be aligned vertically in same line.
- Use
<-
or:=
operator for assignment statements, Use=
for equality check. - Array elements can be represented by specifying the array name followed by the index in square brackets. For example,
A[i]
indicates the -th element of the arrayA
. - For looping or iteration use for or while statements. Always end a for loop with end-for and a while with end-while.
Integer Multiplication
An example of the Grade-School Algorithm for Multiplication of two numbers:
1 | Procedure GradeMult(x, y) |
- at most multiplications
- at most additions (for carries)
- add different -digit numbers ( additions)
At most single digit operations.
Recursion
使用递归的方法可以减少乘法的次数。
将 位数 分为两部分(无法均分则补 ):
于是
因此可以用四个更小的乘法来计算 。
Karatsuba Algorithm
我们可以在没有 与 的情况下得到 。
注意到
只需计算 ,因此就比上面的方法少了一次乘法。
Schönhage-Strassen Algorithm*
Convolution(卷积) of and :
可以看作是多项式相乘 ,然后求系数。
系数可以通过反对角线求和得到。
Schönhage-Strassen 算法:
- 给出 ,要求得 与 的卷积 ;
- 令 ;
- 取样 个点,得到 ,从而可以通过解线性方程得到 ;
- 若取样点是一组复数( 次单位根),线性方程可以通过快速傅立叶(Fast Fourier Transform, FFT)很快地解决。