Вы находитесь на странице: 1из 11

Chapter 1.

See page 2; quoted: ... a person does not really understand something until after teaching it to someone else

actually:

a person does not REALLY understand something until after teaching it to a computer.

An algorithm is a sequence of unambiguous instructions for solving a problem, i.e. for obtaining a required output for any legitimate input in a finite amount of time.

An algorithm is written in pseudocode, i.e. a mixture of a natural language and programming language-like construct if then if then else for do while do repeat until

1. 2. 3. 4. 5. 6. 7. 8. 9.

understand the problem ascertain the capabilities of the computational device choose between exact and approximate problem solving decide on appropriate data structures determine algorithm design techniques select the method of specifying an algorithm prove the algorithms correctness analyze the algorithm code the algorithm

the most important problem types are:


1. sorting
rearrange items of a given list in a specific order, based on some key

2. searching
find a given search key in a given set

3. string processing
search a word in a text

4. graph problems
includes graph traversal problems, shortest-path algorithms, topological sorting, graph coloring problems

5. combinatorial problems
e.g. traveling salesman

next

6. geometric problems
the closest-pair problem, the convex hull problem

7. numerical problems
solve equations & systems of equations, evaluate functions, etc.

Example:

ALGORITHM Euclid (m, n) // compute GCD(m, n) by Euclids algorithm // input: two non-negative, not-both-zero integers m and n // output: Greatest Common Divisor of m and n

while n 0 do r m mod n m n n r return m

Example:

ALGORITHM Euclid (m, n) // compute GCD(m, n) by Euclids algorithm


// input: two non-negative, not-both-zero integers m and n // output: Greatest Common Divisor of m and n

while n 0 do r m mod n m n n r return m Input : m and n Output: the greatest common divisor of m and n Time : ?

Example:
Algorithm sequential search (A[0..n-1], K) (see page 47)

// searches for a given value in a given array by sequential search // input: an array A[0..n-1] and a search key K // output: returns the index of the first element of A that matches K // or -1 if there are no matching elements i 0 while i < n and A[i] K do i i + 1 if i < n return i else return -1

Example:
Algorithm sequential search (A[0..n-1], K) (see page 47)
// searches for a given value in a given array by sequential search // input: an array A[0..n-1] and a search key K // output: returns the index of the first element of A that matches K // or -1 if there are no matching elements

i 0 while i < n and A[i] K do i i + 1 if i < n return i else return -1

Input: A[0..n-1], K Time: ?

output: j such that A[j] = K or 1

Вам также может понравиться