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

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

Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th CSE 221/ICT221 การวิเคราะห์และออกแบบขั้นตอนวิธี Lecture 04: การวิเคราะห์หาความซับซ้อนด้านเวลา ในรูป.

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


งานนำเสนอเรื่อง: "Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th CSE 221/ICT221 การวิเคราะห์และออกแบบขั้นตอนวิธี Lecture 04: การวิเคราะห์หาความซับซ้อนด้านเวลา ในรูป."— ใบสำเนางานนำเสนอ:

1 Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th
CSE 221/ICT221 การวิเคราะห์และออกแบบขั้นตอนวิธี Lecture 04: การวิเคราะห์หาความซับซ้อนด้านเวลา ในรูป แบบของ Big-Oh Dr.Surasak Mungsing Feb-19

2 ปัจจุบันถูกใช้ในการจำแนกฟังก์ชันและกลุ่มของฟังก์ชัน
Big-Oh สัญกรณ์ O ถูกแนะนำมาใช้ในการบ่งบอกอัตราการเติบโตเปรียบเทียบระหว่างฟังก์ชันในปี ค.ศ โดยพิจารณาจากพฤติกรรมเชิงเส้นกำกับที่เรียกว่า Asymptotic behavior ปัจจุบันถูกใช้ในการจำแนกฟังก์ชันและกลุ่มของฟังก์ชัน 24-Feb-19

3 Upper Bound Notation เวลาที่ใช้ในการเรียงลำดับแบบ Insertion Sort มีความซับซ้อนอญุ่ในลำดับ O(n2) อ่าน O ว่า “Big-O” นิยาม f(n) is O(g(n)) if  positive constants c and n0 such that f(n)  c  g(n)  n  n0 ตัวอย่าง เช่น if f(n)=1000n และ g(n)=n2, n0 > 1000 และ c = 1 then f(n0) < 1.g(n0) เราสามารถกล่าวได้ว่า f(n) = O(g(n)) The O notation indicates 'bounded above by a constant multiple of.' 24-Feb-19

4 Big-Oh, the Asymptotic Upper Bound
Big-Oh เป็นสัญกรณ์ที่นิยมใช้กันมากที่สุดในการวิเคราะห์เวลาที่ใช้ในการทำงานของขั้นตอนวิธี เนื่องจากเรามักจะพิจารณาจากเวลาที่ใช้มากที่สุดในการแก้ปัญหา(worst case time) ถ้าเวลาที่ใช้ในการทำงานของขั้นตอนวิธี เป็น O(n2) หมายความว่า เวลาในการทำงานของขั้นตอนวิธี จะไม่เกิน quadratic function สำหรับ input จำนวน n ที่ใหญ่พอ upper bound บางตัวอาจจะดูกว้างเกินไป แต่ก็ยังนับว่าเป็น upper bound ได้ เช่น 2n2 = O(n3) . จากนิยาม ถ้าใช้ค่า c = 1 and n0 = 2 การกล่าวว่า O(n2) จะกระชับกว่า 24-Feb-19

5 สำหรับทุกค่า n>6, f(n) < 1 g(n);
Example 1 สำหรับทุกค่า n>6, f(n) < 1 g(n); f(n) is in O(g(n)) 24-Feb-19

6 สำหรับทุกค่า n>n0 เมื่อ n0= 5 (กรณีนี้ c=1)
Example 2 f(n) is in O(g(n)) ถ้า f(n) < 1 g(n) สำหรับทุกค่า n>n0 เมื่อ n0= 5 (กรณีนี้ c=1) 24-Feb-19

7 ดังนั้น f(n) is in O(h(n)).
Example 3 n0=5, c=3.5, สำหรับทุกค่า n>n0, ถ้า f(n) < c h(n) ดังนั้น f(n) is in O(h(n)). 24-Feb-19

8 24-Feb-19

