CompSci-61B Lecture Topic 4
Algorithm Efficiency

Instructor: Prof. Robert Burns

Ref: Carrano ch.9
elapsed time ref: exampledepot.com

Counting Numbers Of Operations
operations, comparisons, and assignments take time
search loop example:

for (int i = 0; i < n; i++)
  if (a[i] == x)
    break;
n operations: at most, n, average n/2
n comparisons: at most, n, average n/2
measuring elapsed time in milliseconds:
long start = System.currentTimeMillis();
do something here...
long end = System.currentTimeMillis();
long elapsedTimeMillis = end - start;

Scaling Data Structure Size
the effect on efficiency: #of operations, etc.
in "search loop" example:
  doubling n doubles the time

Big Oh
a way to express how an algorithm scales
in "search loop" example: "O(n)"
to get big-oh:
  write formula for worst case #of operations
  simplify the formula,
    so that for values of n
    higher than some number,
    the simplified formula is
    >= the original formula
    times some constant
the answer is not unique!
  so keep it tight

in the search loop:
#of operations = 2 * n
for n >= 1, O(2n) works
  so does O(3n)
for n >= 2, O(n) works
  so does O(n2)
but the tightest is O(n)

Big Omega
opposite of big-oh: lower bound
to get big-omega:
  write formula for best case #of operations
  simplify the formula,
    so that for values of n
    higher than some number,
    the simplified formula is
    <= the original formula
    times some constant
again, the answer is not unique

Big Theta
when big-oh and big-omega overlap:
  choose formula that satisfies both
unsuccessful search loop is big-theta(n)
(no solution for successful search loop)

Operations That Are O(1)
retrieval from an array
adding to end of an array-based list
adding to front of a linked list
retrieving the head of a linked list
retrieving the tail of a doubly-linked list
retrieving the 3rd node's value in a linked list
retrieving from a perfect hash table (no collisions)
measuring elapsed time: independent of "n"

Operations That Are O(log n)
note that O(log n), O(lg n),and O(ln n) are the
  same: different by a constant multiplier

binary search or bisection
rebalance of a red-black tree

Operations That Are O(n)
adding to middle of an array-based list
traversing a list
retrieving an indexed value from a linked list
radix sort

Operations That Are O(n log n)
mergesort, quicksort, heapsort

Operations That Are O(n2)
insertion sort, selection sort

Operations That Are O(2n)
recursive Fibonacci solution

Confirming Big Oh Determinations
counting operations with private int counters
measuring elapsed time
e.g. for O(n2):
  try for various values of n
  elapsed time / n2 should be roughly constant

sample test results for O(n2) operation:
nn2elapsed
millis
elapsed
n2
norm
100010613241.3241.00
20004 x 10654361.3591.03
40001.6 x 107207841.2990.98
80006.4 x 107861201.3451.02
"norm" is the col 4 value divided by the first line's column 4 value


[ Home | Contact Prof. Burns ]