-- Application of Groebner basis to determining whether a given planar non-cyclical -- graph can be colored with 3 colors where neighboring vertices (connected by a single edge) -- have different colors -- We express the problem in terms of polynomials. Assign the three distinct cuve roots of -- unity to represent each color then for n vertices we have n equations in the ideal I of the form: -- X_i ^ 3 - 1 = 0 (X_i represents the vertices since each vertex must be colored -- these polynomials are in the ideal) -- The vertices X_i, X_j connected by an edge must have a different color -- since X_i^3 = X_j^3 [1] we must have that (X_i - X_j)(X_i^2 + X_i*X_j + X_j^2)=0 [2] -- because the two polynomials are equivalent it must be that (X_i^2 + X_i*X_j + X_j^2)=0 -- for all X_i, X_j that are neighbors. These too are in the ideal I. -- Theorem: A graph F is 3 colorable if and only if V(I) is nonempty. -- In other words if there is a common solution to all the polynomials that define a graph -- then it is 3 colorable. -- We us Groebner basis to determine whether or not the variety formed by the above ideal -- is empty. It is if 1 belongs to the resulting Groebner basis isTriColorable = (vertices, vertexList)-> ( R=QQ[z_1..z_(#vertices+1), MonomialOrder=>Lex]; i=1; j=0; K={}; while i < #vertices+1 do ( K=append(K,(z_i)^3-1); i=i+1; ); while j < #vertexList do ( K=append(K,z_(vertexList#j#0)^2 + z_(vertexList#j#0)*z_(vertexList#j#1) + z_(vertexList#j#1)^2); j=j+1; ); I=ideal(K); G= gb(I); return G; ) --Examples --Simple graph, with only one way to color (up to permutation of color) so the Groebner Basis --is also simple example1vertices={1,2,3,4,5,6,7,8}; example1graph={{1,2},{1,5},{1,6},{2,3},{2,4},{2,8},{3,4},{3,8},{4,5},{4,7},{5,6},{5,7},{6,7},{7,8}}; Ex1= time isTriColorable(example1vertices, example1graph) --Same graph as above except with one extra edge notcolorable = {1,2,3,4,5,6,7,8}; notcolorablegraph ={{1,2},{1,5},{1,6},{2,3},{2,4},{2,5},{2,8},{3,4},{3,8},{4,5},{4,7},{5,6},{5,7},{6,7},{7,8}}; Ex2 = isTriColorable(notcolorable, notcolorablegraph) --Final test 1 % Ex2 == 0 1 % Ex1 == 0 M1 = transpose gens Ex1 degree ideal gens Ex1 --Graph that can be colored several different ways, complicated Groebner basis takes longer to compute example3vertices={1,2,3,4,5,6,7,8,9}; example3graph = {{1,2},{1,4},{1,5},{1,6},{2,3},{2,5},{2,7},{3,4},{3,6},{3,9},{5,6},{6,8},{7,8},{8,9}}; Ex3 = time isTriColorable(example3vertices, example3graph) M3 = transpose gens Ex3 dim ideal gens Ex3 degree ideal gens Ex3 degree radical ideal gens Ex3 1 % Ex3 == 0 -- This allows to tell whether or not any planar graph is 3 colorable. Theorems about -- 3 colorability of graphs refer to specific graphs such as triangluated graphs in which -- all vertices have an even degree are always 3 colorable. Or the Grotzch's theorem which -- states that any triangle free (no 3 cycle) graph is 3 colorable -- The End -- Special bonus Four colorable, just the same except that in place -- of cube roots of unity we have quadratic roots, and -- the polynomial of neighboring vertices, should be -- x_i^3 + x_i^2*x_j + x_i*x_j + x_j^3 = 0 isFourColorable = (vertices, vertexList)-> ( R=QQ[z_1..z_(#vertices+1), MonomialOrder=>Lex]; i=1; j=0; K={}; while i < #vertices+1 do ( K=append(K,(z_i)^4-1); i=i+1; ); while j < #vertexList do ( K=append(K,z_(vertexList#j#0)^3 + z_(vertexList#j#0)^2*z_(vertexList#j#1) + z_(vertexList#j#0)*z_(vertexList#j#1)^2+ z_(vertexList#j#1)^3); j=j+1; ); I=ideal(K); G= gb(I); return G; ) fourcolorable = {1,2,3,4,5,6,7,8}; fourcolorablegraph ={{1,2},{1,5},{1,6},{2,3},{2,4},{2,5},{2,8},{3,4},{3,8},{4,5},{4,7},{5,6},{5,7},{6,7},{7,8}}; gbTrace 3 Ex2 =trac isFourColorable(fourcolorable, fourcolorablegraph) --Final test 1 % Ex2 == 0