9 Exercise on O-notation
Show that f(n)=3n2+2n+5 is in O(n2) 10 n2 = 3n2 + 2n2 + 5n2  3n2 + 2n for n  1  f(n) หรือ f(n) ≤10 n2 เมื่อพิจารณา c = 10, n0 = 1 f(n) ≤c g(n) for n  n0 ดังนั้น f(n) is in O(n2) 24-Feb-19

10 เรามักจะใช้สมการอย่างง่ายที่สุดเมื่อเขียนด้วยสัญลักษณ์ Big-Oh
Usage เรามักจะใช้สมการอย่างง่ายที่สุดเมื่อเขียนด้วยสัญลักษณ์ Big-Oh เช่น 3n2+2n+5 = O(n2) สมการเหล่านี้ก็ไม่ผิด แต่เราไม่นิยมเขียนกัน 3n2+2n+5 = O(3n2+2n+5) 3n2+2n+5 = O(n2+n) 3n2+2n+5 = O(3n2) 24-Feb-19

11 Exercise on O-notation
O(n log n) f1(n) = 10 n + 25 n2 f2(n) = 20 n log n + 5 n f3(n) = 12 n log n n2 f4(n) = n1/2 + 3 n log n For each function, find the simplest and g(n) such that f_i(n) = O(g(n)) 24-Feb-19

12 Classification of Function : BIG-Oh
A function f(n) is said to be of at most logarithmic growth if f(n) = O(log n) A function f(n) is said to be of at most quadratic growth if f(n) = O(n2) A function f(n) is said to be of at most polynomial growth if f(n) = O(nk), for some natural number k > 1 A function f(n) is said to be of at most exponential growth if there is a constant c, such that f(n) = O(cn), and c > 1 A function f(n) is said to be of at most factorial growth if f(n) = O(n!). 24-Feb-19

13 Classification of Function : BIG-Oh (cont.)
A function f(n) is said to have constant running time if the size of the input n has no effect on the running time of the algorithm (e.g., assignment of a value to a variable). The equation for this algorithm is f(n) = c Other logarithmic classifications: f(n) = O(n log n) f(n) = O(log log n) 24-Feb-19

14 A polynomial of degree k is O(nk)
Big O Fact A polynomial of degree k is O(nk) Proof: ถ้า f(n) = bknk + bk-1nk-1 + … + b1n + b0 และให้ ai = | bi | ดังนั้น f(n)  aknk + ak-1nk-1 + … + a1n + a0 24-Feb-19

15 Some Rules Transitivity
f(n) = O(g(n)) and g(n) = O(h(n))  f(n) = O(h(n)) Addition f(n) + g(n) = O(max { f(n) ,g(n)}) Polynomials a0 + a1n + … + adnd = O(nd) Heirachy of functions n + log n = O(n); 2n + n3 = O(2n) 24-Feb-19

16 Some Rules Base of Logs ignored logan = O(logbn)
Power inside logs ignored log(n2) = O(log n) Base and powers in exponents not ignored 3n is not O(2n) a(n)2 is not O(an) 24-Feb-19

17 Big-Oh Complexity O(1) The cost of applying the algorithm can be bounded independently of the value of n. This is called constant complexity. O(log n) The cost of applying the algorithm to problems of sufficiently large size n can be bounded by a function of the form k log n, where k is a fixed constant. This is called logarithmic complexity. O(n) linear complexity O(n log n) n lg n complexity O(n2) quadratic complexity 24-Feb-19

18 Big-Oh Complexity (cont.)
O(n3) cubic complexity O(n4) quartic complexity O(n32) polynomial complexity O(cn) If constant c 1, then this is called exponential complexity O(2n) exponential complexity O(en) exponential complexity O(n!) factorial complexity 24-Feb-19

19 Practical Complexity t < 500
24-Feb-19

20 Practical Complexity t < 5000
24-Feb-19

21 Practical Complexity 24-Feb-19

