Personal tools
You are here: Home Classes Fall 2004 - Spring 2005 Old CS 160 Introduction to Algorithm Analysis
Navigation
Log in


Forgot your password?
« May 2008 »
Su Mo Tu We Th Fr Sa
123
456789 10
11121314151617
18192021222324
25262728293031
 
Document Actions

Introduction to Algorithm Analysis

by admin last modified 2005-05-25 15:41

Introduction to Algorithm Analysis

Are some programs better than others?

What are some characteristics of good programs?
  • correctness -- it carries out its specification
  • user friendliness
  • simplicity and clarity
  • robustness --  the ability to survive invalid inputs
  • maintainability
  • efficiency with respect to resource usage
    • CPU time
    • memory space
What makes one program more efficient than another?

Is it possible to measure this quantitatively?

A program's running time depends on:
  • the speed and architecture of the machine it is running on
  • the compiler which translates it into machine code
  • the input data set
  • the complexity of the algorithm it uses

For many programs, the running time depends on the size of the input data set, not the specific data values.  For such programs we can define a function T(n) to which represents the running time of the program on inputs of size n.


example  Determine T(n) for the following program:

int findsum(int[] array){
   int n = array.length;
   int sum = 0;
   int i = 0;
   while(i < n){
     sum += array[i];
     i++;
     }
   return sum;
}


example Determine T(n) for the following program:
int size(){
  if(isEmpty())
    return 0;
  else
    return 1 + getRest().size();
}


Best Case, Worst Case, and Average Case Behavior

For some programs, the running time depends not just on the input size, but the actual input values.  For those programs, analysis can determine
  • best-case running time
  • worst-case running time
  • average running time
for inputs of size n.

example  A program to locate a given Object in an array of Objects.  If the Object is found, the method returns true; otherwise, it returns false.

boolean locate(Object[] array, Object obj){
  boolean found = false;
  int k=0;
  while(!found && k<array.length){
    if(obj==array[k])
      found=true;
    else
      ++k;
  }
  return found;
}

What is the best-case running time of the program?  What is the worst-case running time?  What is the average-case running time?

Does it matter?

Suppose we have four programs which solve the same problem.  The running times of the programs on a data set of size n have been determined to be:

time for program one = 10n microseconds
time for program two = 10 n log10 n microseconds
time for program three = 10n2 microseconds
time for program four = 2n microseconds


What are the running times of these programs on input of size 10? 1000?  1000000?

a.  .0001 second; .01 second; 10 seconds
b.  .0001 second; .03 seconds; 1 minute
c.  .001 second; 10 seconds; 115 days
d.  .001 second; (over 1017 years on input of size 100)

There are many problems for which the running time of the best known algorithm is proportional to 2n.


 

Powered by Plone CMS, the Open Source Content Management System

This site conforms to the following standards: