SHEN’s CLASS NOTES
Chapter 1 Introduction
1.1 Algorithms and problems
An algorithm
is a well-defined computational procedure to solve a computational problem (or problem for short).
An algorithm must be correct. A good algorithm must be readable, time-efficient, 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.
1
SHEN’s CLASS NOTES
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.
1.2 Relationship between an algorithm and a data structure
A data structure
is a way to store and organize data in order to facilitate accesses and modifications.
2
SHEN’s CLASS NOTES
An efficient algorithm relies upon efficient data structure. No single data structure works well for all purposes. It is important to get familiar with commonly used data structures that will be overviewed in Chapters 10-14.
1.3 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.
3
SHEN’s CLASS NOTES
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.
Problem: sorting n numbers Input:
A[1], A[2], …, A[n]
Output: arrange the n numbers such that
A[1] ? A[2] ? …? A[n].
4
SHEN’s CLASS NOTES
Algorithm Selection Sort (A[1..n]); 1 2 3 4 5 6 7
for (i = 1, i ? n, i++) End
do { key = i
for (j = i, j ? n, j++)
do if A[j] < A[key]
then key ? j
A[i] ? A[key]
We could call the language used in example 1 pseudo C++. However, this is not important to identify which language it looks like. We accept the presentation of an algorithm as far as its steps are clearly implementable. Example 2 shows another way to present the same sorting algorithm of Example 1.
Example 2.
5