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

Preview:

DESCRIPTION

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

Citation preview

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: Surasak.mu@spu.ac.th

04/24/23 1

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

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

04/24/23 4

Breadth-First Search ExampleBreadth-First Search Example

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

04/24/23 5

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

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

Breadth-First Search ExampleBreadth-First Search Example

04/24/23 6

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

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

Breadth-First Search ExampleBreadth-First Search Example

04/24/23 7

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

Breadth-First Search ExampleBreadth-First Search Example

04/24/23 8

Breadth-First Search ExampleBreadth-First Search Example

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

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

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)

04/24/23 11

04/24/23 12

04/24/23 13

Depth-First SearchDepth-First Search

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

04/24/23 14

Depth-First SearchDepth-First Search

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

04/24/23 15

Depth-First SearchDepth-First Search

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

04/24/23 16

Depth-First SearchDepth-First Search

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

04/24/23 17

Depth-First SearchDepth-First Search

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

04/24/23 18

Depth-First SearchDepth-First Search

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

04/24/23 19

Depth-First SearchDepth-First Search

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

04/24/23 20

Depth-First SearchDepth-First Search

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

04/24/23 21

Depth-First SearchDepth-First Search

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

04/24/23 22

Depth-First SearchDepth-First Search

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

04/24/23 23

Depth-First SearchDepth-First Search

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

04/24/23 24

Depth-First SearchDepth-First Search

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

04/24/23 25

Depth-First SearchDepth-First Search

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

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

Depth-First Search PropertiesDepth-First Search Properties

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

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

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

04/24/23 28

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

04/24/23 29

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

04/24/23 30

04/24/23 31

04/24/23 32

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

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

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

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

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

04/24/23 34

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

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

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

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)

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.

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

Apr 24, 202340

CSE221/ICT221 Analysis and Design of Algorithms

Recommended