Escolar Documentos
Profissional Documentos
Cultura Documentos
We would like to say the algorithm requires exponential time but in fact
you cannot prove a Ω(n2) lower bound using the usual definition of Ω
since the algorithm runs in linear time for n odd. We should instead define
f(n)=Ω(g(n)) by saying for some constant c>0, f(n)≥ c g(n) for infinitely
many n. This gives a nice correspondence between upper and lower
bounds: f(n)=Ω(g(n)) iff f(n) is not o(g(n)).
big-O notation
Also known as O.
See also Ω(n), ω(n), Θ(n), , little-o notation, asymptotic upper bound, asymptotically
tight bound, NP, complexity, model of computation.
Note: As an example, n² + 3n + 4 is O(n²), since n² + 3n + 4 < 2n² for all n > 10. Strictly
speaking, 3n + 4 is O(n²), too, but big-O notation is often misused to mean equal to
rather than less than. The notion of "equal to" is expressed by Θ(n).
The importance of this measure can be seen in trying to decide whether an algorithm is
adequate, but may just need a better implementation, or the algorithm will always be too
slow on a big enough input. For instance, quicksort, which is O(n log n) on average,
running on a small desktop computer can beat bubble sort, which is O(n²), running on a
supercomputer if there are a lot of numbers to sort. To sort 1,000,000 numbers, the
quicksort takes 20,000,000 steps on average, while the bubble sort takes
1,000,000,000,000 steps!
Any measure of execution must implicitly or explicitly refer to some computation model.
Usually this is some notion of the limiting factor. For one problem or machine, the
number of floating point multiplications may be the limiting factor, while for another, it
may be the number of messages passed across a network. Other measures which may be
important are compares, item moves, disk accesses, memory used, or elapsed ("wall
clock") time.
Introduction to Recursion
Recursion
What is it?
• Sequential search
• Binary search
Algorithm:
Search(dictionary)
{
if (Dictionary has only 1 page)
Sequentially search page for word
else
{
Open the dictionary to the middle page
Determine which half of the dictionary the word is in
Factorial
Meet requirements?
Consider the code fragment:
main()
{
int i = 3; // 1
cout << f(i) << endl; // 2
}
return prod;
}
Definition:
Consider:
int fib(int val)
{
if (val <= 2)
return 1;
else
return fib(val - 1) + fib(val - 2);
}
Call graph for fib(6):
Non-recursive version:
int fib(int val)
{
int current = 1;
int old = 1;
int older = 1;
val -=2;
return current;
}
Definition:
if (remaider == 0)
return b;
else
return gcd(b, remainder);
}