View page as slide show

Click on the presentation icon to the right to start the slide show.

Algorithm Efficiency

Chapter 10 of Prichard and Carrano.

  • measuring the time efficiency of algorithms
  • approximation with Big-O notation
  • examples: sorting algorithms

Efficiency in Time

So far we have thought about

  • time to develop, test, debug and modify code.

Now we will consider

  • running time of an algorithm.

We won't focus on the speed of particular implementations.

  • Anyone can make an implementation run a little faster.
  • But, can get amazing gains in speed with a different algorithm!

Should consider space, or memory, efficiency, too. But we won't.

How to Measure Efficiency

Could write Java implementations of different algorithms and time their execution.

Bad idea, because relative execution times are affected by

  • differences in programming styles,
  • the choice of computer,
  • data used.

Instead, we will count the number of “significant operations” in an algorithm.

Ignore the actual time each operation takes.

How to Measure Efficiency

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 ?

How to Measure Efficiency

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?

  • value of
  • time to run node = node.next,
  • time to compare i < index,
  • time to run ++i,

How to Measure Efficiency

Node node = head;
int i = 0;
while (i < n) {
   node = node.next;
   ++i;
}
System.out.println(node.item);
  • value of
  • time to run node = node.next,
  • time to compare i < index,
  • time to run ++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!

How to Measure Efficiency

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.

How to Measure Efficiency

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?

How to Measure Efficiency

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?

How to Measure Efficiency

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

How to Measure Efficiency

Now, including the outer loop, the total time is

Can you simplify this?

How to Measure Efficiency

Now, including the outer loop, the total time is

Can you simplify this?

Sure!

How to Measure Efficiency

But what are the units of this calculation?

  • seconds
  • milliseconds

Will depend on the computer.

Want to ignore this.

How to Measure Efficiency

But what are the units of this calculation?

  • seconds
  • milliseconds

Want to ignore this.

Instead, just focus on how fast the value grows with the size of the problem.

How to Measure Efficiency

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

Big-O Notation

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 .

Big-O Notation

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.

Big-O Notation

Expression Big-O
?
?

Big-O Notation

Expression Big-O
?

Big-O Notation

Expression Big-O

Worst-Case Analysis

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

  • first element?
  • the 500,000 element?

If all values of are equally likely, the average case performance will be 500,000.

Worst-Case Analysis

If we now let be the size of the list, the

  • best case efficiency is
  • average case efficiency is
  • worst case efficiency is

Worst case is usually easier to calculate, and maybe most relevant.

Large Problems

Big-O analysis applies to large problems.

If your application is small, don't worry about Big-O. Use the algorithm that

  • is easy to understand
  • is easy to implement and debug
  • does not use too much memory.

But, if you want to get a raise for that big insurance company, or get a job at that big search engine company,

  • know how to analyze the efficiency of an algorithm,
  • practice picking or designing more efficient algorithms for large problems.
Recent changes RSS feed CC Attribution-Share Alike 3.0 Unported Driven by DokuWiki