Upload
jacob-berry
View
214
Download
1
Embed Size (px)
Citation preview
Grading
• Exams 60 %
• Lab 20 %
• Participation 5%
• Quizes 15%
Pseudo-Code
• Pseudo-code is not a computer program, but is more structured than usual prose.
• Pseudo-code is – a mixture of natural language and high-level
programming constructs – describe the main ideas behind a generic
implementation of a data structure or algorithm.
• The programming language constructs we choose are those consistent with modern high-level languages such as C, C++, and Java.
Pseudo-Code
• Expressions: We use standard mathematical symbols to express numeric and Boolean expressions. – Left arrow sign (←) as the assignment operator in
assignment statements (equivalent to the = operator in Java)
– Equal sign (=) as the equality relation in Boolean expressions (equivalent to the "==" relation in Java).
• Method declarations: – declares a new method "name" and its parameters.
Algorithm name(param1, par am2,…)
Pseudo-Code
• Decision structures: – if condition then true-actions [else false-actions]. – We use indentation to indicate what actions should be
included in the true-actions and false-actions. • While-loops:
– while condition do actions. – We use indentation to indicate what actions should be
included in the loop actions.• Repeat-loops:
– repeat actions until condition. – We use indentation to indicate what actions should be
included in the loop actions
Pseudo-Code
• For-loops: – for variable-increment-definition do actions. – We use indentation to indicate what actions should be
included among the loop actions.
• Array indexing: – A[i] represents the ith cell in the array A. – The cells of an n-celled array A are indexed from A[0]
to A[n − 1] (consistent with Java).
• Method calls: – object.method(args) (object is optional if it is
understood).
Pseudo-Code
• Method returns: – return value. – This operation returns the value specified to the method that called this
one.
• Comments: – { Comment goes here. }. – We enclose comments in braces.
• When we write pseudo-code, we must keep in mind that we are writing for a human reader, not a computer. – No low-level implementation details. – Not gloss over important steps. – Finding the right balance is an important skill that is refined
through practice.
The Seven Functions • We briefly discuss the seven most important functions used in the analysis of algorithms.
The Constant Function
• f(n) = c, such as c = 5, c = 27, or c = 210. • it doesn't matter what the value of n is; • f (n) will always be equal to the constant value c.
• Since we are most interested in integer functions, the most fundamental constant function is g(n) = 1.
• Note that any other constant function, f(n) = c, can be written as a constant c times g(n). • That is,f(n) = cg(n) in this case.
• As simple as it is, the constant function is useful in algorithm analysis, because it characterizes the number of steps needed to do a basic operation on a computer, like
– adding two numbers, – assigning a value to some variable, – or comparing two numbers.
The Seven FunctionsThe Logarithm function
• f(n) = logbn, for some constant b > 1. The value b is known as the base of the logarithm.
• This function is defined as follows:x = logb n if and only if bx = n.By definition, logb 1 = 0.
• Computing the logarithm function exactly for any integer n involves the use of calculus, but we can use an approximation that is good enough for our purposes without calculus.
• In particular, we can easily compute the smallest integer greater than or equal to logan, for this number is equal to the number of times we can divide n by a until we get a number less than or equal to 1.
log327 is 3, since 27/3/3/3 = 1. log464 is 4, since 64/4/4/4/4 = 1 log212 is 4, since 12/2/2/2/2 = 0.75 ≤ 1.
The Seven Functions• Indeed, since computers store integers in binary, the most common base
for the logarithm function in computer science is 2 – logn = log2n
• calculators have a button marked LOG, but this is typically for calculating the logarithm base-10, not base-two.
• (Logarithm Rules): Given real numbers a > 0, b > 1, c > 0 and d > 1, we have:
1. logbac = logba + logbc2. logba/c = logba− logbc3. logbac = clogba4. logba = (logda)/logdb5. b log
da = a log
db.
• Also, as a notational shorthand, we use logcn to denote the function (logn)c.
The Seven Functions
The Linear function
• Another simple yet important function is the linear function, f(n)= n.• That is, given an input value n, the linear function f assigns the
value n itself.• This function arises in algorithm analysis any time we have to do a
single basic operation for each of n elements. • For example,
– comparing a number x to each element of an array of size n will require n comparisons.
– The linear function also represents the best running time we can hope to achieve for any algorithm that processes a collection of n objects that are not already in the computer's memory, since reading in the n objects itself requires n operations.
The Seven FunctionsThe N-Log-N function
• f(n) = nlogn,• that is, the function that assigns to an input n the value of n times the logarithm
base-two of n.• This function grows a little faster than the linear function and a lot slower than the
quadratic function.
The Quadratic function
• f(n) = n2.• That is, given an input value n, the function f assigns the product of n with itself (in
other words, "n squared").• The main reason why the quadratic function appears in the analysis of algorithms is
that there are many algorithms that have nested loops, where the inner loop performs a linear number of operations and the outer loop is performed a linear number of times. Thus, in such cases, the algorithm performs n · n = n2 operations.
The Seven Functions
The Cubic Function and Other Polynomials
• f(n) = n3,
Polynomials
• A polynomial function is a function of the form,
• f(n) = a0 + a1n + a2n2 + a3n3 + … + adnd,
where a0,a1,…,ad are constants, called the coefficients of the polynomial, and ad ≠ 0. Integer d, which indicates the highest power in the polynomial, is called the degree of the polynomial.
• Using a summation, we can rewrite the formula
1 + 2 + 3 + … + (n − 2) + (n − 1) + n = n(n + 1)/2 as:
Analysis of Algorithms • The primary analysis tool we will use involves characterizing the
running times of algorithms and data structure operations.• Running time is a natural measure of "goodness," since time is a
precious resource—computer solutions should run as fast as possible.
• In general, the running time of an algorithm or data structure method– increases with the input size– affected by the hardware environment and software environment. – we would like to focus on the relationship between the running time of
an algorithm and the size of its input.• We are interested in characterizing an algorithm's running time as a
function of the input size. But what is the proper way of measuring it?