[toc]
Lecture 5-6
P2.What is complexity analysis?
什么是算法復雜度
- The complexity of an algorithm is the amount of resources it needs in order to execute.
- Time complexity - Amount of time the algorithm needs
- Space complexity - Amount of memory the algorithm needs
P4-5.Basic operations of an algorithm & What is a basic operation?
- 不同算法的Basic operation是什么。排序中的遍歷過程、merge比較
- A basic operation can be seen as an operation that is fundamental for the performance of a particular algorithm.
- The performance of the algorithm is in principle proportional(成正比) to the number of basic operations.
不同算法的Basic operation是什么
- In sorting algorithms: comparing two elements
- Graph traversal: traversing an edge
- For some algorithms a basic operation can be a whole loop, including all operations inside the loop
P7.Complexity as a function of the input - Examples
-
常見算法的復雜度、最好的情況、最壞的情況、在什么情況下最好
時間復雜度.png
- 當已經排好序的情況下最好
P20.Average and Worst case analysis
- 掌握最壞的情況、不用掌握平均的
- The worst-case complexity W(n) of an algorithm is the maximum number of basic operations performed for any input of n.
- The average-case complexity A(n) of an algorithm is the average number of basic operations performed by the algorithm, where each input I occurs with some probability p(I).
P23.Relative growth rate of functions
- 給出一個表達式,可以用Big O的形式表達出來、去掉常數項和低次項還有系數
P29.Example:
,
, and
- 上下界、O的上界、Ω的下界、Θ的確界
-
各個界的概念無需記住
各個界.jpg
-
例子
example.jpg 考點
給出一個表達式,可以用Big O的形式表達出來、去掉常數項和低次項還有系數
P36.P and NP
- NP中N 的含義、NP是否等于P、一般是不等、說明原因。 證明一個問題是NP問題,寫出步驟。P與NP的關系(目前認為兩者不等,如果P與NP相等會怎么樣,那么所有的問題都是可解的)
- P is the class of problems that can be solved by plynomial bound algorithms.
-
NP(non-deterministic polynomial-time) is more complicated to describe.
? It contains problems that we believe are more difficult to solve
than those in P.
? We believe that they cannot be solved using any polynomial
bound algorithm.
? However, we do not know whether this is true or not.
證明一個問題是NP問題,寫出步驟。
P44. NP-complete
什么是NP-complete
- 所有的P問題都可以歸約到它。All problems in NP can be reduced to it.
如何證明一個問題(P2)是NP-complete
- So P2 is a NP problem.
- P1 is a NP complete, all other problems R in NP can be polynomially reduced to P1.
- Show P1 can be polynomially reduced to P2
P46.How to show that a problem is N P complete I
- 過于具體、可以簡化
P47.How to show that a problem is N P complete II
- 具體證明步驟、會證明
- 把第二條分成兩點
P48.Amortized analysis - Initial example
- 只需要掌握基本概念、 a sequence of operations是關鍵詞
- Amortized analysis is a technique for analyzing the running time of repeated operations on a data structure.
The cost for a single insert might be much worse than the amortized analysis. 單次的操作開銷可能會比均攤分析的時間復雜度高,因為均攤分析計算的是平均的時間復雜度
It is used when one is interested in calculating a worst-case average bound per operation for any sequence of operations
It does not say anything about the cost of a specific operation in the sequence.
It is particular useful in situations where there are few expensive (slow) operations that are compensated by many inexpensive (fast)operations.
P52.Amortized or worst-case analysis
- In applications where it is important that all operations have a low cost, it might be more appropriate to use a worst-case analysis than an amortized analysis.
P53.Amortized vs average-case analysis
- 兩者并不一樣、AC是發生的概率,并不考慮多個步驟再平均
Amortized analysis provides an upper bound on the average cost per operation whereas average-case analysis provides an average cost per iteration, where consideration is taken to the probability of the occurrence of dierent inputs.
Amortized analysis provides an upper bound on the running time of a sequence of operations. Average-case analysis provides no such bound.
Amortized analysis needs no information about the probability on inputs.