Professional Documents
Culture Documents
Assignmnet NO1 BCS-5A-5D
Assignmnet NO1 BCS-5A-5D
Assignmnet NO1 BCS-5A-5D
More on Asymptotic
Assignment Running
NO1-BCS-5(A,D). BSETime Analysis
CMPSC 122
When analyzing the running time of algorithms, we really care about what happens in the long run, or what happens when n
is large. Thus…
• We are not terribly concerned with what happens when __________________ (input size is small)
• We are concerned with how quickly _______________________________________ (running time functions grow)
Examples:
1. Does 23n2 dominate n + 7?
n3
2. How about vs. 2n3 + 7?
5
(Please note that we are being a little informal with some of this analysis in this course.)
1. nn
2. n!
3. cn for all constants c s.t. c > 1, where if a > b, an dominates bn
4. nc for all constants c s.t. c ≥ 2, where if a > b, ca dominates cb
5. n lg n
6. n
7. lg n
8. c
Two notes:
• Since all constants grow at the same rate, anything that you might think to call Θ(c) gets simplified to Θ(1).
• In Θ-notation, each function in each of families 1-2 and 5-8 would all reduce to the same thing. Different constant
choices in families 3 and 4 yield different Θ-notation expressions.
• This list leaves some matters vague, but covers most of the common functions that arise.
• You should convince yourself of the correctness of this ranking by plotting several of these kinds of functions on the
same axis.
Problem: Simplify each function of n to Θ-notation and then rank the functions by dominance.
n! nn
3n! + n3 17 lg n + n 3 + n lg n 100 n + n 200000 + 50 n n lg n + + 7n
12 2
Problem: Simplify each function of n to Θ-notation and then rank the functions by dominance.
n 37
n 35 + lg n lg 3n + 3 2n(1+ 3lg n)
n(n + 1)
III. O-Notation
Another kind of asymptotic notation is O-notation. While O-notation was invented before Θ-notation, it is less precise. So,
let's first give a more formal definition for Θ-notation:
We say a function f (x) is Θ(g (x)) when… (for all sufficiently large n…)
Example: 2n2 is ______________ and also 2n2 is ______________. But we could also say 2n2 is _____________.
Example: n! is O(_________)
In some sense, we could think of a Θ-notation bound as being the best O-notation bound possible, and sometimes you'll see
the expression "best big-O bound" as a result.
Example 1: for i = 1 to n
{
a=b+c-i
}
Example 8: for i = 1 to n
{
a=b+c–i
}
for i = 1 to n
{
a=b+c–i
}
Example 9: for i = 1 to n
{
for j = 1 to n
{
a = b + c + max(i, j)
}
}
z = z + 2^a
}
for j = n downto 1
{
b = 50 + j - a
}
}
for j = 2n/3 to n
// 1 elementary operation
}
Backing up, what did we conclude was the worst-case running time for a binary search when n is a power of 2?
Let's look at a few not-as-nice input sizes, again, in the worst case:
• 33
• 34
• 37
• 63
• 150
• 700
In the remainder of the course, we'll look at more advanced data structures, algorithms on those structures, and sorting
algorithms. In all cases, we'll make a point of analyzing their asymptotic running times. Your final project will carry the
analysis of running times of sorts further and make connections between theory and practice.
Examples of code fragments we have handled here are ones where the math is "nice." In 360, we'll look at sequences and
counting tools that will help you to be able to analyze the running time of "harder" problems.
Finally, as 465 is the main analysis course in the curriculum, we'll look at more formal definitions of O and Θ-notation, as
well as a few other kinds of asymptotic notation there. We'll also encounter many more advanced analysis techniques, and a
running theme will be to analyze, formally, the running time of many new algorithms.