Dr.Surasak Mungsing E-mail: [email protected]

40
CSE 221/ICT221 CSE 221/ICT221 Analysis and Design of Analysis and Design of Algorithms Algorithms Lecture 11: Lecture 11: กกกกกกกกกกกกกกกกกกกกกกกกกกกกกกกก กกกกก Dr.Surasak Mungsing Dr.Surasak Mungsing E-mail: [email protected] 06/28/22 1

description

CSE 221/ICT221 Analysis and Design of Algorithms Lecture 11: การวิเคราะห์ค่าความซับซ้อนของการค้นหา. Dr.Surasak Mungsing E-mail: [email protected]. Graph Search Methods. ปัญหากราฟจำนวนมากแก้โดยวิธีการค้นหา (Many graph problems solved using a search method) - PowerPoint PPT Presentation

Transcript of Dr.Surasak Mungsing E-mail: [email protected]

Page 1: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

CSE 221/ICT221 CSE 221/ICT221 Analysis and Design of Algorithms Analysis and Design of Algorithms Lecture 11:Lecture 11:การวเิคราะห์ค่าความซบัซอ้นของการค้นหา

Dr.Surasak MungsingDr.Surasak MungsingE-mail: [email protected]

04/24/23 1

Page 2: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

Graph Search MethodsGraph Search Methods

• ปัญหากราฟจำานวนมากแก้โดยวธิกีารค้นหา (Many graph problems solved using a search method) การหาเสน้ทางจากโหนดหนึ่งไปยงัอีกโหนดหนึ่ง (Path

from one vertex to another) กราฟเชื่อมต่อกันหรอืไม่ (Is the graph connected?) การหาต้นไมแ้ผ่ของกราฟ (Find a spanning tree) Etc.

• วธิกีารค้นหาท่ีรูจ้กักันทั่วไป (Commonly used search methods): Breadth-first search. Depth-first search.

04/24/23 2

Page 3: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/23 3CSE221 การวเิคราะหแ์ละออกแบบขัน้ตอนวธิี

Page 4: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/23 4

Breadth-First Search ExampleBreadth-First Search Example

ถ้าใหโ้หนดแทนเมอืงต่างๆกราฟก็เหมอืนถนนท่ีเชื่อมเมอืงซึ่งสามารถท่ีพาไปยงัเมอืงต่างๆได้

Page 5: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/23 5

จากเมอืง Chicago, สามารถท่องไปยงัเมอืงต่างๆได้โดยวธิ ีbreadth-first search ได้ดังน้ี

เริม่ด้วยการเดินทางไปยงัเมอืงท่ีอยูใ่กล้ท่ีสดุก่อน ได้แก่เมอืง Saint Louis, Milwaulkee, Detroit และ Indianapolis

Breadth-First Search ExampleBreadth-First Search Example

Page 6: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/23 6

จากนัน้ ก็เดินทางต่อไปยงัเมอืงท่ีใกล้ท่ีสดุของเมอืงต่างๆท่ียงัไมไ่ด้

จากเมอืง Milwaulkee ไปยงัเมอืง Minneapolis และเมอืง Omaha

Breadth-First Search ExampleBreadth-First Search Example

Page 7: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/23 7

จากเมอืง St. Louis ไปเมอืง Kansas City, Tulsa, Nashville และ Lexington

Breadth-First Search ExampleBreadth-First Search Example

Page 8: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/23 8

Breadth-First Search ExampleBreadth-First Search Example

จากเมอืง Indianapolis ไปเมอืง Cincinnati และเมอืง Columbus

Page 9: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/23 9

Data structures for BFS algorithmData structures for BFS algorithm

1. สรา้ง array ของโหนดจากกราฟ G. แต่ละโหนดจะกำาหนดส(ีcolor), ระยะทาง(distance) และโหนดก่อนหน้า(predecessor):

- color: (White, Grey, Black).White: ถ้ายงัไมเ่คยตรวจสอบมาก่อน ดังนัน้ในกาเริม่ต้นทกุ

โหนดเป็นส ีWHITEGrey: ถ้าถกูตรวจแล้วแต่โหนดประชดิของโหนดนี้ยงัไมถ่กู

ตรวจBlack: ถ้าโหนดถกูตรวจสอบแล้วและโหนดประชดิก็ถกูตรวจ

