Graph drawing algorithms, such as those described here, check all the vertices two-by-two and apply additional forces if two vertices are connected by an edge. If we have a very large graph, checking all pairs of vertices would be costly. Is there any graph drawing algorithm that draws a large graph using only existing edges, not by verifying all possible pairs?
EDIT
By drawing algorithm, I meant an algorithm that assigns a 2D or 3D position to each vertex such that rendering spheres or circles (or any other shape) as vertices at their assigned positions lead to a plausible visual representation of the whole graph.
Check this Spring-Electrical Embedding
It is in O(nlog n).
If you have sparse matrix you can consider about creating graph as list of neighbours or simplier like pair of vertices (e.g. (1, 3) and 1 and 3 are the numbers of vertices).
Related
Suppose a set of 2d polygons is given.
What I am trying to do is to link all the polygons together by segments in a such way that:
each of the segments does not intersect any polygons in between (i.e. it connects the "visible" points of the polygon)
the path along the segments from one polygon to another does not have any loops(if you consider polygons to be the nodes of the graph and the segments to be the edges then the graph has no cycles, i.e. there is a tree of polygons).
For example:
At the moment I am solving the problem by brute force search of the segments from each polygon to all the others checking if there is no intersections in between. And then I am using Kruskal algorithm to find a tree, so the path could be constructed. The solution is ok for "simple" polygons but quite time consuming for more complex ones when the number of vertices and polygons is big.
Are there similar problems or better solutions to this problem ?
Just for fun I'm trying to create a non-flash version of http://www.jurjans.lv/stuff/net/FreeNet.htm. It's all pretty straightforward stuff, but I'm mentally stuck on how to generate the initial network.
I could do it square by square with lots of if/else logic checking neighboring squares, but frankly it seems very laborious and I wonder if there's a much much MUCH smarter way. Generate a mathematical graph or something similar and then translate that into the grid?
I'm not asking for someone to code it all for me - just point me in the right direction!
The completed circuit appears to be a spanning tree.
There is an easy way to generate random minimum spanning trees by:
assigning random weights from some distribution to the edges of an undirected graph, and then constructing the minimum spanning tree of the graph.
So to summarise:
Build a graph with a vertex at the centre of each square
Add edges between each vertex and its neighbours above/down/left/right
Assign random weights (e.g. uniform real from 0 to 1) to each edge
Construct minimum spanning tree e.g. with Prim or Kruskal
Convert graph into tiles
If there are certain shapes you want to forbid (e.g. a fully connected vertex) you may want an extra iteration to increase the weight for edges used in any tiles that are illegal, and then regenerate the spanning tree until you end up with a legal graph.
I have adjacency matrix for graph. I need to vizualize this graph without intersecting edges.
Vertex in the graph can be arranged randomly. I know one solution - enumeration of all edges for intersections. If the edges intersect, then rearrange the vertex, but it's too expensive for a large number of vertexes (more than 20).
Any other ideas how to check for intersecting edges?
It's really easy to visualise any graph in a 3D plane with disjoint edges.
1) Place all the vertices in any point in the 3D plane such that no three vertices are collinear and no four vertices are in same plane.
2) Traverse through the adjacency matrix and draw a line/curve to connect the vertices.
In a 2D plane it's not guaranteed for the existence of a solution. For example take the worst case scenario that there are some 10 vertices and every vertex is connected to each other.
Lets say I have a set of points and I have lines/edges between them. All of these edges create non-overlapping triangles within the convex hull of my points. All points are connected to triangles.
How can I efficiently check which points are part of which triangle? I could check incident points of each edge and gradually construct a triple of points, but that sounds awefully slow (o(n^2)?).
Is there something like linesweep or so to do that?
cheers.
If you have a 2-dimensional set-up like you described, then you have a fully triangulated planar graph (no intersecting edges when you exclude the endpoints) which spans the convex hull of your points. In this case, if you sort the edges around each vertex circularly according to the angle they make with the vertex, then you know for sure that each pair of adjacent edges makes a triangle. Furthermore, every triangle can be found this way if you perform this procedure for each vertex. Each triangle will be found 3 times when you iterate over all vertices. You can either use a hash table to detect duplicates, or sort all your triangles when you are done to identify duplicates. If you use hash table, then the overall complexity if you have V vertices is O(V log d), where d is the maximum degree of a vertex (because the total number of edges is linear in the number of vertices because you have a planar graph). So absolute worst-case is O(V log V), which is the same worst-case if you sort all triangles to find duplicates (because the max number of triangles is also linear in the number of vertices). The only caveat to make this work is that you need to know the neighbor vertices (i.e. the incidental edges) for each vertex.
The edges define an undirected graph G and the triangles are the set of cycles in G with length=3.
Geometric triangulations typically have relatively low nodal degree (degree d is the number of edges adjacent to each node, d<=10 is typical for geometric triangulations) and, as such, here is a reasonably efficient O(n*d^3) algorithm that can be used to construct the set of triangles.
Setup a graph-like data structure, supporting access to the list of edges adjacent to each node.
Iterate over all nodes. Consider all pairs of edges adjacent to a given node i. For a given pair of edges adjacent to i, we have a potential nodal triplet i,j,k. This triplet is a triangle if there is an edge joining nodes j,k, which can be checked by scanning the edge lists of j,k.
Duplicate triangles will be generated by a naive implementation of (2). Maintain a hash table of triangles to reject duplicate triplets as they're considered.
I've assumed that the edges define a valid disjoint triangulation, being non-intersecting, etc.
Hope this helps.
In the general case finding a Maximum Independent Subset of a Graph is NP Hard.
However consider the following subset of graphs:
Create an NxN grid of unit square cells.
Build a graph G by creating a vertex corresponding to every cell. Notice that there are N^2 vertices.
Create an edge between two vertices if their cells share a side. Notice there are 2N(N-1) edges.
A Maximum Independent Subset of G is obviously a checker pattern. A cell at the Rth row and Cth column is part of it if R+C is odd.
Now we create a graph G' by copying G and removing some vertices and edges. (If you remove a vertex also remove all edges it ended of course. Also note you can remove an edge without removing one of the vertices it ends.)
By what algorithm can we find a Maximum Independent Subset of G' ?
Read up here. I think you're still hosed, it remains NP-hard.
Since your degree is at most 4, the simple greedy algorithm obtains an approximation ratio of 2. Your resulting graph is also planar, so there is a good approximation algorithm (any fixed approximation ratio in poly time).