






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







We want to give an upper bound on the amount of time it takes to solve a problem.
defn: v(n) = O(f(n)) โ โ constants c and n 0 such that |v(n)| โค c|f(n)| whenever n > n 0
Termed complexity : has nothing to do with difficulty of coding or understanding, just time to execute.
Important tool for analyzing and describing the behavior of algorithms
Is an n 2 algorithm always better than a n^3 algorithm? No, it depends on the specific constants, but if n is large enough, an O(n^3 ) is always slower.
Complexity Class: O(1), O(log n), O(n), O(n log n), O(n 2 ) O(2n^ )
For small n , c may dominate.
Intractable: all known algorithms are of exponential complexity
The complexity is the running time of the cond plus the larger of the complexities of S1 and S2.
Analyze nested for loops inside out. The total complexity of a statement inside a group of nested for loops is the complexity of the statement multiplied by the product of the size of each for loop.
for i = 1,m for j = 1,n x++;
O(mn)
Consider a pictorial view of the work. Let each row represent the work done in an iteration. The area of the figure will then represent the total work.
for (beg = 1;beg < m; beg++) for (j = beg; j < m; j++) x++;
In this example, our complexity picture is triangular.
Recursive Algorithms
The complexity for recursive algorithms requires additional techniques. There are three general techniques for finding complexity. The pictorial approach, the formulaic approach, and the mathematics approach. You should be able to use any two of them.
procedure B(N) for i = 1, N x = x + i Call B(N/2) Call B(N/2) end procedure
If we let T(n) represent the time to solve this problem, the time is represented recursively as T ( n ) = 2 T ( n /2) + n. This is called a recurrence relation
procedure B(N) x = x + i Call B(N/2) end procedure
If we let T(n) represent the time to solve this problem, the time is represented recursively as T ( n ) = T ( n /2) +1.
procedure B(N) x = x + i Call B(N/2) Call B(N/2) end procedure
If we let T(n) represent the time to solve this problem, the time is represented recursively as T ( n ) = 2 T ( n /2) +1.
Mathematicians have developed a formula approach to determining complexity.
Theorem:
T(n)= a T(n/b) + O(nk^ )
if a > b k^ the complexity is O(n logb a)
if a= b k^ the complexity is O(nk^ log n)
if a < bk^ the complexity is O(nk^ )
In this case:
Let's use the theorem to revisit the same problems we solved pictorially.
procedure B(N) for i = 1, N x = x + i Call B(N/2) Call B(N/2) end procedure
a=2 b=2 k=1 (as work is n)
Since a=bk^ , we are in the ``equals'' case.
if a= b k^ the complexity is O(nk^ log n) = O( n log g) which is exactly what our pictures told us.
procedure B(N) for i = 1, N x = x + i Call B(N/2) end procedure
a=1 b=2 k=
Since a < bk^ we are in the ``less than'' case.
if a < bk^ the complexity is O(nk^ ) = O(n) which is exactly what our pictures told us.
procedure B(N) x = x + i Call B(N/2) end procedure
While the math types may like this approach, it usually confuses the non-math types more than helps.
Suppose our recurrence relation is:
and
We use the telescoping technique (plug and chug technique). (You may have learned something you like better in your discrete math classes.) We set up a series of equations. Since we would never stop for infinite n, let n = 32 to see the pattern, and generalize for any n.
Now by substituting each equation into the previous equation we have
Notice the upper limit on the summation is related to the size of n. Since 2^4 = n/2 in this case, we conjecture the upper bound (call it b) will always be determined by 2b^ = n/2, i.e. b = log 2 n -1. We would actually have to consider a few more cases to convince ourselves this is the actual relationship. Notice, however, that we expect to perform the recursion a logarithmic number of times as we halved the problem size each time.
using our geometric sum technique
Thus,
It would be nice to be able to check our work: For the case of n = 32, we got
Since 3(32)/2 - 2 = 46, we are reasonably certain we did the computations correctly.
Telescoping, again : If that was confusing, you could also have done the same thing as follows:
But since n =
Thus,
Suppose the relation was
and
This is a common form. You break a problem of size n into two equal sized pieces (n/2) and have to solve each piece (as long as n/2 is bigger than our base case.). The breaking apart of the problem and the accumulating the results takes time proportional to n.
Backwards substitution yields: In general,