EECS 311 Chapter 2 Notes Chris Riesbeck EECS
EECS 311: Chapter 2 Notes Chris Riesbeck EECS Northwestern
o Unless otherwise noted, all tables, graphs and code from Mark Allen Weiss' Data Structures and Algorithm Analysis in C++, 3 rd ed, copyright © 2006 by Pearson Education, Inc.
Maximal Subsequence Sum Problem o o o Given a sequence of N positive and negative integers A 1, A 2, A 3, …, An Find the subsequence Aj, … Ak with the largest sum Example: -2, 11, -4, 13, -5, -2 5 209 13 6
Triple-Loop Algorithm For every start Forpoint every end point Add up the subsequence Save the biggest
Algorithm 1 Run-Times
Double-Loop Algorithm For every start point For every end point New sum is old sum + Save thenext item biggest
Algorithm 2 Run-Times
Divide and Conquer Algorithm Sum for max Sumright for 1 subsequence element startingsubsequence right of center Return largest of the 3 sums Sums for max subsequences in left and right halves Sum for max left subsequence ending on center
Analysis of Algorithm 3 To derive T(N), time to solve a problem with N items … O(N) + … Base case T(1) = O(1) Recursive case T(N) = 2 T(N/2) +… … O(N) + … … O(1)
Analyzing Divide and Conquer o o T(1) = O(1) T(N) = 2 T(N/2) + N n n o o T(2) = 2 T(1) + 2 = 4 = 2*2 T(4) = 2 T(2) + 4 = 12 = 4*3 T(8) = 2 T(4) + 8 = 32 = 8*4 T(16) = 2 T(8) + 16 = 80 = 16*5 T(2 k) = 2 k * (k+1) For general N, O(N log N)
Algorithm 3 Run-Times
Single-Loop Algorithm For every start point New sum is old sum + next item If bigger, save; if negative, forget and start over
Algorithm 4 Run-Times
Algorithmic Analysis
Run-times for small N
Run-times for large N
Typical Growth Rates Don't confuse with log N which is < log N
Binary Search If it does, Does this how long algorithm does it take, always stop? in the worst case? If it does, does it always return the right answer?
Does it halt? o Find a measure M such that n n o you can prove it monotonically decrease on every iteration the algorithm halts when M passes some threshold, e. g. , 0 Binary search example: n n n M = high – low M decreases by at least 1 every iteration algorithm halts when M < 0
Does it give the right answer? o Proof by cases n When it returns a value, is it correct (no false positives)? n When it returns not found, is it correct (no false negatives)?
When it returns a value, is it correct? o Proof by contradiction: n n o Assume desired property is false. Prove contradiction results. Binary search example: n n Assume k ≠ -1 is returned and a[k] ≠ x. k is returned on line 19. This means a[k] is neither > nor < than x. a[k] must be <, > or =. Contradiction.
When it returns not found, is it correct? o Proof by induction: n n n o Prove P is true for K , typically 1 or 2 Prove P is true for N+1 if it’s true for N Then P is true for all N ≥ K Binary search example: n n Assume x not in a[]. Assume a[] has 1 element. o n a[0] ≠ x and code correctly returns not found. Assume a[] has N + 1 elements. o Proof by cases: n n If a[mid] < x, search will look at a[mid] … a[high], which has less than N elements. By assumption, that search returns correct answer. Similarly if a[mid] > x. Ergo, binary search returns not found correctly for all N ≥ 1
Greatest Common Divisor If it does, Does this how long algorithm does it take, always stop? in the worst case? If it does, does it always return the right answer?
Exponentiation If it does, Does this how long algorithm does it take, always stop? in the worst case? If it does, does it always return the right answer?
Hailstone Numbers void print. Hail. Stones(int n) { cout << n << ": "; If it does, Does this while (n > 1) { how long algorithm cout << " " << n; does it take, always stop? in the worst if ( n % 2 == 0 ) case? n /= 2; else If it does, does it always return n = 3 * n + 1; the right } answer? cout << " " << n << endl; } http: //en. wikipedia. org/wiki/Collatz_conjecture
- Slides: 25