0% found this document useful (0 votes)
10 views

Intelligent Algorithms - CH2

AI learning

Uploaded by

Mahmoud Souliman
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Intelligent Algorithms - CH2

AI learning

Uploaded by

Mahmoud Souliman
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

‫الفصل الثاني‪ :‬التوازن ذاكرة ‪ -‬زمن‬ ‫مقرر الخوارزميات الذكية‬

‫مقرر الخوارزميات الذكية‬


‫الفصل الثاني‪ :‬التوازن ذاكرة ‪ -‬زمن‬
‫‪Space-for-time tradeoffs‬‬

‫‪-0-‬‬
‫الفصل الثاني‪ :‬التوازن ذاكرة ‪ -‬زمن‬ ‫مقرر الخوارزميات الذكية‬

‫‪-1-‬‬
‫الفصل الثاني‪ :‬التوازن ذاكرة ‪ -‬زمن‬ ‫مقرر الخوارزميات الذكية‬

‫رقم الصفحة‬ ‫العنوان‬


‫‪3‬‬ ‫‪ .1‬مبدأ التوازن ذاكرة ‪ -‬زمن‬
‫‪4‬‬ ‫‪ .2‬مسألة البحث عن سلسلة‬
‫‪4‬‬ ‫‪ .1.2‬حل مسألة البحث عن سلسلة باستخدام القوة الغاشمة‬
‫‪ .2.2‬حل مسألة البحث عن سلسلة باستخدام خوارزمية‬
‫‪5‬‬
‫‪Horspool’s Algorithm‬‬
‫‪7‬‬ ‫بالعد ‪Count Sort‬‬
‫‪ .3‬الفرز ّ‬
‫‪8‬‬ ‫‪ .4‬مسألة التهشير ‪Hashing‬‬
‫‪9‬‬ ‫‪ .1.4‬تابع التهشير‬
‫‪10‬‬ ‫‪ .2.4‬العنونة المفتوحة ‪Open Addressing‬‬
‫‪11‬‬ ‫‪ .3.4‬توليد تسلسل التحقيق ‪Probing Sequence‬‬
‫‪13‬‬ ‫‪ .5‬مشروع الفصل‪ :‬مسألة الفرز‬

‫‪-2-‬‬
‫ زمن‬- ‫ التوازن ذاكرة‬:‫الفصل الثاني‬ ‫مقرر الخوارزميات الذكية‬

‫ زمن‬- ‫ مبدأ التوازن ذاكرة‬.1


‫ يقوم النوع األول بمعالجة وتحس ي ييين لخل المس ي ييألة بينما‬:‫يوجد نوعين من خوارزميات الذاكرة مقابل الزمن‬
:‫يقوم النوع الثاني بإعالة هيكلة هذا الدخل‬
Two varieties of space-for-time algorithms:
• input enhancement: preprocess the input (or its part) to store some info
to be used later in solving the problem.
▪ counting sorts
▪ string searching algorithms
• prestructuring: preprocess the input to make accessing its elements
easier.
▪ hashing

-3-
‫ زمن‬- ‫ التوازن ذاكرة‬:‫الفصل الثاني‬ ‫مقرر الخوارزميات الذكية‬

‫ مسألة البحث عن سلسلة‬.2


:‫ليكن لدينا مسألة البحث عن سلسلة جزئية ضمن سلسلة نصية‬
pattern: a string of m characters to search for.
text: a (long) string of n characters to search in.

‫ حل مسألة البحث عن سلسلة باستخدام القوة الغاشمة‬.1.2


:‫ كما يلي‬Brute force algorithm ‫ُيمكن حل هذه المسألة بأسلوب القوة الغاشمة‬
Step1: Align pattern at beginning of text.
Step2: Moving from left to right, compare each character of pattern to the
corresponding character in text until either all characters are found to match
(successful search) or a mismatch is detected.
Step 3: While a mismatch is detected and the text is not yet exhausted, realign
pattern one position to the right and repeat Step 2.
.O (n*m) :‫من الواضح أن تعقيد الخوارزمية من مرتبة‬

:‫الخوارزمية‬
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-
‫ زمن‬- ‫ التوازن ذاكرة‬:‫الفصل الثاني‬ ‫مقرر الخوارزميات الذكية‬

Horspool’s Algorithm ‫ حل مسألة البحث عن سلسلة باستخدام خوارزمية‬.2.2


‫ والتي تعتمد على بناء جدول‬Horspool’s Algorithm ‫من طرق المعالجة األولية المش ي ي ي ييهورة خوارزمية‬
:‫حدل االنزياح الواجب القيام به عند اختالف حرفين‬
ّ ‫انزياح في البداية ُي‬
• preprocesses pattern to generate a shift table that determines how much
to shift the pattern when a mismatch occurs.
• always makes a shift based on the text’s character c aligned with the last
character in the pattern according to the shift table’s entry for c.

:Shift ‫ُنبين فيما يلي آلية عملية اإلزاحة‬


Look at first (rightmost) character in text that was compared:
• The character is not in the pattern
C (C not in pattern)
BAOBAB

• The character is in the pattern (but not the rightmost)


