1 / 92

9 . การออกแบบขั้นตอนวิธี

9 . การออกแบบขั้นตอนวิธี. Algorithm Design Algorithm Design Techniques Practice Problems. ลักษณะของขั้นตอนวิธีที่ดี. แก้ปัญหาได้อย่างมีประสิทธิภาพ ให้คำตอบที่ถูกต้อง ใช้เวลาในการปฏิบัติการน้อย ชัดเจนและกะทัดรัด. Algorithm Design. definition requirement strategy techniques.

Télécharger la présentation

9 . การออกแบบขั้นตอนวิธี

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. 9. การออกแบบขั้นตอนวิธี Algorithm Design Algorithm Design Techniques Practice Problems 344-211 Algorithmic Process and Programming , created by Dararat Saelee

  2. ลักษณะของขั้นตอนวิธีที่ดีลักษณะของขั้นตอนวิธีที่ดี • แก้ปัญหาได้อย่างมีประสิทธิภาพ • ให้คำตอบที่ถูกต้อง • ใช้เวลาในการปฏิบัติการน้อย • ชัดเจนและกะทัดรัด

  3. Algorithm Design • definition • requirement • strategy • techniques

  4. Algorithm Design Definition • Algorithm Definition : a method to solve a problem described as a sequence of steps to be performed in a specific logical order • Several algorithms for solving the same problem may exist , based on very different ideas , performance (efficiency , cost and speed) Algorithm หมายถึง กรรมวิธีที่มีขั้นตอนแบบทีละขั้นอย่างชัดเจนในการแก้ปัญหาใดปัญหาหนึ่ง เพื่อให้ได้คำตอบที่ถูกต้องสำหรับทุกรูปแบบข้อมูล ภายในเวลาและทรัพยากรณ์ที่จำกัด

  5. Algorithm Design Requirement Satisfied Requirements : • unambiguousness • generality • correctness • finiteness

  6. Satisfied Requirements unambiguousness • easier to understand and to program , so contain fewer bugs. • sometimes simpler algorithm are more efficient than more complicated algorithm.

  7. Satisfied Requirements generality • easier to design an algorithm in more general terms. • handle a range of input that is natural for the problem.

  8. Satisfied Requirements correctness must be proved by 1) Mathematical Induction. 2) Testing with sample input data that possibly prove the algorithm failure or give wrong answer.

  9. Satisfied Requirements finiteness–must concern about 1) execution in finite steps. 2) termination in finite time.

  10. Algorithm Design Strategy • Computational Device • Solving Decision • Data Structure • Efficiency

  11. Algorithm DesignStrategy Computational Device • Von Neumann • sequential algorithm • Multiprocessor • parallel algorithm • network algorithm • distributed algorithm

  12. Algorithm DesignStrategy Solving Decision • choose between solving the problem with approximation or exact algorithm • approximation algorithm - square roots, integrals - shortest path

  13. Algorithm DesignStrategy Solving Decision (cont.) • choose to solve the problem with non-recursive or recursive algorithm • recursion is easy to program, but uses a large number of function calls that affect to execution efficiency.

  14. Algorithm DesignStrategy Data Structure • to solve problems easier, we need to use appropriate data structure. - student id : int or string ? - matrix 10x5 : array 2D or 50 var.? - graph, tree : array or linked list ?

  15. Algorithm DesignStrategy Efficiency • time : how fast the algor. runs • space : how much extra memory the algor. needs • worst / best / average case - sequential search : n / 1 / n/2

  16. Algorithm DesignStrategy Efficiency (cont.) • Order of Growth for Input size - when input size is large, how is the run time ? - order of growth : O (big oh) - input size : n

  17. Algorithm DesignStrategy Efficiency (cont.) - O(n2) : n = 10  running time = 100 n = 100  running time = 10,000 - O(2n) : n = 10  running time = 1,024 - O(log2n) : n = 10  running time = 3.3 n = 100  running time = 6.6

  18. Algorithm Design Techniques • To provide guidance for designing algorithms for new problems • To make it possible to classify algorithms according to design idea

  19. Algorithm Design Techniques • No any general technique can solve all problems • e.g. Unsorted data cannot use with Binary search algorithm

  20. Algorithm DesignTechniques An algorithm design technique (or strategy or paradigm) is a general approach to solving problems algorithmically that is applicable to a variety of problems from different areas of computing.

  21. Algorithm Design Techniques • Greedy Method • Divide and Conquer • Decrease and Conquer / Prune-and-Search • Transform and Conquer

  22. Algorithm Design Techniques • Dynamic Programming • Randomized Algorithms • Backtracking Algorithms

  23. Motto Today เพราะแสวงหา มิใช่เพราะรอคอย เพราะเชี่ยวชาญ มิใช่เพราะโอกาส เพราะสามารถ มิใช่เพราะโชคช่วย 344-211 Algorithmic Process and Programming , created by Dararat Saelee

  24. Greedy Method • “take what you can get now” • make a decision that appears to be good (close to optimal solution) • proceed by searching a sequence of choices iteratively to decide the (seem to be) best solution Greedy Algorithms หมายถึง เป็นอัลกอริทึมที่จะหาคาตอบโดยการเลือกทางออกที่ดีที่สุดที่พบได้ในขณะนั้นเพื่อให้ได้คาตอบที่ดีที่สุด แต่ในบางครั้ง Greedy Algorithms อาจจะไม่สามารถหาคาตอบของปัญหาที่ดีที่สุดได้เสมอไป

  25. Greedy Method ตัวอย่างวิธีคิดแบบ Greedy • Coin Changing • Fractional Knapsack • Bin Packing • Task Scheduling

  26. Greedy Method ตัวอย่างวิธีคิดแบบ Greedy • Prim’s • Kruskal’s • Dijkstra’s • Huffman Code graph tree

  27. Greedy Method Coin Changing สมมุติว่าเรามีเหรียญขนาดดังต่อไปนี้เหรียญ 10 บาท, เหรียญ 5 บาท, และเหรียญ 1 บาทและสมมุติว่าเราต้องการแลกเงิน 89 บาท เราจะได้เงินเหรียญดังนี้ 10 บาท 8 เหรียญ, 5 บาท 1เหรียญ, 1 บาท 4 เหรียญจะเห็นว่าอัลกอริทึมที่เราใช้ก็คือ เราเลือกเหรียญที่มีค่ามากที่สุด แต่ไม่มากกว่า 89 บาท ออกมาก่อน (เหรียญ 10 บาท 8 เหรียญ ) จากนั้นลบค่านี้ออกจาก 89 บาท ก็จะเหลือ 9 บาท หลังจากนั้นเราเลือกเหรียญที่มีค่ามากที่สุดแต่ไม่เกิน 9 บาท นั่นก็คือได้(เหรียญ 5 บาท 1 เหรียญ) แล้วลบค่านี้ออกจาก 9 บาท จะเหลืออยู่อีก 4 บาท และในที่สุดเราก็จะได้(เหรียญ 1 บาทอีก 4 เหรียญ)

  28. Greedy Method Fractional Knapsack • มีสิ่งของ n ประเภทซึ่งแต่ละประเภท(i) กำหนดให้มีจำนวน xi ชิ้น มีค่าความสำคัญ bi และมีน้ำหนัก wi • ต้องการหาจำนวนสิ่งของแต่ละประเภทที่บรรจุลงในเป้ที่รับน้ำหนักได้ไม่เกิน W กิโลกรัม • ให้เลือกหยิบสิ่งของทีละชิ้นที่มีค่าดัชนี (vi=bi/wi) สูงสุดและทำให้น้ำหนักรวมไม่เกิน W ก่อน

  29. Greedy Method • ตัวอย่างเช่น มีของ 4 ประเภทคือ • หนังสือ 4 เล่ม มี b1=10และ w1=0.6 (v1=16.7) • ขนม 2 กล่อง มี b2=7และ w2=0.4 (v2=17.5) • น้ำ 2 ขวด มี b3=5และ w3=0.5(v3=10) • ซีดีเพลง 8แผ่นมี b4=3และ w4=0.2 (v4=15) • เป้รับน้ำหนักได้ไม่เกิน 4 กิโลกรัม • เลือก ขนม2 กล่อง หนังสือ 4 เล่ม และซีดีเพลง 4 แผ่น • ได้ค่าความสำคัญสูงสุดและน้ำหนักไม่เกิน 4 กิโลกรัม

  30. Greedy Method • Bin Packing • given N items of sizes s1 , s2 , …, sN; • while 0 < si< 1 • find a solution to pack these items in the fewest number of bins • 2 algor. versions : • on-line : an item must be placed in a bin before the next item is read • off-line : all item list are read in a bin

  31. Optimal Bin Packing solution given an item list with sizes : 0.2 , 0.5 , 0.4 , 0.7 , 0.1 , 0.3 , 0.8 0.8 0.3 0.5 0.7 0.1 0.4 0.2 Bin 1Bin 2Bin 3 344-211 Algorithmic Process and Programming , created by Dararat Saelee

  32. Bin Packing strategy Next fit : fill items in a bin until the next item can’t fit , then insert a new bin (never look back) [0.2 , 0.5 , 0.4 , 0.7 , 0.1 , 0.3 , 0.8] empty empty empty empty empty 0.1 0.8 0.5 0.7 0.4 0.3 0.2 Bin 1Bin 2Bin 3 Bin 4Bin 5 344-211 Algorithmic Process and Programming , created by Dararat Saelee

  33. Bin Packing strategy First fit : fill items in a bin , but if any first previous bin can fit the next item then we can fill in until no any bin can fit , then insert a new bin[0.2 , 0.5 , 0.4 , 0.7 , 0.1 , 0.3 , 0.8] empty empty empty empty 0.1 0.8 0.3 0.5 0.7 0.4 0.2 Bin 1Bin 2Bin 3 Bin 4 344-211 Algorithmic Process and Programming , created by Dararat Saelee

  34. Bin Packing strategy Best fit : fill items in a bin by trying to place the new item in the bin that left the smallest space [0.2 , 0.5 , 0.4 , 0.7 , 0.1 , 0.3 , 0.8] empty empty 0.3 empty 0.1 0.8 0.5 0.7 0.4 0.2 Bin 1Bin 2Bin 3 Bin 4 344-211 Algorithmic Process and Programming , created by Dararat Saelee

  35. Class Exercise • ร้านขนมเค้กแห่งหนึ่งรับทำขนมเค้กตามคำสั่งของลูกค้า โดยการสั่งขนมเค้กอาจเป็น 1 ก้อน ½ ก้อน ¼ ก้อนและขนมเค้กที่ลูกค้าได้รับต้องเป็นขนาดก้อนที่ไม่มีรอยแบ่งภายในก้อน ทั้งนี้ในการทำขนมเค้ก ทางร้านไม่ต้องการให้เหลือขนมเค้กมากจึงต้องคำนึงถึงจำนวนก้อนขนมเค้กที่น้อยที่สุดที่ต้องทำ • ตัวอย่าง ถ้าหากลูกค้า 5 คน มีคำสั่ง 1 ½¼½¼ ดังนั้นทางร้านต้องทำเค้ก 3 ก้อน • จงเขียนโปรแกรมเพื่ออ่านข้อมูลคำสั่งของลูกค้าจากแฟ้มและหาว่าต้องทำเค้กอย่างน้อยที่สุดกี่ก้อน 344-211 Algorithmic Process and Programming , created by Dararat Saelee

  36. Greedy Method Task Scheduling • กำหนดงาน nงานด้วย(เวลาเริ่ม,เวลาเสร็จ)โดยให้ทำงานทีละงานบนเครื่องและจะเอางานใดมาทำบนเครื่องเดียวกันได้ถ้าเวลางานไม่ทับซ้อนกันทั้งนี้ต้องใช้จำนวนเครื่องในการทำงานทั้งหมดน้อยที่สุด • ตัวอย่างงาน (1,3) , (1,4) , (2,5) , (3,7) , (4,7) , (6,9) , (7,8) จะได้ผลดังนี้เครื่องที่ 1: (1,3) , (3,7) , (7,8) เครื่องที่ 2:(1,4) , (4,7) และเครื่องที่ 3:(2,5) , (6,9) 344-211 Algorithmic Process and Programming , created by Dararat Saelee

  37. Greedy Method Job Scheduling (Uniprocessor) j1 j2 j3 j4 Job Time j1 15 j2 8 j3 3 j4 10 0 15 23 26 36 First-come-First-serve : avg. completion time = 25 avg. waiting time = 16 j3 j2 j4 j1 0 3 11 21 36 Shortest Job First :avg. completion time = 17.75 avg. waiting time = 8.75 344-211 Algorithmic Process and Programming , created by Dararat Saelee

  38. Greedy Method FCFS Job Scheduling (Multiprocessor) Job Time j1 3 j2 5 j3 6 j4 10 j5 11 j6 14 j7 15 j8 18 j9 20 j1 j4 j7 0 3 13 28 j2 j5 j8 0 5 16 34 j3 j6 j9 0 6 20 40 344-211 Algorithmic Process and Programming , created by Dararat Saelee

  39. Greedy Method Optimal #1 Job Scheduling (Multiprocessor) Job Time j1 3 j2 5 j3 6 j4 10 j5 11 j6 14 j7 15 j8 18 j9 20 j1 j6 j7 0 3 17 32 j2 j5 j8 0 5 16 34 j3 j4 j9 0 6 16 36 344-211 Algorithmic Process and Programming , created by Dararat Saelee

  40. Greedy Method Job Scheduling (Multiprocessor) Job Time j1 3 j2 5 j3 6 j4 10 j5 11 j6 14 j7 15 j8 18 j9 20 j2 j5 j8 0 5 16 34 j6 j9 0 14 34 j1 j3 j4 j7 0 3 9 19 34 Optimal #2 – minimize completion time 344-211 Algorithmic Process and Programming , created by Dararat Saelee

  41. Divide and Conquer • divide : break a given problem into subproblems • recur : try to solve each in recursive way • conquer : derive the final solution from all solutions 344-211 Algorithmic Process and Programming , created by Dararat Saelee

  42. Solution to the original problem Divideand Conquer Problem of size n Subproblem m of size n/m Subproblem 1 of size n/m Subproblem 2 of size n/m … Solution to Subproblem 1 Solution to Subproblem 2 Solution to Subproblem m 344-211 Algorithmic Process and Programming , created by Dararat Saelee

  43. Divide and Conquer • Merge sort • Quick sort • Binary Tree Traversal • Closest-Pair and Convex-Hall • Selection problem 344-211 Algorithmic Process and Programming , created by Dararat Saelee

  44. Divide and Conquer • Factorial • Fibonacci • Binary search • Strassen’s Matrix Multiplication • Big Integer Multiplication 344-211 Algorithmic Process and Programming , created by Dararat Saelee

  45. Divideand Conquer ตัวอย่าง 4! = ? 4! = 4 * 3! 3! = 3 * 2! 2! = 2 * 1! 1! = 1 * 0! 0! = 1 Factorial • n! = n * (n-1)! • (n-1)! = (n-1) * (n-2)! • … • 1! = 1 • 0! = 1 344-211 Algorithmic Process and Programming , created by Dararat Saelee Describe with Tree structure

  46. Divideand Conquer Fibonacci num.: 1,1,2,3,5,8,13,… • fibo (n) = fibo (n-1) + fibo (n-2) • fibo (n-1) = fibo (n-2) + fibo (n-3) • fibo (n-2) = fibo (n-3) + fibo (n-4) • … • fibo (3) = fibo (2) + fibo (1) • fibo (2) = 1 • fibo (1) = 1 344-211 Algorithmic Process and Programming , created by Dararat Saelee

  47. mid mid mid Divide and Conquer Search = 37 Binary search 1215 18 23 26 37 39 41 4348 12 15 18 23 263739 41 4348 12 15 18 23 26373941 43 48 344-211 Algorithmic Process and Programming , created by Dararat Saelee

  48. I J K L A B C D E F G H Divide and Conquer Strassen’s Matrix Multiplication • Z = X * Y ; matrix n xn • I = AxE + BxG , J = AxF + BxH • K = CxE + DxG , L = CxF + DxH = x 344-211 Algorithmic Process and Programming , created by Dararat Saelee

  49. Divide and Conquer Big Integer Multiplication • multiply 2 N-digit numbers : X , Y • XY = XLYL10N + (XLYR + XRYL)10N/2 + XRYR • XLYR+XRYL = (XL-XR)(YR-YL) + XLYL + XRYR • require : 2 subtraction , 3 multiplication • D1 = XL-XR , D2 = YR-YL • XLYL , XRYR , D1D2 • D3 = D1D2 +XLYL + XRYR 344-211 Algorithmic Process and Programming , created by Dararat Saelee

  50. Divide and Conquer Big Integer Multiplication • X = 61,438,521 & Y = 94,736,407 • XL = 6143 , XR = 8521 • YL = 9473 , YR = 6407 • D1 = XL-XR = -2378, D2 = YR-YL = -3066 • XLYL = 58192639 , XRYR = 54594047 • D1D2 = 7290948 • D3 = D1D2 +XLYL + XRYR = 120077634 • XY = XLYL108 + D3104 + XRYR 344-211 Algorithmic Process and Programming , created by Dararat Saelee

More Related