notes-v1 - Chapter 1 Reasoning About Algorithms An...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
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
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
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. An algorithm is a correct solution to a problem if, for every possible input, it produces an output and the output produced is acceptable, according to the specification of the problem.
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern