Presentation is loading. Please wait.

Presentation is loading. Please wait.

Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 4.

Similar presentations


Presentation on theme: "Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 4."— Presentation transcript:

1 Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 4

2 CS 477/677 - Lecture 42 Methods for Solving Recurrences Iteration method Substitution method Recursion tree method Master method

3 CS 477/677 - Lecture 43 The recursion-tree method Convert the recurrence into a tree: –Each node represents the cost incurred at that level of recursion –Sum up the costs of all levels Used to “guess” a solution for the recurrence

4 CS 477/677 - Lecture 44 Example 2 E.g.: T(n) = 3T(n/4) + cn 2 Subproblem size at level i is: n/4 i Subproblem size hits 1 when 1 = n/4 i ⇒ i = log 4 n Cost of a node at level i = c(n/4 i ) 2 Number of nodes at level i = 3 i ⇒ last level has 3 log 4 n = n log 4 3 nodes Total cost: ⇒ T(n) = O(n 2 )

5 CS 477/677 - Lecture 45 Example 2 - Substitution T(n) = 3T(n/4) + cn 2 Guess: T(n) = O(n 2 ) –Induction goal: T(n) ≤ dn 2, for some d and n ≥ n 0 –Induction hypothesis: T(n/4) ≤ d (n/4) 2 Proof of induction goal: T(n) = 3T(n/4) + cn 2 ≤ 3d (n/4) 2 + cn 2 = (3/16) d n 2 + cn 2 = d n 2 + cn 2 + (3/16) d n 2 - d n 2 = d n 2 + cn 2 - (13/16) d n 2 ≤ d n 2 if: cn 2 - (13/16) d n 2 ≤ 0 d ≥ (16/13)c Therefore: T(n) = O(n 2 )

6 CS 477/677 - Lecture 46 Example 3 W(n) = W(n/3) + W(2n/3) + n The longest path from the root to a leaf is: n → (2/3)n →(2/3) 2 n →… →1 Subproblem size hits 1 when 1 = (2/3) i n ⇔ i=log 3/2 n Cost of the problem at level i = n Total cost: ⇒ W(n) = O(nlgn) for all levels

7 CS 477/677 - Lecture 47 Example 3 - Substitution W(n) = W(n/3) + W(2n/3) + n Guess: W(n) = O(nlgn) –Induction goal: W(n) ≤ dnlgn, for some d and n ≥ n 0 –Induction hypothesis: W(k) ≤ d klgk for any k < n (n/3, 2n/3) Proof of induction goal: Try it out as an exercise!!

8 CS 477/677 - Lecture 48 Master’s method “Cookbook” for solving recurrences of the form: where, a ≥ 1, b > 1, and f(n) > 0 Idea: compare f(n) with n log b a f(n) is asymptotically smaller or larger than n log b a by a polynomial factor n ℇ f(n) is asymptotically equal with n log b a

9 CS 477/677 - Lecture 49 Master’s method “Cookbook” for solving recurrences of the form: where, a ≥ 1, b > 1, and f(n) > 0 Case 1: if f(n) = O(n log b a - ℇ ) for some ℇ > 0, then: T(n) = (n log b a ) Case 2: if f(n) = (n log b a ), then: T(n) = (n log b a lgn) Case 3: if f(n) = (n log b a + ℇ ) for some ℇ > 0, and if af(n/b) ≤ cf(n) for some c < 1 and all sufficiently large n, then: T(n) = (f(n)) regularity condition

10 CS 477/677 - Lecture 410 Why n log b a ? Assume n = b k ⇒ k = log b n At the end of iterations, i = k: Case 1: –If f(n) is dominated by n log b a : T(n) = (n log b n ) Case 3: –If f(n) dominates n log b a : T(n) = (f(n)) Case 2: –If f(n) = (n log b a ) : T(n) = (n log b a logn)

11 CS 477/677 - Lecture 411 Examples T(n) = 2T(n/2) + n a = 2, b = 2, log 2 2 = 1 Compare n log 2 2 with f(n) = n ⇒ f(n) = (n) ⇒ Case 2 ⇒ T(n) = (nlgn)

