31
CS 2430 Day 28

CS 2430 Day 28. Announcements We will have class in ULR 111 on Monday Exam 2 next Friday (sample exam will be distributed next week)

Embed Size (px)

Citation preview

Page 1: CS 2430 Day 28. Announcements We will have class in ULR 111 on Monday Exam 2 next Friday (sample exam will be distributed next week)

CS 2430

Day 28

Page 2: CS 2430 Day 28. Announcements We will have class in ULR 111 on Monday Exam 2 next Friday (sample exam will be distributed next week)

Announcements

• We will have class in ULR 111 on Monday

• Exam 2 next Friday (sample exam will be distributed next week)

Page 3: CS 2430 Day 28. Announcements We will have class in ULR 111 on Monday Exam 2 next Friday (sample exam will be distributed next week)

Agenda (long term)

• Computational complexity

• Searching

– Linear search

– Binary search

• Sorting

Page 4: CS 2430 Day 28. Announcements We will have class in ULR 111 on Monday Exam 2 next Friday (sample exam will be distributed next week)

Computational complexity and big O notation

Page 5: CS 2430 Day 28. Announcements We will have class in ULR 111 on Monday Exam 2 next Friday (sample exam will be distributed next week)

Algorithm efficiency

• Need a way to determine how “efficient” an algorithm so we can compare algorithms

• Could try timings, but they can vary a lot depending on the input and/or hardware

• Another approach is to count the number of steps required to process N elements

Page 6: CS 2430 Day 28. Announcements We will have class in ULR 111 on Monday Exam 2 next Friday (sample exam will be distributed next week)

Critical steps

• To get a feel for the behavior of the algorithm, we just count the dominant instructions

– for a sort, it would be the comparison of array elements

– for a search, it would be the comparison of an array element to the item being searched for

• The count will be some function of N, i.e., the number of items being processed

Page 7: CS 2430 Day 28. Announcements We will have class in ULR 111 on Monday Exam 2 next Friday (sample exam will be distributed next week)

Big O

• The “Big O” of an algorithm is the order of the fastest growing term of the number of critical steps performed (as a function of N)

• Examples:– 2N / 5 + 3 = O(N)– 42 = 42N0 = O(1)– 100N2 + 7N / 2 + 15 = O(N2)

– Log N + 31 = O(Log2 N), where “Log2 N” is the base-2 logarithm of N

Page 8: CS 2430 Day 28. Announcements We will have class in ULR 111 on Monday Exam 2 next Friday (sample exam will be distributed next week)

Big O examples

• Search an unsorted array?

O(N)

• Push onto a Stack?

O(1)

• Remove from a Bag?

O(N)

• Compute the union of two disjoint sets?

???

Page 9: CS 2430 Day 28. Announcements We will have class in ULR 111 on Monday Exam 2 next Friday (sample exam will be distributed next week)

Matrix multiplication

Page 10: CS 2430 Day 28. Announcements We will have class in ULR 111 on Monday Exam 2 next Friday (sample exam will be distributed next week)

public class Matrix

{

private double[][] data; // matrix entries

private int n; // dimension of matrix, assume square

. . .

public Matrix times(Matrix rhs)

{

Matrix lhs = this;

Matrix res = new Matrix(lhs.n); // creates empty Matrix

for (int i = 0; i < res.n; i++)

for (int j = 0; j < res.n; j++)

for (int k = 0; k < lhs.n; k++)

res.data[i][j] += lhs.data[i][k] * rhs.data[k][j];

return res;

}

}

Let N denote the matrix dimension, i.e., lhs.n

What is the big O of times()?

O(N3)

Page 11: CS 2430 Day 28. Announcements We will have class in ULR 111 on Monday Exam 2 next Friday (sample exam will be distributed next week)

Do YOU think it’s possible to do better than O(N3)?

Page 12: CS 2430 Day 28. Announcements We will have class in ULR 111 on Monday Exam 2 next Friday (sample exam will be distributed next week)

Yes!

Middle school matrix multiplication algorithm O(N3)

Strassen algorithm (1969) O(N2.807)

Coppersmith–Winograd algorithm (1987, 1990)

O(N2.376)

Williams algorithm (2011) O(N2.373)

<your name here> (20??) O(N2) – conjectured to be possible!

Page 13: CS 2430 Day 28. Announcements We will have class in ULR 111 on Monday Exam 2 next Friday (sample exam will be distributed next week)

Bigger big O and beyond!

Agrawal–Kayal–Saxena primality test algorithm (2002)

O(N10) –The exponent continues to decrease

Traveling Salesman problem (NP-complete)

O(N!) – brute forceO(2N) – “Dynamic programming”

Halting problem (undecidable) Cannot be solved by any computer given any amount of time. No big O!Cannot even be solved by humans (in general)

Page 14: CS 2430 Day 28. Announcements We will have class in ULR 111 on Monday Exam 2 next Friday (sample exam will be distributed next week)

Searching

Page 15: CS 2430 Day 28. Announcements We will have class in ULR 111 on Monday Exam 2 next Friday (sample exam will be distributed next week)

Linear searchprivate static <E> int lsearch(Comparable<E> a[], int n, E x)

{

for (int i = 0; i < n; i++)

if (a[i].compareTo(x) == 0)

return i;

return -1;

}

Can put this method in any container class that needs it

What is the worst case big O?

O(N)

Page 16: CS 2430 Day 28. Announcements We will have class in ULR 111 on Monday Exam 2 next Friday (sample exam will be distributed next week)

Linear search on average

• What is the average case efficiency of linear search?

