1 / 42

Chapter 8 Searching

Chapter 8 Searching. WATTANAPONG SUTTAPAK Software Engineering, School of Information Communication Technology, University of PHAYAO. จุดประสงค์ บทเรียนที่ 5. Sequential Searching Binary Searching Depth-First Search Breadth-First Search. Sequential Searching. Sequential Searching.

Télécharger la présentation

Chapter 8 Searching

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. Chapter 8Searching WATTANAPONGSUTTAPAK Software Engineering, School of Information Communication Technology, University of PHAYAO

  2. จุดประสงค์ บทเรียนที่ 5 Sequential Searching Binary Searching Depth-First Search Breadth-First Search

  3. Sequential Searching

  4. Sequential Searching หมายถึงการค้นหาในรูปทั่วไป โดยนำไปเปรียบเทียบทีละค่า Linear Searching เช่น A = {12 , 15, 7, 8, 9, 11, 23, 1, 4, 20, 3, 17 , 16} ค้นหา 11 found = 0; for i=1 to A.length if A[i] == 11 found = i; end if end for

  5. Binary Searching

  6. Binary Searching • การค้นหาในรูปแบบแบ่งสองส่วน • devide and conquer • binary search tree • ตัวอย่างการค้นหา 2 • A = {1,2,3,4,5,6,7,8,11} 1 2 3 4 5 6 7 8 11 1 2 3 4 5 6 7 8 11 1 23 4 5 6 7 8 11

  7. Binary Searching • การค้นหาในรูปแบบแบ่งสองส่วน • devide and conquer • binary search tree • ตัวอย่างการค้นหา 2 6 4 8 2 5 7 11 1 3

  8. Exercise ให้เขียนโปรแกรมเปรียบเทียบการทำงานระหว่าง sequential search และ binary searching binary searching ให้เขียนทั้งสองวิธีการ เปรียบเทียบผลที่ได้ ข้อดีข้อเสียของแต่ละวิธี

  9. Depth-First Search

  10. A B C D E F G H I J K L M N O P Q Depth-First Search A B E H N A B E H A B E A B E I A B E I O A B E I A B E I P A B E I A B E A B A A C … ใช้ stack A A B A B D A B A B E A B E H A B E H L A B E H A B E H M A B E H A B E H N

  11. Depth-First Search การหาเส้นทางจากจุดหนึ่งไปอีกจุดหนึ่ง Visited Node F C A B D H G E

  12. Depth-First Search การหาเส้นทางจากจุดหนึ่งไปอีกจุดหนึ่ง Visited Node F C A B D H G E D D

  13. Depth-First Search การหาเส้นทางจากจุดหนึ่งไปอีกจุดหนึ่ง Visited Node F C A B D H G E C D D C

  14. Depth-First Search การหาเส้นทางจากจุดหนึ่งไปอีกจุดหนึ่ง Visited Node F C A B D H G E D D C

  15. Depth-First Search การหาเส้นทางจากจุดหนึ่งไปอีกจุดหนึ่ง Visited Node F C A B D H G E E D D C E

  16. Depth-First Search การหาเส้นทางจากจุดหนึ่งไปอีกจุดหนึ่ง Visited Node F C A B D H G E D G E D C E G

  17. Depth-First Search การหาเส้นทางจากจุดหนึ่งไปอีกจุดหนึ่ง Visited Node F C A B D H G E D H G E D C E G H

  18. Depth-First Search การหาเส้นทางจากจุดหนึ่งไปอีกจุดหนึ่ง Visited Node F C A B H G E D B D H G E D C E G H B

  19. Depth-First Search การหาเส้นทางจากจุดหนึ่งไปอีกจุดหนึ่ง Visited Node F C A H G E D B D H G E D C E G H B

  20. Depth-First Search การหาเส้นทางจากจุดหนึ่งไปอีกจุดหนึ่ง Visited Node F C A A H G E D B D H G E D C E G H B A

  21. Depth-First Search การหาเส้นทางจากจุดหนึ่งไปอีกจุดหนึ่ง Visited Node F C A H G E D B D H G E D C E G H B A

  22. Depth-First Search การหาเส้นทางจากจุดหนึ่งไปอีกจุดหนึ่ง Visited Node F C A G E D B D H G E D C E G H B A

  23. Depth-First Search การหาเส้นทางจากจุดหนึ่งไปอีกจุดหนึ่ง Visited Node F C A E D B D H G E D C E G H B A

  24. Depth-First Search การหาเส้นทางจากจุดหนึ่งไปอีกจุดหนึ่ง Visited Node F C A D B D H G E D C E G H B A

  25. Depth-First Search การหาเส้นทางจากจุดหนึ่งไปอีกจุดหนึ่ง Visited Node F C A F D B D H G E D C E G H B A F

  26. Depth-First Search การหาเส้นทางจากจุดหนึ่งไปอีกจุดหนึ่ง Visited Node F C A D B D H G E D C E G H B A F

  27. Depth-First Search การหาเส้นทางจากจุดหนึ่งไปอีกจุดหนึ่ง Visited Node F C A B D H G E D C E G H B A F

  28. Exercise ให้เขียนโปรแกรมเพื่อหาเส้นทางทั้งหมด จากจุด D ไปยัง B F C A B D H G E

  29. Breadth-First Search

  30. A B C D E F G H I J K L M N O P Q Breadth-First Search ใช้ queue A A B A B C A B C D E A B C D E F G A B C D E F G H I J K A … Q

  31. Breadth-First Search F C A B D H G E

  32. Breadth-First Search D F C A B D H G E

  33. Breadth-First Search D D-C D-E D-F F C A B D H G E

  34. Breadth-First Search D D-C D-E D-F D-E-G F C A B D H G E

  35. Breadth-First Search D D-C D-E D-F D-E-G D-E-G-H F C A B D H G E

  36. Breadth-First Search D D-C D-E D-F D-E-G D-E-G-H D-E-G-H-A D-E-G-H-B F C A B D H G E

  37. Depth-First Search VS Breadth-First Search Sudoku

  38. Depth-First Search VS Breadth-First Search Silly Sort ให้หาผลรวมของตัวเลขที่มีการสลับตำแหน่งกัน โดยผลรวมที่ได้ต้องมีค่าน้อยสุด input บรรทัดแรกเป็นจำนวนตัวเลขที่ต้องการเรียงลำดับ แทนด้วย n เมื่อ บรรทัดที่สองเป็นตัวเลขแต่ละตัวที่ต้องการเรียงลำดับ a1 a2 a3 … an โดย output แสดงผลรวมที่น้อยที่สุดของตัวเลขที่สลับลำดับ

  39. Silly sort ลำดับเลข 1 8 9 7 6 คำตอบคือ ผลรวมตัวเลขสลับตำแหน่งน้อยที่สุดเท่ากับ 41 • วิธีที่ 1 ย้ายตำแหน่งที่ไม่ตรงกับตำแหน่งที่เรียงลำดับแล้ว • ลำดับการสลับตำแหน่งที่น้อยที่สุดคือ • 1 8 9 7 6 เป้าหมายคือ 1 6 7 8 9 • ถ้าคิดง่ายที่สุดคือ เก็บ 1 ไว้ตำแหน่งเดิม แล้วย้ายตัวที่ตำแหน่งตรงกัน นั่นคือ 8,7 9,6 ผลรวม 30 • จะได้ 1 7 6 8 9 จากนั้นย้าย 7 6 ผลรวม 13+30 = 43 • วิธีที่ 2 ย้ายตำแหน่งตัวเลขโดยใช้เลขที่น้อยที่สุดเป็นตัวย้าย • ใช้ 1 เป็นตัวย้ายเลขอื่น • 1,6(7) >>> 6 8 9 7 1 >1,9(17) >>> 6 8 1 7 9 > 1,7(25) >>> 6 8 7 1 9 • 1,8(34) >>> 6 1 7 8 9 >> 1,6(41) >>> 1 6 7 8 9 • คำตอบที่ดีที่สุดคือ วิธี 2 • แล้วรู้ได้อย่างไรว่าต้องใช้ 1 ในการสับเปลี่ยน

  40. Depth-First Search VS Breadth-First Search Maze http://commons.wikimedia.org/w/index.php?title=File%3AMAZE_30x20_DFS.ogv

  41. Initial call PuzzleSolve ( 3 , () ,{ a , b , c } ) PuzzleSolve ( 2 , b ,{ a , c } ) PuzzleSolve ( 2 , c ,{ a , b } ) PuzzleSolve ( 2 , a ,{ b , c } ) PuzzleSolve ( 1 , ab ,{ c } ) PuzzleSolve ( 1 , ba ,{ c } ) PuzzleSolve ( 1 , ca ,{ b } ) bac cab abc PuzzleSolve ( 1 , ac ,{ b } ) PuzzleSolve ( 1 , bc ,{ a } ) PuzzleSolve ( 1 , cb ,{ a } ) acb cba bca ตัวอย่างโจทย์ Breadth-First Search Puzzle Solve

  42. ตัวอย่างโจทย์ Breadth-First Search Subset SUM ให้หาจำนวนครั้งที่เกิดผลรวมเท่ากับ 18

More Related