【MIT 公开课】Computer Science and Programing Lession 8
2017-03-23 23:37
375 查看
第八节课主要讲的是算法的复杂度问题:
一.求a^b:
1.
T(b)=3b+2
O(b) 线性复杂度
2.
递归法:
T(b)=3+T(b-1)
=3*k+T(b-k) 当b-k=1时停止
=3*(b-1)+2
=3b-1
[b]O(b) 线性复杂度[/b]
3.
当b为偶数时:T(b)=6+T(b/2)
当b为奇数时:T(b)=6+T(b-1)=12+T[(b-1)/2]
所以:T(b)=12+12+T(b/2^2)=12*k+T(b/2^k)
当2^k=b时停止,所以k=log2^b
O(logb)对数级复杂度
二.
内嵌循环的复杂度
T=n*m
当m=n时,复杂度为O(n^2)
平方级的复杂度
三.
汉诺塔问题
T(n)=1+2T(n-1)+T(1)=3+2T(n-1)=3+2*3+4T(n-2)=3(1+2+4+……+2^(k-1))+2^kT(n-k)
复杂度为O(Z^n)
指数级复杂度
四.从排好序的数组中查找某个元素
1.
遍历法:O(len(s))线性级复杂度
2.def bsearch(s, e, first, last):
print first, last
if (last - first) < 2: return s[first] == e or s[last] == e
mid = first + (last - first)/2
if s[mid] == e: return True
if s[mid] > e: return bsearch(s, e, first, mid - 1)
return bsearch(s, e, mid + 1, last)二分法:复杂度为对数级,每次把规模减小一半。
一.求a^b:
1.
def exp1(a,b): ans = 1 while (b>0): ans *= a b -= 1 return ans
T(b)=3b+2
O(b) 线性复杂度
2.
def exp2(a,b): if b == 1: return a else: return a*exp2(a,b-1)
递归法:
T(b)=3+T(b-1)
=3*k+T(b-k) 当b-k=1时停止
=3*(b-1)+2
=3b-1
[b]O(b) 线性复杂度[/b]
3.
def exp3(a,b): if b == 1: return a if (b%2)*2 == b: return exp3(a*a, b/2) else: return a*exp3(a,b-1)
当b为偶数时:T(b)=6+T(b/2)
当b为奇数时:T(b)=6+T(b-1)=12+T[(b-1)/2]
所以:T(b)=12+12+T(b/2^2)=12*k+T(b/2^k)
当2^k=b时停止,所以k=log2^b
O(logb)对数级复杂度
二.
def g(n): x = 0 for i in range(n): for j in range(m): x += 1 return x
内嵌循环的复杂度
T=n*m
当m=n时,复杂度为O(n^2)
平方级的复杂度
三.
def Towers(size,fromStack,toStack,spareStack): if size == 1: print 'Move disk from ',fromStack, 'to ',toStack else: Towers(size-1,fromStack,spareStack,toStack) Towers(1,fromStack,toStack,spareStack) Towers(size-1,spareStack,toStack,fromStack)
汉诺塔问题
T(n)=1+2T(n-1)+T(1)=3+2T(n-1)=3+2*3+4T(n-2)=3(1+2+4+……+2^(k-1))+2^kT(n-k)
复杂度为O(Z^n)
指数级复杂度
四.从排好序的数组中查找某个元素
1.
def search(s, e): answer = None i = 0 numCompares = 0 while i < len(s) and answer == None: numCompares += 1 if e == s[i]: answer = True elif e < s[i]: answer = False i += 1 print answer, numCompares
遍历法:O(len(s))线性级复杂度
2.def bsearch(s, e, first, last):
print first, last
if (last - first) < 2: return s[first] == e or s[last] == e
mid = first + (last - first)/2
if s[mid] == e: return True
if s[mid] > e: return bsearch(s, e, first, mid - 1)
return bsearch(s, e, mid + 1, last)二分法:复杂度为对数级,每次把规模减小一半。
相关文章推荐
- 【MIT 公开课】Computer Science and Programing Lession 11
- 【MIT 公开课】Computer Science and Programing Lession 14
- 【MIT 公开课】Computer Science and Programing Lession 12
- 【MIT 公开课】Computer Science and Programing Lession 10
- 【MIT 公开课】Computer Science and Programing Lession 9
- MIT Electrical Engineering and Computer Science 参考书整理
- MIT Computer Science And Artificial Intelligence Laboratory
- MIT 6.00 Introduction to Computer Science and Programming, Fall 2008
- MIT Introduction to Computer Science and Programming (Lesson one )
- MIT 6.00 Introduction to Computer Science and Programming, Fall 2008
- MIT《Introduction to Computer Science and Programming》: Testing and Debugging
- Computer Science and Programming: Books and Resources
- 计算机方向的一些顶级会议和期刊—Top Conferences and Journals in Computer Science
- 6.00 Introduction to Computer Science and Programming Lec 9: Lecture 9: Memory and Search Methods
- 麻省理工学院 电机工程与资讯科学(Electrical Engineering and Computer Science) 在线课程列表
- Wireless Network Deployments (The International Series in Engineering and Computer Science)
- 计算机方向的一些顶级会议和期刊—Top Conferences and Journals in Computer Science
- 6.00 Introduction to Computer Science and Programming Lec 9: Lecture 9: Memory and Search Methods
- 计算机方向的一些顶级会议和期刊—Top Conferences and Journals in Computer Science
- Computer and Computer Science