• Given a random array that contains the target element X, how many comparisons needed?

• Answer = 1*Pr[X is in position 0] + 2*Pr[X is in position 1] + ∙ ∙ ∙ + N*Pr[X is in position N – 1] = ???

• Answer = O(N)

• How did we get this?

Page 17: CS 2430 Day 28. Announcements We will have class in ULR 111 on Monday Exam 2 next Friday (sample exam will be distributed next week)

How to get the answer!

Answer = 1*Pr[X is in position 0] + 2*Pr[X is in position 1] + ∙ ∙ ∙ +

N*Pr[X is in position N – 1]

= 1 / N + 2 / N + ∙ ∙ ∙ + N / N

= (1 / N) * N*(N+1) / 2

= (N + 1) / 2

= O(N)

Page 18: CS 2430 Day 28. Announcements We will have class in ULR 111 on Monday Exam 2 next Friday (sample exam will be distributed next week)

Can we do better if the array is sorted?

Page 19: CS 2430 Day 28. Announcements We will have class in ULR 111 on Monday Exam 2 next Friday (sample exam will be distributed next week)

Yes!

Page 20: CS 2430 Day 28. Announcements We will have class in ULR 111 on Monday Exam 2 next Friday (sample exam will be distributed next week)

Binary search

• Array MUST be in sorted order (either ascending or descending)

• Algorithm: how do you look up a word in the dictionary?

• At each iteration of the algorithm, disregard half of the input that is currently under consideration

• Extremely efficient!

Page 21: CS 2430 Day 28. Announcements We will have class in ULR 111 on Monday Exam 2 next Friday (sample exam will be distributed next week)

Search for “17”

2 3 5 7 11 13 17 19 23 29 31 37 41 43 47

2 3 5 7 11 13 17

11 13 17

17

(0 + 14) / 2 == 7

(0 + 6) / 2 == 3

(4 + 6) / 2 == 5

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

0 1 2 3 4 5 6

4 5 6

6

Page 22: CS 2430 Day 28. Announcements We will have class in ULR 111 on Monday Exam 2 next Friday (sample exam will be distributed next week)

Binary search algorithmprivate static <E> int bsearch(Comparable<E> a[], int n, E x)

{

int lo = 0, hi = n - 1;

while (lo <= hi)

{

int mid = (lo + hi) / 2;

int result = a[mid].compareTo(x);

if (result == 0)

return mid;

if (result > 0)

hi = mid - 1;

else

lo = mid + 1;

}

return -1;

}

Page 23: CS 2430 Day 28. Announcements We will have class in ULR 111 on Monday Exam 2 next Friday (sample exam will be distributed next week)

Another example

Page 24: CS 2430 Day 28. Announcements We will have class in ULR 111 on Monday Exam 2 next Friday (sample exam will be distributed next week)

private static <E> int bsearch(Comparable<E> a[], int n, E x)

{

int lo = 0, hi = n - 1;

while (lo <= hi)

{

int mid = (lo + hi) / 2;

int result = a[mid].compareTo(x);

if (result == 0)

return mid;

if (result > 0)

hi = mid - 1;

else

lo = mid + 1;

}

return -1;

}

2 3 5 7 11 13 17 19 23 29 31 37 41 43 47

lo

mid

hi

17 x

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

Page 25: CS 2430 Day 28. Announcements We will have class in ULR 111 on Monday Exam 2 next Friday (sample exam will be distributed next week)

private static <E> int bsearch(Comparable<E> a[], int n, E x)

{

int lo = 0, hi = n - 1;

while (lo <= hi)

{

int mid = (lo + hi) / 2;

int result = a[mid].compareTo(x);

if (result == 0)

return mid;

if (result > 0)

hi = mid - 1;

else

lo = mid + 1;

}

return -1;

}

2 3 5 7 11 13 17 19 23 29 31 37 41 43 47

lo

mid

hi

35 x

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14

Page 26: CS 2430 Day 28. Announcements We will have class in ULR 111 on Monday Exam 2 next Friday (sample exam will be distributed next week)

Binary search efficiency

Page 27: CS 2430 Day 28. Announcements We will have class in ULR 111 on Monday Exam 2 next Friday (sample exam will be distributed next week)

Worst case scenario

• What if target item not present?

• How many iterations of the loop?

• Each time through the loop, roughly half the array elements are disregarded

Page 28: CS 2430 Day 28. Announcements We will have class in ULR 111 on Monday Exam 2 next Friday (sample exam will be distributed next week)

Worst case big O

• How many times can you divide N by 2 before you get 1?

• Example: N = 100; 100 / 2 = 50; 50 / 2 = 25; 25 / 2 = 12; 12 / 2 = 6; 6 / 2 = 3; 3 / 2 = 1; Answer: 6 times

• In general, solve for r: N / 2r = 1

• We get: r = Log2 N (we’ll just write “Log N”)

• Final answer: O(Log N)

Page 29: CS 2430 Day 28. Announcements We will have class in ULR 111 on Monday Exam 2 next Friday (sample exam will be distributed next week)

Any questions?

Page 30: CS 2430 Day 28. Announcements We will have class in ULR 111 on Monday Exam 2 next Friday (sample exam will be distributed next week)

Review

• Linear search: O(N) (worst and average case)

• Binary search: O(Log N) (worst case)

• Binary search works with sorted input

• How to get the input sorted?

Page 31: CS 2430 Day 28. Announcements We will have class in ULR 111 on Monday Exam 2 next Friday (sample exam will be distributed next week)

Next: sorting