How to partition a graph using a quadtree? - java

I have a program that allows the user to draw vertices and edges on a JFrame of size 1000 by 750. Now I need to use a quadtree to partition the input graph depending on how many vertices there are in a single quadrant. I would really appreciate it if someone can point me in the right direction on how to achieve this?
Additional information:
I have an Edge class which stores: source (vertex), target (vertex) and weight.
I have a Vertex class which stores: name, x-coordinates, y-coordinates, and Edge[] adjacentList.
I also have a Graph class which stores two ArrayLists: edges and vertices.

I've recently implemented code, that should solve your problem. It's free for download on my recent blog post. Quadtrees for Space Decomposition, Java Implementation http://kirstywilliams.co.uk/blog/2012/08/quadtrees-java-implementation/

Some more quadtree implementations which are both apache licensed:
Apache SIS - no tests, fast, but several improvements necessary
GraphHopper - with tests, less memory consumption but probably a bit slower. The distance calculation is pluggable.
But if you already have a connected graph in memory you can create a much more memory efficient (but also a bit complicated) solution. Parts are implemented here, more details available here.
Another interesting repo (but AGPL I think as neo4j itself) with some spatial collections.

Related

Graph Coloring Visualization

I've written an genetic algorithm that tries to find the chromatic number for a given graph. I've been using the DIMACS benchmark graphs to test it.
I have to present the results of my algorithm to my faculty at end of the term, but the graphs are not visually interesting. Are there any good libraries out there (in Java hopefully since that is what our GA is in) for dynamically coloring images? I've been looking around and all I've found are GIS stuff (I'm thinking I just don't know what to call this sort of library).
You can try:
JUNG http://jung.sourceforge.net/
JGraphT and JGraph library http://jgrapht.org/visualizations.html
Both of them can handle million of nodes/edges (if you have enough RAM). As I remember, they also allowing coloring of nodes / edges.

Best way to traverse an almost complete undirected weighted graph

need help with an optimized solution for the following problem http://acm.ro/prob/probleme/B.pdf.
Depending on the cost i either traverse the graph using only new edges, or using only
old edges, both of them work, but i need to pass test in a limited number of milliseconds,
and the algorithm for the old edges is dragging me down.
I need a way to optimize this, any suggestions are welcome
EDIT: for safety reasons i am taking the algorithm down, i'm sorry, i'm new so I don't
know what i need to do to delete the post now that it has answers
My initial algorithmic suggestion relied on an incorrect reading of the problem. Further, a textbook breadth-first search or Dijkstra on a graph of this size is unlikely to finish in a reasonable amount of time. There's likely an early-termination trick that you can employ for large cases; the thread Niklas B. linked to suggests several (as well as some other approaches). I couldn't find an early-termination trick that I could prove worked.
These micro-optimisation notes are still relevant, however:
I'd suggest not using Java's built-in Queue container for this (or any other built-in Java containers for anything else in a programming contest). It turns your 4-byte int into a gargantual Integer structure. This is very probably where your blowup is coming from. You can use a 500000-long int[] to store the data in your queue and two ints for the front and back of the queue instead. In general, you want to avoid instantiating Objects in Java contest programming because of their overhead.
Likewise, I'd suggest representing the edges of the graph as either a single big int[] or a 500000-long int[][] to cut down on that piece of overhead.
I only saw one queue in you code. That means you are searching from one direction only.
You may want to take a look at
Bidirectional Search

Creating a Graph with Too Many Vertices

I am going to build a graph in my java project and I am using Jung library.
My problem is that I have too many vertices and with CircleLayout or FRlayout I can't have a good visualization of the graph; I mean the vertices are very very close to each other.
Can anybody help me what I can do in such a situation?
I want to have a nice visualization of the graph in which the vertices are far enough to be able to see which vertex is connected to which vertex.
Here you see the current visualization of graph vertices.
(Originally, this was a comment, but to give the question a chance to bre removed from the "unanswered" queue: )
One important question might be what the topology of the graph is. Does the topology resemble a circle? That is, does each vertex have exactly two neighbors?
Regardless of that, you should try the other layouts as well. Particularly the SpringLayout2. It has quite some tuning parameters, but usually causes a "nice" distribution of the vertices for a wide variety of graph topologies

How to calculate a timely list of GPS points in a given radius in memory (Java)?

I know this would be quite easy to do with PostGIS in a database, but I only expect my points to live for a couple of minutes max.
I would like to have a list of GPS points that live for 1-5min. When I add a new point, I would like to calculate a list of points from the aged list that are in a 1-10km radius of the new point.
It is still recommended to perform these operations in a database like PostGIS or Mongo?
If not, how would one go about calculating this in memory?
You can compute a quadkey, it's similar to a quadtree and bing maps use it for the tile server. A quadkey can be computed with a morton curve. You can download my php class hilbert-curve # phpclasses.org.
Databases are very slow, because they access slow disk memory.
For your solution, you need an indexing technic that Geo Spatial DBs uses, too, (PostGis or Oracle-Spatial). In your case your index stays in memory:
It is an (geo-) spatial index.
There are quad tree and R-Tree as state of the art. (sometimes a kd-tree also is used)
Quad tree are easier to implement, R-Tree can be twice as fast than quadtrees.
I would use the quad tree.
The most demanding part of your work is the delete operation of an point.
This is tricky and much slower than insert.
One solution, recomended by the original author of the quadtree, and which I would try: Mark the points as deleted, but keep them in the Quad tree.
Evry x minutes rebuild the whole quadtree, by creating a new one from thedata of the old one but by only keeping the points still existing.
Once the points are in the quad tree, a range search is reduced only to points "nearby"

How do I find the overlapping area between two arbitrary polygons

I'm trying to create a method that will take in two arbitrary lists of nodes, for a subject and a clipping polygon, and output either:
a) the area of the overlap
b) a list of nodes for the resulting (clipped) polygon so that I can calculate the area
I've found lots of examples which clip an arbitrary polygon using a rectangular window (which is fairly standard in graphics) but that's not what I need. I understand that it's fairly complex, particularly when you get holes, convex polygons and the like. The only simplifying assumption which I can make is that the arbitrary polygons will not contain any holes.
I'm not at all an expert in this field, so would something like the Sutherland-Hodgman algorithm work? Are there any libraries out there which already do this, or is my best bet to simply implement the algorithm as described in pseudo-code on Wikipedia?
Thanks for the help!
Are there any libraries out there which already do this ...
Polygon clipping is a complex task. I wouldn't recommend trying to do it yourself unless you want to spend many months on it.
Wikipedia lists a number of clipping libraries (and IIRC in that list only Clipper is free for use in commercial applications):
http://en.wikipedia.org/wiki/Boolean_operations_on_polygons#External_links
ps: I admit to a personal bias for Clipper since I'm the author :)
More info here: http://angusj.com/delphi/clipper.php
Sounds like Weiler-Atherton is the one you need:
The algorithm requires polygons to be
clockwise and not reentrant (self
intersecting). The algorithm can
support holes (as counter-clockwise
polygons wholly inside their parent
polygon), but requires additional
algorithms to decide which polygons
are holes.
Your polygons fit those criteria, right?
I don't know about implementations, but it sounds like you would be better off implementing W-A than S-H if either of your polygons could be concave.
Try gpc.
I found that using the JavaGeom library worked very well. It integrates the code from the Java port of GPC (which is no longer available) and thus allows arbitrary polygon operations. Using SimplePolygon2D and Polygon2DUtils.intersection() I was able to get the desired operation.
I've tried a lot of different libraries and the one that worked best was the JTS Topological Suite which is pure Java and LGPL2 licensed.

Categories