


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Example Dimension Maxima, Brute Force Algorithm, Running Time Analysis, The maximal points, Analysis of the brute force maxima algorithm, Worst case time, Average case time are the key points in this study notes file.
Typology: Study notes
1 / 4
This page cannot be seen from the preview
Don't miss anything!



Let us do an example that illustrates how we analyze algorithms. Suppose you want to buy a car. You want the pick the fastest car. But fast cars are expensive; you want the cheapest. You cannot decide which is more important: speed or price. Definitely do not want a car if there is another that is both faster and cheaper. We say that the fast, cheap car dominates the slow, expensive car relative to your selection criteria. So, given a collection of cars, we want to list those cars that are not dominated by any other.
Here is how we might model this as a formal problem.
The car selection problem can be modeled this way: For each car we associate (x, y) pair where x is the speed of the car and y is the negation of the price. High y value means a cheap car and low y means expensive car. Think of y as the money left in your pocket after you have paid for the car. Maximal points correspond to the fastest and cheapest cars. The 2-dimensional Maxima is thus defined as
Here is set of maximal points for a given set of points in 2-d.
Our description of the problem is at a fairly mathematical level. We have intentionally not discussed how the points are represented. We have not discussed any input or output formats. We have avoided programming and other software issues.
To get the ball rolling, let’s just consider a simple brute-force algorithm, with no thought to efficiency. Let P = {p 1 , p 2 ,... , pn} be the initial set of points. For each point pi, test it against all other points pj. If pi is not dominated by any other point, then output it. This English description is clear enough that any (competent) programmer should be able to implement it. However, if you want to be a bit more formal, it could be written in pseudo code as follows:
1 for i 1 to n 2 do maximal true 3 for j 1 to n 4 do 5 if (i 6= j) and (P[i].x _ P[j].x) and (P[i].y _ P[j].y) 6 then maximal false; break 7 if (maximal = true) 8 then output P[i]
There are no formal rules to the syntax of this pseudo code. In particular, do not assume that more detail is better. For example, I omitted type specifications for the procedure Maxima and the variable maximal, and I never defined what a Point data type is, since I felt that these are pretty clear from context or just unimportant details. Of course, the appropriate level of detail is a judgment call. Remember, algorithms are to be read by people, and so the level of detail depends on your intended audience. When writing pseudo code, you should omit details that detract from the main ideas of the algorithm, and just go with the essentials.
You might also notice that I did not insert any checking for consistency. For example, I assumed that the points in P are all distinct. If there is a duplicate point then the algorithm may fail to output even a single point. (Can you see why?) Again, these are important considerations for implementation, but we will often omit error checking because we want to see the algorithm in its simplest form. Here are a series of figures that illustrate point domination.
Figure 1.2: Points that dominate (4, 11)
Figure 1.3: Points that dominate (9, 10)
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:
1 1
n n
i j
= =
= (^) ∑ +∑
1
n
i
=
= (^) ∑ +
= (4n + 2)n = 4n 2 + 2n 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, n 2 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 Θ(n^2 ). This is called the asymptotic growth rate of the function. We will discuss this Θ-notation more formally later.