如何设计一个时间复杂度量级较低的演算法是我们必须一直摆在心中.ppt

如何设计一个时间复杂度量级较低的演算法是我们必须一直摆在心中.ppt

  1. 1、本文档共34页,可阅读全部内容。
  2. 2、有哪些信誉好的足球投注网站(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
  3. 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载
  4. 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
查看更多
如何设计一个时间复杂度量级较低的演算法是我们必须一直摆在心中

Floor and ceiling Modular arithmetic For any integer a and any positive integer n, the value a mod n is the remainder (or residue) of the quotient a/n : a mod n =a -?a/n?n. If(a mod n) = (b mod n). We write a ? b (mod n) and say that a is equivalent to b, modulo n. We write a ? b (mod n) if a is not equivalent to b modulo n. Analyzing Algorithms Based on: 物件導向資料結構 — 使用Java語言, 江振瑞 著, 松崗圖書公司, 2005. Introduction to the Design and Analysis of Algorithms -- A strategic approach, 2E, R.C.T. Lee et. al., NcGraw Hill, 2005. Introduction to Algorithms, Cormen et. al., MIT Press. Analyzing algorithms Analyzing an algorithm has come to mean predicting the resources that the algorithm requires. Resources: memory, communication, bandwidth, logic gate, time. We usually assume that the algorithm is performed on a processor with RAM Analyzing algorithms The running time of an algorithm on a particular input is the number of primitive operations or “steps” executed. It is convenient to define the notion of step so that it is as machine-independent as possible Complexity 一般我們使用 時間複雜度(time complexity) 空間複雜度(space complexity) 來評估演算法的執行時間與所佔用記憶體空間, 這些複雜度愈低,則表示演算法愈好。 我們比較關注time complexity! Three Cases 演算法的時間複雜度分析分為以下三種: 最佳狀況(best case)時間複雜度:考慮演算法執行時所需要的最少執行步驟數。 最差狀況(worst case)時間複雜度:考慮演算法執行時所需要的最多執行步驟數。 平均狀況(average case)時間複雜度:考慮所有可能狀況下演算法的平均執行步驟數。 Usually, we concentrate on finding only on the worst-case running time Reason: ?It is an upper bound on the running time ?The worst case occurs fair often The average case is often as bad as the worst case. For example, the insertion sort. Again, quadratic function. Worst-case and average-case analysis An Alg. for Testing Primes 我們可以看出,輸入大於2的任意正整數n,若n是質數,則演算法Prime1需要執行整數除法求餘數(n%i)動作與整數比較((n%i)=0)動作n-2次之後,才可以知道n是質數。另外,若n不是質值,則演算法Prime1只要執行整數除法求餘數與整數比較動作1次,就可以知道n不是質數了。 Algorithm Prime1(n): Input:一個大於2的正整數n Output:true或false(表示n是質數或不是質數) for i←2 to n-1 do if (n%i)=0 then return false return true 我們可以看出,輸入大於2的任意正整數n,若n是質數,則演算

文档评论(0)

wangyueyue + 关注
实名认证
内容提供者

该用户很懒,什么也没介绍

1亿VIP精品文档

相关文档