22 Things to Remember in Analysis
ค่าคงที่ (Constants) หรือเทอมที่อยู่ในลำดับต่ำ (low-order terms) จะถูกละทิ้ง เช่น ถ้า f(n) = 2n2 ดังนั้น f(n) = O(n2) ทรัพยากรที่สำคัญที่สุดที่วิเคราะห์คือ running time; ปัจจัยอื่นที่สำคัญรองลงมาได้แก่ algorithm used และ input to the algorithm ค่า Parameter N, โดยปกติหมายถึงจำนวนข้อมูลที่ใช้ในการประมวลผลซึ่งมีผลกระทบมากที่สุดต่อระยะเวลาที่ใช้ในการทำงานของอัลกอริธึม N อาจหมายถึง degree of polynomial, ขนาดของไฟล์ข้อมูลที่ใช้ในการเรียงลำดับหรือการค้นหา, จำนวนของโหนดในกราฟ เป็นต้น 24-Feb-19

23 Things to Remember in Analysis
กรณี Worst case หมายถึงระยะเวลาการทำงานของอัลกอริธึมในกรณีที่ข้อมูลนำเข้ามีการเรียงตัวกันในรูปแบบที่ต้องใช้เวลาในการทำงานมากที่สุด กรณี Average case หมายถึงระยะเวลาการทำงานของอัลกอริธึมที่คาดว่าจะใช้สำหรับประมวลผลข้อมูลนำเข้าโดยปกติทั่วๆไป (typical input data) 24-Feb-19

24 General Rules for Analysis (1)
1. ประมวลผลกลุ่มคำสั่งตามลำดับ (Consecutive statements) นับเวลาของกลุ่มคำสั่งที่ใช้เวลานานที่สุด เช่น ส่วนของคำสั่งที่เป็น loop เดียว ทำงานมากที่สุด n ครั้ง ความซับซ้อนของขั้นตอนวิธีจะอยู่ในลำดับ O(n) ถ้าส่วนของคำสั่งมีการทำงานเป็นสอง loop ซ้อนกัน แต่ละ loop ทำงานมากที่สุด n ครั้ง ความซับซ้อนของขั้นตอนวิธีจะอยู่ในลำดับ O(n2). Block #1 t1 t2 Block #2 t1+ t2 = max(t1,t2) 24-Feb-19

25 General Rules for Analysis(2)
2. คำสั่งประเภท If/Else if cond then S1 else S2 Block #1 t1 Block #2 t2 Max(t1,t2) 24-Feb-19

26 General Rules for Analysis(3)
3. For Loops Running time of a for-loop is at most the running time of the statements inside the for-loop times number of iterations for (i = sum = 0; i < n; i++) sum += a[i]; for loop iterates n times, executes 2 assignment statements each iteration ==> asymptotic complexity of O(n) 24-Feb-19

27 General Rules for Analysis(4)
4. Nested For-Loops ทำการวิเคราะห์จากข้างในออกมา (Analyze inside-out) เวลาที่ใช้ทั้งหมดคือเวลาที่ใช้ในการประมวลผลคำสั่งคูณด้วยผลคูณของขนาดของ loop ทั้งหมด เช่น for (i =0; i < n; i++) for (j = 0, sum = a[0]; j <= i ; j++) sum += a[j]; printf("sum for subarray - through %d is %d\n", i, sum); 24-Feb-19

28 General Rules for Analysis
24-Feb-19

29 General Rules for Analysis
กลยุธในการวิเคราะห์ วิเคราะห์จากข้างในออกมา (analyze from inside out) วิเคราะห์การเรียกใช้ฟังก์ชันก่อน (analyze function calls first) 24-Feb-19

30 การวิเคราะห์ความซับซ้อนของขั้นตอนวิธีการเรียงลำดับข้อมูล
Next Lecture: การวิเคราะห์ความซับซ้อนของขั้นตอนวิธีการเรียงลำดับข้อมูล 24-Feb-19


ดาวน์โหลด ppt Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th CSE 221/ICT221 การวิเคราะห์และออกแบบขั้นตอนวิธี Lecture 04: การวิเคราะห์หาความซับซ้อนด้านเวลา ในรูป.

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


Ads by Google