Intelligent Algorithms - CH2
Intelligent Algorithms - CH2
-0-
الفصل الثاني :التوازن ذاكرة -زمن مقرر الخوارزميات الذكية
-1-
الفصل الثاني :التوازن ذاكرة -زمن مقرر الخوارزميات الذكية
-2-
زمن- التوازن ذاكرة:الفصل الثاني مقرر الخوارزميات الذكية
-3-
زمن- التوازن ذاكرة:الفصل الثاني مقرر الخوارزميات الذكية
:الخوارزمية
ALGORITHM BruteForceStringMatch (T [0..n - 1], P [0..m - 1])
//Implements brute-force string matching
//Input: An array T [0..n - 1] of n characters representing a text and
an array P [0..m - 1] of m characters representing a pattern
//Output: The index of the first character in the text that starts a
// matching substring or – 1 if the search is unsuccessful
for i ← 0 to n – m do
j←0
while j < m and P [ j ] = T [ i + j ] do
j←j+1
if j = m return i
return –1
-4-
زمن- التوازن ذاكرة:الفصل الثاني مقرر الخوارزميات الذكية
-5-
زمن- التوازن ذاكرة:الفصل الثاني مقرر الخوارزميات الذكية
:الخوارزمية
ALGORITHM ShiftTable (P [0..m – 1])
//Fills the shift table used by Horspool’s and Boyer-Moore algorithms
//Input: Pattern P [0..m – 1] and an alphabet of possible characters
//Output: Table [0..size – 1] indexed by the alphabet’s characters and
// filled with shift sizes computed by formula (7.1)
initialize all the elements of Table with m
for j ← 0 to m – 2 do Table [P [ j ]] ← m – 1 – j
return Table
-6-
زمن- التوازن ذاكرة:الفصل الثاني مقرر الخوارزميات الذكية
:مخطط الكود
COUNTING SORT(A,B,k)
1 for i = 1 to k
2 do c[i] = 0
3 for j = 1 to length[A]
4 do C[A[j]] = C[A[j]] + 1
5 //C[i] now contains the number of elements equal to i
6 for i = 2 to k
7 do C[i] = C[i] + C[i-1]
8 //C[i] now contains the number of elements less than or equal
to i
9 for j = length[A] downto 1
10 do B[C[A[j]]] = A[j]
11 C[A[j]] = C[A[J]] - 1
-7-
زمن- التوازن ذاكرة:الفصل الثاني مقرر الخوارزميات الذكية
-8-
زمن- التوازن ذاكرة:الفصل الثاني مقرر الخوارزميات الذكية
تابع التهشير.1.4
In a phone directory, we are more interested in finding the phone number of a
subscriber. Can we use hashing in this case?
Yes, but there are two obstacles:
1. A name is comprised of a string of alphabetic characters so they must be
converted to numbers.
2. The number of all possible strings that can be made from, say, 7 characters
only is much more than the number of 7-digit phone numbers - about 267
= 8,031,810,176 (why?) - hence a perfect hash function will require an array
with 8,031,810,176 locations, most of them are unused.
-9-
الفصل الثاني :التوازن ذاكرة -زمن مقرر الخوارزميات الذكية
مخطط اإلدراج:
مخطط البحث:
مخطط الحذف:
- 10 -
زمن- التوازن ذاكرة:الفصل الثاني مقرر الخوارزميات الذكية
T[j] = “Deleted”
return j;
else
i = i + 1;
until (T[j] = nil) or (i = m)
return “Element not found !”
- 11 -
زمن- التوازن ذاكرة:الفصل الثاني مقرر الخوارزميات الذكية
:مثال
h1(k)= k mod 13
h2(k) = 1 + k mod 11
(T[0..12] m=13)
Probing Sequence for k=79
<1, 4, 7, 10>
(T[1]=79)
Probing Sequence for k=96
<5 ,4, 3, 2, 1, 0, 11, 10, . .>
(T[5]=96)
Probing Sequence for k=14
<1, 5, 9>
(T[9]=14)
:مثال
Show the hash table (0..6) after inserting the following numbers:
1, 13, 15, 14, 22, 8, 12
0 1 2 3 4 5 6
14 1 15 22 8 12 13
- 12 -
زمن- التوازن ذاكرة:الفصل الثاني مقرر الخوارزميات الذكية
- 13 -
زمن- التوازن ذاكرة:الفصل الثاني مقرر الخوارزميات الذكية
- 14 -