งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

งานนำเสนอกำลังจะดาวน์โหลด โปรดรอ

1 Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them? At the heart of computer program.

งานนำเสนอที่คล้ายกัน


งานนำเสนอเรื่อง: "1 Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them? At the heart of computer program."— ใบสำเนางานนำเสนอ:

1 1 Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them? At the heart of computer program design are two (sometimes conflicting) goals. 1.To design an algorithm that is easy to understand, code, debug. 2.To design an algorithm that makes efficient use of the computer’s resources.

2 2 Algorithm Efficiency (cont) Goal (1) is the concern of Software Engineering. Goal (2) is the concern of data structures and algorithm analysis. When goal (2) is important, how do we measure an algorithm’s cost?

3 3 How to Measure Efficiency? 1.Empirical comparison (run programs) 2.Asymptotic Algorithm Analysis Critical resources: Factors affecting running time: For most algorithms, running time depends on “size” of the input. Running time is expressed as T(n) for some function T on input size n.

4 4 Growth Rate The growth rate for an algorithm is the rate at which the cost (running time) of the algorithm grows as the size of its input grows.

5 5 Big O Notation หรือ อันดับขนาด (Order of Magnitude) – หมายถึงปริมาณที่เครื่องคอมพิวเตอร์ทำไม่ขึ้นกับ ขนาดของโปรแกรมหรือ จำนวนบรรทัดของโปรแกรม – เป็นฟังก์ชันที่ได้จากการประมาณค่าทาง คณิตศาสตร์ซึ่งเป็นฟังก์ชันที่สัมพันธ์กับขนาดของ ปัญหา ให้ข้อมูลในการเปรียบเทียบอัลกอลิทึม List[1] = 0; List[2] = 0; : List [1000] = 0; Algorithm 1 For(i=1;i<=n;i=i+1) List [i] = 0; Algorithm 2 Algorithm 1 และ Algorithm 2 มีอันดับ ขนาดคือ O(n)

6 6 Examples of Growth Rate (1) for (i=1; i<=1000; i=i+1) 1000 times >; for (i=1; i<=1000; i=i+2) 500 times >; for (i=1; i<1000; i=i*2) 10 times >; for (i=1000; i>=1; i=i/2) 10 times >;

7 7 Examples (2) for (j=1; j<=10; j=j+1) 10 times for (i=1; i<=10; i=i+1) 10 iterations >; 100 iterations for (j=1; j<=10; j=j+1) 10 iterations for (i=1; i<=10; i=i*2) log 2 10 iterations >; 10*log 2 10 iterations for (j=1; j<=10; j=j+1) 10 times for (i=1; i<=j; i=i+1) ( 10+1)/2 times >; 55 iterations

8 8 Example: Input Size = n for (i=1; i<=n; i=i+1) T(n) = n >; for (i=1; i<=n; i=i+2) T(n) = n/2 >; for (i=1; i; T(n) for (i=n; i>=1; i=i/2) >;

9 9 Examples (2) for (j=1; j<=n; j=j+1) for (i=1; i<=n; i=i+1) >; for (j=1; j<=n; j=j+1) for (i=1; i<=n; i=i*2) >; for (j=1; j<=n; j=j+1) for (i=1; i<=j; i=i+1) >;

10 10 Examples (3) // Find largest value int largest(int array[], int n) { int currlarge = 0; // Largest value seen for (int i=1; i

11 11 Growth Rate Graph

12 12 Best, Worst, Average Cases Sequential search for K in an array of n integers: Begin at first element in array and look at each element in turn until K is found Best case: Worst case: Average case:

13 13 Which Analysis to Use? While average time appears to be the fairest measure, it may be difficult to determine. When is the worst case time important?

14 14 Big-O Notation Big-oh notation represents the growth rate. It's useful to be able to estimate the cpu or memory resources an algorithm requires. Example: If T(n) = 3n 2 then T(n) is in O(n 2 ). Wish tightest upper bound: While T(n) = 3n 2 is in O(n 3 ), we prefer O(n 2 ).

15 15 Big-Oh Examples Example 1: Finding value X in an array T(n) is in O(n). Example 2: T(n) = c 1 n 2 + c 2 n T(n) is in O(n 2 ). Example 3: T(n) = c. We say this is in O(1).

16 16 Running Time Examples (1) Example 1: O(1) a = b; This assignment takes constant time, so it is O(1). Example 2: O(n) sum = 0; for (i=1; i<=n; i++) O(n) sum += n;

17 17 Running Time Examples (2) Example 3: O(n 2 ) sum = 0; O(1) for (j=1; j<=n; j++) O(n 2 ) for (i=1; i<=j; i++) sum++; for (k=0; k

18 18 Running Time Examples (3) Example 4: O(n 2 ) sum1 = 0; O(1) for (i=1; i<=n; i++) O(n 2 ) for (j=1; j<=n; j++) sum1++; sum2 = 0; O(1) for (i=1; i<=n; i++) O(n 2 ) for (j=1; j<=i; j++) sum2++; sum is n 2. sum is (n+1)(n)/2.

19 19 Running Time Examples (4) Example 5: O(nlogn) sum1 = 0; for (k=1; k<=n; k*=2) O(logn) for (j=1; j<=n; j++) O(n) sum1++; sum2 = 0; for (k=1; k<=n; k*=2) O(n) for (j=1; j<=k; j++) sum2++; Second loop is  2 k for k = 0 to log n. First loop is  n for k = 1 to log n.

20 20 Algorithm Efficiency Constant O(1) การข้าถึงสมาชิกตัวที่ i ใน แถวลำดับ Logarithmic O(logn) การค้นหาแบบ Binary Search LinearO(n) การค้นหาแบบ Sequential Search Linear logarithmicO(n(logn)) การจัดเรียงแบบ Merge Sort Quadratic O(n 2 ) การจัดเรียงแบบธรรมดา

21 21 Algorithm Efficiency Assumes instruction speed of one microsecond and 10 instructions in loop. EfficiencyBig-OIterationsEst. Time Logarithmic Linear O(logn) O(n) 14 10,000 Microseconds.1 seconds Linear logarithmic Quadratic O(n(logn) O(n 2 ) 140,000 10, seconds min. Polynomial O(n k )10,000 k Hours Exponential O(c n )2 10,000 Intractable Factorial O(n!)10,000!Intractable

22 22 Faster Computer or Algorithm? What happens when we buy a computer 10 times faster? T(n)T(n)nn’n’Changen’/n 10n1,00010,000n’ = 10n10 20n5005,000n’ = 10n10 5n log n2501,842  10 n < n’ < 10n n22n n’ =  10n n2n 1316n’ = n Old machine can run 10,000 basic operations/hour. New machine can run 100,000 basic operations/hour.


ดาวน์โหลด ppt 1 Algorithm Efficiency There are often many approaches (algorithms) to solve a problem. How do we choose between them? At the heart of computer program.

งานนำเสนอที่คล้ายกัน


Ads by Google