Graph colouring in python using adjacency matrix - python

How can I implement graph colouring in python using adjacency matrix? Is it possible? I implemented it using list. But it has some problems. I want to implement it using matrix. Can anybody give me the answer or suggestions to this?

Is it possible? Yes, of course. But are your problems with making Graphs, or coding algorithms that deal with them?
Separating the algorithm from the data type might make it easier for you. Here are a couple suggestions:
create (or use) an abstract data type Graph
code the coloring algorithm against the Graph interface
then, vary the Graph implementation between list and matrix forms
If you just want to use Graphs, and don't need to implement them yourself, a quick Google search turned up this python graph library.

Implementing using adjacency is somewhat easier than using lists, as lists take a longer time and space. igraph has a quick method neighbors which can be used. However, with adjacency matrix alone, we can come up with our own graph coloring version which may not result in using minimum chromatic number. A quick strategy may be as follows:
Initalize: Put one distinct color for nodes on each row (where a 1 appears)
Start: With highest degree node (HDN) row as a reference, compare each row (meaning each node) with the HDN and see if it is also its neighbor by detecting a 1. If yes, then change that nodes color. Proceed like this to fine-tune. O(N^2) approach! Hope this helps.

Related

Simulation - Voxel Grid

I'm trying to build a simulation that will take place in a 1000x1000x1000 space. For each point in space, I need to be able to encode 2 or 3 properties.
I also need to be able to do some basic operations on the space, such as, given a point, find the properties of the 26 adjacent 3D neighbors of the point.
All points will lie on vertices in the 1000x1000x1000 space (i.e. every point is discrete).
I wrote up a version in python using numpy and it is much too slow. I've also looked for libraries that would speed it up but couldn't find anything.
Does anyone know of a python library that would provide useful helper functions and would be appropriate for a simulation of this size?
Using Numpy to together with the Numba python-compiler for the more intricate algorithms can take you a long way.
Also, I think you are refering to a "stencil" algorithm, and numba has specific stencil-functionality that could help you.
But start with a smaller grid during development!

Efficient determination of adjacent cells in a Voronoi diagram from scipy.spatial.Voronoi

I'm working on an algorithm that uses Voronoi diagrams. I need to know for every given cell, which cells it has as neighbors; that is, which neighbors it shares an edge with. This is similar to an existing question. However, I already have an algorithm that computes this, but I'm looking to speed it up and avoid redundant calculations.
Currently I'm doing this with the output from scipy.spatial.Voronoi which gives me arrays of the vertices, points, etc that I can build this mapping with. However, I'm running this algorithm with a lot of points and I'd like to speed up the process.
My understanding is that scipy and Qhull calculate the Delaunay triagulation and then uses that to compute the Voronoi diagram. I think (but could be mistaken) that adjacency information can be found from the Delaunay triangulation. I'm wondering if there is a way to extract this information (if it exists) from scipy/Qhull when I generate the Voronoi diagram.
If not, are there any preferred methods to do this? Would I be better off, in the long run, using Qhull directly ?
Thanks.
I think it's only possible with fortune algorithm:https://cs.stackexchange.com/questions/80939/voronoi-diagram-status-structure-in-fortunes-algorithm.
Look for half-egde.
Maybe you can implement the half-edge with other solution but not with qhull.

octree vs graph data structure

I need to implement in python a data structure that each node in the data structure represents a rectangle on a plane.
the operation that I need from the data structure is:
1) split a node, that split a rectangle into 4 rectangles with the same
size(in the end I suppose to get something like from A to B in this example)
2) get all neighbor rectangles(for some computation)
Up to now, I thought about two options both of them not optimal, the first one is to use some kind of octree/quadtree which make the splitting very easy but I'm not sure about finding all the neighbor rectangles. the second is a Graph which enables me to find the neighbors very easy but makes it difficult to split a node.
I didn't succeed to think about an elegant solution for doing both things, and I will appreciate suggestions, even better if they are implemented in a python library.

Implementations of planar graphs/maps (with embeddings)

