CPS130, Lecture 1: Introduction to Algorithms

advertisement
9/2/2002
CPS130, Lecture 1: Introduction to Algorithms
1. Welcome
See the 130 homepage: http://www.cs.duke.edu/courses/fall02/cps130/ for
administrative details. This is a mathy type course and is not always the most popular.
But that will change, I hope, this semester. Work hard and don’t cheat and everything
will be great! The key is hard work consistently.
2. Algorithms
An Algorithm is a procedure or a methodology for transforming a given input to a welldefined output. Algorithms have been around since way before modern electronic
computers. Cooking recipes are essentially algorithms. Programming can be viewed as
the implementation of an algorithm as a precise sequence of steps or instructions in a
specific environment; e.g., computer programming an algorithm for a specific hardware
platform. Often it is convenient to explain an algorithm is a pseudo programming
language. Algorithms are used to solve problems and the problem modeling environment
gives rise to the input and output sets.
Mathematically, an algorithm could be thought of as a function, o  f (i; n) , where i  I ,
the input set and o  O , the output set. The integer n specifies that f produces the output
string “f terminated after n steps” when this is true; this string is in O. It is reasonable to
expect that n should be related to the “size” of i. If n is a known function of i, we could
remove it from the argument list of f, but let’s leave it there.
CPS130 teaches design techniques for constructing algorithms and analysis techniques
for studying how they execute; e.g., determining n. It also studies the structure of the
sets I and O.
Example 1: find an f to compute the max of an array of m positive integers.
Example 2: Determine whether a graph, G, is planar and, if so, imbed it in the plane.
Example 3: Feed Betsy dinner when
I  {kibbles, squash,carrots,salmon,shredded cheese} .
3. Numbers
Numbers begin from childhood with the natural numbers, N ={0,1,2,3,…}. Each number,
i+1, is regarded as the “successor of i. “ +” is regarded as a successor function that takes i
to i+1. The existence of the successor set N is closely related to the “principle of
mathematical induction” which we will use extensively.
1
From the natural numbers we get the integers, I = {0, 1, 2, 3,...} by allowing the
natural numbers to have additive inverses (i- + i =0, i  N). Next we get the rational
numbers, R by allowing all ratios a/b, a and b integer except b  0. Essentially all of
cps130 uses just these number sets. But what about:
Example 4: find an algorithm to compute
2.
Well, that not Algorithms, that’s Numerical Analysis, cps150. How silly! The right view
is that cps130 emphasizes discrete or combinatorial algorithms which tend to use the
above number sets and cps150 emphasizes numerical algorithms (like in calculus) and
needs real numbers even though essentially no computer can represent real numbers
exactly (or even all the rationals because they use floating point numbers in fixed
precision).
Now
2 = lim x i
i 
where
x i1  1/ 2(x i  2 / x i ), x 0  1 (why?).
Clearly each xi is rational but 2  a/b, a, b integer. For if so, then a2 = 2 b2 and there
cannot be the same number of factors of 2 on each side of the equation. So to get the real
numbers, R, we need to stick in these other numbers represented by their converging
(Cauchy) sequences as above. Or we can just assume the existence of numbers that
satisfy the axioms of the reals as in most calculus books (remember lub and glb?). This
assumed number set contains also the rationals, integers and natural numbers. By the
way, this is where  ,  and limits come into play in calculus but they will be used less in
cps130.
4. Functions
A function, f, from a set S to a set T maps elements in S to elements in T such that
elements in S are uniquely mapped; that is, if y = f(x) and z = f(x), then y = z. The set
D  S of elements, which map to some element in T is called the domain of f; the set R
 T of elements to which some element in D is mapped is called the range of f.
Example 5: f(x) = x is a function which maps [0,1] to [0,1].
2
Example 6: l(x) = ln(x)  y such that e y  x. See below.
ln(x); x from 1 to 10, space 1; y from 1 to 10, space 1
Functions are special cases of relations, and we will see later that when S and T are finite
we can represent them conveniently as directed graphs.
We are interested in how functions grow so we can use them to bound the computational
requirements of algorithms. An important class of functions will be the polyplogq class:
PpLq = { f | f(x) = xp (log x)q, p and q nonzero natural numbers, x  1}.
Let’s first review polynomials, the exponential function, and the log function. By the
way, although we are writing f(x) and think of x to be a real number, most cps130
numeric functions will be functions from N to N and we may write f(n) to indicate this.
Polynomials
A polynomial of degree n is a function:
n
pn (x)   a i x i .
i 0
When the ai are complex numbers (review!) with an =1, p n can be written as
3
n
pn (z)   (z  z j ) ;
j1
that is, the polynomial has n complex roots at the zj.
Example 6:
p 2 (z)  az 2  bz  c , a,b,c all real, has roots r   (b  b2  4ac) / 2a, a  0 by the
quadratic formula. The two roots, if complex, are a conjugate pair. On the other hand any
cubic (or odd degree) polynomial must have one real root.
Polynomials, as in pn , can be represented by the array A = [a0, a1, …, an] . To evaluate
pn(x) we let
Pn(x) = an
and
Pi-1(x) = ai-1 + x Pi(x), 1  i  n (as on line 4 of text, p.39).
This “nesting” of the Pj gives pn(x) = P0(x) and is called Horner’s rule.
Exponential and Log Functions
The exponential function, e(x) = ex , for any real number x can be defined by it’s power
series expansion

e x   (x n / n!)
n 0
or as the solution to the simple differential equation
(ODE)
dy
 y with y(0) =1.
dx
4
ex ; x from 1 to 10, space 1; y from 1 to 10, space 1
Notice that ln(x) and ex are inverse function since
eln(x)  x for x  (0,) and ln(ex )  x for all real x.
The key relationship between e and ln is that
y = ex if and only if (iff) x = ln(y) , y>0.
(LGEX)
From calculus (or from ODE above) we recall that
x
ln(x)   (1/ s)ds , x>0 ;
0
also

ln(1  x)   (1)i 1x i / i, for |x|  1.
i 1
See text, pp 52-54 for more properties. Logarithms do not need to use e 2.71828… as a
base. For any b>0, the generalization of LGEX above is
y = bx iff x = logb(y) , y>0.
5
When b=1, y=1 for all x and this is uninteresting. For 0<b<1, you can take b+ = 1/b to
convert to a base greater than unity. The most common bases are b = e, 2 , or 10; we will
denote these logs as ln, lg, and log, respectively. For any base we have
logb(x y) = logb(x) + logb(y) , x>0, y>0
and
logb(xp) = p logb(x), x>0, p real .
If a>1 and b>1 are two bases and y = ax = bz, then by definition and the property above,
z = logb(y) = logb(ax) = x logb(a) = loga(y) logb(a). That is, to convert from logb to loga ,
we use
loga(y) = logb(y)/logb(a).
As a consequence, for any base b>1, all graphs bx and logb(x) look like those above for
ln(x) and ex.
Example 7:
16 = 24 ; lg(16) = 4;
29 = 101.462… ; log(29) = 1.462…
log(x) = lg(x)/lg(10) = lg(x)/3.3219
6
Download