แล้วด้วย - distance: จำานวน edges ท่ีจะต้องเดินทางนับจากจุดเริม่ต้น มค่ีา

เป็น INFINITY เมื่อเริม่ต้น - predecessor: โหนดก่อนหน้าก่อนท่ีจะถึงโหนดน้ี

2. สรา้งคิว Q ของโหนดส ีGREY ทัง้หมด (โหนดท่ีไปถึงแล้วแต่ยงัไมไ่ด้ไปต่อ)

3. ใหก้ารแทนขอ้มูลกราฟ G เป็นแบบ adjacency list

Page 10: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

Time ComplexityTime Complexity Each visited vertex is put on (and so

removed from) the queue exactly once. When a vertex is removed from the

queue, we examine its adjacent vertices. O(n) if adjacency matrix used O(vertex degree) if adjacency lists used

Total time O(mn), where m is number of vertices in the

component that is searched (adjacency matrix)

O(n + sum of component vertex degrees) (adj. lists) = O(n + number of edges in component)

Page 11: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/23 11

Page 12: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/23 12

Page 13: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/23 13

Depth-First SearchDepth-First Search

ลองนึกภาพคนเดินหาทางออกจากเขาวงกต เขาจะเดินไปขา้งหน้าเรื่อยๆ จนกระทัง่ถึงทางตันหรอืพบเสน้ทางท่ีเคยเดินผ่านมาก่อน จากนัน้จะเล้ียวไปทางแยกท่ียงัไมเ่คยไปมาก่อนเพื่อเดินหน้าค้นหาทางออกจากเขาวงกตต่อไปสำาหรบักราฟแผนท่ีเมอืง Chicago เหตกุารณ์จะเป็นดังนี้

Page 14: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/23 14

Depth-First SearchDepth-First Search

จากเมอืง Chicagoไปยงัเมอืง Milwaukee

Page 15: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/23 15

Depth-First SearchDepth-First Search

จากเมอืง Milwaukee ไปยงัเมอืง Minneapolis

Page 16: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/23 16

Depth-First SearchDepth-First Search

จากเมอืง Minneapolis ไปยงัเมอืง Omaha

Page 17: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/23 17

Depth-First SearchDepth-First Search

จากเมอืง Omaha ไปยงัเมอืง Kansas City

Page 18: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/23 18

Depth-First SearchDepth-First Search

จากเมอืง Kansas City ไปยงัเมอืง St. Louis

Page 19: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/23 19

Depth-First SearchDepth-First Search

จากเมอืง St. Louis ไปยงัเมอืง Tulsa

Page 20: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/23 20

Depth-First SearchDepth-First Search

จากเมอืง St. Louis ไปยงัเมอืง Nashville

Page 21: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/23 21

Depth-First SearchDepth-First Search

จากเมอืง Nashville ไปยงัเมอืง Lexington

Page 22: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/23 22

Depth-First SearchDepth-First Search

จากเมอืง Lexingtonไปยงัเมอืง Cincinnati Cincinnati

Page 23: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/23 23

Depth-First SearchDepth-First Search

จากเมอืง Cincinnatiไปยงัเมอืง Columbus

Page 24: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/23 24

Depth-First SearchDepth-First Search

จากเมอืง Columbusไปยงัเมอืง Detroit Detroit

Page 25: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/23 25

Depth-First SearchDepth-First Search

จากเมอืง Columbusไปยงัเมอืง Indianapolis

Page 26: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/23 26

Data structures for DFS algorithmData structures for DFS algorithm

1. สรา้ง array ของโหนดจากกราฟ G. แต่ละโหนดจะกำาหนด color, predecessor, start_time และ end_time: - color: (White, Grey, Black).

White: ถ้ายงัไมเ่คยตรวจสอบมาก่อน ดังนัน้ในกาเริม่ต้นทกุโหนดเป็นส ีWHITE

Grey: ถ้าถกูตรวจแล้วแต่โหนดประชดิของโหนดน้ียงัไมถ่กูตรวจ

Black: ถ้าโหนดถกูตรวจสอบแล้วและโหนดประชดิก็ถกูตรวจแล้วด้วย

