抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

Chapter 0. Prologue

Johann Gutenberg: Typography

Al Khwarizmi: Indian numerals

Precise, unambiguous, mechanical, efficient, correct: algorithms.

Fibonacci Sequence

Formally

Fn={Fn1+Fn2n>11n=10n=0F_n=\left\{ \begin{aligned} &F_{n-1}+F_{n-2}&n>1\\ &1&n=1\\ &0&n=0 \end{aligned} \right.

An exponential algorithm.

![image-20230412223528776](C:\Users\Steven Meng\AppData\Roaming\Typora\typora-user-images\image-20230412223528776.png)

Let T(n)T(n) be the number of computer steps needed to compute fib1(n).

For n1n\le1

T(n)2T(n)\le2

For n>1n>1

T(n)=T(n1)+T(n2)+3T(n)=T(n-1)+T(n-2)+3

By induction, for all nNn \in \N

T(n)FnT(n)=T(n1)+T(n2)+3Fn1+Fn2=FnT(n)\ge F_n\\ T(n)=T(n-1)+T(n-2)+3\ge F_{n-1}+F_{n-2}=F_n

![image-20230412224315685](C:\Users\Steven Meng\AppData\Roaming\Typora\typora-user-images\image-20230412224315685.png)

Arithmetic operations on arbitrarily large numbers cannot possibly be performed in a single, constant-time step.

It therefore makes more sense to seek an machine-independent characterization
of an algorithm’s efficiency.

Big-OO notation

f(n)f(n) and g(n)g(n) are the running times of two algorithms on inputs of size nn.

Let f(n)f(n) and g(n)g(n) be functions from positive integers to positive reals. We say f=O(g)f=O(g) (which means that “f grows no faster than g”) if there is a constant c>0c>0 such that f(n)cg(n)f(n)\le c\cdot g(n).

f=O(g)f=O(g) is a very loose analog of “fgf\le g.” It differs from the usual notion of \le because of the constant cc, so that for instance 10n=O(n)10n=O(n).

Other similar notations

We also define analogs of $\ge $ and == as follows:

  • f=Ω(g)f=\Omega(g) means g=O(f)g=O(f).
  • f=Θ(g)f=\Theta(g) means f=O(g)f=O(g) and f=Ω(g)f=\Omega (g).

Simple rules

![image-20230412225757794](C:\Users\Steven Meng\AppData\Roaming\Typora\typora-user-images\image-20230412225757794.png)

Chapter 1. Algorithms with Numbers

数论算法

Factoring: Given a number NN, express it as a product of its prime factors.(质因数分解)

Primality: Given a number NN, determine whether it is a prime.(素性判断, AKS Test)

Assumption: Factoring is HARD

Basic arithmetic

Prove that in the big-OO notation, the base is irrelevant, which means that,

Θ(f(N)logaN)=f(N)logbN\Theta (f(N)\cdot \log_a N )=f(N)\cdot \log_b N

Proof

O(f(N)C)=f(N)logNO(f(N)\cdot C)=f(N)\log N

![image-20230412230729515](C:\Users\Steven Meng\AppData\Roaming\Typora\typora-user-images\image-20230412230729515.png)

Addition

The sum of any three single-digit numbers is at most two digits long for any b2b\ge2

3×(b1)b213\times (b-1) \le b^2-1

This simple rule gives us a way to add two numbers in any base: align their right-hand ends, and then perform a single right-to-left pass in which the sum is computed digit by digit, maintaining the overflow as a carry. Since we know each individual sum is a two-digit number, the carry is always a single digit, and so at any given step, three single-digit numbers are added.

Given two binary numbers x and y, how long does our algorithm take to add them?

O(n)O(n)

Is there a faster algorithm?

Optimal!

A single instruction we can add integers whose size in bits is within the word length of today’s computer – 64 perhaps. But it is often useful and necessary to handle numbers much larger than this, perhaps several thousand bits long.

![image-20230422211726036](C:\Users\Steven Meng\AppData\Roaming\Typora\typora-user-images\image-20230422211726036.png)

![image-20230422211738259](C:\Users\Steven Meng\AppData\Roaming\Typora\typora-user-images\image-20230422211738259.png)

Modular arithmetic

![image-20230422211813555](C:\Users\Steven Meng\AppData\Roaming\Typora\typora-user-images\image-20230422211813555.png)

Two’s complement

![image-20230422212104717](C:\Users\Steven Meng\AppData\Roaming\Typora\typora-user-images\image-20230422212104717.png)

![image-20230422212137134](C:\Users\Steven Meng\AppData\Roaming\Typora\typora-user-images\image-20230422212137134.png)

结合律、交换律和分配律。

Modular Addition

![image-20230422212551189](C:\Users\Steven Meng\AppData\Roaming\Typora\typora-user-images\image-20230422212551189.png)

Modular multiplication

Modular exponentiation

Euclid’s algorithm for greatest common divisor

Modular inverse

We say xx is the multiplicative inverse of a modulo NN if ax1ax\equiv 1 mod NN.

There can be at most one such xx modulo NN with ax1ax\equiv 1 mod NN, denoted by a1a^{-1}.

If there are two distinct xx and yy satisfying ax1ax \equiv 1 mod NN and ay1ay\equiv 1 mod NN. x≢yx\not\equiv y mod NN.

a(xy)0(modN)a(x-y)\equiv 0 \pmod{N}

So a=0a=0 mod NN.

![image-20230422214911179](C:\Users\Steven Meng\AppData\Roaming\Typora\typora-user-images\image-20230422214911179.png)

Primality Testing

S={1,2,,p1}={a1modp,a2modp,,a(p1)modp}S=\{1,2,\cdots,p-1\}=\{a\cdot 1\bmod p, a\cdot 2 \bmod p,\cdots,a\cdot (p-1)\bmod p\}

We can multiply together its elements in each of these representations to get

(p1)!ap1(p1)!(modp)(p-1)!\equiv a^{p-1}\cdot(p-1)!\pmod{p}

(p1)!(p-1)! is relatively prime to pp.

![image-20230422215824135](C:\Users\Steven Meng\AppData\Roaming\Typora\typora-user-images\image-20230422215824135.png)

![image-20230422220009962](C:\Users\Steven Meng\AppData\Roaming\Typora\typora-user-images\image-20230422220009962.png)

![image-20230422220030975](C:\Users\Steven Meng\AppData\Roaming\Typora\typora-user-images\image-20230422220030975.png)

![image-20230422220356722](C:\Users\Steven Meng\AppData\Roaming\Typora\typora-user-images\image-20230422220356722.png)

![image-20230422220446311](C:\Users\Steven Meng\AppData\Roaming\Typora\typora-user-images\image-20230422220446311.png)

Lagrange’s prime number theorem. Let π(x)\pi(x) be the number of primes x\le x. Then π(x)x/(lnx)\pi(x)\approx x/(\ln x), or more precisely,

limxπ(x)x/lnx=1\lim_{x\to \infin} \frac{\pi(x)}{x/\ln x}=1

比较丰富,所以随便挑一个 nn 位数 NN,如果通过素性测试,那么输出 NN

选到的概率为 π(N)/N=lnN=O(n)\pi(N)/N=\ln N=O(n)O(n)O(n) 次测试。

Cryptography

![image-20230422221314567](C:\Users\Steven Meng\AppData\Roaming\Typora\typora-user-images\image-20230422221314567.png)

BS 算法

![image-20230422221606417](C:\Users\Steven Meng\AppData\Roaming\Typora\typora-user-images\image-20230422221606417.png)

![image-20230422221614567](C:\Users\Steven Meng\AppData\Roaming\Typora\typora-user-images\image-20230422221614567.png)

The Rivest-Shamir-Adelman (RSA) cryptosystem

An example of public-key cryptography

  • Anybody can send a message to anybody else using publicly available information, rather like addresses or phone numbers.
  • Each person has a public key known to the whole world and a secret key known only to him- or herself.
  • When Alice wants to send message x to Bob, she encodes it using his public key.
  • Bob decrypts it using his secret key, to retrieve x.
  • Eve is welcome to see as many encrypted messages for Bob as she likes, but she will not be able to decode them, under certain simple assumptions.

Property

![image-20230422222639537](C:\Users\Steven Meng\AppData\Roaming\Typora\typora-user-images\image-20230422222639537.png)

ed=1(mod(p1)(q1))ed=1\pmod{(p-1)(q-1)}, we can write

ed=1+k(p1)(q1)ed=1+k(p-1)(q-1)

xedx=x(xk(p1)(q1)1)=x((xp1)k(q1)1)x^{ed}-x=x(x^{k(p-1)(q-1)}-1)=x((x^{p-1})^{k(q-1)}-1)

如果 mod pp,那么就会发现等于 0,mod qq 也是如此。

因此 (xe)dx(x^e)^d\equiv x mod NN.

![image-20230422223438395](C:\Users\Steven Meng\AppData\Roaming\Typora\typora-user-images\image-20230422223438395.png)

Universal Hashing

![image-20230422224144188](C:\Users\Steven Meng\AppData\Roaming\Typora\typora-user-images\image-20230422224144188.png)

![image-20230422224307966](C:\Users\Steven Meng\AppData\Roaming\Typora\typora-user-images\image-20230422224307966.png)

![image-20230422224424672](C:\Users\Steven Meng\AppData\Roaming\Typora\typora-user-images\image-20230422224424672.png)

![image-20230422224624272](C:\Users\Steven Meng\AppData\Roaming\Typora\typora-user-images\image-20230422224624272.png)

![image-20230422225005163](C:\Users\Steven Meng\AppData\Roaming\Typora\typora-user-images\image-20230422225005163.png)

Chapter 2. Divide-and-conquer algorithms

![image-20230422225248681](C:\Users\Steven Meng\AppData\Roaming\Typora\typora-user-images\image-20230422225248681.png)

![image-20230422225606828](C:\Users\Steven Meng\AppData\Roaming\Typora\typora-user-images\image-20230422225606828.png)

![image-20230422225643248](C:\Users\Steven Meng\AppData\Roaming\Typora\typora-user-images\image-20230422225643248.png)

![image-20230422225902755](C:\Users\Steven Meng\AppData\Roaming\Typora\typora-user-images\image-20230422225902755.png)

Recurrence relations

Master theorem

T(n)=aT(nb)+f(n)n>bT(n) = a T\left(\frac{n}{b}\right)+f(n)\qquad \forall n > b

T(n)={Θ(nlogba)f(n)=O(nlogbaϵ)Θ(f(n))f(n)=Ω(nlogba+ϵ)Θ(nlogbalogk+1n)f(n)=Θ(nlogbalogkn),k0T(n) = \begin{cases}\Theta(n^{\log_b a}) & f(n) = O(n^{\log_b a-\epsilon}) \\ \Theta(f(n)) & f(n) = \Omega(n^{\log_b a+\epsilon}) \\ \Theta(n^{\log_b a}\log^{k+1} n) & f(n)=\Theta(n^{\log_b a}\log^k n),k\ge 0 \end{cases}

strassen乘法

p    qp\iff q

评论