1 / 14

第一章 演算法分析 Algorithm Analysis

第一章 演算法分析 Algorithm Analysis. The early bird gets the worm. 練功前的沉思. Q1: 過去曾操作過某些軟體,其功能雷同, 但為什麼執行起來,花費的時間差異很大? Q2: 以前班上有些同學寫程式 看起來 很厲害,但是 他們寫的程式內容真的很棒的嗎?? Q3: 我在資訊領域中學會了撰寫程式,但同樣一隻 程式,大家寫出來的內容都不太一樣,那我如何 知道我的程式寫的好不好,有沒有方法可以評量? (*)資料結構( DS) 就是這個基礎入門功夫, DS 協助我們

ward
Download Presentation

第一章 演算法分析 Algorithm Analysis

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第一章 演算法分析Algorithm Analysis The early bird gets the worm.

  2. 練功前的沉思 Q1:過去曾操作過某些軟體,其功能雷同, 但為什麼執行起來,花費的時間差異很大? Q2: 以前班上有些同學寫程式看起來很厲害,但是 他們寫的程式內容真的很棒的嗎?? Q3:我在資訊領域中學會了撰寫程式,但同樣一隻 程式,大家寫出來的內容都不太一樣,那我如何 知道我的程式寫的好不好,有沒有方法可以評量? (*)資料結構(DS)就是這個基礎入門功夫,DS協助我們 釐清,如何撰寫一隻程式(或演算法),了解資料本 身如何影響一個演算法的優劣 李麗華--資料結構講義

  3. What is algorithm Definition: (以後簡稱Def) An algorithm is a finite set of instructions that , if followed, accomplishes a particular task. 定義: 演算法就是利用有限的步驟來解決特定的問題. 李麗華--資料結構講義

  4. Characteristics of an Algorithm -All algorithm must have: (1) Input: zero or more quantities supplied externally (2) Output: at least one quantity is produced (3) Definiteness: clear and unambiguous (4) Effectiveness: easy to carry out (5) Finiteness: for all cases, the algorithm terminates 李麗華--資料結構講義

  5. How to measure an algorithm? • 所以說,一支解決特定問題的程式,其實就是一個解決特定問題的algorithm. • 那程式寫的好不好,到底如何量測?? 要量什麼? (1)測量執行的時間(time) (2)測量程式內每個statement被執行的次數 (3)測量程式所須耗用的空間(memory space) • Complexity Analysis 即在分析演算法時,我們運用上述各項測量方式來分析程式的效能. • Time Complexity:程式執行時所花的時間分析 李麗華--資料結構講義

  6. 敘述執行次數說明 李麗華--資料結構講義

  7. Big-O 又是什麼? • 在分析演算法時, 每個個程式敘述被執行的次數稱為order of magnitude,亦稱為Big-O. • Big-O的定義為: 若f(n)此函數代表一個程式敘述須要被執行的總次數,如果可以找到g(n),i.e., f(n)<= cg(n) for all n, & c:constant, 則 f(n)=O(g(n)) • f(n)=amnm+am-1nm-1+…+a1n1+a0, 則 f(n)=O(nm) (請參見課本證明) 李麗華--資料結構講義

  8. Big-O範例說明 Examples: 4n + 2  O(n) 7n - 3  O(n) 200n + 6  O(n) 10n2 + 5n - 2  O(n2) 9n3- n2 + 5n - 5  O(n3) 7*2n + n3 + n2  O(2n) : 李麗華--資料結構講義

  9. 常見的Big-O及其優劣 O(1): constant time O(log n): sub-linear time O(n): linear time O(n log n): n log n time O(n2): quadratic time O(n3): cubic time O(2n): exponential time O(n!): factorial time 這..這也太不可思議了… 這演算法真的太優了… 好棒的演算法……… 這仍然令人激賞…….. well,算是大眾化囉…. 嗯…該用點心了…. 天那,指數? 太不妙了… 我哭,真的沒輒了嗎? 優 劣 李麗華--資料結構講義

  10. Plot of Big-O Performance(參見P13) 李麗華--資料結構講義

  11. Time needed for various time complexity (參見P13) 李麗華--資料結構講義

  12. Times on a 1 billion instruction per second computer(p.40) 李麗華--資料結構講義

  13. 大師—該動動腦囉!! • 課本第一章習題, 請解答下列: • 第3題(全部): 將程式列出並在右邊寫出對照的執行次數, 最後並找出加總次數及正確的Big-O值 • 第4題: 如題目所要求, 列出當X=1,3,13,21時,在while迴圈內所須要執行的次數, 及所有加總次數 • 第5題: 將程式列出, 並在右邊寫出對照的執行次數, 最後並找出加總次數及正確的Big-O值 (*)本次作業以紙本繳交 李麗華--資料結構講義

  14. Now there is no time to think of what you do not have. Think of what you can do with what there is. 與其懊腦所失,不如掌握現在 --Earnest Hemingway(1899-1961) 李麗華--資料結構講義

More Related