CS 139 - Algorithms

Average Algorithm

  1. Understand the problem:
    1. In averaging, a list of numbers is summed and the total is divided by the number of individual numbers.
    2. Special cases
      1. If there are no numbers, an average cannot be obtained.  (The zero case)
    3. Input to the process is a list of numbers.
    4. Output from the process is an average.
    5. As humans, we don't notice what kind of numbers we are dealing with.  Integers and real numbers are all the same.  The process is the same.
  2. Design a solution
    1. Obtain the list of numbers
      1. If verbally provided, write down each number as given one beneath the other in a column, lining up decimal positions.
      2. If provided in writing, copy the numbers onto the paper in a column, lining up decimal positioins.
    2. On your paper, write a label called count.
    3. Count up the number of numbers and write this beside the label.
    4. Draw a line horizontally underneath the numbers.
    5. Sum the numbers and put the total underneath the line.
    6. Write a label called result onto your paper.
    7. Divide the sum by count and write the result on your paper following the result label.
    8. Announce the result.
  3. Test the solution.
    1. Test cases should involve "normal" cases and outliers.
    2. Always think about 0.
    3. Consider the entire domain of available numbers.
    4. What would some good test cases be for this algorithm?

Airplane Algorithm

  1. Understand the problem:
    1. There are a number of different models of paper airplane.  
    2. You must choose one and try it.
    3. Input to the process is a plain sheet of 81/2 by 11 paper.
    4. Output from the process is a paper airplane that flies.
    5. As English speaking humans, our language is ambiguous, and it is hard to separate what we know from what must be stated.
  2. Design a solution - Some issues
    1. Using only language, one must determine how to orient the paper and describe the different folds.
  3. Some good solutions
    1. "Best Paper Airplane"
    2. Another solution site
    3. The common theme among a lot of instructions is a diagram.  Diagrams are the way we communicate precisely as humans.
    4. You didn't have a convenient way to diagram.  Clearly the written word leaves much room for interpretation.
  4. Some issues
    1. Lack of specificity
      1. Orient the paper in portrait style (short ends on top and bottom)
      2. Fold right side of paper over the left.
    2. Another example.
      1. ...
      2. Fold upper left corner toward the center.

Computer Algorithms

  1. Have their own language.
  2. You do need to be very specific.
  3. You cannot have any "and then a miracle happens" steps.

A good algorithm - Does your algorithm have these traits?

  1. Simple - Each step does one thing.  There are no unnecessary steps
  2. Precise - Unambiguous.  Proper sequencing of steps.
  3. Correct - Leads to a correct solution for all expected input.
  4. Complete - No missing steps.
  5. Abstraction - Probably not.  Abstraction is the idea that you might have steps with substeps.  For example, you could have specified how to get a piece of paper, but it is enough to tell the reader to obtain one.  They will fill in the detail and carry out the necessary steps (which could be different in different situations).