1
Algorithm Analysis
-بالجزء األول من هذا الشابتر رح نكمل موضوع ال Analysisونوخذ مجموعة من ال
.Notations
-وبالجزء الثاني من الشابتر رح نعمل Analysisلنوعين من ال Sorting Techniques
اللي هم ال .Insertion Sort + Merge Sort
Chapter 2 || 2.1 :
… Example: Find The Minimum
)]▪ Alg.: MIN (a[1], …, a[n
;]m ← a[1 Line 1: )(1
for i ← 2 to n Line 2: )(n
if a[i] < m Line 3: )(n - 1
;]then m ← a[i Line 4: )(n - 1
عندي مثال وهو إيجاد ال Minimum Valueبين عناصر Arrayمعينة.
▪ فبال Algorithmبحكيلك الخطوات اللي بده يمشي عليها إليجاد ال Minimumفبالبداية
رح يسند أول قيمة في ال Arrayللمتغير Mويعتبره هو ال Minimumبشكل مبدئي وبعدها
يمشي ب Loopمن العنصر الثاني في ال Arrayوحتى nاللي هي عدد عناصر ال Array
وكل مرة يشيك إذا كانت قيمة العنصر اللي واقف عنده بال Arrayأًصغر من ال mفخليه
يسند القيمة لل ،mلحد ما تنتهي عناصر ال Arrayويطلع ال .Minimum
في واحد رح يسألني عن ال ] a[1عارف ... -
ذكرنا قبل هالمرة إنه ال Algorithmما بنكتبها بلغة برمجة ،ف هون قصده بال ] a[1إنه أول
عنصر في ال .Array
وكمان بال Loopكاتب إنه i 2وقصده هون بثاني عنصر بال Arrayوليس الثالث. -
والسهم هو سهم اإلسناد ،كمان مرة هاي عبارة عن Algorithmوما بتعبر عن الكود.
https://web.facebook.com/groups/IntelligenceGroup.CS/
2
طيب اآلن بدنا نعمل Analysisلل .... Algorithm
ً
أوًل :هاي ال Algorithmصحيحة وبتأدي الوظيفة فال Correctness -
ثانيًا :ال Efficiencyيعني بدنا نحسب ال ... Running Time -
Running Time:
- In Worst Case:
T(n) =1 [first step] + (n) [for loop] + (n-1) [if condition] +
(n-1) [the assignment in then] = 3n - 1
جملة إسناد أول قيمة في ال (1) Array -
-ال Loopرح تمشي لل (n) N
-جملة ال ifرح تتنفذ بعدد مرات ال Trueيعني أقل من شرط اللوب ب )n-1( 1
-الجملة التابعة لل ifرح تتنفذ بال )n-1( Worst Case
-يعني ال Worst Caseاللي هي إنه يكون آخر عنصر بال Arrayهو أصغر عنصر ،ف
الجملة التابعة ل ifرح تتنفذ ) (n-1من المرات.
بالتالي .Running Time in Worst Case = 3n -1
- In Best Case:
T(n) =1 [first step] + (n) [for loop] + (n-1) [if condition] +
(0) [the assignment in then] = 2n
-بال Best Caseاللي هي إنه يكون أول عنصر في ال Arrayهو أصغر عنصر ،فبالتالي
ال Loopرح تمشي بشكل طبيعي من ثاني عنصر لل nوهذا رح يستغرق nمن المرات..
ال if statementرح تتنفذ n-1من المرات ،إلنه كل مرة رح يشيك. -
https://web.facebook.com/groups/IntelligenceGroup.CS/
3
إلنه أول عنصر هو اولريدي أول عنصر، ما رح يتنفذ وًل مرةif تبع الBody ولكن ال-
. ف ما رح يكون في قيمة أصغر منهArray في ال
.Running Time in Best Case = 2n بالتالي
Typical Running Time Functions:
(constant running time):
- Instructions are executed once or a few times.
logN (logarithmic)
- A big problem is solved by cutting the original problem in
smaller sizes, by a constant fraction at each step.
N (linear)
- A small amount of processing is done on each input element.
N logN
- A problem is solved by dividing it into smaller problems, solving
them independently and combining the solution.
N2 (quadratic)
- Typical for algorithms that process all pairs of data items (double
nested loops).
N3 (cubic)
- Processing of triples of data (triple nested loops).
Nk (polynomial).
2N (exponential)
- Few exponential algorithms are appropriate for practical use.
https://web.facebook.com/groups/IntelligenceGroup.CS/
4
Asymptotic Notations
O Notation: asymptotic “less than and equal”: f(n) “≤” g(n)
notation: asymptotic “greater than and equal”: f(n) “≥” g(n)
notation: asymptotic “equality”: f(n) “=” g(n)
. بنوصف فيها إنه فنكشن أقل أو يساوي فنكشن آخر:O Notation
f(n) = O (g(n)) ف بقدر أكتبه على الصورةf(n) ≤ g(n) ف لو كان عندي-
ً
أصًل ؟ طيب كيف ممكن أعرف إنه فنكشن أقل من غيره -
... 2 Functions للlimit الجواب هو من خًلل إيجاد ال
𝑓(𝑛)
If 0 ≤ lim < ∞ , Then f(n) ≤ g(n)
𝑛→∞ 𝑔(𝑛)
. بنوصف فيها إنه فنكشن أكبر أو يساوي فنكشن آخر: notation
f(n) = (g(n)) ف بقدر أكتبه على الصورةf(n) ≥ g(n) ف لو كان عندي-
𝑓(𝑛)
If 0 < lim ≤ ∞ , Then f(n) ≥ g(n)
𝑛→∞ 𝑔(𝑛)
. بنوصف فيها إنه فنكشن معين يساوي فنكشن آخر: notation
f(n) = (g(n)) ف بقدر أكتبه على الصورةf(n) = g(n) ف لو كان عندي-
𝑓(𝑛)
If 0 < lim < ∞ , Then f(n) = g(n).
𝑛→∞ 𝑔(𝑛)
https://web.facebook.com/groups/IntelligenceGroup.CS/
5
Examples:
notation:
- n2/2 – n/2 = (n2).
- (6n3 + 1)logn /(n + 1) = (n2logn).
- n vs. n2 n ≠ (n2), But n = O(n2).
notation:
- n3 vs. n2 n3 = (n2).
- n vs. logn n = (logn).
- n vs. n2 n ≠ (n2), But n = O(n2).
O notation:
- 2n2 vs. n3 2n2 = O(n3)
- n2 vs. n2 n2 = O(n2)
- n3 vs. nlogn n3 ≠ nlogn, But n3 = (nlogn).
https://web.facebook.com/groups/IntelligenceGroup.CS/
6
-الرسومات فوق بتبين فرق السرعة بين كل فنكشن والثاني بحيث المحور السيني
بمثل حجم اًلنبوت والمحور الصادي بمثل سرعة ال Algorithmالتقريبية.
ً -لحظ كيف ال n3اللي هو أسرع من ال 2nكان أبطأ منه لحد ما وصل جحم
اًلنبوت للقيمة 10وبعد هيك بلّش الفرق اللي بينهم يوضح بشكل كبير.
https://web.facebook.com/groups/IntelligenceGroup.CS/
7
Recursive Algorithms
Binary search: for an ordered array A, finds if x is in the array
]A[lo…hi
) Alg.: BINARY-SEARCH (A, lo, hi, x
)if (lo > hi
return FALSE
mid ← (lo + hi) / 2
]if x = A[mid
return TRUE
)]if (x < A[mid
)BINARY – SEARCH(A, lo, mid - 1, x
)]if (x > A[mid
)BINARY - SEARCH(A, mid + 1, hi, x
-شوية مراجعة لموضوع ال Recursionففي عنا Arrayاسمها Aوبدنا نعمل Search
على عنصر ،xوبدنا نطبق مبدأ ال Binary Searchباعتبار إنه هاي ال Arrayهي
.Ordered
ودرسنا آلية عمله بال Data Structuresبس كتذكير سريع فيه ..
-ف هو ببلش بحث من المنتصف وببلش عملية مقارنة بين العنصر اللي في الوسط وبين
العنصر اللي ببحث عنه (اللي هو )xف إذا كان متساويين ف برجعه وص ّل هللا وبارك ،وهاي
طب ًعا ال Best Caseفي ال ... Binary Search
إذا ما طلعوا متساويين ،ببلش يعمل مقارنة فإذا كان العنصر اللي ببحث عنه أكبر من العنصر
اللي في الوسط فأنا مش بحاجة إلنه أبحث عن العناصر اللي قبل العنصر اللي في الوسط إلنه
كلهم أصغر من الرقم اللي ببحث عنه (إلنها ... )Ordered
فبصير يبحث بالجزء األيمن من ال Arrayوبعيد نفس اآللية كمان مرة ،بيجي بقارن العنصر
اللي بالوسط بال Arrayالجديدة وهكذا تستمر العملية Recursivelyلحتى يًلقيه ف يرجع
Trueأو ما يًلقيه ف يرجع .False
https://web.facebook.com/groups/IntelligenceGroup.CS/
8
عشان كل مرةlo,hi فهو رح يحتاج لمتغيرين اللي همRecursively وطبعًا كونه اشتغل-
.x عنده ويقل عدد العناصر اللي رح يبحث فيهم عن العنصرArray تنقص ال
1 2 3 4 5 6 7 8
2 3 5 7 9 10 11 12
mid
lo hi
Recurrences
Recursive Algorithms للRunning Time هو طريقة تمثيل الRecurrences ال -
..
E.g.: T(n) = T(n-1) + n .4 ورح نفصل كل صغيرة وكبيرة فيه بشابتر
؟Recursive Algorithm كيف بنعرف إنه هالتعبير هو عبارة عن تعبير ل-
ً ، في طرفي المعادلةT(n) الجواب هو من خًلل إنه بكون
فمثًل المعادلة اللي فوق بتبين إنه ال
بواحد ونعوض فيn ف كل مرة رح ينقصT(n-1) + n رح تساوي كل مرة الT(n)
. ويتوقفBase Case المعادلة من جديد لحد ما نوصل لل
Useful for analyzing recurrent algorithms.
Methods for solving recurrences:
Iteration method.
Substitution method.
Recursion tree method.
Master method.
... Master Method + Iteration Method رح ندرس ال -
https://web.facebook.com/groups/IntelligenceGroup.CS/
9
Sorting:
Comparison methods:
Iterative methods:
- Insertion sort
- Bubble sort
- Selection sort
-
. عاديةLoop اللي الجمل اللي فيهاIteration المقصود بال
. تبعته بالجزء الثاني من الشابترAlgorithm ورح نشوف الInsertion Sort زي-
Divide and conquer:
- Merge sort
- Quick sort
.Recursive Algorithms ف المقصود فيهاDivide and Conquer لما نحكي -
.Merge Sort + Quick Sort زي ال-
يعني بعتمدوا علىcomparison methods الجزئين اللي ذكرتهم فوق هذول بنسميهم
.Array للSorting المقارنات لحتى يعملوا
.Non-comparison methods بينما النوع الثاني هو
Non-comparison methods:
- Counting sort
- Radix sort
- Bucket sort
الهمFile ومش مطلوبين معنا لكن رح ينزل على اًليليرنينغ، وهذول ما رح نتطرق الهم-
.للي بحب يشوفهم
https://web.facebook.com/groups/IntelligenceGroup.CS/
10
Types of Analysis
Worst case (e.g. cards reversely ordered)
- Provides an upper bound on running time.
- An absolute guarantee that the algorithm would not run longer,
no matter what the inputs are.
Best case (e.g., cards already ordered)
- Input is the one for which the algorithm runs the fastest.
Average case (general case)
- Provides a prediction about the running time.
- Assumes that the input is random.
،Time Complicity معينة فحكينا إنه بنحسب الAlgorithm لAnalysis لما نعمل
: أنواع3 بكون علىTime وحساب ال
لعناصر الSorting كمثال إنه أحكيلك بدي تعمل، اللي هي أسوأ األحوال:Worst Case -
. مرتبة بالعكسArray فأسوأ األحوال إنه تكون هاي الArray
لSorting زي احكيلك اعمل، أفضل األحوال وبتكون أسرع ما يمكن:Best Case -
. هون بكون أسرع ما يمكنTime فالSorted بالفعلArray ولكن هاي الArray
Sorting اللي بدي أعمللهاArray إنه تكون الGeneral Case أو ال:Average Case -
.Non Sorted وًل هيSorted يعني ًل هي،فيها قيم عشوائية
By: Albara Fwaz
#Intelligence_Group
https://web.facebook.com/groups/IntelligenceGroup.CS/