Search for a "close point" efficiently [closed] - java

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 9 years ago.
Improve this question
I'm asking this because I'm sure there has been brought up before but not sure how to call it.
I need an efficient way to search and store some point in a metric space. Specifically, I need to find the weather at some points in space and time. I have an API to do that, but don't want to make another request if I have already queried in the past about a point a few inches away from the new point and a few seconds before it, as the weather there would be the same.
So when I receive a new point, I need to ask - do I have in the cache a point "close enough" (whose distance from the new point is a below a threshold)?
If I do - take the data associated with that point. Otherwise, cache the new point.
This can be done easily using a serial check but I'm interested in ways to do it more efficiently.
Thanks!

Suppose your thresshold is t, you can split your search space to a grid,
with cells having width and height t.
Every cell will have a list of points that lie in it.
Now when given a new point, you compute which cell it falls into,
let this be the cell [i,j], you check this cell and all of its neigbors
(i.e. 9 cells altogether), whether they contain any points and if they do,
these are your candidates for closer-than-thresshold points.
You will now compute the distance of all of these.
Since the cells are t wide and t high, distance of all points
lying in any other cell is at least t.
You can store the grid cells in a TreeMap, with the comparator based on [i,j] pairs.
(you only store the cells which have at last one point in them).

Wouldn't you know. Going to a search engine called Google and using some time to string words together, you get Nearest neighbor search as the first result. Who'da thunk it.

Maybe what you need is a Voronoi diagram built from your cache points.

Related

Detect whether there is a way out of a 3D room [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm developing this for Bukkit, but I am open to porting a java-only solution.
Now, I have a character in a room with an opening exposed to the outside for radiation to seep in like this:
(sorry for terrible paint skills) In this picture the algorithm should return false, however if that white bit was covered, it would return true.
I need a way of detecting whether the character is inside a room or not. Please remember that a room can be any size, and does not have to be a cuboid.
The way I'd like it to work is that I'd have an arbitrary position above the roof, and if this maze algorithm would be able to reach this position, I'd know that the character is not completely covered.
EDIT: Actually if the exit was 20+ units away, I wouldn't mind the algorithm timing out.
This is not an answer per se, but it may be a good point to move on from.
The definition for room:
a part or division of a building enclosed by walls, floor, and
ceiling.
The definition for building:
a structure with a roof and walls
The definition of a structure:
a building or other object constructed from several parts
Can you see how poorly defined these are? (The definitions for building and structure are recursive!) Until you can come up with a clear cut, black and white definition for what you consider to be a room, you (and us) have very little hope of coming up with a good solution.

Generating random roads [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am trying to generate random roads on a swing application. However, I am unable to create the roads such that they do not over-intersect with each other. I mean one road should not cross over another unless they have a common intersection point.
I tried generating random points and connecting them using minimum spanning trees but it does not seem to work.
Do you have any ideas?
I would try the easy thing first: generate a bunch of random roads and keep only those that don't intersect any you've found so far. This has O(n^2) runtime, but it's easy to understand and implement.
In pseudocode:
points = (lots of random points)
roads = empty list
repeat n times:
r = road(pick_random(points), pick_random(points))
if r does not intersect anything in roads list:
add r to roads
This has the advantage over minimum spanning trees that it may generate cycles for you, which would make for a more interesting road network.

Complete word-database for Java-App to check if a word is actually a legit word, is SQL appropriate in this case? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
I am going to write a game in which I have often have to check if a string of letters is actually a word or not. My question is about how to do this the fastest with the least computation-able power as possible (for instance an old smart-phone). With if possible not much start-up time to make it a quick and responsive app.
I once did this look-up by first reading in a word-file with almost all words into an appropriate sized hash-map of around 650,000 words*. (* might be more, I am not sure if this is the exhausted list yet).
Would a SQL database be appropriate here? I am thinking of buying a book about it so I can learn and implement one. Also I have no idea how you could create a hash-map, save it for later and then load one. Is that too much of a hacker solution or is that technique used more often? So would it make sense for me to learn SQL or do it with saving a hashmap and then later restoring it.
A database SQL could be appropriate if you plan to query it every time you need to check a word, but this is not the fastest solution; querying every single word slows down the response time but it should use less memory if the words number is high (you must measure the memory consumed by the db vs the memory consumed by the map). Checking if a word is inside a map is not so computationally expensive, it must calculate the hash and iterate over the array of items with the same hash.
Personally I would choose a map if the memory requirements of keeping all the words in memory can be satisfied. You can store the dictionary as plain text file (one line -> one word) and read it in a background thread when the application starts.
If memory is an issue, this seems like a good use for a B-Tree. This allows for O(log n) search time while searching a large amount of records with minimal memory usage. For this sort of application it sounds like loading the entire thing into memory is not going to be a good idea.

LSH implementation for finding clusters [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
Hie guys.
I am very new to stack exchange and I am currently doing a research on graph theory.
The set of questions I'm going to ask are very introductory since I'm a beginner level programmer (not acquainted with hashing, buckets, vectors etc data structure wise).
My idea is to take in a dataset of the form (timestamp t, node i, node j) which says that there is an edge between i and j at time t. The idea is to search the neighborhood set of each nodes and hash them. If their "vectors" (I don't understand what that is) hash into the same bucket - they are candidates for cluster formation.
But he problem is I want to do experiments and try to run it. But have no idea how do I implement a hash function, and then bucket them together.
I'm not saying help me out with the code. But a pointer (pseudo code) would be very helpful. Like telling me to initialize a hash table etc etc
A hash code is an integer which is calculated from the properties of whatever it is you want to hash. That number is then used as an index into an array.
In this case it seems that you want to use the N dimensions of your vector to calculate this hash code. It's up to you to write a function that calculates that hash codes in a way that vectors that should be clustered all get the same hash code.
Language specific details about hash tables in Java or Python is very easy to find with a web search.

Programming and evaluating Dijkstra algorithm [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 years ago.
Improve this question
I have to do a project about shortest path algorithm. I am not really a professional I am just a normal student. I have to pick up first a problem ( train route, bus route, any travel route) then choose a suitable algorithm to solve it. Then I have to program it (using coding language java, python..) Then after that I should evaluate it(how speed is the algorithm, time complexity, etc) and if there is a better algorithm that I can be implement instead of what I chose first.
Choosing the problem is the hardest part as I
Don't know what kind of problem I should explore to use of the algorithm!
Regards
Let's take the city where you live in. Take any two bus stations, namely A and B, and the entire commuter network. This network is a graph. Stations are nodes, commuter connections between stations are edges (edge weight = time it takes the bus to travel between two stations).
Say, you want to travel from A to B in the shortest amount of time possible (you wont want to waste time, would you?).
Dijkstras Algorithm has as input the graph representing the commuter network and the node A and is able to deduce the shortest path from A to any other station (which includes the shortest path from A to B).
Does that help you? Wikipedia on Dijkstra has more detailed examples.

Categories