12 CS 477/677 - Lecture 412 Examples T(n) = 2T(n/2) + n 2 a = 2, b = 2, log 2 2 = 1 Compare n with f(n) = n 2 ⇒ f(n) = (n 1+ ℇ ) Case 3 ⇒ verify regularity cond.: a f(n/b) ≤ c f(n) ⇒ 2 n 2 /4 ≤ c n 2 ⇒ c = ½ is a solution (c<1) ⇒ T(n) = (n 2 )

13 CS 477/677 - Lecture 413 Examples (cont.) T(n) = 2T(n/2) + a = 2, b = 2, log 2 2 = 1 Compare n with f(n) = n 1/2 ⇒ f(n) = O(n 1- ℇ ) Case 1 ⇒ T(n) = (n)

14 CS 477/677 - Lecture 414 Examples T(n) = 3T(n/4) + nlgn a = 3, b = 4, log 4 3 = 0.793 Compare n 0.793 with f(n) = nlgn f(n) = (n log 4 3+ ℇ ) Case 3: check regularity condition: 3(n/4)lg(n/4) ≤ (3/4)nlgn = c f(n), c=3/4 ⇒ T(n) = (nlgn)

15 CS 477/677 - Lecture 415 Examples T(n) = 2T(n/2) + nlgn a = 2, b = 2, log 2 2 = 1 Compare n with f(n) = nlgn –seems like case 3 should apply f(n) must be polynomially larger by a factor of n ℇ In this case it is only larger by a factor of lgn

16 CS 477/677 - Lecture 416 The Sorting Problem Input: –A sequence of n numbers a 1, a 2,..., a n Output: –A permutation (reordering) a 1 ’, a 2 ’,..., a n ’ of the input sequence such that a 1 ’ ≤ a 2 ’ ≤ · · · ≤ a n ’

17 CS 477/677 - Lecture 417 Why Study Sorting Algorithms? There are a variety of situations that we can encounter –Do we have randomly ordered keys? –Are all keys distinct? –How large is the set of keys to be ordered? –Need guaranteed performance? –Does the algorithm sort in place? –Is the algorithm stable? Various algorithms are better suited to some of these situations

18 CS 477/677 - Lecture 418 Stability A STABLE sort preserves relative order of records with equal keys Sort file on first key: Sort file on second key: Records with key value 3 are not in order on first key!!

19 CS 477/677 - Lecture 419 Insertion Sort Idea: like sorting a hand of playing cards –Start with an empty left hand and the cards facing down on the table –Remove one card at a time from the table, and insert it into the correct position in the left hand compare it with each of the cards already in the hand, from right to left –The cards held in the left hand are sorted these cards were originally the top cards of the pile on the table

20 CS 477/677 - Lecture 420 Example

21 CS 477/677 - Lecture 421 INSERTION-SORT Alg.: INSERTION-SORT(A) for j ← 2 to n do key ← A[ j ] Insert A[ j ] into the sorted sequence A[1.. j -1] i ← j - 1 while i > 0 and A[i] > key do A[i + 1] ← A[i] i ← i – 1 A[i + 1] ← key Insertion sort – sorts the elements in place a8a8 a7a7 a6a6 a5a5 a4a4 a3a3 a2a2 a1a1 12345678 key

22 CS 477/677 - Lecture 422 Loop Invariant for Insertion Sort Alg.: INSERTION-SORT(A) for j ← 2 to n do key ← A[ j ] Insert A[ j ] into the sorted sequence A[1.. j -1] i ← j - 1 while i > 0 and A[i] > key do A[i + 1] ← A[i] i ← i – 1 A[i + 1] ← key Invariant: at the start of each iteration of the for loop, the elements in A[1.. j-1] are in sorted order

23 CS 477/677 - Lecture 423 Proving Loop Invariants Proving loop invariants works like induction Initialization (base case): –It is true prior to the first iteration of the loop Maintenance (inductive step): –If it is true before an iteration of the loop, it remains true before the next iteration Termination: –When the loop terminates, the invariant gives us a useful property that helps show that the algorithm is correct

24 CS 477/677 - Lecture 424 Loop Invariant for Insertion Sort Initialization: –Just before the first iteration, j = 2 : the subarray A[1.. j-1] = A[1], (the element originally in A[1] ) – is sorted