O (O occurs once in pattern)
BAOBAB
A (A occurs twice in pattern)
BAOBAB

• The rightmost characters do match


B
BAOBAB

:‫يتم ملء جدول اإلزاحة وفق ما يلي‬


distance from c’s rightmost occurrence in pattern among its first m-1 characters
to its right end.
t(c) = pattern’s length m, otherwise
:BAOBAB ‫ يكون جدول اإلزاحة للسلسلة‬:ً‫مثال‬

-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

ALGORITHM HorspoolMatching (P [0..m – 1], T [0..n – 1]


//Implements Horspool’s algorithm for string matching
// Input: Pattern P [0..m – 1] and text T [0..n – 1]
//Output: The index of the left end of the first matching substring
// or – 1 if there are no matches
ShiftTable (P [0..m – 1]) //generate Table of shifts
i←m–1 //position of the pattern’s right end
while i < n – 1 do
k←0
while k ≤ m – 1 and P [m – 1– k] = T [ i + k ] do
k←k+1
if k = m
return i – m + 1
else i ← i + Table [T [ i ]]
return –1

-6-
‫ زمن‬- ‫ التوازن ذاكرة‬:‫الفصل الثاني‬ ‫مقرر الخوارزميات الذكية‬

Count Sort ‫بالعد‬


ّ ‫ الفرز‬.3
:O(n) ‫تقوم هذه الخوارزمية بفرز مجموعة من األعدال المحصورة ضمن مجال معين وبتعقيد من مرتبة‬
• Counting sort is a linear time sorting algorithm used to sort items when
they belong to a fixed and finite set. Integers which lie in a fixed interval,
say k1 to k2, are examples of such items.
• The algorithm proceeds by defining an ordering relation between the items
from which the set to be sorted is derived (for a set of integers, this relation
is trivial). Let the set to be sorted be called A. Then, an auxiliary array
with size equal to the number of items in the superset is defined, say B.
For each element in A, say e, the algorithm stores the number of items in
A smaller than or equal to e in B(e). If the sorted set is to be stored in an
array C, then for each e in A, taken in reverse order, C[B[ e]] = e. After
each such step, the value of B(e) is decremented.
• The algorithm makes two passes over A and one pass over B. If size of
the range k is smaller than size of input n, then time complexity=O(n).
Also, note that it is a stable algorithm, meaning that ties are resolved by
reporting those elements first which occur first.

:‫مخطط الكود‬
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-
‫ زمن‬- ‫ التوازن ذاكرة‬:‫الفصل الثاني‬ ‫مقرر الخوارزميات الذكية‬

Hashing ‫ مسألة التهشير‬.4


:‫وهي تقنية تُستخدم للبحث عن عنصر في مصفوفة بدون استخدام ألي من خوارزميات البحث التقليدية‬
Hashing is a method for locating and finding elements in an array without having
to use any of the search techniques. In hashing, the location of an element in
an array is directly obtainable, or through some simple calculations, from the
value of a key within that element.
Hashing eliminates the need to search for an element because we can directly
access the element by means of its key value.
:‫مثال‬
Assume we want to create a phone directory for 7-digit phone numbers. Each
record (element) in the directory contains the phone number, the name of the
subscriber (owner of that number), and his/her address.
If we want to find the record corresponding to a number very fast, we can simply
use the phone numbers themselves as indices to the locations in which the
corresponding records are stored. The number of array elements in this directory
is 107.

-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.

Can we still use hashing?


Yes, but the hash function may not be a perfect hash function. One very simple
hash function in this case is the sum of the ASCII codes of the characters of the
name, but this will restrict the number of array locations to 854 locations, and
consequently will cause collisions (more than one name will be mapped to the
same array location).

-9-
‫الفصل الثاني‪ :‬التوازن ذاكرة ‪ -‬زمن‬ ‫مقرر الخوارزميات الذكية‬

‫‪ .2.4‬العنونة المفتوحة ‪Open Addressing‬‬


‫في حال كانت نتيجة التهشي ي ي ييير نفسي ي ي ييه ألكثر من لخل حالة ت ي ي ي ييارب ‪ )Collision Resolution‬يجب‬
‫عندها وض ي ييع التس ي ييجيلة األخرآ في مكان آخر من االس ي ييتراتيجيات المس ي ييتخدمة لذل اس ي ييتراتيجية العنونة‬
‫المفتوحة حيث توضيع جميع العناصير في مصيفوفة واحدة يكون كل عنصير من المصيفوفة اما فارًاً أو‬
‫يحوي عنصر يتم البحث عن عنصر بتسلسل ُيدعى تسلسل التحقيق ‪:Probing Sequence‬‬
‫>)‪<h(k,0) , h(k,1), . . . , h(k, m-1‬‬

‫مخطط اإلدراج‪:‬‬

‫)‪Hash_Insert (T, k‬‬


‫;‪i=0‬‬
‫‪repeat‬‬
‫;)‪j = h(k, i‬‬
‫‪if T[j] = nil or T[j]=“Deleted” then‬‬
‫;‪T[j]= k‬‬
‫;‪return j‬‬
‫‪else‬‬
‫;‪i = i + 1‬‬
‫‪until i = m‬‬
‫“ ! ‪Error “Hash Table Overflow‬‬

‫مخطط البحث‪:‬‬

‫)‪Hash_Search (T, k‬‬


‫;‪i=0‬‬
‫‪repeat‬‬
‫;)‪j = h(k, i‬‬
‫‪if T[j] = k then‬‬
‫;‪return j‬‬
‫‪else‬‬
‫;‪i = i + 1‬‬
‫)‪until (T[j] = nil) or (i = m‬‬

‫مخطط الحذف‪:‬‬

‫)‪Hash_Delete (T, k‬‬


‫;‪i=0‬‬
‫‪repeat‬‬
‫;)‪j = h(k, i‬‬
‫‪if T[j] = k then‬‬

‫‪- 10 -‬‬
‫ زمن‬- ‫ التوازن ذاكرة‬:‫الفصل الثاني‬ ‫مقرر الخوارزميات الذكية‬

T[j] = “Deleted”
return j;
else
i = i + 1;
until (T[j] = nil) or (i = m)
return “Element not found !”

Probing Sequence ‫ توليد تسلسل التحقيق‬.3.4


:‫ُيمكن استخدام الطرق التالية‬
Linear Probing ‫التحقيق الخطي‬
:‫يكون تابع التهشير‬
h(k, i) = (h’(k) + c1.i + c2.i2) mod m
(i=0, 1, . . ., m-1)
:Probing Sequence ‫ويكون تسلسل التحقيق‬
<h’(k), h’(k)+1, . . . , h’(k)+m-1>

Quadratic Probing ‫التحقيق التربيعي‬


:‫يكون تابع التهشير‬
h(k, i) = (h’(k) + i) mod m
(i=0, 1, . . ., m-1)
:Probing Sequence ‫ويكون تسلسل التحقيق‬
<h’(k), h’(k)+c1+c2, . . . , h’(k)+c1.(m-1)+c2.(m-1)2 >

Double Hashing ‫التهشير المضاعف‬


:‫يكون تابع التهشير‬
h(k, i) = (h1(k) + i.h2(k)) mod m
(i=0, 1, . . ., m-1)
:Probing Sequence ‫ويكون تسلسل التحقيق‬
<h1(k), h1(k)+h2(k), h1(k)+2.h2(k), . . ., h1(k)+(m-1).h2(k)>

:‫لتحقيق أف ل تهشير م اعف‬


Choosing h1, h2 and m:
• m Primary number
• h1 = k mod m
• h2= 1 + (k mod m’)
(m’=m-1 or m’=m-2)

- 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 -
‫ زمن‬- ‫ التوازن ذاكرة‬:‫الفصل الثاني‬ ‫مقرر الخوارزميات الذكية‬

‫ مسألة الفرز‬:‫ مشروع الفصل‬.5


‫المطلوب حل وبرمجة مسألة الفرز وفق الخوارزميتين التاليتين والمقارنة بينهما‬

:)‫خوارزمية الفرز بالعد المقارنة‬


