Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

‫‪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/

You might also like