python学习笔记:算法复杂度

1. 算法复杂度:是指算法在编写为可执行程序后运行所需的资源,包括时间资源和内存资源,分别用时间复杂度空间复杂度描述。

2. big-O符号:(big O notation)代表“order of ...”(......阶),由德国数论学家Paul Bachmann于1892年著作中首先引入,其后由德国数论学家Edmund Landau推广,有时也称为朗道符号(Landau symbol)。描述函数的渐近行为,在CS中,在分析算法复杂度方面用处很大。

    3. 算法复杂度影响因素:

  • 算法采用的策略
  • 编译代码质量
  • 问题输入规模N
  • 机器执行指令速度

一、时间复杂度

(1)时间频度:记为T(n),对算法程序中语句执行次数的描述,也称为语句频度

(2)时间复杂度:记为O(n),对算法程序时间频度变化趋势的描述。当n趋近于无穷大时,若有某个辅助函数f(n),T(n)/f(n)为不等于0的常数,则T(n)=O(f(n)),这里f(n)称为T(n)的同数量级函数。

   1. 常见算法复杂度(从小到大,from http://www.krivers.net/15112-s18/notes/notes-efficiency.html

  • 常数:O(1)
  • 对数:O(logn)
  • 方根:O(n^0.5)
  • 线性:O(n)
  • 准线性:O(nlogn)
  • 平方:O(n^2)
  • 指数:O(k^n)
python学习笔记:算法复杂度_第1张图片 http://science.slc.edu/~jmarshall/courses/2002/spring/cs50/BigO/index.html

  2. python内置函数时间复杂度 (from http://www.krivers.net/15112-s18/notes/notes-efficiency.html)

General
Function/Method Complexity Code Example
Print O(N)
# where N is the size of the input
print(input)
Range in Iteration Number of iterations = (end - start)/step
for i in range(start, end, step):...
Strings: s is a string with N characters
Function/Method Complexity Code Example
Len O(1)
len(s)
Membership O(N)
"a" in s
Get single character O(1)
value = s[index]
Get slice O(end - start)
s[start:end]
Get slice with step O((end - start)/step)
s[start:end:step]
Chr() and Ord() O(1)
chr(s)
ord(s)
Concatentation O(len(s1) + len(s2))
s3 = s1 + s2
Character Type Methods O(N)
s.isalnum()
s.isalpha()
s.isdigit()
s.islower()
s.isspace()
s.isupper()
String Edit Methods O(N)
s.lower()
s.upper()
s.replace()
s.strip()
Substring Search Methods O(N)
s.count()
s.startswith()
s.endswith()
s.find()
s.index()
Lists: L is a list with N elements
Function/Method Complexity Code Example
Len O(1)
len(L)
Append O(1)
L.append(value)
Extend O(K)
# len(a) = K
L.extend(a)
Concatentation with += O(K)
# len(a) = K
L += a
Concatentation with + O(N + K)
# len(a) = K
L = L + a
Membership Check O(N)
item in L
Pop Last Value O(1)
L.pop()
Pop Intermediate Value O(N)
L.pop(index)
Count values in list O(N)
L.count(item)
Insert O(N)
L.insert(index, value)
Get value O(1)
value = L[index]
Set value O(1)
L[index] = value
Remove O(N)
L.remove(value)
Get slice O(end - start)
L[start:end]
Get slice with step O((end - start)/step)
L[start:end:step]
Sort O(N log (N))
L.sort()
sorted(L)
Multiply O(N*D)
#where D is an int
A = L*D
Minimum O(N)
min(L)
Maximum O(N)
max(L)
Copy O(N)
copy.copy(L)
Deep Copy O(N*M)
# where L is a 2D list with N rows and M cols
copy.deepcopy(L)
Sets: s is a set with N elements
Function/Method Complexity Code Example
Len O(1)
len(s)
Membership

O(1)

原因在于:python中的set是基于哈希表实现的,这与dict一样,因此,计算elem的哈希函数获得地址,若地址存储的值=key,则查询成功,次过程不需要进行比较。

elem in s
Adding an Element O(1)
s.add(elem)
Removing an Element O(1)
s.remove(elem)
s.discard(elem)
Union O(len(s) + len(t))
s|t
Intersection O(min(len(s), len(t)))
s&t
Difference O(len(s))
s - t
Clear O(len(s))
s.clear()
Copy O(len(s))
s.copy()
Dictionaries: d is a dictionary with N key-value pairs
Function/Method Complexity Code Example
Len O(1)
len(d)
Membership O(1)
key in d
Get Item O(1)
value = d[key]
d.get(key, defaultValue)
Set Item O(1)
d[key] = value
Delete Item O(1)
del d[key]
Clear O(N)
d.clear()
Copy O(N)
d.copy()

你可能感兴趣的:(python学习笔记:算法复杂度)