Lecture Notes 02 (17 January 2002)

Algorithms


Overall Reading
Brookshear: Ch. 0.1, 4.1-4.2

Outline:

  • The Concept of an Algorithm (Ch. 4.1 [Br])
  • Examples from daily life
  • Pitfalls of ill-defined algorithms
  • Algorithm Representation (Ch. 4.2 [Br])

  • Examples: Elementary School Arithmetic

  • The Concept of an Algorithm (Ch. 4.1 [Br])

    "An algorithm is an ordered set of unambiguous, executable steps, defining a terminating process"

    Informally, an algorithm is a process which can be followed "mindlessly."
  • Examples form real world

    We are given directions all of the time: cooking, electronics, music.




  • Examples of ill-defined algorithms

  • ordered?

    Creating a "To Do" list such as

      Pick up groceries
      Pay the bills
      Practice piano
      Prepare a lecture
    
    Is not the same as defining an algorithm

  • unambiguous?

    An algorithm for organizing my mail:

      For each piece of mail received,
         * If it is a bill, pay it
         * If it is an advertisement, throw it away.
         * If it is addressed to my wife, put it in pile "Susan"
         * If it is a postcard, place it on the refrigerator door
         * If it is in an oversized envelope put it on the desk
    
    If following this algorithm, what am I supposed to do with a postcard, addressed to my wife, from Domino's Pizza?

  • executable?

    Algorithm for breaking the world record for the high-jump:

    1. Set the bar at 2.46 meters
    2. Take 30 paces backwards and face the bar
    3. Run as fast as you can
    4. Jump over the bar, without knocking it down
    5. Celebrate by dancing
    
    Algorithm for a secure cryptography scheme:
    1. Let X = the largest possible prime number
    2. Let Y = the second largest prime number
    3. Let P = X*Y be your "public key" for encryption
    

  • terminating?

    Shampoo instructions:

      1. Wet hair
      2. Lather shampoo
      3. Rinse
      4. Repeat
    

  • Algorithm Representation (Ch. 4.2 [Br])

    We wish to try to make a distinction between the algorithm itself and the representation.
  • Whether instructions are written in English, Japanese or Spanish, the underlying algorithm is the same.

  • Sometimes, the algorithm representation depends upon the target audience.

  • If talking to a meteorologist, you can simply say "Convert the Celsius reading to its Fahrenheit equivalent."
    But if talking to a layman, you would have to explain, F = (9/5)C + 32.

  • A "pinch" of backing soda may seem great to a chef, but I get very nervous about whether I am using the correct size pinch.

  • If talking to an adult, you might simply ask them to perform the sum: 345 + 1422.
    But if talking to a grade school student, you need to give more detailed instructons.
    [let's revisit this below]

  • If talking to a Pentium processor, you better represent your algorithm with instructions which it understands.
  • Note: Do not confuse lack of detail in the representation with inherent ambiguity in the algorithm.

  • Primitive
    For each particular domain, we usually consider a set of basic building blocks, which need no further explanation.
    Examples: Origami, elementary arithmetic, logic circuits, CPU instructions, programming languages

    When creating a system of primitives, must define:

  • Syntax - how is the primitive represented symbolically (what words or pictures are used)
  • Semanics - what is the underlying meaning of the primitive.
  • Pseduocode
    In formal programming languages, you need to be very precise syntactically because an automated translator will be converting your program into machine code.

    When communicating between humans, we want to be precise, but not overly structured. We will give more examples later, but what we imagine is being able to describe algorithms usning notation such as:

      if (sales have decreased)
         then (lower the price by 5%)
         else (raise the price by 2%)
    

  • Examples: Elementary School Arithmetic

  • Euclidean Algorithm for finding the greatest common divisor of two positive integers (as described on p.4 of [Br])

    Description: This algorithm assumes that its input consists of two positive integers and proceeds to compute the greatest common divisor of these two values.

    Procedure:

  • Step 1. Assign M and N the value of the larger and smaller of the two input values, respectively.

  • Step 2. Divide M by N, and call the remainder R.

  • Step 3. If R is not 0, then assign M the value of N, assign N the value of R, and return to step 2; otherwise, the greatest common divisor is the value currently assigned to N.
  • Example: GCD(48,18)


  • Adding two whole numbers

    We all learned how to do addition in elementary school. At this point, we seem to know the process to follow by heart.

    Example: What is 3587+206?

    What is that process? Can you formalize the algorithm.

    Please note: Do not simply walk someone through this particular example; explain the general process.

    Note also: An algorithm is the process. In describing it, you do not need to explain why it works, simply the process itself.





    Here is my attempt.


  • Adding a list of many whole numbers

  • Long Division

    Is process for calculating 8/7 terminating?

  • Alphabetizing items

  • comp150 Class Page
    mhg@cs.luc.edu
    Last modified: 17 January 2002