1 / 108

บทที่ 2 การวิเคราะห์อัลกอริทึม

บทที่ 2 การวิเคราะห์อัลกอริทึม. หัวข้อ. การวิเคราะห์อัลกอริทึม แบบทดลอง และ แบบคณิตวิเคราะห์ การนับจำนวนการทำงานของคำสั่งพื้นฐาน อัตราการเติบโตของฟังก์ชัน สัญกรณ์ เชิงเส้นกำกับ การวิเคราะห์การทำงาน แบบลำดับ, แบบเลือกทำ, แบบวงวน, แบบเรียกซ้ำ. ขั้นตอนการออกแบบอัลกอริทึม.

Télécharger la présentation

บทที่ 2 การวิเคราะห์อัลกอริทึม

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. บทที่ 2การวิเคราะห์อัลกอริทึม

  2. หัวข้อ • การวิเคราะห์อัลกอริทึม • แบบทดลอง และแบบคณิตวิเคราะห์ • การนับจำนวนการทำงานของคำสั่งพื้นฐาน • อัตราการเติบโตของฟังก์ชัน • สัญกรณ์เชิงเส้นกำกับ • การวิเคราะห์การทำงาน • แบบลำดับ, แบบเลือกทำ, แบบวงวน, แบบเรียกซ้ำ

  3. ขั้นตอนการออกแบบอัลกอริทึมขั้นตอนการออกแบบอัลกอริทึม

  4. จุดประสงค์ของการวิเคราะห์อัลกอริทึมจุดประสงค์ของการวิเคราะห์อัลกอริทึม • เพื่อศึกษาประสิทธิภาพของอัลกอริทึม • เวลาการทำงาน • ปริมาณหน่วยความจำที่ใช้ในการทำงาน

  5. ประเภทการวิเคราะห์อัลกอริทึมประเภทการวิเคราะห์อัลกอริทึม • Mathematical Analysis • Experimental Analysis • แปลงเป็นโค้ด • สั่งทำงานกับข้อมูลทดสอบ • จับเวลาการทำงาน • บันทึกผล • วิเคราะห์ความสัมพันธ์ระหว่างเวลาการทำงานกับปริมาณข้อมูล มักเป็นรูปแบบฟังก์ชันทางคณิตศาสตร์

  6. SelectionSort selectionSort(d[1..n]){ for (k = n; k > 1; k--) { maxI=1 for (i = 2; i <= k; i++) if (d[i] > d[maxI]) maxI = i d[k]d[maxI] } }

  7. Selection Sort (ภาษา C) void selectionSort(int d[], int n) { intk, i; for (k = n - 1; k > 0; k--) { intmaxI=1; for (i = 0; i <= k; i++) if (d[i]> d[maxI]) maxI= i; intt = d[k]; d[k] = d[maxI]; d[maxI] = t; } }

  8. #include <stdio.h> #include <stdlib.h> #include <windows.h> int main(intargc, char *argv[]) { inti, k, n, repeat = 10; for( n = 0; n<=20000; n+=2000) { int*d = malloc( n * sizeof(int) ); long sum = 0; for (k=0; k<repeat; k++) { for (i=0; i<n; i++) d[i] = i; long start = GetTickCount(); selectionSort(d, n); sum += GetTickCount() - start; } printf("%d \t %f \n", n, (float)sum/repeat); } system("PAUSE"); return 0; }

  9. Selection Sort (ภาษา Java)

  10. public class TestSelectionSort { public static void main(String[] args) { intrepeat = 10; for (int n = 0; n <= 20000; n += 2000) { int[]d = new int[n]; long sum = 0; for (int k = 0; k < repeat; k++) { for (inti = 0; i <n;i++) d[i] = i; { long start = System.currentTimeMillis(); selectionSort(d); sum += System currentTimeMillis() - start; } System.out.printf("%d \t %f \n", (double)sum/repeat); } }

  11. public class TestSelectionSort { public static void main(String[] args) { intrepeat = 10; for (int n = 0; n <= 20000; n += 2000) { int[]d = new int[n]; long sum = 0; for (int k = 0; k < repeat; k++) { for (inti = 0; i <n;i++) d[i] = i; { long start = System.currentTimeMillis(); selectionSort(d); sum += System currentTimeMillis() - start; } System.out.printf("%d \t %f \n", (double)sum/repeat); } } for (inti = 0; i < 10000; i++) selectionSort(new int[1]);

  12. สั่งทำงาน พร้อมบันทึกผล • ภาษา C : ใช้ GNU-C Compiler (3.4.2) • ภาษา Java : ใช้ Java 6 (build 1.6.0_14-b08) • Interpreted-only mode (option –Xintตอนทำงาน) • Compilation to native code (บังคับ compile ด้วยการเรียกเมท็อดว้ำ ๆ สัก 10 000 ครั้ง) • Intel Core 2 DuoP8400 2.26GhzRam 3GBWindows XP

  13. เปรียบเทียบเวลาการทำงานเปรียบเทียบเวลาการทำงาน

  14. ใช้จำนวนการทำงานของคำสั่งแทนเวลาใช้จำนวนการทำงานของคำสั่งแทนเวลา static intcounter= 0; static void selectionSort(int[] d) { counter = 1+d.length +d.length-1; for (int k = d.length-1; k > 0; k--) { intmaxI=1; counter += 2 + k+2 + k+1; for (inti = 0; i <= k; i++) { counter += 1; if (d[i] > d[maxI]) { maxI=i; counter+=1; } } counter += 3; intt = d[k]; d[k] = d[maxI]; d[maxI] = t; } }

  15. เขียนโปรแกรมทดลองนับคำสั่งเขียนโปรแกรมทดลองนับคำสั่ง public class TestSelectionSort { public static void main(String[] args) { intrepeat = 10; for (int n = 0; n <= 20000; n += 2000) { int[]d = new int[n]; long sum = 0; for (int k = 0; k < repeat; k++) { for (inti = 0; i <n;i++) d[i] = i;; counter = 0; selectionSort(d); sum += counter; } System.out.printf("%d \t %f \n", n, (double)sum/repeat); } }

  16. นับจำนวนการทำงานของคำสั่งนับจำนวนการทำงานของคำสั่ง

  17. นับเฉพาะคำสั่งตัวแทน static intcounter= 0; static voidselectionSort(int[] d) { for (int k = d.length-1; k > 0; k--) { intmaxI=1; for (inti = 0; i <= k; i++) { counter += 1; if (d[i] > d[maxI]) { maxI= i; } } int t d[k]; d[k] = d[maxI]; d[maxI]=t; } }

  18. นับทุกคำสั่ง VS. นับคำสั่งตัวแทน

  19. Mathematical Analysis • ไม่ต้องเขียนเป็นโปรแกรม ไม่ต้องสั่งทำงานจริง ไม่ต้องวาดกราฟ วิเคราะห์จากอัลกอริทึม จะได้เป็นฟังก์ชั่น 1

  20. นับทุกคำสั่ง VS. นับคำสั่งตัวแทน

  21. คำสั่งตัวแทนต้องเป็นคำสั่งพื้นฐานคำสั่งตัวแทนต้องเป็นคำสั่งพื้นฐาน • คำสั่งพื้นฐาน คือคำสั่งที่ • ใช้เวลาการทำงานไม่เกินค่าคงที่ค่าหนึ่ง • ใช้เวลาการทำงานไม่แปรตามขนาดของ input • เช่น + - * / if เปรียบเทียบ return break …

  22. การวิเคราะห์อัลกอริทึมการวิเคราะห์อัลกอริทึม • เวลาการทำงาน แปรตามจำนวนการทำงานของคำสั่ง • จำนวนการทำงานของคำสั่ง แปรตาม จำนวนการทำงานของคำสั่งตัวแทน • เพื่อความง่าย เราวิเคราะห์อัลกอริทึมเชิงเวลาด้วยการหาความสัมพันธ์ของ • จำนวนการทำงานของคำสั่งตัวแทน • ปริมาณข้อมูล

  23. วัดปริมาณข้อมูลขาเข้าวัดปริมาณข้อมูลขาเข้า • พิจารณาว่า ขนาดของ input แปรตามค่าอะไรของ input • ตัวอย่าง: sort(d[1…n]) • Input เป็นอาเรย์จำนวน n ช่อง • แต่ละจำนวนมีค่าจำกัด • ดังนั้น n แทนปริมาณข้อมูล • ตัวอย่าง:shortestPath( V, E, w, s ) • Input เป็นกราฟถ่วงน้ำหนัก • ขนาดของกราฟ แปรตามจำนวนปมและจำนวนเส้นเชื่อม • ดั้งนั้น |V|+|E| แทนปริมาณของข้อมูล

  24. เปรียบเทียบผลการวิเคราะห์เปรียบเทียบผลการวิเคราะห์

  25. เปรียบเทียบเวลาการทำงานเปรียบเทียบเวลาการทำงาน

  26. เปรียบเทียบอัตราการเติบโตเปรียบเทียบอัตราการเติบโต

  27. อัตราการเติบโต

  28. ทบทวนสูตรคณิตศาสตร์

  29. อัตราการเติบโตของฟังก์ชันอัตราการเติบโตของฟังก์ชัน

  30. f(n) vs. g(n) • ใครโตเร็วกว่ากัน ให้ดูที่พฤติกรรมตอนที่ค่า n เยอะๆ ว่าใครนำหน้าใคร คือ ค่าคงตัวที่ไม่ใช่ 0

  31. ตัวอย่าง = = = สรุปได้ว่า

  32. = = = = สรุปได้ว่าโตเท่ากับ

  33. L'Hôpital's rule • ถ้า และ เป็นฟังก์ชันที่หาอนุพันธ์ได้ โดยที่ , • และหาค่าของ • จะได้ว่า

  34. เรียงลำดับฟังก์ชันตามการเติบโตเรียงลำดับฟังก์ชันตามการเติบโต

  35. สัญกรณ์เชิงเส้นกำกับ

  36. สัญกรณ์เชิงเส้นกำกับ (Asymptotic Notations)

  37. little-o

More Related