With a coworker we are looking for a method to calculate the maximum number of points in a plane 2D that can communicate as long distance they can represented by "D", the code is requested on Java and each one of the points must be considered as an object with two coordinates, "X" and "Y" who must be represented as 2 int in the code.
We found that if we select any of this points in the plane, is possible determinate the radius D of the circle around the point selected, where all of the mid points contained inside of this radius can communicate with the target point.
Then after that you can use alliterations for determinate all the communications in each plane 2D at a determinate distance "D" and find which is the zone with maximum number of communications between all the mid points.
After all the mentioned my question is the following:
Exist another way more easy to do it in Java?
Some friend suggest to us do it in C# language because it includes a library which facilitates this kind of representation with the use of pointers and the memory addresses, but is a primordial requirement to do it in Java.
If you have any suggestions or a better way to approach our problem would be very appreciated for us.
Related
I've done a fair bit of reading around this, and know that discussions regarding this algorithm in Java have been semi-frequent. My issue with implementing Dijkstra's algorithm in Java is simply that I'm not sure how to prepare my data.
I have a set of coordinates within an array, and a set of 1s and 0s in a matrix that represent whether there is a path between the points that the coordinates represent. My question is, how do I present this information so that I can search for the best path with Dijkstra? I have seen many people create a "Node" class, but they never seem to store the coordinates within that Node. Is there some standardized way of creating this kind of structure (I suppose it's a graph?) that I am simply missing?
Any help would be appreciated.
There are two main options:
1. You can use an adjacency matrix in which rows an columns represent your nodes. The value matrix[x, y] must be the weight(e.g. distance/cost etc.) to travel from x to y. You could use the Euclidian distance to calculate these values from your coordinate array;
2. You can implement a couple of classes (Node, Edge - or just Node with a internal Map to another node and the weight as a map value) - it is a graph indeed.
I'm trying to make a program that calculates the distance between one point to another in a 3D array, and then returns the point's distance to the origin. What kind of method(s) could I use for this? How should I think? Are there any good tutorials about this (that aren't too basic)? I'm a beginner and very new to methods and would really appreciate some help. :)
Here's the math basics about calculating distance between two points in 3D: https://math.stackexchange.com/questions/42640/calculate-distance-in-3d-space
The relevant Java functions that will help you are:
Math.sqrt: square root (e.g. Math.sqrt(6) => 2.44948974278)
Math.pow: raise first argument to the power of the second (e.g. Math.pow(2,4) => 16)
Give it a try and post your code if you want more specific help.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to compute the best latitude longitude pairs for several locations.
I have a database with locations and for each location I may have multiple coordinates. Most of these coordinates seem relevant for the location as they are located within 5 meters from each other.
So I can derive a new (final) latitude longitude pair by averaging them.
Sometimes however I have a point (sometimes more then one) that is located several hundred meters away.
Given a set of few (maximum 10) latitude longitude points, I would like to find and keep only those points that make sense and discard those who are too far away from others.
What approach / algorithm should I use ?
Note I work with Java.
Simple approach:
Compute the distance of all points to some arbitrary point.
Find the median distance of all points.
Discard all points whose abs (dist - median) > value.
A bit better than the centroid approach which could get screwed by few far away points that are clustered together.
The simplest approach is likely to be:
Find the centroid (average long/lat) point for a given set of points
Compute the distance from each point in the set to the centroid. Discard all points with a distance over a certain constant value (calling these points noise)
Recompute the centroid from the remaining non-noise points, call that the location.
This should be pretty simple to implement in java and certainly can be O(N), N being the number of points in your set.
Your problem is a specific case of K-means clustering, in that you know which real-world data correspond to which samples whereas in the general case you don't have that knowledge. So look into that problem and assorted approaches if you want more research.
There are a couple of questions you need to ask yourself:
Which point should be treated as "not making sense" if you have only two points being 100 meters away.
Which point should be treated as "not making sense" if you have two separate clusters of points?
What should you do if you have a continuous row of points that still fit within the margin of error counting to the closest neighbour, but in total span over the limit?
The question you've asked is hard to answer without clear criteria, although I'd try looking through clustering algorithms.
If we would skip problems I've mentioned, I'd say that it's computationally heavy, but you can go by
calculating the distances between all points in given set
sorting them by the sum of distances
filtering out the one with highest sum
Iterating over until there are no points for which the sum of distances is greater than errorMargin * N-1 where N is the current number of points.
Still you need to take the border cases into consideration, cause for instance problem mentioned in 1) would leave you with a single random point - I doubt you're ok with that, so you need to carefully analyse your domain.
If you are using Java8 then the following code provides an elegant solution.
Collector<Location, ?, Location> centreCollector = new CentreCollector();
Location centre = locations.stream().collect(centreCollector);
centre = locations.stream().filter(centre::furtherThan(NOISE_DISTANCE)).collect(centreCollector);
You have 2 things to create. The CentreCollector class which implements Collector and averages Location objects as they are streamed to it; and the furtherThan method which returns a Predicate that compares the distance between this and a given location to a given distance.
A slightly more elegant method would be to calculate the standard deviation of the distances to the centre and then discard any locations that are more than a certain number of standard deviations from the average distance. This would have the advantage of taking account of sets of locations in which all or most of the samples are more than the NOISE_DISTANCE from the centre. In that case the CentreCollector will have to return a more complex object that holds the location and statistical information and have furtherThan as a member of that class rather than of Location. Let me know in the comments if you want me to post the equivalent code for using standard deviations.
I'm trying to implement the absolute rank-file distance described on Knight-Distance from the Chess Programming Wiki, but I'm a little confused on what ints a and b are supposed to be in
int knightDistance(int a, int b).
Don't you need two sets of coordinates to figure this out (start location and destination)?
I thought maybe they were using 0,0 as the start and then you just give the difference between start and end locations, but that gives bad output.
How is this supposed to work, and does this algorithm work for any size grid, or just 8×8?
I believe that a and b are the rank and file distance deltas from the current position. From symmetry, only the difference between the current position and the desired destination matter.
I have two ArrayList, Double data type,
1.latitudes
2. longitudes,
each has over 200 elements
say i give a random test coordinates, say (1.33, 103.4), the format is [latitude, longitude]
is there any algorithm to easily find closest point,
or do i have to brute force calculate every possible point, find hypotenuse, and then compare over 200 hypotenuses to return the closest point? thanks
Sort the array of points along one axis. Then, locate the point in the array closest to the required point along this axis and calculate the distance (using whatever metric is appropriate to the problem topology and scale).
Then, search along the array in both directions until the distance to these points is greater than the best result so far. The shortest distance point is the answer.
This can result in having to search the entire array, and is a form of Branch and bound constrained by the geometry of the problem. If the points are reasonably evenly distributed around the point you are searching for, then the scan will not require many trials.
Alternate spatial indices (like quad-trees) will give better results, but your small number of points would make the setup cost in preparing the index much larger than a simple sort. You will need to track the position changes caused by the sort as your other array will not be sorted the same way. If you change the data into a single array of points, then the sort will reorder entire points at the same time.
If your arrays are sorted, you can use binary search to find a position of a requested point in array. After you find index, you should check four near by points to find the closest.
1)Suppose you have two sorted arrays longitudes-wise and latitudes-wise
2)You search first one and find two nearby points
3)Then you search second one and find two more points
4)Now you have from two to four points(results might intersect)
5)These points will form a square around destination point
6)Find the closest point
it's not true that closest lat (or long) value should be choosen to search over the long (or lat) axis, in fact you could stay on a lat (or long) line but far away along the long (or lat) value
so best way is to calculate all distances and sort them