Chapter 0. Prologue
Johann Gutenberg: Typography
Al Khwarizmi: Indian numerals
Precise, unambiguous, mechanical, efficient, correct: algorithms.
Fibonacci Sequence
Formally
An exponential algorithm.
![image-20230412223528776](C:\Users\Steven Meng\AppData\Roaming\Typora\typora-user-images\image-20230412223528776.png)
Let be the number of computer steps needed to compute fib1(n)
.
For
For
By induction, for all
![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- notation
and are the running times of two algorithms on inputs of size .
Let and be functions from positive integers to positive reals. We say (which means that “f grows no faster than g”) if there is a constant such that .
is a very loose analog of “.” It differs from the usual notion of because of the constant , so that for instance .
Other similar notations
We also define analogs of $\ge $ and as follows:
- means .
- means and .
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 , express it as a product of its prime factors.(质因数分解)
Primality: Given a number , determine whether it is a prime.(素性判断, AKS Test)
Assumption: Factoring is HARD
Basic arithmetic
Prove that in the big- notation, the base is irrelevant, which means that,
Proof
![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
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?
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 is the multiplicative inverse of a modulo if mod .
There can be at most one such modulo with mod , denoted by .
If there are two distinct and satisfying mod and mod . mod .
So mod .
![image-20230422214911179](C:\Users\Steven Meng\AppData\Roaming\Typora\typora-user-images\image-20230422214911179.png)
Primality Testing
We can multiply together its elements in each of these representations to get
is relatively prime to .
![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 be the number of primes . Then , or more precisely,
比较丰富,所以随便挑一个 位数 ,如果通过素性测试,那么输出 。
选到的概率为 , 次测试。
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)
, we can write
如果 mod ,那么就会发现等于 0,mod 也是如此。
因此 mod .
![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
strassen乘法