Date post: | 02-Jan-2016 |
Category: |
Documents |
Upload: | clarke-stephenson |
View: | 35 times |
Download: | 6 times |
1
การสร�างดรรชนีและการค้�นีหา
Chapter 5Indexing and Searching
2
การสร�างดรรชนีและการค้�นีหา•แฟ้�มข้�อม�ลผกผ�นี ( Inverted Files )
•Suffix Trees และ Suffix Arrays •แฟ้�มข้�อม�ลลายเซ็�นี ( Signature Files )
•การค้�นีหาตามล�าด�บ ( Sequential Searching )
•Hashing Algorithm
3
แฟ้�มข้�อม�ลผกผ�นี ( Inverted Files )
•แฟ้�มข้�อม�ลผกผ�นก�คื�อล�กษณะหน��งข้องดรรชน�คื��ศั�พท์!ในแนวคื$ดท์��กล%�วม�ช��งต้�นอ�นประกอบด�วยคื��ศั�พท์!ท์��ม�ก�รจั�ดระบบเพ��อให�คื�นห�ได�ง%�ย โดยแต้%ละคื��ศั�พท์!ช�/ไปย�งต้��แหน%งในเอกส�รท์��ต้�องก�รส�บคื�น
ประกอบด�วยส%วนส��คื�ญ 3 ส%วน ได�แก% •ส%วนข้องพจัน�น2กรม ( Dictionary ) •ส%วนข้องต้��แหน%ง ( Address ) •และส%วนข้องเน�/อห� ( Text )
4
Dictionary
Address
Text
ร�ปที่" 1 โค้รงสร�างข้องแฟ้�มข้�อม�ลผกผ�นี
5
•ส%วนข้องพจัน�น2กรมประกอบด�วยคื��ศั�พท์!ต้%�ง ๆ ท์��ไม%ซ้ำ�/�ก�นเร�ยกว%� เท์อม เท์อมเหล%�น�/ม�ก�รจั�ดกล2%มอย%�งเป5นระบบเพ��อให�ง%�ยต้%อก�รคื�นห� ว$ธี�หน��งคื�อ จั�ดเร�ยงต้�มล��ด�บข้องต้�วอ�กษรหร�อต้�มต้�วอ�กษรก%อนหล�ง เช%น จั�ดเร�ยงเป5นร�ป Array ย�งจั�ดได�เป5นร�ป Binary Search Tree (BST) , Trie , B-Tree และ ว$ธี�ก�ร Hashing
6
•ส%วนข้องต้��แหน%ง จัะเป5นก�รบ%งบอกต้��แหน%งข้องเท์อมต้%�ง ๆ ท์��ปร�กฏในเน�/อห�ข้องเอกส�ร บท์คืว�มหร�อหน�งส�อต้%�ง ๆ ท์��เท์อมเหล%�น�/นรวมอย�% ก�รบ%งบอกต้��แหน%งจั�งจัะระบ2เป5น ช2ด/เล%ม/หน��/บรรท์�ด/ล��ด�บ เป5นต้�น
7
•ส%วนข้องเน�/อห�จัะเป5นข้�อม�ลในเอกส�ร บท์คืว�ม หร�อหน�งส�อ โดยท์��วไปจัะม�ปร$ม�ณข้�อม�ลม�กม�ยมห�ศั�ล จั�งต้�องจั�ดเก�บอย%�งเป5นระบบและบ%อยคืร�/งจัะต้�องท์��ก�รลดข้น�ดข้องข้�อม�ลลง ว$ธี�ก�รหน��งก�คื�อ ก�รบ�บอ�ดข้�อม�ล ( Data Compression ) โดยท์��วไปจัะใช�ว$ธี�ก�รข้อง Huffiman Code ย�งข้ย�ยผลม�ใช�ได�ก�บก�รบ�บอ�ดข้องคื��ด�วย ผลก�คื�อข้น�ดข้องข้�อม�ลโดยรวมได�ลดลงไปอย%�งม�ก
8
ต�วอย%างที่" 1 ให�เอกสารช'ดหนี("งเป)นีด�งนี*
Document Text
1 Pease porridge hot , pease porridge cold.
2 Pease porridge in the pot.
3 Nine days old.
4 Some like it hot, some like it cold.
5 Some like it in the pot.
6 Nine days old.
9
แฟ้�มข้�อม�ลผกผ�นหล�งก�รประมวลผล จัะม�ร�ปต้�มร�ป 2.1 ด�งน�/
Term
colddayshotinit
likenineold
peaseporridge
potsomethe
Occurrence
1/6,4/83/2,6/21/3,4/42/3,5/44/3,4/7,5/34/2,4/6,5/23/1,6/13/3,6/31/1,1/4,2/11/2,1/5,2/22/5,5/64/1,4/5,5/12/4,5/5
10
การสร�างแฟ้�มข้�อม�ลผกผ�นีก�รสร��งและบ��ร2งร�กษ�แฟ้�มข้�อม�ลผกผ�นม�หล�ยว$ธี� ในท์��น�/จัะน��เสนอใน 2 ว$ธี� คื�อ
•Memory-based Inversion•Sort-based Inversion
11
Memory-based Inversion
•ว$ธี�ก�รสร��งต้�ว Dictionary ท์��ม�โคืรงสร��งข้�อม�ลท์��เปล��ยนแปลงได� เช%น Trie , B-Tree หร�อ Hash Table ในก�รจั�ดเก�บเท์อมต้%�ง ๆ และเช��อมต้%อไปย�ง Node ท์��เก�บต้��แหน%งข้องเท์อมท์��ปร�กฏในเอกส�รแต้%ละช2ด เม��อเอกส�รท์�/งหมดได�ร�บก�รประมวลผลจันคืรบ ก�จัะได�ส%วนข้อง Dictionary ท์��จั�ดสร��งข้�/นโดยประกอบด�วยเท์อมต้%�ง ๆ พร�อมด�วยล�กสรช�/ไปย�งส%วนข้องต้��แหน%ง
12
•ว$ธี�ก�รข้อง Memory-based Inversion ใช�ปฏ$บ�ต้$ก�รท์�/งหมดอย�%บนหน%วยคืว�มจั��หล�กท์��ให�เวล�ในก�รประมวลผลท์�/งหมดม�คืว�มรวดเร�วม�ก ต้�/งแต้%ก�รสร��งส%วนข้อง Dictionary และส%วนข้อง Address ข้องต้��แหน%งท์��ปร�กฏ
13
Sort-based Inversion
•ว$ธี�น�/จัะสร��งส%วนข้อง Address เก�บในจั�นแม%เหล�กม�ว$ธี�ก�ร คื�อ ต้�ดแบ%งข้�อม�ลเป5นส%วน ๆ และน��ส%วนย%อยน�/นท์��ก�ร Sort ใน Main Memory และเข้�ยนกล�บไปย�งจั�นแม%เหล�ก เม��อคืรบท์2กส%วนย%อยแล�วให�ท์��ก�ร Merge ส%วนย%อยเหล%�น�/นจั�กช2ดเล�กรวมเป5นช2ดใหญ% และได�ผลล�พธี!ข้องส%วน Address ท์��จั�ดเก�บอย%�งเป5นระบบ
14
ว$ธี�ข้องก�ร Sort-based Inversion
15
การค้�นีหาก�รคื�นห�คื��ศั�พท์!ด�วยแฟ้�มข้�อม�ลผกผ�น ประกอบ
ด�วย 3 ข้�/นต้อนหล�ก ด�งน�/ • ก�รคื�นห�ในส%วนข้องพจัน�น2กรม (Dictionary
Search ) • ก�รด�งข้�อม�ลข้องต้��แหน%งท์��ปร�กฏ (Retrieval
of Occurrences )• ก�รจั�ดก�รก�บต้��แหน%งท์��ปร�กฏ
(Manipulation of Occurrences )
16
Dictionary Search
•ข้�/นแรกเป5นก�รคื�นห�ในส%วนพจัน�น2กรมก%อน Query โดยท์��วไปประกอบด�วยคื��ศั�พท์!เป5นคื��เด��ยวหร�อหล�ยคื�� หล�ยร�ปแบบ รวมท์�/ง Boolean Operators AND , OR , NOT ให�แยกออกเป5นคื��เด��ยว หร�อเท์อมเด��ยว และท์��ก�รคื�นห�ต้%อไปในส%วนน�/
17
•เช%น ก�รคื�นห�คื��ศั�พท์!ใน B-Tree สมม2ต้$คื��ศั�พท์!น�/นแท์นด�วยส�ญล�กษณ! Q เร$�มด�วยก�รคื�นห� Q ใน Root Node ห�กพบก�จัะต้�มล�กศัรท์��ช�/ไปย�งส%วนข้องต้��แหน%งและท์��ก�รคื�นต้%อไปในข้�/นท์��สอง ห�กไม%พบก�จัะเล�อกเส�นท์�งข้อง Tree ไปย�ง Node ต้%อไปท์�� Q แท์รกระหว%�งเท์อม 2 เท์อมใน Root สมม2ต้$ว%�เป5น a และ b โดยเข้�ยนคืว�มส��พ�นธี!ได�เป5น a < Q < b
18
•กระบวนก�รต้%อไปจัะเป5นก�รวนซ้ำ�/� โดยก�รคื�นห� Q ใน Node ใหม% จั�ก Node หน��งไปส�%อ�ก Node หน��ง จันกว%�จัะพบ หร�อไม%พบ เม��อถึ�งปล�ยท์�งข้อง Tree อ�นเป5นท์�งต้�น ห�กพบก�ต้�มล�กศัรท์��ช�/ไปย�งส%วนข้องต้��แหน%งและท์��ก�รคื�นต้%อไปในข้�/นท์��สอง ห�กไม%พบก�ร�ยง�นว%�ไม%พบ Q ใน Tree และส$/นส2ดก�รคื�นห�
19
Retrieval of Occurrences
•ในข้�/นต้อนน�/จัะว$�งต้�มล�กศัรท์��ช�/จั�กส%วนข้องพจัน�น2กรมไปย�งส%วนข้องต้��แหน%ง หร�อ Posting File เม��อพบคื��ศั�พท์!และช�/ม�ย�ง Posting File แล�ว ก�ด�งข้�อม�ลต้�ม Record ท์��ปร�กฏท์��ก�ร Decompression เพ��อให�ข้�อม�ลกล�บส�%สภ�พเด$ม นอกจั�กน�/ให�ท์��ก�ร Un-packing ข้�อม�ลเพ��อให�เป5นหน%วยอ$สระเฉพ�ะท์��บ%งบอกต้��แหน%งท์��ปร�กฏน�/น และเร�ยงล��ด�บเป5น Array เพ��อส%งต้%อไปย�งข้�/นท์��ส�ม ท์��ก�รประมวลผลต้%อไป
20
Manipulation of Occurrences
•Single-Word Search •Phrase Search •Proximity Search •Boolean Operation
21
3. Suffix Trees และ Suffix Arrays
เป5นก�รส�บคื�นในเอกส�รในอ�กร�ปแบบหน��ง โดยต้�วดรรชน�ไม%ได�สร��งจั�กคื��ส��คื�ญโดดๆแต้%สร��งจั�กต้�วเน�/อห�ข้องเอกส�รด�วยก�รมองเอกส�รท์�/งระบบเป5นต้�วอ�กข้ระท์��เร�ยงต้%อย�วอย%�งไม%จั��ก�ด และส%วนท์%องห�งข้องม�น (Suffix) แน%นอนท์��ส2ด ท์%อนห�งข้องแต้%ละ String ไม%ใช%เพ�ยง 1 ท์%อน แต้%ม�ก�นหล�ยๆท์%อน เช%น ห�กต้�ว String ม�คืว�มย�ว 13 ต้�วอ�กษร ท์%อนห�งข้องม�นย%อมจัะม�คืว�มย�วต้�/งแต้% 1 ต้�วอ�กษร จันถึ�ง 2,3, ….12 รวมท์�/ง 13 ต้�วอ�กษร
22
ต�วอย%างเช%นี
ให� String S = my university จัะม� suffix เป5นS[1] = my university s[8] =
ersityS[2] = y university s[9] = rsityS[3] = university s[10]
= sityS[4] = university s[11]
= ityS[5] = niversity s[12]
= tyS[6] = iversity s[13] = yS[7] = versity
23
•ข้�อด�ข้องก�รสร��งดรรชน�ในร�ปแบบน�/คื�อ ไม%ต้�องไปสนใจัโคืรงก�รข้องเน�/อห�ว%�จัะเป5นร�ปแบบใด และไม%ต้�องสนใจัต้�วคื��ส��คื�ญใดๆ แต้%ด�เป5นเพ�ยงร�ปแบบข้อง String และ Suffix ข้องม�นเป5นพ�/นฐ�น ก�รส�บคื�นก�ส�ม�รถึท์��ได�อย%�งรวดเร�วเช%นก�น
•ไม%ว%�จัะเป5นก�รคื�นคื��ส%วนหน�� (Prefix Searching)
•ก�รคื�นคื��ข้��งเคื�ยง (Proximity Searching) •ก�รคื�นคื��ท์��เป5นช%วง (Range Searching) และก�รคื��อ��นๆ อ�กหล�ยร�ปแบบก�ส�ม�รถึท์��ได�
24
โค้รงสร�างข้อง Suffix Tree •Suffix Tree เป5นโคืรงสร��งข้�อม�ลแบบต้�นไม�ท์��สร��งจั�กท์2กท์%อนห�งข้อง Text ซ้ำ��งถึ�อเป5นส�ยอ�กข้ระย�วต้ลอด
•คื2ณสมบ�ต้$ข้อง Suffix Tree ท์��ว%� Suffix Tree ส��หร�บ String S ท์��ม�คืว�มย�วเป5น n เป5นโคืรงสร��งข้�อม�ลแบบต้�นไม�ท์��ประกอบด�วย leaf Nodeจั��นวน n ซ้ำ��งถึ�กก��ก�บด�วยต้�วเลข้ 1 ถึ�ง n โดยแต้%ละก$�ง (Edge) ในต้�นไม%จัะถึ�กก��ก�บด�วย Substring ข้อง S โดยท์�� Suffix ท์�� i ข้อง S น�/น เข้�ยนได�เป5น s[i] คื�อเส�นท์�งจั�กห�ว (Root) ถึ�ง Leaf Node ท์�� I ซ้ำ��ง Node ท์��ไม%ใช%ห�ว และ Leaf Node จัะม�ก�รแต้กก$�งอย%�งน�อยได� 2 Nodes
25
•ก��หนดให� S = Mississippi เป5นส�ยอ�กข้ระย�ว 11ต้�วอ�กษร เร�จัะได� Suffix ข้องม�นด�งต้%อไปน�/
• S [1] = Mississippi• S [2] = ississippi• S [3] = ssissippi• S [4] = sissippi• S [5] = issippi• S [6] = ssippi• S [7] = sippi• S [8] = ippi• S [9] = ppi• S [10] = pi• S [11] = i
26
การสร�าง Suffix Trees
•ต้%อไปน�/ จัะแสดงก�รสร��ง Suffix Tree เป5นข้�/นต้อนท์�ละข้�/น ต้�มแนวคื$ดข้อง Ukkonen โดยใช�ต้�วอย%�งข้อง String = Mississippi
• เร$�มจั�กต้�ว Suffix Tree ท์��ว%�งเปล%�ก%อนและเต้$ม m จั�ก Mississippi เป5นอ�กษรแรก
Tree1Tree-- >---m…
27
•เพ$�มต้�วอ�กษรท์�� 2 ซ้ำ��งเป5น i ได�เป5น Suffixes ‘mi’ และ ‘i’
Tree2•Tree-- >|---mi…
||---i…
28
•ข้�/นต้อนต้%อไปเป5น ‘mis’ จัะได�ว%�• Tree3• Tree-- >|---mis…
||---is…||---s…
29
Ukkonen Algorithm•Ukkonen ได�น��เสนอก�รสร��ง Suffix Tree
อย%�งม�ประส$ท์ธี$ภ�พโดยใช�เวล�เป5นเช$งเส�น O(n) ว$ธี�เร$�มจั�กอ�กษรต้�วแรกข้อง String ในก�รสร��งต้�นไม� แล�วกว�ดจั�กซ้ำ��ยไปข้ว�ท์�ละต้�วอ�กษรจันกว%�จัะหมด String
30
•Pseudo-code ส��หร�บ Ukkonen Algorithm
• N = length of the stringST_Create Tree
Calls n times to SPA (Single Phase Algorithm). SPA:
Increase the variable e (virtual end of all leaves).
Calls SEA (Single Extension Algorithm) starting with the first extension that does not already exist in the tree and ending at the first extension that already exists.SEA:
Follow suffix link.Check if current suffix exists in the tree.If it does not – apply rule 2 and then create a
new suffix link.apply_rule_2:Create a new leaf and maybe a new internal
node as well.
create_node:Create a new node or a leaf.
31
การค้�นีหาในี Suffix Tree •ก�รคื�นห� Pattern หร�อ Substring ใน Suffix Trees ท์��ได�ง%�ยและรวดเร�ว โดยก�รท์%องไปใน Suffix Trees ท์�ได�ท์��ก�รสร��งข้�/น จั�ก Root ไปจันกระท์��งพบ pattern หร�อไม%พบ Pattern ใน Suffix Trees ซ้ำ��งจัะใช�เวล�ในก�รคื�นห�เป5น O(m log |∑|) เม��อ Substring ท์��ต้�องก�รคื�นห�ม�คืว�มย�ว m และประกอบด�วยอ�กข้ระ |∑| ต้�ว
32
•ส��หร�บก�รคื�นคื��ส��คื�ญหร�อ Pattern ใน Suffix Array จัะใช�ว$ธี�ก�รคื�นแบบ Binary Search ด�วยก�รผ%�คืร��งห�วท์��ยข้องข้อบเข้ต้ในก�รส�บคื�น เปร�ยบเท์�ยบ Pattern ก�บคื��ในช%องท์�งข้อง Array น�/น ห�กไม%พบก�จัะลดข้น�ดก�รคื�นเหล�อคืร��งหน��ง โดยเล�อกเอ�ส%วนห�วหร�อส%วนท์��ยข้�/นอย�%ก�บ Pattern ม�ก�รล��ด�บม�กหร�อน�อยกว%�คื��ในช%องกล�งข้อง Array น�/น ท์��ก�รเปร�ยบเท์�ยบเช%นน�/ต้%อไปจันกว%�จัะพบหร�อหมดช%อง Array ในก�รส�บคื�น
33
•Algorithm ข้องก�รคื�นห� Pattern หร�อ Substring ใน Suffix Trees เป5นด�งน�/
•Input : The tree, the string P, and the length of P.output : If the substring is found – returns the index of the starting position of the substring in the tree source string. If the substring is not found – returns ST_ERROR.
34
4. แฟ้�มข้�อม�ลลายเซ็�นี ( Signature Files ) •ว$ธี�ก�รสร��งดรรชน�ข้องแฟ้�มข้�อม�ลล�ยเซ้ำ�นได�ม�จั�กก�ร
คื$ดพ�/นฐ�นท์��ว%� ห�กเท์อมหร�อคื��ศั�พท์!ท์��ประกอบข้�/นเป5นเอกส�รน�/นม�รห�สเฉพ�ะข้องม�นในแต้%ละเท์อม เร�ยกรห�สเฉพ�ะน�/นว%�เป5นล�ยเซ้ำ�น น��ล�ยเซ้ำ�นข้องเท์อมต้%�ง ๆ ในเอกส�รม�รวมก�นเป5นก�รเฉพ�ะกล2%มอย%�งม�น�ยส��คื�ญ ก�จัะได�เป5นล�ยเซ้ำ�นข้องเอกส�รน�/น ๆ เม��อต้�องก�รคื�นห�เท์อมท์��สนใจั ก%อนอ��นต้�องแปลงเป5นล�ยเซ้ำ�นข้องเท์อมน�/นก%อน และท์��ก�รต้รวจัสอบจั�กล�ยเซ้ำ�นข้องเอกส�รท์2กฉบ�บท์��ต้�องก�รคื�นห�ให�ได�อย%�งรวดเร�ว เม��อพบก�ร Match ข้องล�ยเซ้ำ�น ก�เช��อได�ว%�น%�จัะม�เท์อมน�/นปร�กฏอย�%ในเอกส�ร ท์��ก�รต้รวจัสอบในร�ยละเอ�ยดข้องเอกส�รเป5นก�รย�นย�น
35
•ก�รก��หนดล�ยเซ้ำ�นส�ม�รถึท์��ได�ด�วยก�ร Hashing ต้�วอย%�งเอกส�รใน ห�วข้�อท์�� 1 ท์��ก�ร Hashing ข้องเท์อมจัะได�ผลล�พธี!ด�งน�/
term Hash String
colddayshotinit
likenineold
peaseporridge
potsomethe
1000 0000 0010 0100
0010 0100 0000 1000
0000 1010 0000 0000
0000 1001 0010 0000
0000 1000 1000 0010
0100 0010 0000 0001
0010 1000 0000 0100
1000 1000 0100 0000
0000 0101 0000 0001
0100 0100 0010 0000
0000 0010 0110 0000
0100 0100 0000 0001
1010 1000 0000 0000
36
Document
Text Signature
123456
Pease porridge hot , pease porridge cold.
Pease porridge in the pot.
Nine days old.Some like it hot,
some like it cold.Some like it in the
pot.Nine days old.
1101 1111 0010 0101
1110 1111 0110 0001
1010 1100 0100 1100
1100 1110 1010 0111
1110 1111 1110 0011
1010 1100 0100 1100
น��เท์อมท์��ม�ล�ยเซ้ำ�นในร�ป Hash String ม�รวมก�น ต้�มท์��ปร�กฏในเอกส�ร ด�วยว$ธี�ก�ร OR หร�อ Superimpose ก�จัะได�ล�ยเซ้ำ�นข้องเอกส�รแต้%ละฉบ�บเป5นด�งร�ป
37
•ข้�อด�ข้องแฟ้�มข้�อม�ลล�ยเซ้ำ�นคื�อก�รสร��งดรรชน�ข้องเอกส�ร ไม%ได�ม�คืว�มสล�บซ้ำ�บซ้ำ�อนม�กน�ก เอกส�รแต้%ละฉบ�บก�จัะได�ต้�วล�ยเซ้ำ�นจั�กก�ร OR ข้องบ$ท์ ข้องเท์อมท์��ประกอบม�นก�รปฏ$บ�ต้$ก�รท์��ได�รวดเร�วม�กเพร�ะเป5นก�รท์��บน Main Memory ด�วยคื��ส��ง OR ข้อง Bitwise คืว�มย�วข้อง Word ในหน%วยคืว�มจั��จัะเป5น 32 หร�อ 64 Bits ก�ท์��ได�เร�วใน 1 คื��ส��ง คืว�มเร�วในก�รประมวลผลในกรณ�น�/ เป5นเช$งเส�นต้รงโดยท์��ไม%ต้�องม�ก�ร Sort หร�อ Merge ข้อแฟ้�มข้�อม�ลดรรชน�แต้%อย%�งไร
38
การสร�าง Signature File •ก�รสร��งแฟ้�มข้�อม�ลชน$ดน�/ไม%ม�คืว�มสล�บซ้ำ�บซ้ำ�อน โดย
ข้�อคืว�มในเอกส�รจัะถึ�กต้�ดแบ%งเป5น Block ซ้ำ��งแต้%ละ Block ก�จัะสร��งต้�วล�ยเซ้ำ�นข้องม�นด�วยก�รรวมข้องเท์อมจั�กก�ร OR ข้อง Bitwise
•อ�กว$ธี�หน��งในก�รสร��งแฟ้�มข้�อม�ลล�ยเซ้ำ�นเป5นก�รจั�ดเก�บแต้%ละบ$ท์ข้องล�ยเซ้ำ�นโดยเฉพ�ะเร�ยกประเภท์น�/ว%� Bitsliced Signature File โดยแฟ้�มข้�อม�ลท์��หน��งเก�บเฉพ�ะบ$ท์ท์��หน��งข้องล�ยเซ้ำ�นท์�/งหมด แฟ้�มข้�อม�ลช2ดท์��สองเก�บเฉพ�ะบ$ท์ท์��สอง และต้%อ ๆ ไป ห�กล�ยเซ้ำ�นข้องเท์อมเป5น 32 บ$ท์ ก�จัะม�แฟ้�ม ช2ดรวม 32 แฟ้�ม
39
•ก�รคื�นห�เท์อมท์��สนใจัจัะต้รวจัเช�คืเฉพ�ะบ$ท์ท์��เป5น 1 ข้องล�ยเซ้ำ�นข้องเท์อมน�/น ก�บแฟ้�มช2ดข้��งต้�นก�ท์��ได� Text Block ท์��สอดคืล�อง ( Match ) เป5นผลล�พธี! ท์��ก�รต้รวจัสอบในร�ยละเอ�ยดเป5นก�รย�นย�น
40
Document
Text Signature
123456
Pease porridge hot , pease porridge cold.Pease porridge in the pot.Nine days old.Some like it hot, some like it cold.Some like it in the pot.Nine days old.
1001 1111 0010 01011110 1111 0110 00011010 1100 0100 11001100 1110 1010 01111110 1111 1110 00111010 1100 0100 1100
11111111
41
111111
Position Slice
Position Slice
Position Slice
Position Slice
1 111111 2 110110 3 011011 4 000000
5 111111 6 111111 7 110110 8 110010
9 000110 10 011011 11 110110 12 000000
13 001001 14 101101 15 000110 16 110110
42
5. การค้�นีหาตามล�าด�บ ( Sequential Searching )
•ห�วข้�อน�/จัะเร$�มด�วยว$ธี�ก�ร Brute Force ซ้ำ��งเป5นก�รกว�ดห�ท์��ง%�ยท์��ส2ด แต้%ใช�เวล�ม�กท์��ส2ดอ�นจัะใช�เป5นก�รเปร�ยบเท์�ยบก�บว$ธี�ก�รอ��นในห�วข้�อน�/ ได�แก% KMP , BM และ Shift-Or Algorithms
43
Brute Force•ว$ธี�ก�รข้อง Brute Force เป5นว$ธี�ท์��ง%�ยท์��ส2ดใน
บรรด�ว$ธี�ก�รท์�/งหมดข้องก�รกว�ดห�เท์อมในเอกส�ร ด�วยก�รเท์�ยบเคื�ยงอ�กข้ระในเท์อมน�/นก�บอ�กข้ระในเอกส�รจั�กต้�นชนปล�ย ณ ต้��แหน%งหน��งๆ ผลท์��ได�คื�อสอดคืล�อง ( Match ) หร�อไม%สอดคืล�อง ( Mis-
Match ) ณ. ต้��แหน%งน�/นในเอกส�ร เม��อร�ยง�นผลท์��ได� ข้� /นต้%อม�ก�จัะเล��อนต้��แหน%งก�รเท์�ยบเคื�ยงไปอ�กต้��แหน%งท์��ก�รต้รวจัสอบคืว�มสอดคืล�อง ร�ยง�นผลและวนซ้ำ�/�ด�วยก�รเล��อนไปอ�กหน��งต้��แหน%ง จันกว%�จัะส$/นส2ดข้องเอกส�ร
44
Brute Force Algorithm
•Algorithm ข้องก�รคื�นห�ด�วยว$ธี� Brute Force เป5นด�งต้%อไปน�/
Input : P and T the pattern and text string; m, the length of P and m ≠ 0 .
Output : The return value is the index T where a copy of P begins, or -1 if no match for P is found
45
Example : Brute Force Algorithm
1 1 1 1 2 3 4 5 6 7 8 9 0
1 2 Text : a b c d a b a b c b
a dPattern : a b a b c b a Move a
a a b a b c b a
a b a b c bก�รคื�นห�ด�วยว$ธี� Brute Force ข้อง Pattern ‘a b c d’ ใน text
46
Knuth-Morris-Pratt Algorithm
•เม��อม�ก�รเท์�ยบเคื�ยงข้อง Pattern ก�บ text ในต้��แหน%งต้%อต้��แหน%งจั�กซ้ำ��ยไปข้ว� และปร�กฏว%�ไม%สอดคืล�องก�นในต้��แหน%งไดท์��ถึ�ดม� ให�เล��อน Pattern น�/น ไปท์�งข้ว� ให�ได�ระยะไกลท์��ส2ด โดยคื��นวณจั�กต้�วอ�กข้ระใน Pattern น�/น เป5นส��คื�ญ
47
KMP Algorithm
•KMP Algorithm ประกอบด�วย 2 ส%วนหล�กด�งน�/
KMP Flow Chart Building KMP Search Algorithm
48
Exemple : KMP Algorithm Move a b a b a b a b c
dPattern : a b a b a b c
b
#Text : … a b a b a b x
การค้�นีหาด�วยว+ธี KMP ข้อง Pattern ‘abababcb’ ในี text
49
Boyer-Moore Algorithm
•ว$ธี�ก�รคื�นห�เท์อมในเอกส�รด�วยว�ก�รข้อง Boyer-Moore (BM) น�/นจัะเท์�ยบเคื�ยงอ�กข้ระในเท์อมท์��เร�ยกว%� pattern ก�บอ�กข้ระ ในเอกส�รท์��เร�ยกว%� Text จั�กข้ว�ไปซ้ำ��ย ว$ธี�ก�รแบบ BM ม�ประส$ท์ธี$ภ�พในก�รคื�นห�เท์อมได�อย%�งรวดเร�วม�ก เน��องจั�กไม%ต้�องท์��ก�รเท์�ยบเคื�ยงท์2กต้�วอ�กษรใน pattern ก�บ Text แต้%จัะท์��ก�รเท์�ยบเคื�ยงอ�กษรต้�วข้ว�ส2ดข้อง pattern ก�บ Text ก%อน ห�กไม%สอดคืล�องก�นก�จัะท์��ก�รเล��อนต้��แหน%งไปท์�งข้ว� อย%�งน�อย 1ต้��แหน%งและจัะส�ม�รถึเล��อนได�ไกลส2ดเท์%�ก�บ Search คืว�มย�วข้อง Pattern น�/นได�
50
•ส�ระส��คื�ญข้อง BM Search อย�%ท์��ก�รเล��อนต้��แหน%งไปท์�งข้ว�น�/นจัะไปได�ไกลเท์%�ได ซ้ำ��งจัะเป5นไปได�ใน 2 กรณ� คื�อ
•1. Bad –character Shift•2. Good-suffix Shift
51
Bad –character Shift
ว$ธี�น�/ใช�ในกรณ�เม��อม�ก�รเท์�ยบเคื�ยงแล�ว พบก�ร Mis-Match ข้องต้�วอ�กษรใน text ก�บใน pattern ให�เล��อนต้�ว pattern ไปท์�งข้ว�จันกว%�จัะพบต้�วอ�กษรใน pattern ให�ต้รงก�บต้��แหน%งต้�วอ�กษรท์��เก$ดก�ร Mis-Match ก%อนหน��น�/น
52
•ส��หร�บก�ร Preprocessing ข้อง Bad –character ด�งน�/
•Input : P, a string of character; m the length of P and m ≠ 0 .
• Output : occ, the array of occurrence, define for indexes 1 ,…,m.
•void bmInitocc(char[ ] P, int m, int [ ] occ)
• int i;• for (i = 0; i < ALPHABET_SIZE; i++)• occ[i] = -1;• for(i = 0;i<m;i++)• occ[pi] = m – I;
53
0 1 2 3 4 5 6 7 8 9 …..
Text a b b a b a b a c b a
Pattern b a b a c
b a b a c
Shift
การค้�นีหาด�วยว+ธี BM ในีกรณี Bad –character Shift
54
Good-suffix Shift
•บ�งคืร�/ง ก�รเล��อนข้อง Bad –character Shift อ�จัไม%เป5นผล เพร�ะเก$ดก�รเล��อนท์��ม�คื%�ต้$ดลบ ซ้ำ��งเป5นก�รผ$ดหล�กก�ร ด�งน�/น ในกรณ�น�/ก�จัะเล��อนไปให�ไกลส2ดต้�มก�รเร�ยงต้�วอ�กษรใน Pattern จัะเหม�ะสมกว%� ว$ธี�น�/เร�ยกว%� Good-suffix Shift ซ้ำ��งเป5นไปได�ใน 2 กรณ� ด�/งน�/
55
•กรณ�ท์��1 เม��อพบส%วนข้อง Pattern (Matching Suffix ) ณ. ต้��แหน%งใด ๆ ในText ก�จัะท์��ก�รเล��อนต้��แหน%งข้อง Pattern ไปท์�งข้ว�
V
VV
V V
Text
Pattern
Shift
ก�รเล��อนต้��แหน%งข้อง Pattern เม."อพบ Matching Suffix ที่"ต�าแหนี%งใด ๆ ในี Text
56
•ก�ร Preprocessing Algorithm ข้อง Boyer-Moore ส��หร�บ Good-suffix Shift ในกรณ�ท์�� 1เป5นด�งน�/
• Input : P, a string of character; m the length of P and m ≠ 0 .
• Output : s, the array of good-suffix Shift, define for indexes 1 ,…,m.
•void bm Preprocessl(char[ ] P, int m, int [ ] f)
• int i = m, j = m – 1;• f[i] = j;•while (I > 0)•while (j <=m && p[i-1] != p[i-1])• if (s[i]==0)• s[i] = j – i;• j = f[j];• i--;• j--;• f[i] = j;
57
•กรณ�ท์��2 เม��อพบส%วนข้อง Pattern (Part of Matching Suffix) ท์��ต้��แหน%งเร$�มต้�นข้อง Text ก�จัะเล��อนต้��แหน%งข้อง Pattern ให�ได�ไกลส�งส2ด
Text
Pattern
shift
V
Compare
U a U
U a U
ก�รเล��อนต้��แหน%งข้อง Patternเม��อพบ Part of Matching Suffix ท์��ต้��แหน%งเร$�มต้�นข้อง text
58
• ก�ร Preprocessing Algorithm ข้อง Boyer-Moore ส��หร�บ Good-suffix Shift ในกรณ�ท์�� 2 เป5นด�งน�/
• Input : P, a string of character; m the length of P and m ≠ 0 .
• Output : s, the array of good-suffix Shift, define for indexes 1 ,…,m.
• void bmPreprocessl(char[ ] P, int m, int [ ] f)• int i, j;• j = f[0];• for(i = 0;i<m;i++)• if (s[i] == 0)• s[i] == j;• if ( i== j)• j = f[j];
59
Shift-Or Algorithm •ว$ธี�ก�ร Shift-Or ม�ข้� /นต้อน Preprocessing ท์��สร��งต้�ร�งอย%�งง%�ย ๆ ไม%ซ้ำ�บซ้ำ�อนย2%งย�กก�รคื�นห� Pattern ก�ท์��ก�รเท์�ยบเคื�ยงด�วย Bitwise Operation ข้อง Shift และ Or เท์%�น�/น จั�งส�ม�รถึปฏ$บ�ต้$บน Hardwaer ได�อย%�งรวดเร�ว และเป5น Real Time โดยไม%ม� Delay นอกจั�กน�/ไม%ม�ก�รเก�บ Buffer จั�งเป5นคื2ณสมบ�ต้$หล�กท์��โด%ดเด%นข้องว$ธี�น�/
60
•Shift-Or Preprocessing เป5นด�งน�/•int preSO(char[ ] P, int m, unsigned
int [ ] S)•unsigned int j, lim;• int i;• for (i = 0; i < ALPHABET_SIZE; i++)•S[i] = ~0;•for (lim = I = 0, j = 1; I < m; i++,j<<=1)•S[Pi] & = ~j;• lim |= j; • lim = ~(lim >> 1);•reture(lim);
61
Hashing Algorithm
•เร��อง Hashing Algorithm เป5นว$ธี�ท์��ท์��ก�รแปลงคื��ศั�พท์!ในเอกส�รเป5นต้�วเลข้ด�วยว$ธี�ก�รใดว$ธี�ก�รหน��ง จั�กต้�วเลข้น�/นก�จัะเป5นดรรชน�ในก�รคื�นห� คืว�มส��คื�ญจั�งอย�%ท์��เล�อกห� Hash Function ท์��ม�ประส$ท์ธี$ภ�พ เพ��อให�ต้�วเลข้หล�งก�รแปลงคื��ส��คื�ญเหล%�น�/น ม�ก�รกระจั�ยต้�วอย%�งท์��วถึ�ง ไม%กระจั2กต้�วจันเก$ดกรณ�ชนก�น ( Collision )
62
ต้�วอย%�งข้อง Hash Function แบบพ�/นฐ�น เป5นด�งน�/
•1. แบบอ�ศั�ยก�รห�ร (Modulo) สม�ก�รท์�งคืณ$ต้ศั�สต้ร!ข้องว$ธี�น�/เข้�ยนอย�%
ในร�ป H(k) = k mod m + j• โดย k เป5นคื��ส��คื�ญท์��ต้�องเป5นต้�วเลข้ หร�อถึ��เป5น
String ข้องต้�วอ�กษรก�จั��เป5นต้�องแปลงเป5นต้�วเลข้ก%อน
• m เป5น เลข้จั��นวนเต้�มท์��สอดคืล�องก�บข้น�ดข้อง Bucket
•ส%วน j เป5นคื%�คืงท์��เพ��อปร�บคื%�ต้�วเลข้ในต้��แหน%งท์��เร$�มต้�น
63
2. แบบพ�บ (Folding)
•น$ยมใช�ในกรณ�ท์��ม�คื��ส��คื�ญเป5นต้�วเลข้ข้น�ดใหญ%กระจั�ยหร�อเป5นต้�วอ�กษรเช%น ช��อย�วๆ แต้%ต้�ร�ง Hash Table ม�ข้น�ดเล�กกว%�ม�ก ว$ธี�ก�รห�ต้�วเลข้ท์��อย�%ในต้�ร�งข้น�ด 10,000 ช%อง หร�อ จั�ก 0-9999 ด�วย Hash Function ด�งน�/
•H(k) = ((k[0] + k[7]) mod 10) x 1000 +•((k[1] + k[6]) mod 10) x 100 +•((k[2] + k[5]) mod 10) x 10 +•((k[3] + k[4]) mod 10)
64
3. การว+เค้ราะห1ต�าแหนี%งต�วเลข้ (Digit Analysis)
•เป5นก�รน��บ�งต้�วเลข้หร�อต้�วอ�กษรในคื��ส��คื�ญม�ใช�เป5นคื%� Key หร�อเพ��อเป5นคื��ท์��ใช�ในก�รคื��นวณ Adderss ข้อง Hash Table อ�กต้%อหน��ง โดยจัะเล�อกเพ�ยงคื%�บ�งต้��แหน%งใน Key ม�ใช� เพ��อให�ได�ต้�วเลข้ท์��ม�ก�รกระจั�ยอย%�งสม���เสมอม�กท์��ส2ด
65
การที่�า Hashing
•ก�รก��หนดคื%�เร$�มต้�น (Initialization)
•ก�รเพ$�มข้�อม�ล (Insertion)•ก�รด�งข้�อม�ล (Retrieval)•ก�รลบข้�อม�ล (Deletion)
66
ว+ธีการที่�า Hashing
•ว$ธี�ก�รท์�� Hashing ท์��น$ยมท์��วไป ประกอบด�วยว$ธี�ก�รหล�ก 2ว$ธี� ด�งน�/
•1. Separate Chaining•2. Open Adderssing
67
การที่�า Rehashing
•ก�รท์�� Rehashing หม�ยถึ�งก�รท์�� Hash คื��ส��คื�ญในต้�ร�งท์�/งหมดท์��เหล�ออย�%หล�งก�ร Insert และ Delete เป5นรอบใหม%อ�กคืร�/งหน��ง
•ก�รท์�� Rehashing น�/นส�ม�รถึก��หนดในโปรแกรมเพ��อให�ข้บวนก�รน�/ท์��ง�นได�อย%�งอ�ต้โนม�ต้$เม��อถึ�งเวล�ข้องต้�ร�งเข้��ใกล�เต้�ม
Uninformed Search Strategies
• Breadth-first search (FIFO-queue)• Depth-first search• Iterative deepening search• Bidirectional search• Uniform-cost search
• Also known as blind search
68
Breadth-First Search (FIFO- queue)
•Every time a new state is reached▫New states put on the bottom of the agenda
•When state “NA” is reached▫New states “NAD”, “NAN”, “NAA” added to
bottom▫These get explored later (possibly much later)
•Graph analogy▫Each node of depth d is fully expanded before
any node of depth d+1 is looked at
69
Breadth-First Search (FIFO-queue)
• Branching rate▫ Average number of edges coming from a node (3 above)
• Uniform Search▫ Every node has same number of branches (as above)
70
Breadth-first search
Expand shallowest unexpanded node••Implementation:• fringe is a FIFO queue, i.e., new successors
go at end▫
71
Breadth-first search
Expand shallowest unexpanded node•Implementation:• fringe is a FIFO queue, i.e., new successors
go at end▫
72
Breadth-first search
Expand shallowest unexpanded node•Implementation:• fringe is a FIFO queue, i.e., new successors
go at end▫
73
Breadth-first search
Expand shallowest unexpanded node•Implementation:• fringe is a FIFO queue, i.e., new successors
go at end▫
74
Depth-First Search (LIFO-queue)
• Same as breadth-first search▫ But new states are put at the top of agenda
• Graph analogy▫ Expand deepest and leftmost node next
• But search can go on indefinitely down one path▫ D, DD, DDD, DDDD, DDDDD, …
• One solution to impose a depth limit on the search ท์�งออกหน��งท์��จัะก��หนดข้อบเข้ต้คืว�มล�กในก�รคื�นห�▫ Sometimes the limit is not required
Branches end naturally (i.e. cannot be expanded) ต้�วส�ข้�จัะส$/นส2ดเอง
75
Depth-First Search (Depth Limit 4)
76
Depth-first search
Expand deepest unexpanded node••Implementation:• fringe = LIFO queue, i.e., put successors at
front▫
77
Depth-first search
Expand deepest unexpanded node••Implementation:• fringe = LIFO queue, i.e., put successors at
front▫
78
Depth-first search
Expand deepest unexpanded node••Implementation:• fringe = LIFO queue, i.e., put successors at
front▫
79
Depth-first search
Expand deepest unexpanded node••Implementation:• fringe = LIFO queue, i.e., put successors at
front▫
80
Depth-first search
Expand deepest unexpanded node••Implementation:• fringe = LIFO queue, i.e., put successors at
front▫
81
Depth-first search
Expand deepest unexpanded node••Implementation:• fringe = LIFO queue, i.e., put successors at
front▫
82
Depth-first search
Expand deepest unexpanded node••Implementation:• fringe = LIFO queue, i.e., put successors at
front▫
83
Depth-first search
Expand deepest unexpanded node••Implementation:• fringe = LIFO queue, i.e., put successors at
front▫
84
Depth-first search
Expand deepest unexpanded node••Implementation:• fringe = LIFO queue, i.e., put successors at
front▫
85
Depth-first search
Expand deepest unexpanded node••Implementation:• fringe = LIFO queue, i.e., put successors at
front▫
86
Depth-first search
Expand deepest unexpanded node••Implementation:• fringe = LIFO queue, i.e., put successors at
front▫
87
Depth-first search
Expand deepest unexpanded node••Implementation:• fringe = LIFO queue, i.e., put successors at
front▫
88
Depth-limited search= depth-first search with depth limit li.e., nodes at depth l have no successors
• Recursive implementation:
89
State- or Action-Based Definition?
• Alternative ways to define strategies
• Agenda stores (state, action) rather than state▫ Records “actions to perform”▫ Not “nodes expanded”
• Only performs necessary actions
• Changes node order• Textbook is state-oriented
▫ Online notes action-oriented
90
Depth- v. Breadth-First Search
•Suppose branching rate b•Breadth-first
▫Complete (guaranteed to find solution)▫Requires a lot of memory
At depth d needs to remember up to bd-1 states•Depth-first
▫Not complete because of indefinite paths or depth limit เน��องจั�กเส�นท์�งไม% จั��ก�ด หร�อ จั��ก�ด คืว�มล�ก
▫But is memory efficient หน%วยคืว�มจั��ท์��ม�ประส$ท์ธี$ภ�พ Only needs to remember up to b*d states
91
Iterative Deepening Search• Idea: do repeated depth first searches
▫ Increasing the depth limit by one every time▫ DFS to depth 1, DFS to depth 2, etc.▫ Completely re-do the previous search each time
• Most DFS effort is in expanding last line of the tree▫ e.g. to depth five, branching rate of 10
DFS: 111,111 states, IDS: 123,456 states Repetition of only 11%
• Combines best of BFS and DFS▫ Complete and memory efficient▫ But slower than either
92
Iterative deepening search
93
Iterative deepening search l =0
94
Iterative deepening search l =1
95
Iterative deepening search l =2
96
97