Algorithms_week3_Analysis of Algorithms

官网总结
INTRO
研究算法是为了减少计算机运行时间从而降低成本
比如FFT(快速傅里叶算法)。an algorithm for breaking down the wave form of n samples of a signal into periodic components.将信号的N个采样波形分为若干周期分量 And that’s at the basis for dvds and jpegs and, and many other appl ications. There’s an easy way to do it that takes time proportional to N^2. But the FFT algorithm, takes only N log N steps. And the difference between N log N and N^2 is, is the difference between being able to solve a large problem and not being able to solve it.
又比如N body simulation problem N体仿真问题。The easy algorithm takes time proportional to N^2, but Appel’s algorithm was an N log N algorithm that again, meant that scientists can do N body simulation for huge values of N.

OBSERVATIONS

在这里插入图片描述
如果代码是这样的,怎么计算需要多久
在这里插入图片描述
可以用java自带函数
在这里插入图片描述
根据实践测试,发现运行时间和数据数量呈现幂定律 power law
在这里插入图片描述
当得到这个模型时,可以根据公式predict运行N个样本需要花多久,不用再实际运行。
除了system independent effects (algorithm和input data),实际的运行时间还由system dependent effects包括hardware, software, system等决定。

MATHEMATICAL MODELS
The total running time of a program is determined by two primary factors: the cost of executing each statement and the frequency of execution of each statement.
我们需要知道:加减乘除、取整、三角函数、length()等基础函数分别需要多久nanoseconds纳秒
在这里插入图片描述
注意:连接字符串string concatenation的时间和字符串长度成正比,而非常数操作时间
在这里插入图片描述
但是每个细节都考虑的话太麻烦了,所以现在都是 rather than going in and counting every little detail, we take some basic operation that’s maybe the most expensive and or the one that’s executed the most often, and the one that costs time frequency is the highest, and use that as a proxy for the running time.
在上面这个2-sum的例子中我们选择array access用来估计running time。在这里插入图片描述
经过以上2个简化的步骤,我们现在可以估计2-sum的运行时间约为aN^2。
同理,对于3-sum,3CN ~ 1/6N^3。 tilde it for each triple每个3整数组访问数据组三次,得到~ 1/2N^3。
在这里插入图片描述

Order-of-Growth Classifications
在这里插入图片描述
从上图可以看出log算法的优越性,即使size很大log需要花的时间也很小。当linear或者NlogN的时候,时间和size成正比,这时就要考虑input data的数量了。
【例】binary search:给一个有序整数数组和一个值,想知道这个值在数组中是否存在,在什么位置。代码如下

在这里插入图片描述
证明:根据递归关系,T(N)~logN
在这里插入图片描述
用binary search优化3-sum算法
在这里插入图片描述
N^2logN 比 N^3 好

Theory of Algorithms
在现实中,会有best case (determined by easiest input), worst case, average case. 为了适应客户需求,需要知道input对运行时间的影响。
在这里插入图片描述
两种解决办法
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

MEMORY
在这里插入图片描述
在这里插入图片描述
float单精度浮点 long长整型 double双精度浮点
在这里插入图片描述
在这里插入图片描述
总结
在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44241082/article/details/85601881