Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

2d Maxima Brute force algorithm, Lecture notes of Computer Science

The topic covered in this document is finding the maximal point using brute force algorithm

Typology: Lecture notes

2019/2020

Uploaded on 06/15/2020

imran-khan-32
imran-khan-32 🇨🇳

1 document

1 / 5

Toggle sidebar

Related documents


Partial preview of the text

Download 2d Maxima Brute force algorithm and more Lecture notes Computer Science in PDF only on Docsity! Lecture Notes CMSC 251 Lecture 2: Analyzing Algorithms: The 2-d Maxima Problem (Thursday, Jan 29, 1998) Read: Chapter 1 in CLR. Analyzing Algorithms: In order to design good algorithms, we must first agree the criteria for measuring algorithms. The emphasis in this course will be on the design of efficient algorithm, and hence we will measure algorithms in terms of the amount of computational resources that the algorithm requires. These resources include mostly running time and memory. Depending on the application, there may be other elements that are taken into account, such as the number disk accesses in a database program or the communication bandwidth in a networking application. In practice there are many issues that need to be considered in the design algorithms. These include issues such as the ease of debugging and maintaining the final software through its life-cycle. Also, one of the luxuries we will have in this course is to be able to assume that we are given a clean, fully- specified mathematical description of the computational problem. In practice, this is often not the case, and the algorithm must be designed subject to only partial knowledge of the final specifications. Thus, in practice it is often necessary to design algorithms that are simple, and easily modified if problem parameters and specifications are slightly modified. Fortunately, most of the algorithms that we will discuss in this class are quite simple, and are easy to modify subject to small problem variations. Model of Computation: Another goal that we will have in this course is that our analyses be as independent as possible of the variations in machine, operating system, compiler, or programming language. Unlike programs, algorithms to be understood primarily by people (i.e. programmers) and not machines. Thus gives us quite a bit of flexibility in how we present our algorithms, and many low-level details may be omitted (since it will be the job of the programmer who implements the algorithm to fill them in). But, in order to say anything meaningful about our algorithms, it will be important for us to settle on a mathematical model of computation. Ideally this model should be a reasonable abstraction of a standard generic single-processor machine. We call this model a random access machine or RAM. A RAM is an idealized machine with an infinitely large random-access memory. Instructions are exe- cuted one-by-one (there is no parallelism). Each instruction involves performing some basic operation on two values in the machines memory (which might be characters or integers; let’s avoid floating point for now). Basic operations include things like assigning a value to a variable, computing any basic arithmetic operation (+, −, ∗, integer division) on integer values of any size, performing any comparison (e.g. x ≤ 5) or boolean operations, accessing an element of an array (e.g. A[10]). We assume that each basic operation takes the same constant time to execute. This model seems to go a good job of describing the computational power of most modern (nonparallel) machines. It does not model some elements, such as efficiency due to locality of reference, as described in the previous lecture. There are some “loop-holes” (or hidden ways of subverting the rules) to beware of. For example, the model would allow you to add two numbers that contain a billion digits in constant time. Thus, it is theoretically possible to derive nonsensical results in the form of efficient RAM programs that cannot be implemented efficiently on any machine. Nonetheless, the RAM model seems to be fairly sound, and has done a good job of modeling typical machine technology since the early 60’s. Example: 2-dimension Maxima: Rather than jumping in with all the definitions, let us begin the discussion of how to analyze algorithms with a simple problem, called 2-dimension maxima. To motivate the problem, suppose that you want to buy a car. Since you’re a real swinger you want the fastest car around, so among all cars you pick the fastest. But cars are expensive, and since you’re a swinger on a budget, you want the cheapest. You cannot decide which is more important, speed or price, but you know that you definitely do NOT want to consider a car if there is another car that is both faster and 3 Lecture Notes CMSC 251 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 that are not dominated by any other. Here is how we might model this as a formal 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 car selection problem can be modeled in this way. If for each car we associated (x, y) values where x is the speed of the car, and y is the negation of the price (thus high y values mean cheap cars), then the maximal points correspond to the fastest and cheapest cars. 2-dimensional Maxima: Given a set of points P = {p1, p2, . . . , pn} in 2-space, each represented by its x and y integer coordinates, output the set of the maximal points of P , that is, those points pi, such that pi is not dominated by any other point of P . (See the figure below.) (2,5) 2 4 6 8 10 2 4 6 8 10 12 14 (9,10) (13,3) (15,7) (14,10) (12,12) (7,13) (11,5) (4,11) (7,7) (5,1) (4,4) 14 12 16 Figure 1: Maximal Points. Observe that our description of the problem so far has been at a fairly mathematical level. For example, we have intentionally not discussed issues as to how points are represented (e.g., using a structure with records for the x and y coordinates, or a 2-dimensional array) nor have we discussed input and output formats. These would normally be important in a software specification. However, we would like to keep as many of the messy issues out since they would just clutter up the algorithm. Brute Force Algorithm: To get the ball rolling, let’s just consider a simple brute-force algorithm, with no thought to efficiency. Here is the simplest one that I can imagine. Let P = {p1, p2, . . . , 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 pseudocode as follows: Brute Force Maxima Maxima(int n, Point P[1..n]) { // output maxima of P[0..n-1] for i = 1 to n { maximal = true; // P[i] is maximal by default for j = 1 to n { if (i != j) and (P[i].x <= P[j].x) and (P[i].y <= P[j].y) { 4
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved