EECS 311: Chapter 2 Notes Chris Riesbeck EECS Northwestern.

25
EECS 311: Chapter 2 Notes Chris Riesbeck EECS Northwestern

Transcript of EECS 311: Chapter 2 Notes Chris Riesbeck EECS Northwestern.

Page 1: EECS 311: Chapter 2 Notes Chris Riesbeck EECS Northwestern.

EECS 311: Chapter 2 Notes

Chris RiesbeckEECS Northwestern

Page 2: EECS 311: Chapter 2 Notes Chris Riesbeck EECS Northwestern.

Unless otherwise noted, all tables, graphs and code from Mark Allen Weiss' Data Structures and Algorithm Analysis in C++, 3rd ed, copyright © 2006 by Pearson Education, Inc.

Page 3: EECS 311: Chapter 2 Notes Chris Riesbeck EECS Northwestern.

Maximal Subsequence Sum Problem

Given a sequence of N positive and negative integers A1, A2, A3, …, An

Find the subsequence Aj, … Ak with the largest sum

Example: -2, 11, -4, 13, -5, -2

65 9 1320

Page 4: EECS 311: Chapter 2 Notes Chris Riesbeck EECS Northwestern.

Triple-Loop Algorithm

For every start pointFor every

end point

Add up the subsequence

Save the biggest

Page 5: EECS 311: Chapter 2 Notes Chris Riesbeck EECS Northwestern.

Algorithm 1 Run-Times

Page 6: EECS 311: Chapter 2 Notes Chris Riesbeck EECS Northwestern.

Double-Loop Algorithm

For every start point

For every end point

New sum is old sum + next itemSave the

biggest

Page 7: EECS 311: Chapter 2 Notes Chris Riesbeck EECS Northwestern.

Algorithm 2 Run-Times

Page 8: EECS 311: Chapter 2 Notes Chris Riesbeck EECS Northwestern.

Divide and Conquer Algorithm

Sum for 1 element

subsequence

Sums for max subsequences

in left and right halves

Sum for max left subsequence

ending on center

Sum for max right subsequence

starting right of centerReturn largest of

the 3 sums

Page 9: EECS 311: Chapter 2 Notes Chris Riesbeck EECS Northwestern.

Analysis of Algorithm 3To derive T(N), time to solve a problem with N items

Base case T(1) = O(1)

Recursive case T(N) = 2T(N/2) +…

… O(N) + …

… O(N) + …

… O(1)

Page 10: EECS 311: Chapter 2 Notes Chris Riesbeck EECS Northwestern.

Analyzing Divide and Conquer

T(1) = O(1) T(N) = 2T(N/2) + N

T(2) = 2T(1) + 2 = 4 = 2*2 T(4) = 2T(2) + 4 = 12 = 4*3 T(8) = 2T(4) + 8 = 32 = 8*4 T(16) = 2T(8) + 16 = 80 = 16*5

T(2k) = 2k * (k+1) For general N, O(N log N)

Page 11: EECS 311: Chapter 2 Notes Chris Riesbeck EECS Northwestern.

Algorithm 3 Run-Times

Page 12: EECS 311: Chapter 2 Notes Chris Riesbeck EECS Northwestern.

Single-Loop Algorithm

For every start point

New sum is old sum + next item

If bigger, save;if negative, forget and start over

Page 13: EECS 311: Chapter 2 Notes Chris Riesbeck EECS Northwestern.

Algorithm 4 Run-Times

Page 14: EECS 311: Chapter 2 Notes Chris Riesbeck EECS Northwestern.

Algorithmic Analysis

Page 15: EECS 311: Chapter 2 Notes Chris Riesbeck EECS Northwestern.

Run-times for small N

Page 16: EECS 311: Chapter 2 Notes Chris Riesbeck EECS Northwestern.

Run-times for large N

Page 17: EECS 311: Chapter 2 Notes Chris Riesbeck EECS Northwestern.

Typical Growth Rates

Don't confuse with log log N

which is < log N

Page 18: EECS 311: Chapter 2 Notes Chris Riesbeck EECS Northwestern.

Binary Search

Does this algorithm

always stop?

If it does, does it always return

the right answer?

If it does, how long does it take, in the worst case?

Page 19: EECS 311: Chapter 2 Notes Chris Riesbeck EECS Northwestern.

Does it halt? Find a measure M such that

you can prove it monotonically decrease on every iteration

the algorithm halts when M passes some threshold, e.g., 0

Binary search example: M = high – low M decreases by at least 1 every iteration algorithm halts when M < 0

Page 20: EECS 311: Chapter 2 Notes Chris Riesbeck EECS Northwestern.

Does it give the right answer? Proof by cases

When it returns a value, is it correct (no false positives)?

When it returns not found, is it correct (no false negatives)?

Page 21: EECS 311: Chapter 2 Notes Chris Riesbeck EECS Northwestern.

When it returns a value, is it correct? Proof by contradiction:

Assume desired property is false. Prove contradiction results.

Binary search example: 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.

Page 22: EECS 311: Chapter 2 Notes Chris Riesbeck EECS Northwestern.

When it returns not found, is it correct? Proof by induction:

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: Assume x not in a[]. Assume a[] has 1 element.

a[0] ≠ x and code correctly returns not found. Assume a[] has N + 1 elements.

Proof by cases: 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

Page 23: EECS 311: Chapter 2 Notes Chris Riesbeck EECS Northwestern.

Greatest Common Divisor

Does this algorithm

always stop?

If it does, does it always return

the right answer?

If it does, how long does it take, in the worst case?

Page 24: EECS 311: Chapter 2 Notes Chris Riesbeck EECS Northwestern.

Exponentiation

Does this algorithm

always stop?

If it does, does it always return

the right answer?

If it does, how long does it take, in the worst case?

Page 25: EECS 311: Chapter 2 Notes Chris Riesbeck EECS Northwestern.

Hailstone Numbersvoid printHailStones(int n) { cout << n << ":"; while (n > 1) { cout << " " << n; if ( n % 2 == 0 ) n /= 2; else n = 3 * n + 1; } cout << " " << n << endl;}http://en.wikipedia.org/wiki/Collatz_conjecture

Does this algorithm

always stop?

If it does, does it always return

the right answer?

If it does, how long does it take, in the worst case?