Está en la página 1de 6

CS502-Fundamentals of Algorithms

Lecture No.04

Lecture No. 4
1.10.1 Analysis of the brute-force maxima algorithm.
Assume that the input size is n, and for the running time we will count the number of time that any element of P is accessed. Clearly we go through the outer loop n times, and for each time through this loop, we go through the inner loop n times as well. The condition in the if-statement makes four accesses to P. The output statement makes two accesses for each point that is output. In the worst case every point is maximal (can you see how to generate such an example?) so these two access are made for each time through the outer loop.

Thus we might express the worst-case running time as a pair of nested summations, one for the i-loop and the other for the j-loop:

For small values of n, any algorithm is fast enough. What happens when n gets large? Running time does become an issue. When n is large, n2 term will be much larger than the n term and will dominate the running time. We will say that the worst-case running time is (n2). This is called the asymptotic growth rate of the function. We will discuss this -notation more formally later. The analysis involved computing a summation. Summation should be familiar but let us review a bit here. Given a finite sequence of values a1, a2, . . . , an, their sum a1 + a2 + . . . + an is expressed in summation notation as

Page 1 Copyright Virtual University of Pakistan

of 6

CS502-Fundamentals of Algorithms

Lecture No.04

Page 2 Copyright Virtual University of Pakistan

of 6

CS502-Fundamentals of Algorithms If n = 0, then the sum is additive identity, 0. Some facts about summation: If c is a constant

Lecture No.04

and

Some important summations that should be committed to memory.

Arithmetic series

Quadratic series

Geometric series

If 0 < x < 1 then this is (1), and if x > 1, then this is (xn). Harmonic series For n 0

Page 3 Copyright Virtual University of Pakistan

of 6

CS502-Fundamentals of Algorithms

Lecture No.04

1.11 Analysis: A Harder Example


Let us consider a harder example.

How do we analyze the running time of an algorithm that has complex nested loop? The answer is we write out the loops as summations and then solve the summations. To convert loops into summations, we work from inside-out. Consider the inner most while loop.

It is executed for k = j, j - 1, j - 2, . . . , 0. Time spent inside the while loop is constant. Let I() be the time spent in the while loop. Thus

Consider the middle for loop.

Page 4 Copyright Virtual University of Pakistan

of 6

CS502-Fundamentals of Algorithms Its running time is determined by i. LetM() be the time spent in the for loop:

Lecture No.04

Finally the outer-most for loop.

Let T() be running time of the entire algorithm:

Page 5 Copyright Virtual University of Pakistan

of 6

CS502-Fundamentals of Algorithms

Lecture No.04

1.11.1

2-dimension Maxima Revisited

Recall the 2-d maxima problem: Let a point p in 2-dimensional space be given by its integer coordinates, p = (p.x, p.y). A point p is said to dominated by point q if p.x q.x and p.y q.y. Given a set of n points, P = {p1, p2, . . . , pn} in 2-space a point is said to be maximal if it is not dominated by any other point in P. The problem is to output all the maximal points of P. We introduced a brute-force algorithm that ran in (n2) time. It operated by comparing all pairs of points. Is there an approach that is significantly better? The problem with the brute-force algorithm is that it uses no intelligence in pruning out decisions. For example, once we know that a point pi is dominated by another point pj, we do not need to use pi for eliminating other points. This follows from the fact that dominance relation is transitive. If pj dominates pi and pi dominates ph then pj also dominates ph; pi is not needed.

Page 6 Copyright Virtual University of Pakistan

of 6

También podría gustarte