Chapter 3 The Efficiency of Algorithms 國立雲林科技大學 資訊工程研究所 張傳育 (Chuan-Yu Chang ) 博士 Office: ES 709 TEL: 05-5342601 ext. 4337

Slides:



Advertisements
Similar presentations
The Efficiency of Algorithms
Advertisements

Chapter 3: The Efficiency of Algorithms Invitation to Computer Science, Java Version, Third Edition.
CPSC 171 Introduction to Computer Science Efficiency of Algorithms.
The Efficiency of Algorithms
布林代數的應用--- 全及項(最小項)和全或項(最大項)展開式
第七章 抽樣與抽樣分配 蒐集統計資料最常見的方式是抽查。這 牽涉到兩個問題: 抽出的樣本是否具有代表性?是否能反應出母體的特徵?
學習C++的基本語法 認識關鍵字與識別字的不同 學習程式碼偵錯的流程 學習如何提高程式的可讀性
Section 1.2 Describing Distributions with Numbers 用數字描述分配.
: Boxes ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 11003: Boxes 解題者:蔡欣燁 解題日期: 2007 年 3 月 19 日.
: Factstone Benchmark ★★☆☆☆ 題組: Problem Set Archive with Online Judge 題號: : Factstone Benchmark 解題者:鐘緯駿 解題日期: 2006 年 06 月 06 日 題意: 假設 1960.
Lecture 8 Median and Order Statistics. Median and Order Statistics2 Order Statistics 問題敘述 在 n 個元素中,找出其中第 i 小的元素。 i = 1 ,即為找最小值。 i = n ,即為找最大值。 i = 或 ,即為找中位數。
Graph V(G 1 )={0, 1, 2, 3, 4, 5, 6, 7, 8, 9} E(G 1 )={(0, 2), (0, 3), (1, 4), (2, 3), (2, 5), (2, 6), (3, 6), (3, 7), (4, 7), (5, 6), (5,
:New Land ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 11871: New Land 解題者:施博修 解題日期: 2011 年 6 月 8 日 題意:國王有一個懶兒子,為了勞動兒子,他想了一個 辦法,令他在某天早上開始走路,直到太陽下山前,靠.
: OPENING DOORS ? 題組: Problem Set Archive with Online Judge 題號: 10606: OPENING DOORS 解題者:侯沛彣 解題日期: 2006 年 6 月 11 日 題意: - 某間學校有 N 個學生,每個學生都有自己的衣物櫃.
: ShellSort ★★☆☆☆ 題組: Problem D 題號: 10152: ShellSort 解題者:林一帆 解題日期: 2006 年 4 月 10 日 題意:烏龜王國的烏龜總是一隻一隻疊在一起。唯一改變烏龜位置 的方法為:一隻烏龜爬出他原來的位置,然後往上爬到最上方。給 你一堆烏龜原來排列的順序,以及我們想要的烏龜的排列順序,你.
Chapter 2 聯立線性方程式與矩陣 緒言 線性方程式組 (systems of linear equations) 出現 在多數線性模式 (linear model) 中。根據以往解 題的經驗,讀者們也許已發現方程式的解僅與 該方程式的係數有關,求解的過程也僅與係數 的運算有關,只要係數間的相關位置不改變,
Chapter 3 Growth of Functions Asymptotic notation Θ-notation: f(n) = Θ(g(n)) , g(n) is an asymptotically tight bound for f(n) 。 Θ(g(n)) = {f(n)|
STAT0_sampling Random Sampling  母體: Finite population & Infinity population  由一大小為 N 的有限母體中抽出一樣本數為 n 的樣 本,若每一樣本被抽出的機率是一樣的,這樣本稱 為隨機樣本 (random sample)
1. 假設以下的敘述為一未提供 “ 捷徑計算 ” 能力的程式段,試用程 式設計的技巧,使此敘述經此改 寫的動作後,具有與 “ 捷徑計算 ” 之 處理方法相同之處理模式。 if and then E1 else E2 endif.
8.1 何謂高度平衡二元搜尋樹 8.2 高度平衡二元搜尋樹的加入 8.3 高度平衡二元搜尋樹的刪除
: The Playboy Chimp ★★☆☆☆ 題組: Problem Set Archive with Online Judge 題號: 10611: The Playboy Chimp 解題者:蔡昇宇 解題日期: 2010 年 2 月 28 日 題意:給一已排序的數列 S( 升冪.
Analyzing Algorithms Based on: 1. 物件導向資料結構 — 使用 Java 語言, 江振 瑞 著, 松崗圖書公司, Introduction to the Design and Analysis of Algorithms -- A strategic.
Integrated single vendor single buyer model with stochastic demand and variable lead time 指導教授:林燦煌 博士 指導教授:林燦煌 博士 研 究 生:黃笙源 研 究 生:黃笙源 M. Ben-Daya, M. Hariga.
: Walking on a Grid ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 10913: Walking on a Grid 解題者:陳盈村 解題日期: 2008 年 5 月 2 日 題意:給定一個 N*N 矩陣,題目要求依照.
Chapter 3 The Efficiency of Algorithms
Greedy Algorithms. 2 Greedy Methods ( 描述 1) * 解最佳化問題的演算法, 其解題過程可看成是由一 連串的決策步驟所組成, 而每一步驟都有一組選擇 要選定. * 一個 greedy method 在每一決策步驟總是選定那目 前看來最好 的選擇. *Greedy.
:Problem D: Bit-wise Sequence ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 10232: Problem D: Bit-wise Sequence 解題者:李濟宇 解題日期: 2006 年 4 月 16.
Chapter 3: The Efficiency of Algorithms Invitation to Computer Science, C++ Version, Third Edition Additions by Shannon Steinfadt SP’05.
: War on Weather ★★☆☆☆ 題組: Contest Volumes Archive with Online Judge 題號: 10915: War on Weather 解題者:陳明凱 題意:題目總共會給你 k 個點座標代表殺手衛星的位置, 距離地球表面最少 50 公里以上,並且會給你.
: Fast and Easy Data Compressor ★★☆☆☆ 題組: Problem Set Archive with Online Judge 題號: 10043: Fast and Easy Data Compressor 解題者:葉貫中 解題日期: 2007 年 3.
選舉制度、政府結構與政 黨體系 Cox (1997) Electoral institutions, cleavage strucuters, and the number of parties.
Fourier Series. Jean Baptiste Joseph Fourier (French)(1763~1830)
: Little Red Riding Hood ★★★☆☆ 題組: Contest Volumes Archive with Online Judge 題號: 11067: Little Red Riding Hood 解題者:陳明凱 解題日期: 2008 年 3 月 14 日 題意:
Chapter 2 Getting Started Insertion Sort: 能有效率地排序小數字的演算法 範例 :
: Problem A : MiniMice ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11411: Problem A : MiniMice 解題者:李重儀 解題日期: 2008 年 9 月 3 日 題意:簡單的說,題目中每一隻老鼠有一個編號.
Johnson’s algorithm Johnson’s演算法可用於計算All pairs shortest path問題。
: Count DePrimes ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11408: Count DePrimes 解題者:李育賢 解題日期: 2008 年 9 月 2 日 題意: 題目會給你二個數字 a,b( 2 ≦ a ≦ 5,000,000,a.
: Placing Lampposts ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 10859: Placing Lampposts 解題者:陳志瑜 解題日期: 2011 年 5 月 10 日 題意:美化為 Dhaka City.
:Nuts for nuts..Nuts for nuts.. ★★★★☆ 題組: Problem Set Archive with Online Judge 題號: 10944:Nuts for nuts.. 解題者:楊家豪 解題日期: 2006 年 2 月 題意: 給定兩個正整數 x,y.
Lecture 7 Sorting in Linear Time. Sorting in Linear Time2 7.1 Lower bounds for sorting 本節探討排序所耗用的時間複雜度下限。 任何一個以比較為基礎排序的演算法,排序 n 個元 素時至少耗用 Ω(nlogn) 次比較。
: Efficient Solutions ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 11020: Efficient Solutions 解題者:陳宜佐 解題日期: 2007 年 4 月 24 日 題意:給定 M 個 case.
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2008.
Section 4.2 Probability Models 機率模式. 由實驗看機率 實驗前先列出所有可能的實驗結果。 – 擲銅板:正面或反面。 – 擲骰子: 1~6 點。 – 擲骰子兩顆: (1,1),(1,2),(1,3),… 等 36 種。 決定每一個可能的實驗結果發生機率。 – 實驗後所有的實驗結果整理得到。
Image Interpolation Use SSE 指導教授 : 楊士萱 學 生 : 楊宗峰 日 期 :
JAVA 程式設計與資料結構 第二十章 Searching. Sequential Searching Sequential Searching 是最簡單的一種搜尋法,此演 算法可應用在 Array 或是 Linked List 此等資料結構。 Sequential Searching 的 worst-case.
Chapter 3: The Efficiency of Algorithms Invitation to Computer Science, C++ Version, Fourth Edition.
演算法 8-1 最大數及最小數找法 8-2 排序 8-3 二元搜尋法.
: Flip Sort ★★☆☆☆ 題組: Problem Set Archive with Online Judge 題號: 10327: Flip Sort 解題者:歐子揚 解題日期: 2010 年 2 月 26 日 題意:在這個問題中使用一種排序方式 (Flip) ,意思就是 只能交換相鄰的.
845: Gas Station Numbers ★★★ 題組: Problem Set Archive with Online Judge 題號: 845: Gas Station Numbers. 解題者:張維珊 解題日期: 2006 年 2 月 題意: 將輸入的數字,經過重新排列組合或旋轉數字,得到比原先的數字大,
Chapter 2. Recurrence Relations (遞迴關係)
Chapter 10 m-way 搜尋樹與B-Tree
Chapter 3: The Efficiency of Algorithms
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2009.
2005/7 Linear system-1 The Linear Equation System and Eliminations.
連續隨機變數 連續變數:時間、分數、重量、……
統計緒論 _ Chap2 資料整理 2.1 基本的資料整理  排序,例: length of 60 sea trouts ( 課本 p13 ) ( 可用 EXCEL)  長條圖,例 2.1 number of times of delay in a week for the 48 flights.
資料結構實習-六.
1 Introduction to Java Programming Lecture 2: Basics of Java Programming Spring 2010.
: SAM I AM ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11419: SAM I AM 解題者:李重儀 解題日期: 2008 年 9 月 11 日 題意: 簡單的說,就是一個長方形的廟裡面有敵人,然 後可以橫的方向開砲或縱向開砲,每次開砲可以.
: Finding Paths in Grid ★★★★☆ 題組: Contest Archive with Online Judge 題號: 11486: Finding Paths in Grid 解題者:李重儀 解題日期: 2008 年 10 月 14 日 題意:給一個 7 個 column.
著作權所有 © 旗標出版股份有限公司 第 14 章 製作信封、標籤. 本章提要 製作單一信封 製作單一郵寄標籤.
:Rings and Glue ★★☆☆☆ 題組: Problem Set Archive with Online Judge 題號: 10301: Rings and Glue 解題者:施博修 解題日期: 2011 年 5 月 18 日 題意:小約翰有了個大麻煩,他不小心將 rings.
幼兒行為觀察與記錄 第八章 事件取樣法.
: How many 0's? ★★★☆☆ 題組: Problem Set Archive with Online Judge 題號: 11038: How many 0’s? 解題者:楊鵬宇 解題日期: 2007 年 5 月 15 日 題意:寫下題目給的 m 與 n(m
McGraw-Hill/Irwin © 2003 The McGraw-Hill Companies, Inc.,All Rights Reserved. 肆 資料分析與表達.
计算机科学概述 Introduction to Computer Science 陆嘉恒 中国人民大学 信息学院
Invitation to Computer Science 6th Edition Chapter 3 The Efficiency of Algorithms.
Chapter 3: The Efficiency of Algorithms
Chapter 3: The Efficiency of Algorithms
Invitation to Computer Science 5th Edition
Presentation transcript:

Chapter 3 The Efficiency of Algorithms 國立雲林科技大學 資訊工程研究所 張傳育 (Chuan-Yu Chang ) 博士 Office: ES 709 TEL: ext

2 Properties of Algorithms  Necessary properties  A well-ordered collection of unambiguous and effectively computable operations that, when, executed, produces a result and halts in a finite amount of time.  What about desirable properties?  Correctness

3 Attributes of Algorithms  Correctness :  First, make it correct!  Correct results may not be as straightforward as it seems. May be giving correct results to the wrong problem  Second, the algorithm must give correct results for all possible input values  Third, Issue of accuracy of the result we are willing to accept as correct.  Attribute of an algorithm: --Ease of understanding. --Clarity --Ease of handling --Elegance (style) --Efficiency

4 Example: adding … +100 Solution 1:  Step 1: Set the value of sum to 0  Step 2: Set the value of x to 1  Step 3: While x is less than or equal to 100 do step 4 and 5  Step 4: Add x to sum  Step 5: Add 1 to the value of x  Step 6: Print the value of sum  Step 7: Stop Solution 2: Gauss’s Method  1+100=101, 2+99=101,…,50+51=101  50*101=5050 Solution 3: 梯形公式  (100/2)(100+1)=5050 聰明但不容易理解 不同的方法 (elegant)

5 Attributes of Algorithms (cont.)  一個演算法最好能同時具備容易理解 (ease of understanding) 及優雅的形式 (elegant) 。  The Limited Resources  電腦科學家必須留意演算法所使用的資源 (Time & Space ) 。  Time and Space are not unlimited resources  “ Efficiency ” is the term used to describe an algorithm’s careful use of resources.

6 Measure Efficient Algorithm  How can we measure the time efficiency of an algorithm?  Kinds of computer to be used ?  Data from ?  Data organization ?  Benchmark  Use the same input data and running an algorithm on different machines gives a comparison of machine speeds on identical tasks.

7 Formal Definition  Algorithm ’ s time efficiency  An indication of amount of “ work ” required by the nature of the algorithm itself and the approach it uses.  Count the number of instruction executions  It is the number of steps each algorithm requires, not the time the algorithm takes on a particular machine, that is important for comparing two algorithms that do the same task.

8 Data Cleanup Problem  We want to perform a “data cleanup” and remove the 0 entries from the list before the average is computed.    Legit: legitimate element The value 0 will be removed

9 A Choice of Algorithms  The shuffle- left algorithm (Figure 3.1)  左手保存目前位置 ,且忽略非零值,當遇到 0 值時,以右 手邊第一個非零值取代之,且 0 右邊的所有值均左移一位。  The copy-over algorithm (Figure 3.2)  將每個 legitimate 值 ,複製到一個新的串列。  The converging-pointers algorithm (Figure 3.3)  左手指到串列的最左端,右手指到串列的最右端,當左手遇到 非零值時,則往右移位;當遇到零值時,則以右手所指到的非 零值來取代,右手往左移一位。

10 Figure 3.1 The shuffle- left algorithm 一開始 legit 的值設定成串 列長度,當遇到一次 0 值, 則 legit 減 1 。 0 右邊的所有項都 被往左複製一次

11 Figure 3.2 The copy-over algorithm

12 Figure 3.3 The converging-pointers algorithm

13 Comparisons 可用 time- 和 space-efficient 來衡量?  Case by case

14 Measuring Efficiency  Sequential search (Figure 3.1) --Best Case : 1 --Worst Case : n --Average Case : n/2  Usually interested not in the behavior of an algorithm on little problems but in its behavior as the size of a problem (n) gets vary large.  In the New York City telephone directory, n may be large as If the sequential search algorithm were executed on a computer that could do comparisons per seconds, it would require on the average about ( /2)*(1/50000)=200 sec

15 Figure 3.4 Sequential Search Algorithm Peripheral task

16 Order of Magnitude  The worst case behavior of the sequential search algorithm on a list n names requires n comparisons. If c is a constant factor representing the peripheral work, it requires cxn total work.  對於大的 n ,常數項 c 可忽略。  Order of magnitude n is written Θ(n)  Sequential search is therefore an Θ(n) algorithm in both worst case and average case.

17 Work = 2n for Various Values of c (figure 3.6) 以 sequential search 為例:  n 筆資料,最多需要 n 次的比較。  假設其週邊工作為 c  則需要 cn 的工作量。  右圖為 c=2 時,資料筆數 n 和 總工作量之間的關係圖。

18 Work = cn for Various Values of c (figure 3.7)

19 Work = cn for Various Values of c (figure 3.8)

20 Calling Information To write the calling information table out. Pseudocode  For each of rows 1 through 4 do the following For each of columns 1 through 4 do the following Write the entry in this row and column  The number of write operations is 4x4=16  If there are n districts, the total works are nxn=n

21 A Comparison of n and n 2 (Figure 3.10)

22 A comparison of n and n 2 (figure 3.11)

23 Figure 3.12 For large enough n, 0.25n 2 has larger values than 10n

24 Figure 3.13 A Comparison of two extreme O(n 2 ) and O(n) Algorithm

25 The tortoise and the hare  Pentium Pro 200 具有 75 megaflops ,成本 $2000  Cray T3E 900 具有 670 gigaflops ,成本 $31 millions nPentium Pro O(n)Cray O(n 2 ) sec =2.3hr sec=9.7day

26 Analysis of Algorithms (data cleanup) Shuffle- leftCopy- overConverging pointer TimeSpaceTimeSpaceTimeSpace Best CaseO(n)n n n Worst Case O(n 2 )nO(n)2nO(n)n Average Case O(n 2 )nO(n) n ≦ x ≦ 2n O(n)n Analysis of three data clean up algorithm

27 Analysis of Algorithms (Cont.)  Sort  Selection Sort  best case: n 2, worst case: n 2, average case: n 2

28 Analysis of Algorithms (Cont.)  Selection Sort  Ex: Pass 0: Pass 1: Pass 2: Pass 3: Pass 4: 比較次數 n-1 n-2 : 1 比較總次數 =n(n-1)/2 比較總次數 =10

29 Exchange the value of X and Y 1. Copy the current value at position Y into position X 2. Copy the current value at position X into position Y Step 1 Step 2

30 Exchange the value of X and Y (cont.) 1. Copy the current value at position X into position T 2. Copy the value at position Y into position X 3. Copy the current value at position T into position Y Step 2 Step 3 Step 1

31 Binary Search  Binary Search  The list being searched is already sorted.  Search the midpoint of the list.  best case: 1, worst case: (log n), average case: (log n)

32 Binary Search Tree The basic shapes of n and log n log n grows much more slowly than n

33 Pattern Matching  Finding all occurrences of a pattern of the form P 1 P 2 …P m within text of the form T 1 T 2 …T n.  Forward match At each position beginning an attempt to match each pattern character against the text characters. The process stops only after text position n-m+1 (n the length of the text string, m the length of the pattern string) The best case: if the first character of the pattern is nowhere in the text. ( require n-m+1 comparisons) The worst case 1: if the pattern almost occurs everywhere in the text. (require nxm comparisons=> O(mxn)) The worst cases 2: the pattern is found at each location in the text. (require nxm comparisons => O(mxn))

34 When Things Get Out Of Hand  Polynomially bounded  Order of magnitude determines how quickly the values grow as n increases.  The work done by any of these algorithms is no worse than a constant multiple of n 2, which is polynomial in n.  Non-polynomially bounded  Is it possible to start at city A, go through every city exactly once, and end up at A?  A collection of nodes and connecting edges is called a graph

35 When Things Get Out Of Hand (cont.) Hamiltonian circuit  A path through a graph that begins and ends at the same node and goes through all other nodes exactly once.  If there are n node in the graph, then a Hamiltonian circuit, if it exists, must have exactly n links. AB CD

36 When Things Get Out Of Hand (cont.)  The number of paths that must be examined is the number of nodes at the bottom level of the tree.  The bottom of the corresponding tree would be at level n, and there would be 2 n paths to examine.  An O(2 n ) algorithm is called an exponential algorithm

37 When Things Get Out Of Hand (cont.)  Exponential algorithm  Comparisons of log n, n, n 2 and 2 n

38 When Things Get Out Of Hand (cont.)  Comparisons of four orders of magnitude  假設單一指令的執行時間為 sec ,則不同 order 所 需執行時間,如下表所示: Order log n0.0003s s s0.001 s n s0.01 s0.1 s n2n s0.25 s1 s1.67min 2n2n s3570 years4*10 16 centuries Too big to compute

39 Intractable problem  No polynomially bounded algorithm to solve.  They are solvable, but the solution algorithms all require so much work as to be virtually useless. bin-packing problem  Given an unlimited number of bins of volume 1 unit, and given n objects, all of volume between 0.0 and 1.0, find the minimal number of bins needs to store the n objects.  ( 只能找出 Approximation algorithm)