Creating a Graph with Too Many Vertices - java

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

Related

K farthest points from vertex in graph edu.uci.ics.jung

I want to find K farthest points from a given vertex in a jung implementation of directed graph.
I assume that BFSDistanceLabeler does the job. However, it doesn't provide api for returning K farthest points, so I'll have to do it manually by traversing all vertices in graph and calling getDistance method. Or is there a better way?
But there is a bigger challenge for me. Despite the fact the graph is directed, I want to treat it as undirected for the distance labeler. Is it possible to somehow switch fast from from directed graph to its undirected version?
Why do I need to treat graph as undirected?
I analyze a very large network (milions of vertices) in consequent steps. In every step a small part of network (thousands of vertices) is loaded into a graph and analysed. This analysis requires directed graph and provides result for a one specific vertex which has to be located in the center of loaded area.
When I move from step A to step B, I could delete the whole previous graph and create a new one. Nevertheless, this would be very time consuming. Because I know my new vertex of interest is near to the previous one, a big part of graph can be reused.
And that is why I need to remove K farthest vertices for the new main vertex and replace them with new vertices from surrounding of this vertex.
Lets take a look at bottom picture with graph and lets say that vertex 1 is our vertex of interest. Since the graph is directed vertex number 6 is farthest. However, if graph was treated as undirected, then vertex number 4 would be the farthest and that is what i need.
There is not going to be an asymptotically faster way to find all the farthest vertices from a given input vertex than finding the distances to all vertices.
You can get the farthest vertices more efficiently by calling getVerticesInOrderVisited(), and then traversing the list in reverse order starting from the 'tail': this list should be populated in increasing order of distance from the root (set), so just get vertices from the list until the distance for each one starts decreasing.
(Note: this will not pick up the vertices that may be completely disconnected from the root vertex, which you may consider to be the "farthest"; getUnvisitedVertices() will do that.)
To answer your second question*: essentially what you want to do is to treat the directed edges as undirected. JUNG allows you to do this; instead of using getSuccessors()/getPredecessors(), you can call getNeighbors(), for instance.
As you've inferred, BFSDistanceLabeler doesn't do this; it wants to respect the edge direction if it exists, so it uses getSuccessors().
So here are some options:
Use jung.algorithms.transformation.DirectionTransformer.toUndirected(). This is very easy, but involves creating a bunch of new (undirected) edges
Create a subclass of BFSDistanceLabeler that overrides labelDistances(), and replaces getSuccessors() with getNeighbors(). This is pretty easy, if not very elegant.
Create a GraphDecorator subclass that overrides getSuccessors() to call getNeighbors() on its delegate graph. Then create an instance of your subclass where your original graph is the delegate.
This is the most elegant and general solution. (And at some point it may be useful for us to provide utilities that do this for you; please feel free to file an issue on the JUNG GitHub page: https://github.com/jrtom/jung/issues)
Hope this helps.
*For future reference, please split unrelated questions into separate StackOverflow questions; it makes them easier to answer and to find.

Place tables with relations [duplicate]

To simplify the problem, I have a graph that contains nodes and edges which are on a 2D plane.
What I want to be able to do is click a button and it make the automatically layout the graph to look clean. By that I mean minimal crossing of edges, nice space between nodes, maybe even represent the graph scale (weighted edges).
I know this is completely subjective of what is a clean looking graph, but does anyone know of an algorithm to start with, rather than reinventing the wheel?
Thanks.
You will find http://graphdrawing.org/ and this tutorial, by Roberto Tamassia, professor at Brown University, quite helpful.
I like a lot Force-Directed Techniques (pp. 66-72 in the tutorial) like the Spring Embedder.
You assume there is a spring or other force between any two adjacent nodes and let nature (simulation) do the work :)
I would suggest that you take a look at at graphviz. The dot program can take a specification of a graph and generate an image of the network for you somewhat "cleanly". I've linked to the "theory" page that gives you some links that might be relevant if you're interested in the theoretical background. The library and tools themselves are mature enough if you simply want a solution to a problem with layout that you're facing.
I would say as Noufal Ibrahim, but you could also look more precisely at the C API of the graphviz project. It includes a lib to build your graph (libgraph.pdf) with all the nodes and edges, and a lib to layout the graph (libgvc.pdf) (just compute each nodes position), so you can then display it in your own UI for example.
Also JGraph if you want the layouts in Java (I work on the project).
A good visual guide how the most popular layouts actually look: follow the link

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.

How to partition a graph using a quadtree?

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.

How to get the cut-set using the Edmonds–Karp algorithm?

I implemented the Edmonds–Karp algorithm using the Pseudocode that I found in the Edmonds–Karp algorithm wiki page: http://en.wikipedia.org/wiki/Edmonds%E2%80%93Karp_algorithm
It works great, yet the algorithm output is the max flow value(min cut value), I need to the list of edges that this cut contains
I tried to change the algorithm, with no success, can you guys help?
Thank you
If you already have the flow, then compute the residual graph. Then do a depth first search from the source (or breadth first search, I don't think it matters), to compute the vertices in one half of the cut (S). The remaining vertices are in the other half of your cut, T.
This gives you your cut (S, T). If you specifically want the edges between S and T, you could iterate through all the edges, selecting the ones which connect S and T. (Though there may a be a more eleegant way to do this last part.)
Here's some pointers to help clarify how to do this for anyone in the future.
To find the S vertices, do a BFS (or DFS) search from source vertex, only following edges in which some capacity for flow is remaining. (In other words, the non-saturated edges). These ones by definition cannot be in the mincut.
To find the T vertices perform a BFS (or DFS) search from the sink vertex, only connecting to vertices that were not already added to the S set. These can have residual flow, or could be fully saturated, it doesn't matter. Because you are performing BFS from the sink, you'll need to make sure you follow the opposite direction the edge is pointed if the graph is directed. NOTE: It's important to only include vertices that can be reached from the sink in your T set, otherwise you'll end up including edges in your min-cut that do not belong there. (This is what threw me for a long time.)
Once you have these two sets of vertices you can then iterate over all the edges of the graph. Anyone that has a source node in S and a target node in T is part of your min-cut. It's important to note, though, that this is just one of many possible min-cuts. It's much more time intensive to find all the possible S-T min-cuts in a graph.
Hope this helps any future internet people trying to figure this out! Good luck!
If you already know the maximal flow, then the minimal cut is (S, T), where S is the set of vertices reachable from the source in the residual network, T is the complementary set. Edges that connect a vertex from S and a vertex from T belong to the cut.

Categories