Lecturers Boontee Kruatrachue Room no 913 Kritawan Siriboon
Lecturers : Boontee Kruatrachue Room no. 913 Kritawan Siriboon Room no. 913 Text : Data Structures & Algorithm Analysis in C, C++, … Mark Allen Weiss, Addison Wesley รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ Searching KMITL 01076249 Data Structures & Algorithms : Tree 21
Searching Terminology • Search, Record, Table/File • Key 5601 Ann 85 – Internal Key (Embedded Key) – External Key 1 Rec Ann Ben Dan record . . . Ron . . . Zu . . . 5603 Dan 50 . . . . table/file Key 2 Rec 5601 Ann 85 Harry. . . Elka 5602 Ben 93 35 . . . 38 . . . 50 . . . 85 5602 Ben 93 86 . . . 93 95 . . . • Successful Search/Unsuccessful Search • Internal Search / External Search รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 2
Searching Search Unordered Table Search Ordered Table Array • Sentinel Search • Move to Front • Transposition - Sequential Search (Linear Search) - Hashing เครอตราช - Sequential Search (Linear Search) - Index Sequential Search - B-Tree - Binary Search รศ. ดร. บญธร List & Tree Search รศ. กฤตวน ศรบรณ -Binary Search Tree - AVL (Hight Balanced) Tree - Heap KMITL 01076249 Data Structures & Algorithms : Tree 2
Searching Unorder List rec 19 1 56 2 2 3 found = false; //Typical version i= 1; loop (i<=n) and (not found) if (key == rec[i]. key) { found. Index = i; found = true; else i= i +1; end if end loop รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ 7 4 25 5 18 6 . . . 40 n pos = n+1; //More efficient version i = 1; loop (i<>pos) if (key == rec[i]. key) pos = i; else i = i+1; end if endloop if (i<=n) search = i; else search = 0; end if KMITL 01076249 Data Structures & Algorithms : Tree 2
Sentinel Search rec 19 56 2 7 25 18 . . . 40 1 2 3 4 5 6 . . . n rec[n+1] = key; //adding sentinel i = 1; loop (key <> rec[i]. key) i = i+1; end loop O(n) Best Case Worst Case Avg Case Sentinel 1 n (n+1)/2 if (i<n) search = i; else search = 0; end if rec 19 56 2 7 25 18 40 Sentinel รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 2
Searching Unordered Table • Sentinel Search rec 19 19 56 2 7 2 56 25 7 18 . . . 25 40 Sentinel 18 40 Sentinel • Move to Front rec 19 56 2 7 25 18 40 rec 7 56 2 19 25 18 40 • Transposition รศ. ดร. บญธร เครอตราช rec 19 56 2 7 25 18. . . 40 rec 19 56 7 2 25 18. . . 40 รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 2
Searching Search Ordered Table Search Unordered Table Array • Sentinel Search • Move to Front • Transposition - Sequential Search (Linear Search) - Hashing เครอตราช - Sequential Search (Linear Search) - Index Sequential Search - B-Tree - Binary Search รศ. ดร. บญธร List & Tree Search -Binary Search Tree - AVL (Hight Balanced) Tree - Heap รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 29
Sequential Search (Linear Search) 2 5 7 8 10 12 15 18 20 22 i h p O(n) Best Case 1 Worst Case n Avg Case (n+1)/2 รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 210
Searching Search Ordered Table Search Unordered Table Array • Sentinel Search • Move to Front • Transposition - Sequential Search (Linear Search) - Hashing เครอตราช - Sequential Search (Linear Search) - Index Sequential Search - B-Tree - Binary Search รศ. ดร. บญธร List & Tree Search -Binary Search Tree - AVL (Hight Balanced) Tree - Heap รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 211
Index Sequential Search B-Tree Order 5 รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 212
Searching Search Ordered Table Search Unordered Table Array • Sentinel Search • Move to Front • Transposition - Sequential Search (Linear Search) - Hashing เครอตราช - Sequential Search (Linear Search) - Index Sequential Search - B-Tree - Binary Search รศ. ดร. บญธร List & Tree Search -Binary Search Tree - AVL (Hight Balanced) Tree - Heap รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 213
Binary Search 0 a 2 1 5 2 7 3 8 4 5 6 1 binary. Search (sequential array) low = 1, high = data. Size r loop ( low <= high) 10 mid = ( low + high ) / 2 if( a[ mid ] < target ) 5 18 low = mid + 1 else if( a[ mid ] > x ) high = mid - 1 2 7 12 20 else low = hight + 1 end if 8 15 22 end loop if (target == a[mid] return mid else return -1 O(log 2 n) end if เครอตราช 8 9 10 12 15 18 20 22 2 รศ. ดร. บญธร 7 รศ. กฤตวน ศรบรณ binary. Search (dynamic) p = q = root loop ( p is not null) if( target < p(data)) p = p->left; else if ( target > p(data)) p = p->right; else q = p p = null end if end loop if (target == q->data return q else return null end if KMITL 01076249 Data Structures & Algorithms : Tree 214
Searching Search Ordered Table Search Unordered Table Array • Sentinel Search • Move to Front • Transposition - Sequential Search (Linear Search) - Hashing เครอตราช - Sequential Search (Linear Search) - Index Sequential Search - B-Tree - Binary Search รศ. ดร. บญธร List & Tree Search -Binary Search Tree - AVL (Hight Balanced) Tree - Heap รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 215
Insertion Re-balancing in AVL (Height-Balanced) Tree h = 4 h = 3 x h = 2 40 - h = 1 10 - 5 15 30 h = 1 / 25 35 43 46 65 - 55 - / 70 h = 1 45 - 80 60 Long Side 50 h = 0 20 - 1 st unbalanced - 90 85 75 - Short Side / 87 - 95 - Hight diff = 2 x // / h = 1 40 20 52 57 30 - 15 - - 65 - - 55 / 85 75 - 87 • Causes some node(s) unbalanced (height diff = 2) only up the inserted path. – x = 1 st unbalanced node • Need rebalancing the (blue) shaded subtree. Rebalancing does not change : h = 4 z / 40 h = 2 y 20 10 60 h = 1 70 50 30 - 15 x h = 1 - h = 1 / h = 0 80 45 - 90 85 55 - 65 - 87 - - - / 75 - Height balanced again→ AVL เครอตราช รศ. กฤตวน ศรบรณ - - Short Side h = 3 - รศ. ดร. บญธร 95 Re-balance Inserting AVL in a long side. – height of the subtree – → ie. Nor height of root. – → Only some part of the shaded subtree’s balance facters need changing. 90 70 50 45 // h = 1 Height Unbalanced → no longer AVL ! Long Side • 60 h = 3 h = 2 10 80 h = 4 h = 1 / h = 0 Insert 15 h = 5 KMITL 01076249 Data Structures & Algorithms : Tree 216 95 -
Searching Search Ordered Table Search Unordered Table Array • Sentinel Search • Move to Front • Transposition - Sequential Search (Linear Search) - Hashing เครอตราช - Sequential Search (Linear Search) - Index Sequential Search - B-Tree - Binary Search รศ. ดร. บญธร List & Tree Search -Binary Search Tree - AVL (Hight Balanced) Tree - Heap รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 217
Heap : Delete Min Delete 13 13 31 ? 1 2 20 19 21 9 10 8 9 2 3 31 ? 68 7 8 14 16 20 21 19 68 65 26 32 31 31 ? 3 65 4 26 7 8 8 9 21 19 32 5 31 5 6 4 9 10 รศ. ดร. บญธร 2 3 10 11 เครอตราช 26 7 8 21 19 32 5 31 5 6 4 3 65 4 8 9 temp 16 0 1 1 2 9 10 10 11 รศ. กฤตวน ศรบรณ heap. • Find place for 31 : reheap. • Try the hole, if not, perlocate 31 down, take the hole’s smaller son up. • Repeat perlocate 31 down to find its place. • Put the deleting data 13 at the last node place in the same array is called in place sort, makes decending order. • Now 13 is out of heap. Delete 13 13 14 20 68 6 7 5 6 • Delete Min = c log 2 n • Delete min n times. • O(n log 2 n) temp 16 0 1 68 10 11 9 10 8 9 Delete 13 13 14 19 21 2 3 this slot is out of heap 0 1 2 3 4 5 6 7 8 9 10 heap size shorter =1 1 2 4 3 4 65 26 32 5 31 10 11 20 0 1 20 6 7 5 6 4 3 4 65 26 32 5 31 7 8 14 temp 16 0 1 14 Delete 13 • Here is the min heap. 13 • Delete root gets min 13, hole at root. temp • Heap size shorter 1: last node. 16 • Last node data, 31, must be in the 2 3 68 6 7 Delete 13 13 14 0 1 20 26 1 2 21 4 3 65 4 31 32 5 13 10 9 7 8 11 10 8 9 temp 16 19 5 6 2 3 68 6 7 14 20 16 26 21 19 68 65 31 32 13 0 1 2 3 4 5 6 7 8 9 10 heap size shorter =1 KMITL 01076249 Data Structures & Algorithms : Tree 218
Searching Search Ordered Table Search Unordered Table Array • Sentinel Search • Move to Front • Transposition - Sequential Search (Linear Search) - Hashing เครอตราช - Sequential Search (Linear Search) - Index Sequential Search - B-Tree - Binary Search รศ. ดร. บญธร List & Tree Search -Binary Search Tree - AVL (Hight Balanced) Tree - Heap รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 219
Inserting & Retriving Data key = index → สามารถ map ไดโดยตรง directly hf(key) = key • รศ. ดร. บญธร จะ retrieve record key = 2 – hash 2 with ดวย hashing function ได 2 hf(2) = 2 – ไปหา record ใน slot index = 2 เครอตราช รศ. กฤตวน ศรบรณ 2 1 2 2 3. . . • จะ insert record key = 2 – hash key 2 ดวย hashing function ได hf(2) = 2 – Insert record ใน slot index = 2 key Other. Data CPU . . . ถา Hash table KMITL 01076249 Data Structures & Algorithms : Tree 2
Mapping Techniques Subtraction • ถาทก id นำหนาดวย 54011 hf(key) = key-54 01 1000 hf(54 01 1004) = 4 hf(54 01 1037) = 37 hf(54 01 1576) = 576 Extraction เอาแคบางสวนของ key 54 01 1576 -> 4156 Summation, Division, . . . รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ – Folding. • แบง key เปนสวนๆ • summation(/subtraction/. . . ) สวนนนๆ 54091576 = 540 +915+76 = 1531 = 531 //array size = 1000 • Modulo เพอใหอยใน range • Midsquare key 2 หรอ part_of_key 2 เอาตรงกลางมา 54011576 -> 576*576 = 331776 -> 177 KMITL 01076249 Data Structures & Algorithms : Tree 2
Mapping String 1 - 2 Mapping String 1 string sum all ASCII chars int string (k[0] + 27 k[1] + 272 k[2]) % Tablesize int Hash. String 1 k = address of first char Hash. Val = 0; loop (*k != null) Hash. Val += *k++ endloop return Hash. Val mod Table. Size Hash. String 2 k = address of first char hv = (k[0]+27* k[1]+729* k[2]) % Tablesize; if (hv<0) // 27=26+blank return hv+= Tablesize else return hv end if • ปญหา : table ใหญ 10, 000 => กระจาย distribute ไมด • ASCII 0 -127, 8 chars => 127*8 =>[0 -1, 016] ถาคด 26 ตวไมนบ blank คดเฉพาะ 3 3 chars แรก 26 = 17, 576 combinations ==> แตจรงๆแลว Eng. ไม random ดตาม dic. ได 2, 851 combinations = 28%) ของ 10, 000) ดงนน table ใหญ ใชไมเตม กระจายไมด รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 2
Mapping String 3 Horner’s rule : Polynomial of 32 string (324 k[0]+323 k[1]+322 k[2]+32 k[3]+k[4]) % Tablesize Hash. String 3 k = address of the rightmost char hv = 0; loop (*k != null) hv = (hv<<5) + *k++ end loop hv = hv % Tablesize return hv • • • รศ. ดร. บญธร int // hv<<1 = hv*2, // hv<<5=hv*25=hv*32 Mapping String 3 Horner’s rule : Polynomial of 32 if keysize = 5 iteration #1 : 32*0 + K[4] = K[4] iteration #2 : 32*K[4] + K[3] iteration #3 : 322*K[4] + 32*K[3] + K[2] iteration #4 : 323*K[4] + 322*K[3] + 32*K[2] + K[1] iteration #4 : 324*K[4] + 323*K[3] + 322*K[2] + 32*K[1] + K[0] เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 2
Mapping String • string ยาวๆ ใชเวลามาก ==> truncation • 438 Washington NY = 438 Wa. NY map string -> array index in the range รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 2
Collission, Synonym Collission เกดเมอ insert 2748 2784 hash แลว ได index ทมของอยแลว Key hash(2748) = 2+7+4+8 mod 10 = 1 (∑of all digits) mod 10 hash(2784) = 1 collission ชน กบ 2748 ตองหาทเกบใหมให Synonyms : 2748 และ 2784 เปน synonyms set of keys ซง hash (map) ไดคาเดยวกน • ตองหาทเกบ เชน 2784 ใหม เพอแกการชน 1 2784 ? Other Data 0 . . . 1 2748 . . . 2 . . . 3 . . . . 9 Collission Resolutions • Open Addressing (Closed Hashing) • Seperate Chainning รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 2
Seperate Chaining (cont. ) • Load factor λ เปนตวเลขทบอกวาม data อยใน table หนาแนนเทาใด = จำนวน ขอมล ใน hash table / table size § ในรปตวอยาง λ = 10/10 = 1. 0 • จากการวเคราะห ทสำคญคอ • h(x) = x mod 10 • Table size = 10 (not prime for simplicity) • จน. ขอมล = 10 : table size ไมคอยสำคญเทาไหร λ • general rule สำหรบ separate chaining hash table § λ~1 § table size เปน prime • ขอเสย ของ separate chaining คอ ใช linked list ซงทำใหชาลง (allocate new node, implement 2 nd data structure) • ขอด : 100% solved collission รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 230
Open Addressing 1: Linear Probing probe ชนครงท Linear Probing i ลอง f(i) = i Quadratic Probing h(k) + f( i ) ลอง : h(k), h(k)+1, h(k)+2, h(k)+3, . . . f(i) = i 2 ลอง : h(k), h(k)+1, h(k)+4, h(k)+9, . . . h(1111) = 1 ชน probe sequence : 1111 1, 1+2, 1+3, . . . 1 2 3 ชน 4 วาง ทงหมด 4 probs Rehash: hash อกครงเพอใหไดทใหม hf(key) 1 ชน = key mod 10 1+1 = 2 ชน Linear Probing • การคำนวณงาย search งาย • มแนวโนมใหเกด การกระจกตว (Clustering (ของ data -> collission รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ 1+2 = 3 ชน 1+3 = 4 วาง 0 . . . 1 2151. . . 2 2872. . . 3 4553. . . 4 1111 . . 9 array size = 10 (not prime for simplicity) KMITL 01076249 Data Structures & Algorithms : Tree 231
Open Addressing 2: Quadratic Probing probe ชนครงท i ลอง h(k) + f( i ) Quadratic Probing f(i) = i 2 ลอง : h(k), h(k)+1, h(k)+4, h(k)+9, . . . Linear Probing f(i) = i ลอง : h(k), h(k)+1, h(k)+2, h(k)+3, . . . h(1111) = 1 ชน probe sequence : 1, 1+4, 1+9, . . . 1111 1 2 5 ชน 10 mod 10 = 1 วาง ทงหมด 4 probs 1+9 = 10 mod 10 = 1 วาง hf(key) = key mod 10 1 ชน 1+1 = 2 ชน 0 1111 1 2151. . . 2 2872. . . 3 1+4 = 5 ชน Quadratic Probing • มการกระจาย (distributes (มากกวา Linear Probing . . . 4 1544 5 1115 . . 9 array size = 10 (not prime for simplicity) รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 2
Open Addressing (Closed Hashing) Open Addressing : พยายาม probing จนพบทวาง • 1 st probe ลอง h(k) • 2 nd probe (ชนครงท 1) ลอง h(k) • 3 rd probe (ชนครงท 2) ลอง h(k) • ith probe (ชนครงท i) ลอง h(k) ith probe ชนครงท i • Linear Probing f(i) = i • Quadratic Probing f(i) = i 2 • Double Hashing 2 hash functions รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ สำหรบ key k + f(1) + f(2) + f( i ) ลอง h(k) + f( i ) ลอง : h(k), h(k)+1, h(k)+2, h(k)+3, . . . ลอง : h(k), h(k)+1, h(k)+4, h(k)+9, . . . KMITL 01076249 Data Structures & Algorithms : Tree 2
Primary Clustering ปรากฎการณท hash คนละ key ไปตกทคนละ slot แตตองมา แยงทกนในทสด ในการ rehash ครงถดๆมา Solutions: ให rehash fn ขนกบจำนวนครงท rehash ดวย จะไดคนละ. . . path เชน. . . . Linear probing rhi→ 21 = ←(h(k)+i) ครงท → 21 ← mod. . . → 21 ←// rehash → 21 ← 10 31 Primary Clustering: ตกทคนละ 52 73 slot แยง slot 115 h(k)=(k+21) mod. . . rh(L) = (L+21) mod. . . //L=last hash value Prob Sequences: key 10 : 31 52 73 94 115 key 31 : 52 73 94 115 key 52 : 73 94 115 key 73 : 94 115 key 94 : 115 รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ 94 . . . i→ 21 ← 115 Linear Probing: rhi = (h(k)+i) mod. . . ไดคนละ series ไมแยง 115 กน Prob Sequences: key 10 : 31 32 33 34 35 key 31 : 52 53 54 55 key 52 : 73 74 75 key 73 : 94 95 key 94 : 115 KMITL 01076249 Data Structures & Algorithms : Tree 234
Primary Clustering Solutions เมอ rehash ครงท j 3. Quadratic probing rhj = (h(k)+sqr(j))) mod. . . key 10 : 31 32 35 40 key 31 : 52 53 56 61 4. rh(i, k) = (i + hkey) mod. . . // เมอ hkey = 1+h(k) mod. . . 5. ใช random permutation ของเลขใดๆในชวง 1. . max p 1, p 2, p 3, . . . rhj = h(k) + pj) mod. . . รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 238
Rehashing 0 1 2 Rehashing 0 6 1 15 2 23 (คา prime ถดไปทใหญขน 3 24 เทา ) 2 3 24 Insert : 23 4 4 5 5 6 13 6 • สราง table ขนใหม • insert data ใหมทงหมด 13 h(x) = x mod 17 • แพง = O(n) Over 70 % full Rehashing 3 ขนาด 17 4 2 ดวย 5 6 6 7 23 8 24 9 10 11 12 h(x) = x mod 7, Linear Probing 13 13 14 15 15 16 รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 240
แบบฝกหด probe ชนครงท i Linear Probing ลอง f(i) = i : Linear Probing h(k) + f( i ) ลอง : h(k), h(k)+1, h(k)+2, h(k)+3, . . . 0 0 0 insert 1 insert 1 54 2 70 2 61 2 83 2 15 2 34 2 54%7 3 70%7 3 61%7 3 83%7 3 15%7 3 34%7 3 = 5 = 0 = 5 = 6 = 1 = 6 4 4 4 5 5 5 6 6 6 probes: h(key) = key mod 7 รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 241
: Quadratic Probing แบบฝกหด probe ชนครงท i ลอง h(k) + f( i ) Quadratic Probing f(i) = i 2 ลอง : h(k), h(k)+1, h(k)+4, h(k)+9, . . . 0 0 0 insert 1 insert 1 54 2 72 2 61 2 33 2 70 2 54%7 3 72%7 3 61%7 3 33%7 3 70%7 3 = 5 = 2 = 5 = 1 = 5 = 0 4 4 4 5 5 5 6 6 6 probes: h(key) = key mod 7 รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 2
เฉลยแบบฝกหด probe ชนครงท i Linear Probing f(i) = i 0 0 insert 1 54 70 2 ลอง h(k) + f( i ) ลอง : h(k), h(k)+1, h(k)+2, h(k)+3, . . . 70 0 70 insert 1 61 2 : Linear Probing 0 insert 1 83 2 70 83 0 insert 1 15 2 2 70 83 15 0 insert 1 34 2 54%7 3 70%7 3 61%7 3 83%7 3 15%7 3 34%7 3 = 5 = 0 = 5 = 6 = 1 = 6 4 5 54 6 probes: 4 5 54 6 1 4 5 6 1 54 61 2 4 5 6 54 61 3 4 5 6 54 61 2 70 83 15 34 4 5 6 54 61 5 h(key) = key mod 7 รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ KMITL 01076249 Data Structures & Algorithms : Tree 243
: Quadratic Probing เฉลยแบบฝกหด probe ชนครงท i ลอง h(k) + f( i ) Quadratic Probing f(i) = i 2 ลอง : h(k), h(k)+1, h(k)+4, h(k)+9, . . . 0 0 insert 1 54 72 2 2 72 61 2 72 1 2 0 1 72 insert 1 33 2 33 1 72 0 insert 1 70 2 54%7 3 72%7 3 61%7 3 33%7 3 70%7 3 = 5 = 2 = 5 = 1 = 5 = 0 4 5 54 6 probes: 4 5 54 6 1 4 5 6 1 54 61 2 h(key) = key mod 7 รศ. ดร. บญธร เครอตราช รศ. กฤตวน ศรบรณ 4 5 6 4 5 54 54 6 61 61 Try : 5, 1 5+1=6, 4 33 1 72 70 5 54 6 61 Try : 0, 3 4 0+1=1, 5+4=9%7=2, 0+4=4, 5+9=14%7=0 KMITL 01076249 Data Structures & Algorithms : Tree 2
- Slides: 44