Time Complexity -mycodeschool

简介: Time Complexity - why should we care?How to analyze Time Complexity?Time Complexity analysis - asymptotic notations.Time Complexity analysis -some general rules.

Time Complexity - why should we care?

n is a prime?

A

B

for 2 to n-1 if i divides n n is not prime

for 2 to sqrt(n) if i divides n n is not prime

this takes (n-2) times

this takes (sqrt(n)-1) times

with the increasing of the number n,B is faster than A.


How to analyze Time Complexity?

Input -rate of growth of time

we suggest a model machine:

single processor

32bit

sequential execution

1 unit time for arithmetical and logical operations

1 unit for assignment and return

O is an Asymptotic notation(渐进符号)

Tsum = k O(1)

Tsumoflist = Cn + C| O(n)

Tsumofmatrix = Cn2 +C|n +C|| O(n2)


Time Complexity analysis - asymptotic notations

O -"big -on" notation -> upper bound

O(g(n))={f(n):there exist constants C and n0 ,f(n)<=Cg(n),for n>=n0}

eg:

f(n) = 5n2 +2n + 1

g(n) = n2 C=5+2+1=8 n0 = 1

f(n)<=8n2 , n>=1

image-20230421174825660.png

Ω - Omega notation->lower bound

Ω(g(n)) = {f(n):there exist constants C and n0 Cg(n)<=f(n),for n>=n0}

eg:

f(n) = 5n2 +2n + 1

g(n) = n2 C=5 n0 = 0

5n2<=f(n) , n>=0

image-20230421175723383.png

θ - Theta notation -Tight bound

θ(g(n)) = {f(n):there exist constants C1 ,C2 and n0 ,C1g(n)<=f(n)<=C2g(n),for n>=n0}

f(n) = 5n2 +2n +1

g(n) = n2

C1 =5 C2 =8 n0 =1

5n2 <= f(n) <=8n2,n>=1

image-20230421180844410.png


Time Complexity analysis -some general rules

We analyze time complexity for:

Very large input-size

Worst case scenari0

Rule:

drop lower order terms

drop constant multiplier

eg:

T(n) = 17n4 + 3n3 + 4n +8-> T(n) = n4 ->O(n4)

Rule:Running Time = the sum of running time of all garments

in a program:

simple statements fragment1 O(1)

signal loop fragment2 O(n)

nested loop fragment3 O(n2)

it takes O(n2)

if we use if fragment2 O(n) else fragment3 O(n2) ,it also takes O(n2)

we always try to analyze it in the worst case

Rule: conditional statements, Pick complexity of condition which is worst case

 

目录
相关文章
|
人工智能 算法
Time complexity analysis of algorithms
时间复杂性的计算一般而言,较小的问题所需要的运行时间通常要比较大的问题所需要的时间少。设一个程序P所占用的时间为T,则        T(P)=编译时间+运行时间。 编译时间与实例特征是无关的,且可假设一个编译过的程序可以运行多次而无需再编译。
PAT (Advanced Level) Practice - 1145 Hashing - Average Search Time(25 分)
PAT (Advanced Level) Practice - 1145 Hashing - Average Search Time(25 分)
124 0
|
分布式计算 Spark 流计算
Real-Time Personalized Recommendation System
A real-time system is a system that processes input data within milliseconds so that the processed data is available almost immediately for feedback.
3154 0
Time range (447392) for take &#39;Take 001&#39; is larger than maximum allowed(100000).
http://www.cnblogs.com/lopezycj/archive/2012/05/16/unity3d_tuchao.html   https://forum.unity3d.com/threads/time-range-447406-for-translation-curve-s-on-node-bone001-on-take-take-001-what-the.
PAT (Advanced Level) Practice - 1096 Consecutive Factors(20 分)
PAT (Advanced Level) Practice - 1096 Consecutive Factors(20 分)
155 0
|
机器学习/深度学习
uva 586 Instant Complexity
点击打开链接uva 586 思路:递归模拟 分析: 1 题目是一道给定一段程序代码的球时间复杂度 2 根据题目的意思,我们可以利用栈和递归的方法,但是栈的方法比较不好写,所以我们利用递归的思路来写 3 当我们遇到LOOP的时候,我们就递...
717 0
|
安全 iOS开发 编译器
Effective Objective-C 2.0
本书是iOS开发进阶的必读书籍之一。文中部分名词的中文翻译略坑,比如对block和GCD的翻译。其他整体还好,原作者写的比较用心。代码规范讲了不少,底层原理讲了一点点,且主要集中在第二章。
1404 0
《Improving Real-Time Performance by Utilizing Cache Allocation Technology》电子版地址
Improving Real-Time Performance by Utilizing Cache Allocation Technology
94 0
《Improving Real-Time Performance by Utilizing Cache Allocation Technology》电子版地址
|
算法 关系型数据库 MySQL
Fundamental Techniques for Order Optimization
这是一篇1996年的老paper了,主要讲解了IBM DB2如何针对query当中的有序性进行优化。但对于后续physical property的优化有较为深远的影响,由于DB2的优化器起源于System-R以及其后续演进的starburst,因此延续了system-R中的interesting order和order property的概念。关于system-R的介绍请看之前的文章。 order这种physical property并不只限于order by算子,基于有序的group by/distinct等,都会利用到数据的排序操作,而排序本身就是比较昂贵的计算,因此应该对其做尽可能的优化
241 0
Fundamental Techniques for Order Optimization
|
安全 Go Windows
Practically Exploiting MS15-014 and MS15-011
If you’re reading this then you’ve probably seen all the media coverage over the last couple of days surrounding MS15-011 and MS15-014.
777 0

热门文章

最新文章