- start_time: เวลาในขัน้ตอนวธิเีมื่อโหนดถกูมาเยอืนเป็นครัง้แรก - end_time: เวลาในขัน้ตอนวธิเีมื่อโหนดประชดิถกูตรวจสอบแล้ว (เมื่อโหนดนี้เป็น black) - predecessor: โหนดก่อนหน้าก่อนท่ีจะถึงโหนดน้ีเป็นครัง้แรก2. ใหก้ารแทนขอ้มูลกราฟ G เป็นแบบ adjacency list

Page 27: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

Depth-First Search PropertiesDepth-First Search Properties

Time complexity เหมอืนกับของ BFS. คณุสมบติัท่ีเกี่ยวกับการหาเสน้ทาง การหาการเชื่อมต่อกันของ

องค์ประกอบและ spanning tree ของ BFS และ DFS เหมอืนกัน

มหีลายปัญหาท่ีวธิขีอง BFS ดีกวา่ DFS และก็มหีลายปัญหาท่ีวธิขีอง DFS ดีกวา่ BFS

Page 28: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/23 28

Breadth-first search VS Depth-first search Breadth-first search VS Depth-first search trees trees

Page 29: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/23 29

Breadth-first search VS Depth-first search Breadth-first search VS Depth-first search trees trees

Page 30: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/23 30

Page 31: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/23 31

Page 32: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/23 32

แนวคิดของเทคนิค แนวคิดของเทคนิค HashingHashing แต่ละค่าคียจ์ะ map ไปยงัตัวเลขท่ีมค่ีาระหวา่ง 0 ถึง

TableSize -1 และเก็บอยูใ่นตำาแหน่งท่ีเหมาะสม การ map ค่าคียท์ำาโดย hash function ซึ่งในทาง

ทฤษฎีจะเป็นฟงัก์ชนัง่ายๆซึ่งสามารถใหผ้ลลัพทธท่ี์แตกต่างกันสำาหรบัค่าคียห์น่ึงๆ

มปีระเด็นท่ีจะต้องพจิารณาคือการเลือก hash function และการแก้ปัญหาเมื่อค่าคีย ์2 ค่าประมวลผลโดย hash function แล้วได้ผลลัพทธเ์ดียวกัน

Page 33: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/23 33

Hash functionHash function

hash function อยา่งง่ายhash(key) = key MOD TableSize

TableSize ควรเป็นตัวเลขจำาเพาะ (prime number) การได้ผลลัพธเ์หมอืนกันจากค่าคียท่ี์แตกต่างกันเรยีกวา่

Collision การแก้ปัญหา Collision ทำาโดยการเก็บค่าคียใ์น Hash

table แบบ Separate Chaining Open Addressing

Page 34: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/23 34

Separate chainingSeparate chainingKey={0, 81, 64, 25, 36, 49, 4, 1, 9, 16}H(x) = x % 10

Page 35: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/23 35

Open addressing with Linear Open addressing with Linear ProbingProbing

Key {89, 18, 49, 58, 69} Hash function: hash(x)=x MOD 10 Collision resolution strategy: f(i)=i

Page 36: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/23 36

Open addressing with Quadratic Open addressing with Quadratic ProbingProbing

Key {89, 18, 49, 58, 69} Hash function: hash(x)=x MOD 10 Collision resolution strategy: f(i)=i2

Page 37: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/23 37

Open addressing with Double Open addressing with Double HashingHashing

Key {89, 18, 49, 58, 69} Hash functions: hash(x)=x MOD 10

hash2(x)=7-(x MOD 7) Collision resolution strategy: f(i)=i*hash2(x)

Page 38: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/23 38

TheoremTheorem

ถ้าใชก้ารแก้ปัญหา Collision แบบ Quadratic probing และ table size เป็นเลข prime แล้ว ขอ้มูลใหมจ่ะมท่ีีลงในตารางเสมอหากวา่ table ยงัอยา่งน้อยครึง่หน่ึงของตารางยงัวา่ง

If quadratic probing is used, and the table size is prime, then a new element can always be inserted if the table is at least half empty.

Page 39: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

04/24/23 39

RehashingRehashing

Open addressing hash table

with linear probingwith input 13, 15, 6, 24

Open addressing hash tablewith linear probingafter 23 is inserted

Open addressing hash tableafter rehashing

Page 40: Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th

Apr 24, 202340

CSE221/ICT221 Analysis and Design of Algorithms