25 CS 477/677 - Lecture 425 Loop Invariant for Insertion Sort Maintenance: –the while inner loop moves A[j -1], A[j -2], A[j -3], and so on, by one position to the right until the proper position for key (which has the value that started out in A[j] ) is found –At that point, the value of key is placed into this position.

26 CS 477/677 - Lecture 426 Loop Invariant for Insertion Sort Termination: –The outer for loop ends when j = n + 1 ⇒ j-1 = n –Replace n with j-1 in the loop invariant: the subarray A[1.. n] consists of the elements originally in A[1.. n], but in sorted order The entire array is sorted! jj - 1

27 CS 477/677 - Lecture 427 Analysis of Insertion Sort cost times c 1 n c 2 n-1 0 n-1 c 4 n-1 c 5 c 6 c 7 c 8 n-1 INSERTION-SORT(A) for j ← 2 to n do key ← A[ j ] Insert A[ j ] into the sorted seq. A[1.. j -1] i ← j - 1 while i > 0 and A[i] > key do A[i + 1] ← A[i] i ← i – 1 A[i + 1] ← key

28 CS 477/677 - Lecture 428 Best Case Analysis The array is already sorted –A[i] ≤ key upon the first time the while loop test is run (when i = j -1) –t j = 1 T(n) = c 1 n + c 2 (n -1) + c 4 (n -1) + c 5 (n -1) + c 8 (n-1) = (c 1 + c 2 + c 4 + c 5 + c 8 )n - (c 2 + c 4 + c 5 + c 8 ) = an + b = Θ(n) “while i > 0 and A[i] > key”

29 CS 477/677 - Lecture 429 Worst Case Analysis The array is in reverse sorted order –Always A[i] > key in while loop test –Have to compare key with all elements to the left of the j -th position ⇒ compare with j-1 elements ⇒ t j = j a quadratic function of n T(n) = Θ(n 2 ) order of growth in n 2 “while i > 0 and A[i] > key”

30 CS 477/677 - Lecture 430 Comparisons and Exchanges in Insertion Sort INSERTION-SORT(A) for j ← 2 to n do key ← A[ j ] Insert A[ j ] into the sorted sequence A[1.. j -1] i ← j - 1 while i > 0 and A[i] > key do A[i + 1] ← A[i] i ← i – 1 A[i + 1] ← key cost times c 1 n c 2 n-1 0 n-1 c 4 n-1 c 5 c 6 c 7 c 8 n-1 ≈ n 2 /2 comparisons ≈ n 2 /2 exchanges

31 CS 477/677 - Lecture 431 Insertion Sort - Summary Idea: like sorting a hand of playing cards –Start with an empty left hand and the cards facing down on the table. –Remove one card at a time from the table, and insert it into the correct position in the left hand Advantages –Good running time for “almost sorted” arrays Θ(n) Disadvantages –Θ(n 2 ) running time in worst and average case –≈n 2 /2 comparisons and n 2 /2 exchanges

32 CS 477/677 - Lecture 432 Bubble Sort Idea: –Repeatedly pass through the array –Swaps adjacent elements that are out of order Easier to implement, but slower than Insertion sort 123n i 1329648 j

33 CS 477/677 - Lecture 433 Example 1329648 i = 1j 3129648 j 3219648 j 3291648 j 3296148 j 3296418 j 3296481 j 3296481 i = 2j 3964821 i = 3j 9648321 i = 4j 9684321 i = 5j 9864321 i = 6j 9864321 i = 7 j

34 CS 477/677 - Lecture 434 Bubble Sort Alg.: BUBBLESORT(A) for i ← 1 to length[A] do for j ← length[A] downto i + 1 do if A[j] < A[j -1] then exchange A[j] A[j-1] 1329648 i = 1j i

35 CS 477/677 - Lecture 435 Bubble-Sort Running Time T(n) = Θ(n 2 ) Alg.: BUBBLESORT(A) for i ← 1 to length[A] do for j ← length[A] downto i + 1 do if A[j] < A[j -1] then exchange A[j] A[j-1] T(n) =c 1 (n+1) +c2c2 c3c3 c4c4 = Θ(n) + (c 2 + c 3 + c 4 ) Comparisons: ≈n 2 /2 Exchanges: ≈n 2 /2

36 CS 477/677 - Lecture 436 Readings Chapter 4


Download ppt "Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 4."

Similar presentations


Ads by Google