course 4 - nuu.edu.tw

40
演算法課程 演算法課程 (Algorithms) (Algorithms) 國立聯合大學 資訊管理學系 陳士杰老師 Course 4 搜尋 Search

Upload: others

Post on 23-Feb-2022

1 views

Category:

Documents


0 download

TRANSCRIPT

1Partial Key Partial Key v.sv.s. Whole Key. Whole Key
Actual Key Actual Key v.sv.s. Transformation Key. Transformation Key
Search
:
External Search: Def: Memory (E.g. Disk)search
B-tree
: Static

: Dynamic

6 ()
Link List)
Linear Search ()
7 ()
//
42 n…531
Time: O(nO(n))
//
location
1
S 0
n ½½
: Search!!
while ( l ≤
u )
(k, S[m]) case “=”: found, i = m, return i; case “<”: uu = m-1; case “>”: ll = m+1;
recurn
0;
= 1 + c log2
Random (Random (Direct) accessDirect) access (e.g., Array)
:
u && i == 0)
(x, S[mid]) case 1 “=”: found, i = mid, return i; case 2 “<”: uu = mid-1; case 3 “>”: ll = mid+1;
recurn
0;


Hash TableHash TableBucketBucketx
Hash Table Bucket ()
Hashing function
Identifier Density = n/T
Loading Density = n/(b×S) = α αHash Table UtilizationCollision / Overflow
Collision Def: (e.g., xy) Hashing Function Hashing Address ( H(x) = H(y))
22 ()
CollisionOverflowOverflow Collision
y → H(y) y
(01Collision)
: Perfect Hashing FunctionPerfect Hashing Function ()
Def: Hashing Function Collision
Uniform Hashing FunctionUniform Hashing Function () Def: Hashing FunctionHashing Address Bucket No.()
24 ()
e.g., 1000Bucket000~999 x = 8125Middle SquareHash Address
Sol:
Middle Square ()
m: m “2”
: Shift ()
Folding Addition ()
28 ()
Sol: x=12320324111220 are partitioned into three decimal digits long.
P1 = 123, P2 = 203, P3 = 241, P4 = 112, P5 = 20. Shift folding:
Folding at the boundaries:
123
302
211
241
29 ()
Digits Analysis ()
;
Ex:
H(x)+1, H(x)+2, …, H(x)-1:
Bucket
:
x
32 ()
Hash Table11buckets (: 0~10)bucketslot Hashing Function = x mod 11x mod 11 “Linear Probing”overflow Hash Table?
5, 16, 33, 21, 22, 27, 38, 17
Sol:
: (Clustering)(Clustering)Searching Time
0 33 1 22 2 3 4 5 5 6 16 7 27 8 38 9 17 10 21
H(33) H(22)
H(21)
33 ()
Quadratic Probing ()
Def: ClusteringH(x)overflow (H(x) ± i2) mod bbbucket1 ≤ i ≤ (b- 1)/2
:
H(x)
:
1
2
3
4
Sol:
0 33 1 22 2 3 4 27 5 5 6 16 7 17 8 9 38 10 21
H(33) H(22)
H(21)
35 ()
0 33 1 22 2 44 3 4 27 5 5 6 16 7 17 8 9 38 10 21
44 ⇒ ?
(0-12) mod 11 = 10
(0-22) mod 11 = 7
(0-32) mod 11 = 22
Rehashing ()
Def: Hashing Functions: f1, f2, f3, … fn f1 overflow f2; :
overflow
function
“Quadratic Probing”overflowHash Table ?
5, 16, 33, 21, 22, 27, 38, 17
Sol:
0 33 1 2 3 4 5 5 6 17 7 8 9 10 21
H(33)H(22)
Binary SearchBinary Search
()? 5 n = log2(n+1) =
1 31
[2] [6] [10] [14] [18] [22] [26] [30]
[1] [3] [5] [7] [9] [11] [13] [15] [17] [19] [21] [23] [25] [27] [29] [31]
40 ()
26, 55, 77, 19, 13, 2, 5, 49
Binary Search “55” ?
Course 4Search
Linear Search ()