I have a list of 2D points on my scene and i have an array of connections between these Points stored as unordered pairs
Pair is defined exactly as here how to write write a set for unordered pair in Java
so i have :
ArrayList<PointF> mPoints = new ArrayList<PointF>();
ArrayList<Pair<PointF>> mConnections = new ArrayList<Pair<PointF>>();
//
PointF mStartPoint = mPoints.get(0);
PointF mEndPoint = mPoints.get(80);
I need to find array of Points which will lead me from source to destination Point.
I think to add to each Pair information about distance but what next ?
This is an instance of a standard path finding problem.
If you need a guaranteed exact solution, go with something like Dijkstra's algorithm. If you need something more efficient, but can live with suboptimal solutions for certain cases, go with the A* algorithm.
See http://en.wikipedia.org/wiki/Dijkstras_algorithm#Algorithm for a solution.
Related
How do I get the data points and centroid that are in a kmeans (llyod) cluster when I use elki?
Also could I plug in those points into one of the distance functions and get the distance between any two of the points?
This question is different, because the main focus of my question is retrieving the data points, not custom data points. Also the answer on the other thread is currently incomplete, since it refers to a wiki that is not functioning at the moment. Additionally I would like to know specifically what needs to be done, because the documentation on all of the libraries is a bit like a wild goose chase and it would be greatly appreciated that if you know/understand the library that you would be direct with the answer so that others with the same problem could also have a good solid reference to refer to, instead of trying to figure out the library.
A Cluster (JavaDoc) in ELKI never stores the point data. It only stores point DBIDs (Wiki), which you can get using the getIDs() method. To get the original data, you need the Relation from your database. The method getModel() returns the cluster model, which for kmeans is a KMeansModel.
You can get the point data from the database Relation by their DBID,
or compute the distance based on two DBIDs.
The centroid of KMeans is special - it is not a database object, but always a numerical vector - the arithmetic mean of the cluster. When using KMeans, you should be using SquaredEuclideanDistanceFunction. This is a NumberVectorDistanceFunction, which has the method distance(NumberVector o1, NumberVector o2) (not all distances work on number vectors!).
Relation<? extends NumberVector> rel = ...;
NumberDistanceFunction df = SquaredEuclideanDistanceFunction.STATIC;
... run the algorithm, then iterate over each cluster: ...
Cluster<KMeansModel> cluster = ...;
Vector center = cluster.getModel().getMean();
double varsum = cluster.getModel().getVarianceContribution();
double sum = 0.;
// C++-style for loop, for efficiency:
for(DBIDRef id = cluster.getIDs().iterDBIDs(); id.valid(); id.advance()) {
double distance = df.distance(relation.get(id), center);
sum += distance;
}
System.out.println(varsum+" should be the same as "+sum);
I have created 3D ArrayList. I know initialization is done using this code
ArrayList<ArrayList<ArrayList>> ll=new ArrayList<ArrayList<ArrayList>>();
for(int j=0;j<n;j++){
ll.add(new ArrayList(new ArrayList()));
}
But I dont know how insert values in it.
I am familiar with 2D arraylist in which inserting value b at a index is done using
ll.get(a).add(b);
but I dont know how to insert value into 3D arrayList.
I am solving question http://www.spoj.com/problems/BENEFACT/
in which I think longest distance in tree is the answer. I used 3D ArrayList
here, 2dimension to store connection of places and 3rd dimension to store distance
Is this correct approach? Is Any other approach possible in which solution is obtained easier than using 3d ArrayList?
I think if you want to access an object at index i,j,k you can do it by
ll.get(i).get(j).get(k)
Or
ll.get(i).get(j).add(k,newObject)
Depending on your need, you probably don't want that. I didn't read the full description of the problem to solve (the link), but you just need 6 points in 3D space.
I'd suggest creating a Point3D class with x, y, and z, and then just a simple List<Point3D>.
I'm writing a simple program and want to know if an approximate position is clicked. I've got a hashmap with the position as key value and want to display a currently invisible object if the user clicks close enough to the position of the object - not just right at it. The position class just holds an x and a y value.
HashMap<Position, Place> places = new HashMap<>(); //Assume this is populated
#Override
class WhatIsHere extends MouseAdapter {
public void mouseClicked(MouseEvent me) {
Place place = places.get(new Position(me.getX(), me.getY()));
if (place != null) {
place.setVisible(true);
} else {
System.out.println("Nothing there");
}
}
}
This bit of code finds the place if you click right on it though I don't know how to look for, say, me.getX()+-10 and find objects in that range.
Do I need to set four ints holding x-10 and x+10 etc. and just loop through all the positions inbetween? It seems awfully dumb to do it that way.
I dislike exercises that require use of a particular collection, regardless of whether it is the best choice. One of the most important things to learn about the collections, and more generally about data structures, is picking which to use for a given job.
However, I understand you have to use HashMap. Here is one way to do it.
Divide the space up into small squares. Identify each square by e.g. the Point at the minimum x and minimum y. Create a HashMap that maps the square that are near at least one of your objects to the list of nearby objects.
To look up a point, calculate the Point identifying the square containing it. Look up that Point in the map. If it is not present, your point is not near any object. If it is present, check your point against each object in the list according to your nearness rules.
For some configurations of your objects, you may be able to ensure that each square is near at most one object. If so, you can replace the list with the object.
You might want to use TreeMap and you would be able to get a sub map which seems to be what you are looking for.
I have a method "connection(int n)" which gives me all the cells number that have relation with cell number "n" now I want a method which gives me all the routes with a specific length "myLength" that start from cell number "start" and just in one direction (as it's usual) I mean we are not allowed to pass some cells more than one time
thanks in advance for your help
P.S. I can't use map tools, graph tools,... with basic tools please
You are looking for BFS.
Model your problem as a graph G = (V,E) such that V = {1,...,n} [all possible values] and E = { (u,v) | connection(u) returns v } [there is a connection between u and v using your connection() method]
In addition to the standard BFS, you will need to add another stop condition when you reached the limited length.
EDIT:
Note that this solution assumes you are looking for a path up-to length, and not exactly length.
BFS doesn't work here for the counter example of a clique if you want exactly of length.
To get all vertices that have a simple path of exactly length - You will probably need a DFS that avoids loops [can be done by maintaining a set that is modified each iteration], but can explore each vertex more then once.
I am trying to develop an algorithm wherein I have a Location Class. In each class, I create a list of its adjacent Locations. I want to know, how can I get the shortest path from one Location to another. I am trying to look for different algorithms but it seems that they doesn't answer my problem.
For example, I have a point A and I want to go to point B,
A - - C - - H - - J
|
F- - K- -B
My idea for this is if B is in the List of adjacent locations of A, then that is the shortest path. If not, it should search the adjacent locations of the adjacent locations of A. But I do not know how to implement this in code or if it is a good algorithm. I also want to display A - C - F - K - B as the route for the shortest path. I am also developing this one on j2me so I am a bit limited on the java features that I can use.
If anyone can help me, it will be much appreciated. Thanks
You are on the right track. What you describe is the start of BFS. BFS is a shortest-path algorithm that is both optimal [finds the shortest path] and complete [always finds a path if one exist] for unweighted graph - so it is probably the right choice.
BFS works on graphs. In here your graph is G = (V,E) such that V = {all locations} [the nodes/vertices/locations] and E = {(u,v),(v,u) | u and v are neighbors} [the edges/links/neighbors]
The idea of BFS is simpilar to what you are suggesting: first check if the starting node is also the target. Then check if one of the neighbors of the starting node is the target, then search for their neighbors....
Regarding getting the actual path from BFS: have a look at this post.
The idea is to maintain a map - for each node [location] - the map will indicate how did you get there? which node discovered it? After the BFS finished - follow the map from target to source, and you get the actual path [reversed of course]. The link provided gives more details about this idea.
Your problem is known in the computing world as a graph search problem, looking for the shortest path between two nodes. Graphs here are not the x and y axis graphs from math, but Nodes (or Locations in your example) connected by edges.
Dijkstra's algorithm is the most commonly used to find the shortest path between two nodes, and for your use case it is simplified slightly because the edges in your scenario all have a weight (or cost) of one. An implementation of this is available in JGraphT, though I'm not sure how easy that is to include in a J2ME environment.
Have a look at the A* algorithm Dijkstra's pathfinding algorithm.