Idea: count the total number of elements smaller than the key; use this number
as the position in the sorted list
:‫وفق ما يلي‬
ALGORITHM ComparisonCountingSort (A [0..n – 1])
//Sorts an array by comparison counting
//Input: An array A [0..n – 1] of orderable elements
//Output: Array S [0..n – 1] of A’s elements sorted in nondecreasing order
for i ← 0 to n – 1 do Count [ j ] ← 0
for i ← 0 to n – 2 do
for j ← i + 1 to n – 1 do
if A [ i ] < A [ j ]
Count [ j ] ← Count [ j ] + 1
else Count [ i ] ← Count [ i ] + 1
for i ← 0 to n – 1 do S [Count [ i ]] ← A [ i ]
return S

:‫ُيبين الشكل التالي مثال عملي‬

- 13 -
‫ زمن‬- ‫ التوازن ذاكرة‬:‫الفصل الثاني‬ ‫مقرر الخوارزميات الذكية‬

:)‫خوارزمية الفرز بالعد (التوزيع‬


Idea: the distribution values indicates the proper positions in the sorted array.
:‫وفق ما يلي‬
ALGORITHM DistributionCountingSort (A [0..n – 1], l, u)
//Sorts an array of integers from a limited range be distribution counting
//Input: An array A [0..n – 1] of integers between l and u (l ≤ u)
//Output: Array S [0..n – 1] of A’s elements sorted in nondecreasing order
for j ← 0 to u – 1 do D [ j ] ← 0 //initialize frequencies
for i ← 0 to n – 1 do D [A [ i ] – l ] ← D [A [ i ] – l ] + 1 //compute
frequencies
for j ← 1 to u – 1 do D [ j ] ← D [ j – 1 ] + D [ j ] //reuse for frequencies
for i ← n – 1 downto 0 do
j←A[j]–l
S [D [ i ] – 1] ← A [ i ]
D[ j ] ← D [ j ] – 1
return S

:‫ُيبين الشكل التالي مثال عملي‬

- 14 -

You might also like