For the purpose of this post, by a plane graph, or a planar map, I will mean an abstract graph that can be drawn in the plane (or equivalently on the sphere), together with the circular order of the edges at every vertex according to a particular such drawing. This extra information determines the embedding on the sphere (up to moving vertices and edges in such a way that they never intersect any other vertices / edges). I do want to allow loops and multiple edges.
For example, suppose that we have build a graph as follows. Draw two vertices (A and B) in the plane, together with two edges connecting these two. The two edges together form a simple closed curve \gamma. Now add two more vertices, A' and B', and connect A and A' with an edge, and B and B' also.
This abstract graph will have two inequivalent embeddings, according to whether the vertices A' and B' are separated by the curve \gamma or not.
My question is: Is there a Python package that implements such plane graphs?
I am interested in a package that can create a drawing of a plane graph (respecting the embedding, of course), as well as execute some standard operations (e.g. give the number of faces, form a dual graph etc.)
If such a package does not exist in Python, I would also be interested in implementations in other languages.
Of course there are various packages that implement the drawing of graphs, and graph-theoretic algorithms. However, I did not notice in any of these the possibility of working with graphs that already come with an embedding. A reference would be much appreciated.
EDIT. Let me elaborate a little further. Two embeddings of the same graph in the sphere are equivalent if they are related by a homeomorphism of the sphere to itself. As mentioned above, the embedding of a planar graph is not unique, in general, so what I am asking is not the same as testing a graph for planarity and drawing some embedding thereof.
There are several combinatorial ways of encoding embeddings up to this equivalence. Probably the simplest is to record the cyclic order of edges at each vertex ("rotation system"), but there are many others. See the article on graph embeddings on Wikipedia for a discussion and references.
There are obvious operations that one may wish to carry out on such a combinatorial embedding, e.g. find the faces of the graph, find the faces an edge/vertex is adjacent to, insert a vertex in a face, subdivide an edge, draw a picture of the embedding etc.
Is there an implementation of one or several of these data structures representing combinatorial graph embeddings available in Python? (I remark that graph embeddings make sense on general surfaces, although I am primarily interested in the case of the sphere.)
I have just noticed that networkx does contain a class for the purpose I describe, namely PlanarEmbedding. See https://networkx.github.io/documentation/latest/reference/algorithms/planarity.html
(I am not certain whether this has been introduced since I asked the question, or whether I missed it at the time.)
The methods that come with the class do seem to be quite basic, and this only implements one of the possible data structures for planar embeddings. I will investigate further; if anyone knows of other implementations, that information would be appreciated.
I'm not sure I completely understand your requirement, but have you looked at this
planarity library. It is a cython wrapper to a c(++?) implementation of planarity algorithms, including drawing.
I've been using it with networkX.

Visualize high dimensional field arrows?

I have a big list of tuples (a, b), where both a and b are 9-dimensional vectors from the same space. This essentially encodes states of a system and some transitions. I would like to visualize the field described by these tuples, as arrows pointing from a->b, either in 2D or 3D. One of my problems however is that this is not a well-behaved vector field (not continuous) but I have reasons to believe that it can probably be laid out nicely, even in 2D.
Does anyone know of a toolbox (for matlab/python) or program that can do this? This would presumably first involve some kind of dimensionality reduction on a and b and then plot little arrows from one point to another.
Thank you for your help!
I'm not 100% sure if this answers your question or not, but you may want to look at Recurrence Plots. If this is what you're after, then you wont need any additional Matlab toolboxes.
Okay, turns out MATLAB can do this but it's not very pretty.
It basically boils down to doing PCA, and then using the quiver function to do the plotting:
My matrix X here contains starting points of my high dimensional nodes in odd rows, and ending points in even rows. Then:
[COEFF, SCORE]= princomp(zscore(X));
x=SCORE(1:2:end,1);
y=SCORE(1:2:end,2);
z=SCORE(1:2:end,3);
u=SCORE(2:2:end,1);
v=SCORE(2:2:end,2);
w=SCORE(2:2:end,3);
quiver3(x,y,z,u-x,v-y,w-z,0);
The downside is that I can't find a good way to color the edges, so I get a huge mess if I just do it trivially. Ah well, good enough for now!
Here's a Matlab toolbox of dimension reduction algorithms. I haven't worked with it, but I have worked with dimension reduction, and it seems like a manifold charting/local coordinates algorithm would be able to extract a low-dimensional representation.
TU Delft Dim. Red. Toolbox

Categories