Click on the presentation icon to the right to start the slide show.
Chapter 10 of Prichard and Carrano.
So far we have thought about
Now we will consider
We won't focus on the speed of particular implementations.
Should consider space, or memory, efficiency, too. But we won't.
Could write Java implementations of different algorithms and time their execution.
Bad idea, because relative execution times are affected by
Instead, we will count the number of “significant operations” in an algorithm.
Ignore the actual time each operation takes.
Node node = head; int i = 0; while (i < n) { node = node.next; ++i; } System.out.println(node.item);
How long does this take to find element ?
Node node = head; int i = 0; while (i < n) { node = node.next; ++i; } System.out.println(node.item);
How long does this take to find element index?
Well….it depends. Good answer! On what?
node = node.next, i < index, ++i, Node node = head; int i = 0; while (i < n) { node = node.next; ++i; } System.out.println(node.item);
node = node.next, i < index, ++i, So, total time is .
Can reduce total time by trying to speed up each operation.
But, time will always grow as grows. Can't be avoided!
Node node = head; int i = 0; while (i < n) { node = node.next; ++i; } System.out.println(node.item);
But, time will always grow as grows. Can't be avoided!
Unless, we come up with a new algorithm.
Unless, we come up with a new algorithm, and data structure!
System.out.println(dataArray[n]);
Time no longer depends on !
Which approach would you use if you often need to print the 1,000,000 element?
double x = 1; for (int i = 0; i < n; i++) for (int j = 0; j < i; j++) for (int k = 0; k < 5; k++) x = x * 2.0;
If x = x * 2.0 takes time, what is total time for all iterations?
double x = 1; for (int i = 0; i < n; i++) for (int j = 0; j < i; j++) for (int k = 0; k < 5; k++) x = x * 2.0;
If x = x * 2.0 takes time, what is total time for all iterations?
Loop for k takes time.
Loop for j takes .
Now, including the outer loop, the total time is
Now, including the outer loop, the total time is
Can you simplify this?
Now, including the outer loop, the total time is
Can you simplify this?
Sure!
But what are the units of this calculation?
Will depend on the computer.
Want to ignore this.
But what are the units of this calculation?
Want to ignore this.
Instead, just focus on how fast the value grows with the size of the problem.
Instead, just focus on how fast the value grows with the size of the problem.
double x = 1; for (int i = 0; i < n; i++) for (int j = 0; j < i; j++) for (int k = 0; k < 5; k++) x = x * 2.0;
For this code, the size of the problem is .
Our formula was . Ignoring all but leaves
Our formula was . Ignoring all but leaves
The expression increases with problem size by a factor of .
Denoted in Big-O notation by , or, it is of order .
Relative rates of growth:
We really are only interested in efficiency for time-consuming problems, ones with large values of .
When is large, is much bigger than .
So, let's ignore lower-order terms.
| Expression | Big-O |
|---|---|
| ? | |
| ? |
| Expression | Big-O |
|---|---|
| ? |
| Expression | Big-O |
|---|---|
Back to searching for the element in a list.
If the list has 1,000,000 elements, the worst case is that it will take 1,000,000 operations to find it.
How long will it take to find the
If all values of are equally likely, the average case performance will be 500,000.
If we now let be the size of the list, the
Worst case is usually easier to calculate, and maybe most relevant.
Big-O analysis applies to large problems.
If your application is small, don't worry about Big-O. Use the algorithm that
But, if you want to get a raise for that big insurance company, or get a job at that big search engine company,