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

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

Asst.Prof. Dr.Surasak Mungsing

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


งานนำเสนอเรื่อง: "Asst.Prof. Dr.Surasak Mungsing"— ใบสำเนางานนำเสนอ:

1 Asst.Prof. Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th
CSC201 Analysis and Design of Algorithms Dynamic Programming, Backtracking Asst.Prof. Dr.Surasak Mungsing Apr-17

2 Common algorithms Greedy Algorithms Divide and Conquer
Dynamic Programming Backtracking algorithms 4/4/2017

3 Dynamic Programming 4/4/2017

4 Dynamic Programming เป็นเทคนิคการเขียนโปรแกรมที่ไม่ใช้ Recursive call เพื่อปรับปรุงประสิทธิภาพการทำงานของขั้นตอนวิธีให้ดีขึ้น โดยทั่วไปแล้วมีหลายกรณีที่การแก้ปัญหาโดยขั้นตอนวิธีแบบ Recursive algorithm ให้ผลลัพธ์เร็วกว่าวิธีการอื่นที่ไม่ใช้ Recursive call ถ้าสงสัยว่าโปรแกรมแปลภาษา (Compiler) อาจส่งผลให้โปรแกรมทำงานอย่างไม่มีประสิทธิภาพ ก็สามารถช่วย Compiler ได้โดยเขียนโปรแกรมแบบ non-recursive บันทึกผลลัพธ์ย่อยของแต่ละขั้นตอนอย่างเป็นระบบลงในตาราง คือหลักการของ Dynamic Programming 4/4/2017

5 กรณีที่ Greedy Method ใช้ไม่ได้ผล
Find a shortest route from S to T. Greedy method solution: S  A  D  T = =23 Dynamic Programming solution: S  C  F  T = = 9 4/4/2017

6 Dynamic Programming Approach
d(S,T) = min { 1 + d(A,T), 2 + d(B,T), 5 + d(C,T) } หาค่า d(A,T) d(A,T) = min { 4 + d(D,T), 11 + d(E,T) } d(D,T) = 18 and d(E,T) = 13 d(A,T) = min { , } = 22 4/4/2017

7 Dynamic Programming Approach
d(S,T) = min { 1 + d(A,T), 2 + d(B,T), 5 + d(C,T) } หาค่า d(A,T) d(A,T) = min { 4 + d(D,T), 11 + d(E,T) } d(D,T) = 18 and d(E,T) = 13 d(A,T) = min { , } = 22 4/4/2017

8 Dynamic Programming Approach
d(S,T) = min { 1 + d(A,T), 2 + d(B,T), 5 + d(C,T) } หาค่า d(B,T) และ d(C,T) d(B,T) = min { 9 + d(D,T), 5 + d(E,T), 16 + d(F,T) } = min { , , } = min { 27, 18, 18 } = 18 d(C,T) = 4 4/4/2017

9 Dynamic Programming Approach
d(S,T) = min { 1 + d(A,T), 2 + d(B,T), 5 + d(C,T) } ดังนั้น Shortest Path จาก S ไป T หรือ d(S,T) คือ d(S,T) = { , , } = min { 23, 20, 9 } = 9 4/4/2017

10 Fibonacci Algorithm let a = 0; let b = 1;
function "fibonacci", parameters 'n' (integer > 0): if n equals 1 return 0; if n equals 2 return 1; return fibonacci(n-2) + fibonacci(n-1); (end of function) let a = 0; let b = 1; while n is greater than 2 do: { let c = a + b; let a = b; let b = c; subtract 1 from n; } (end of loop) return b; (end of function)

11 ตัวอย่างการใช้เทคนิคของ Dynamic programming
ปัญหาการคำนวณตัวเลข Fibonacci number ซึ่งหากใช้วิธีการของ Recursive algorithm จะมีความซับซ้อนด้านเวลาเป็น Exponential คือ O(2n) แต่หากเปลี่ยนมาใช้วิธีการของ non-recursive algorithm เป็นลักษณะของ Linear algorithm จะช่วยให้การทำงานมีประสิทธิภาพยิ่งขึ้นมากเป็น O(n) All-pairs Shortest Path Dijkstra’s algorithm (Single-source, Shortest-path) Resource Allocation Problems 4/4/2017

12 Example - Resource Allocation Problem
ตารางผลตอบการการจัดทรัพยากรให้โครงการ(Profit matrix) P(i,0) = 0 4/4/2017

13 Example - Resource Allocation Problem
Let (i,j) represent the state attained where i resources have been allocated to project 1, 2, 3, …, j เริ่มแรก จัด resource i ให้ Project 1 ดังนั้นจะเหลือเพียง 3 - i ที่สามารถจัดให้ Project 2 4/4/2017

14 Example - Resource Allocation Problem
The first two stage decision of resource allocation problem. Second state decision 4/4/2017

15 Example - Resource Allocation Problem
The resource allocation problem described as a multi-stage graph. 4/4/2017

16 Backward Reasoning Approach
The longest path is S  C  H  L  T 2 resources allocated to project 1 1 resource allocated to project 2 0 resource allocated to project 3 And 0 resource allocated to project 4 4/4/2017

17 Backtracking Algorithm
4/4/2017

18 Backtracking Algorithm
เป็นเทคนิคที่ใช้ในการหาคำตอบให้กับปัญหาที่ต้องมีการทดสอบผลของการการตัดสินใจในแต่ละขั้นตอนว่าจะดำเนินการต่อไปอย่างไร ถ้าได้คำตอบที่ต้องการก็จะหยุดการทำงาน แต่ถ้าผลการตัดสินใจที่ผ่านมาไม่ได้คำตอบที่ต้องการก็จะมีการย้อนกลับขั้นตอนตามเส้นทางการตัดสิใจเดิมและตัดสินใจเดินหน้าค้นหาคำตอบต่อไปใหม่ ตัวอย่างของการใช้เทคนิคนี้ในการออกแบบขั้นตอนวิธีเช่น การแก้ปัญหา 8 ราชินี (Eight Queens Problem) ปัญหายุทธศาสตร์ของเกมส์ (Games) ปัญหาการหาทางออกจากเขาวงกต 4/4/2017

19 Stack Applications: Backtracking
4/4/2017

20 Stack Applications: Backtracking
4/4/2017

21 CSE221/ICT221 Analysis and Design of Algorithms
4-Apr-17 CSE221/ICT221 Analysis and Design of Algorithms


ดาวน์โหลด ppt Asst.Prof. Dr.Surasak Mungsing

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


Ads by Google