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

Slides:



Advertisements
Similar presentations
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 6.
Advertisements

Analysis of Algorithms CS 477/677 Linear Sorting Instructor: George Bebis ( Chapter 8 )
CS 253: Algorithms Chapter 2 Sorting Insertion sort Bubble Sort Selection sort Run-Time Analysis Credit: Dr. George Bebis.
Comp 122, Spring 2004 Divide and Conquer (Merge Sort)
11 Computer Algorithms Lecture 6 Recurrence Ch. 4 (till Master Theorem) Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 5.
Analysis of Algorithms CS 477/677 Sorting – Part B Instructor: George Bebis (Chapter 7)
CS 253: Algorithms Chapter 7 Mergesort Quicksort Credit: Dr. George Bebis.
CS 253: Algorithms Chapter 8 Sorting in Linear Time Credit: Dr. George Bebis.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Sorting. Input: A sequence of n numbers a 1, …, a n Output: A reordering a 1 ’, …, a n ’, such that a 1 ’ < … < a n ’
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu.
Analysis of Algorithms CS 477/677 Midterm Exam Review Instructor: George Bebis.
Lecture 5: Master Theorem and Linear Time Sorting
CS 253: Algorithms Chapter 4 Divide-and-Conquer Recurrences Master Theorem Credit: Dr. George Bebis.
Analysis of Algorithms CS 477/677
CS Main Questions Given that the computer is the Great Symbol Manipulator, there are three main questions in the field of computer science: What kinds.
Analysis of Algorithms CS 477/677
Analysis of Algorithms CS 477/677 Recurrences Instructor: George Bebis (Appendix A, Chapter 4)
Analysis of Algorithms
Analysis of Algorithms CS 477/677
Analyzing Recursive Algorithms A recursive algorithm can often be described by a recurrence equation that describes the overall runtime on a problem of.
1 Computer Algorithms Lecture 7 Master Theorem Some of these slides are courtesy of D. Plaisted, UNC and M. Nicolescu, UNR.
Lecture 2 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
CMPT 438 Algorithms. Why Study Algorithms? Necessary in any computer programming problem ▫Improve algorithm efficiency: run faster, process more data,
Analysis of Algorithms CS 477/677
Getting Started Introduction to Algorithms Jeff Chastine.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 3.
Algorithms IS 320 Spring 2015 Sorting. 2 The Sorting Problem Input: –A sequence of n numbers a 1, a 2,..., a n Output: –A permutation (reordering) a 1.
COSC 3101A - Design and Analysis of Algorithms 2 Asymptotic Notations Continued Proof of Correctness: Loop Invariant Designing Algorithms: Divide and Conquer.
Divide-and-Conquer UNC Chapel HillZ. Guo. Divide-and-Conquer It’s a technique instead of an algorithm Recursive in structure – Divide the problem into.
1/6/20161 CS 3343: Analysis of Algorithms Lecture 2: Asymptotic Notations.
Midterm Review 1. Midterm Exam Thursday, October 15 in classroom 75 minutes Exam structure: –TRUE/FALSE questions –short questions on the topics discussed.
COSC 3101A - Design and Analysis of Algorithms 6 Lower Bounds for Sorting Counting / Radix / Bucket Sort Many of these slides are taken from Monica Nicolescu,
1 Computer Algorithms Lecture 8 Sorting Algorithms Some of these slides are courtesy of D. Plaisted, UNC and M. Nicolescu, UNR.
Lecture 4 1 Advance Analysis of Algorithms. Selection Sort 2 Summary of Steps Find the smallest element in the array Exchange it with the element in the.
2IS80 Fundamentals of Informatics Fall 2015 Lecture 6: Sorting and Searching.
COSC 3101A - Design and Analysis of Algorithms 3 Recurrences Master’s Method Heapsort and Priority Queue Many of the slides are taken from Monica Nicolescu’s.
1 Ch. 2: Getting Started. 2 About this lecture Study a few simple algorithms for sorting – Insertion Sort – Selection Sort (Exercise) – Merge Sort Show.
Lecture 2 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
Recurrences (in color) It continues…. Recurrences When an algorithm calls itself recursively, its running time is described by a recurrence. When an algorithm.
Sorting. 2 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.
CS6045: Advanced Algorithms Sorting Algorithms. Sorting Input: sequence of numbers Output: a sorted sequence.
Recurrences It continues… Jeff Chastine. Recurrences When an algorithm calls itself recursively, its running time is described by a recurrence. A recurrence.
Algorithms Sorting – Part 3.
Lecture 2 Algorithm Analysis
CMPT 438 Algorithms.
Analysis of Algorithms CS 477/677
Recursion Ali.
Analysis of Algorithms CS 477/677
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
CS 3343: Analysis of Algorithms
Analysis of Algorithms CS 477/677
CS 3343: Analysis of Algorithms
Bubble, Selection & Insertion sort
Advance Analysis of Lecture No 9 Institute of Southern Punjab Multan
Lecture 3 / 4 Algorithm Analysis
Lecture No 6 Advance Analysis of Institute of Southern Punjab Multan
Divide and Conquer (Merge Sort)
Algorithms: the big picture
Ch. 2: Getting Started.
Analysis of Algorithms
Analysis of Algorithms
Introduction To Algorithms
Algorithms Recurrences.
Algorithms Sorting.
The Selection Problem.
Algorithms CSCI 235, Spring 2019 Lecture 17 Quick Sort II
Analysis of Algorithms CS 477/677
Presentation transcript:

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

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

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

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 )

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 )

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

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

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

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

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)

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)

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 )

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)

CS 477/677 - Lecture 414 Examples T(n) = 3T(n/4) + nlgn a = 3, b = 4, log 4 3 = Compare n 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)

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

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 ’

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

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

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

CS 477/677 - Lecture 420 Example

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 a1a key

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

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

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

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.

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

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

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”

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”

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

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

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 j

CS 477/677 - Lecture 433 Example i = 1j j j j j j j i = 2j i = 3j i = 4j i = 5j i = 6j i = 7 j

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] i = 1j i

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

CS 477/677 - Lecture 436 Readings Chapter 4