This preview shows pages 1–5. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: SHEN’S CLASS NOTES191 1 Chapter Four Algorithms 4.1 Introduction An algorithm is a welldefined computational procedure to solve a computational problem (or problem for short). An algorithm must be correct. A good algorithm must be readable, timeefficient, space efficient. A problem defines the requirements that valid output values must satisfy for a given set of input values. Usually, we are only interested in those problems that allow infinitely many different sets and sizes of input values. SHEN’S CLASS NOTES191 2 For example, the sorting problem takes n numbers as input set and requires the n numbers be arranged in descending or ascending order as the valid output, where the n can be any positive integer. For a particular set of input values, the problem is referred as an instance of the problem. Therefore, a problem must contain infinitely many instances. Solving a problem is to find a valid output for any given input, which is done by an algorithm. Relationship Between an Algorithm and A program A program is a piece of code written in a particular language such as Pascal, C, C++, Java, or even some assembly language. Therefore, programs are language dependent or even machine dependent. A program can often be viewed as a detailed implementation of an algorithm with some limitations such as memory capacity, the largest number allowed, the largest size of an array, etc. SHEN’S CLASS NOTES191 3 An algorithm is of more abstract notion. It is language independent, machine independent. An algorithm is well defined as far as its steps can clearly implemented by any known language. Algorithms and programs are very closed related. We often use a known real language such as Pascal, C, C++ to describe an algorithm. Then, the algorithm looks like a program. However, in such a program, the grammar of the language will not be strictly followed and some notations may be changed to make the algorithm more readable. Such a language is called a pseudo language. Example 1 (4.1.1) See page 147 Finding the Maximum of Three Numbers. Leave this for students to read. Rigorous to say, this is not an algorithm, but a piece of program. Example 2 (4.1.2) Finding the Maximum Value in a sequence. The following algorithm finds the largest of the numbers SHEN’S CLASS NOTES191 4 s 1 , s 2 , …, s n . Input: s , n / / s represents the hole sequence. Output: large (the largest value in the sequence s ). max ( s , n ) { large = s 1 for i = 2 to n if ( s i > large ) large = s i Return large } Let us prove that the above algorithm is correct. We claim that the following loop invariant is true: The value in large is the largest value in the subsequence s 1 , s 2 , …, s i ....
View
Full
Document
This note was uploaded on 04/12/2008 for the course CS 191 taught by Professor Shen during the Fall '06 term at University of MissouriKansas City .
 Fall '06
 Shen
 Algorithms

Click to edit the document details