This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Chapter 1 Reasoning About Algorithms An algorithm takes some type of input and applies a sequence of steps in order to produce some type of output. In general, these inputs and outputs can take many forms. For example, input can come from the keyboard, a mouse’s movements or a sensor that is reading the temperature. Outputs might be shapes drawn on a screen, characters sent to a printer, or movements of a robot’s arm. For now, we will focus on offline algorithms, where all of the input is available when the algorithm begins running, and the algorithm is intended to produce some output and terminate. Online algorithms, where there are constant streams of inputs and outputs, are also fascinating objects to study but they are more complicated, so we start with offline algorithms. Many of the techniques we use in analyzing offline algorithms can be generalized to deal with online algorithms too. For example, an online problem can often be analyzed as an infinite sequence of steps, where each step is accomplished by an offline algorithm. For the offline algorithms we study, the inputs and outputs are finite, but usually unbounded. This means that any individual input given to the algorithm can be represented as a finite string of characters (chosen from a finite alphabet), but we put no limit on the length of the string. Thus, there are usually infinitely many possible inputs, but each possible input is of finite size. Similarly, the outputs produced by the algorithms are finite but unbounded. A problem specification describes 1. which inputs are permitted, and 2. which outputs are acceptable for each possible input. These two parts of the problem specification are called the preconditions and postconditions of the problem, respectively. When you design an algorithm to solve the problem, the preconditions are statements that you can assume are 1 2 CHAPTER 1. REASONING ABOUT ALGORITHMS true before the algorithm is run, and the postconditions are statements that ought to be true when (and if) the algorithm terminates. Example 1 Addition of two natural numbers. Precondition: the input must be a pair of natural numbers x and y . Postcondition: the output must be the integer x + y . (To be more precise, we might also specify the representation used for the inte gers. For example, we might say that they must be represented in binary with no leading 0’s.) Example 2 Finding the maximum element in an array of integers. Precondition: the input is an array A [1 ..n ] of integers (where n ≥ 1 ). Postcondition: the output must be some integer i ∈ { 1 ,...,n } such that for all j ∈ { 1 ,...,n } , A [ i ] ≥ A [ j ] . Notice that there may be several different acceptable outputs for a single input if the maximum value occurs several times in the array....
View
Full
Document
This note was uploaded on 02/13/2012 for the course CSE 3101 taught by Professor Andy during the Winter '10 term at York University.
 Winter '10
 andy
 Algorithms

Click to edit the document details