Is there a graph coloring algorithm where limits can be placed on number of vertices per color - algorithm

I understand that graph coloring is a NP-complete problem. I was wondering if adding a restriction on the number of vertices that can have a given color makes the problem simpler? I can't seem to find any algorithm which does this. For example if I have a graph, I'd like to say "what is the smallest coloring of this graph such that each color has at most 3 vertices", or if it simplifies the problem "is there a way to color this graph with 4 colors such that each color has at most 3 vertices"?
Thanks!

This problem is still NP-complete by a simple reduction from the original graph coloring problem: a graph with n nodes is k-colorable if and only if the graph can be colored with k colors and no color is assigned to more than n nodes. In other words, the general version of the problem you're phrasing has graph coloring as a special case, and so it will still be NP-hard.
Hope this helps!

I would say that looking for the chromatic number of a graph given the restriction that a k-coloring exists can be easily added to an exact DSATUR based algorithm [Randall-Brown 72] [San Segundo 11] and prune the search space when one vertex has to be assgined color k. In any case the problem remains in NP.

Related

Greedy colouring algorithm on graph in adjacency list representation

Suppose you have been given a simple undirected graph and the graph has a max degree of d. You are given d + 1 colors, represented by numbers starting from 0 to d and you want to return a valid placement of colors such that no two adjacent vertices share the same color. And as the title suggests, the graph is given in adjacency list representation. The algorithm should run in O(V+E) time.
I think the correct way to approach this is by using a greedy coloring algorithm. However, this may sound stupid but I am stuck on the part where I try to find the first available color that hasn't been used by its neighbors for each vertex. I don't really know how I can do it so that it runs in O(number of neighbors) time for each vertex that helps to fit under time complexity requirements.

Rearranging a graph so that certain nodes are not adjacent?

EDIT: Precisely, I am trying to find two disjoint independent sets of known size in a graph shaped like a triangular grid, which may have holes and has a variable perimeter shape.
I'm not very well versed in graph theory, so I'm not sure if there exists an efficient solution for this problem. Consider the following graphs:
The colors of any two nodes can be swapped. The goal is to ensure that no two red nodes are adjacent, and no two green nodes are adjacent. The edges marked with exclamation points are invalid. Basically, I need to write two algorithms:
Determine that the nodes in a given graph can be arranged so that red and green nodes are not adjacent to nodes of the same color.
Actually rearrange the nodes.
I'm a little lost on how to implement this. It's not too difficult to separate the nodes of one color, but repeating the process for the second color may mess up the first color. Without a way to determine whether the graph can actually be arranged properly, this process could loop forever.
Is there some kind of algorithm that I can use/write for this? I'm mainly interested in the first image's graph (a triangular grid), but a generic algorithm would work as well.
First, let's note that the problem is a variant of graph coloring.
Now, if you only dealing with 2 colors (red,green) - coloring a graph with 2 colors is fairly easy, and is basically done by finding out if the graph is bipartite, and coloring each "side" of the graph in one color. Finding if a graph is bipartite is fairly simple.
However, if you want more than two colors, the problem becomes NP-Complete, and is actually a variant of the Graph Coloring Problem.
Graph Coloring Problem:
Given a graph G=(V,E) and a number k determine if there is a
function c:V->{1,2.,,,.k} such that c(v) = v(u) -> (v,u) is not an
edge.
Informally, you can color the graph in k colors, and you need to determine if there is some coloring such that you never color 2 nodes that share an edge with the same color.
Note that while it seems your problem is slightly easier, since you already know what is the number of nodes in each color, it doesn't really make a difference.
Assume you have a polynomial time algorithm A that solves your problem.
Now, given an instance (G,k) of graph coloring - there are only O(n^3) possibilities to #color1,#color2,#color3 - so by examining each of these and invoking A on it, you can find a polynomial time solution to Graph-Coloring. This will mean P=NP, which is most likely (according to most CS researchers) not the case.
tl;dr:
For 2 colors: find out if the graph is bipartite - and give one color to each side of the graph.
For 3 or more colors: There is no known efficient solution, and the general belief is one does not exist.
I thought this problem would be easier for planar graph, but unfortunately it's not the case. Best match for this problem I was able to find is minimum sum coloring and largest bipartite subgraph.
For largest bipartite subgraph, assume that number of reds + number of greens exactly match the size of largest bipartite subgraph. Then this problem is equivalent to your. Paper claims that it's still NP-hard even for planar graphs.
For minimum sum coloring, assume that red color has weight 1, green color has color 2, and we have infinitely many blue* colors with some weight of >graph size. Then if answer is exactly minimal sum coloring, there is no polynomial algorithm to find it (although paper referes to such algorithm for chordal graphs).
Anyway, it seems that the closer your red+green count to the 'optimal' in some sense subgraph, the more difficult problem is.
If you can afford inexact solution, or relaxed solution then you only spearate, say, reds, you have an option. As I said in comment, approximate solution of maximum independent set problem for planar graph. Then color this set into red and blue colors, if it is large enough.
If you know that red+green is much less than total number of vertices, another approximation can work. Look at introduction chapter of this article. It claims that:
For graphs which are promised to have small chromatic number, a better
guarantee is known: given a k-colorable graph, an algorithm due to
Karger et al. [12] uses semidefinite programming (SDP) to find an
independent set of size about Ω(n/∆^(1−2/k)).
Your graph is for sure 4-colorable, so you can count on large enough independent set. The same article states that greedy solution already can find large enough independent set.

Coloring a Tree

There is a coloring a tree problem in one of contest which i could not solve.Problem Statement:We have to Color a tree such that the no two adjacent vertex is having the same color and the cost of coloring the tree is minimum
Problem Link
I have no idea what algorithm should i used in such type of problemsPLease the algorithm behind this.
This problem was more than just coloring a tree since there was a minimization problem. You can read the editorial here: https://www.facebook.com/notes/1047761065239794/

Introduction to algorithms A creative approach Exercise 5.25

Below is the exercise 5.25 in 《Introduction to algorithms, a creative approach》. After reading it several times, I still can't understand what it means. I can color a tree with 2 colors very easily and directly using the method it described, not 1+LogN colors.
《Begin》
This exercise is related to the wrong algorithm for determining whether a graph is bipartite, described in Section 5.11.In some sense, this exercise shows that not only is the algorithm wrong, but also the simple approach can not work. Consider the more general problem of graph coloring: Given an undirected graph G=(V,E), a valid coloring of G is an assignment of colors to the vertices such that no two adjacent vertices have the same color. The problem is to find a valid coloring, using as few colors as possible. (In general, this is a very difficult problem; it is discussed in Chapter 11.)
Thus, a graph is bipartite if it can be colored with two colors.
A. Prove by induction that trees are always bipartite.
B. We assume that the graph is a tree(which means that the graph is bipartite). We want to find a partition of the vertices into the two subsets such that there are no edges connecting vertices within one subset.
Consider again the wrong algorithm for determining whether a graph is bipartite, given in Section 5.11: We take an arbitrary vertex, remove it, color the rest(by induction), and then color the vertex in the best possible way. That is, we color the vertex with the oldest possible color, and add a new color only if the vertex is connected to vertices of all the old colors. Prove that, if we color one vertex at a time regardless of the global connections, we may need up to 1+logN colors.
You should design a construction that maximizes the number of colors for every order of choosing vertices. The construction can depend on the order in the following way.
The algorithm picks a vertex as a next vertex and starts checking the vertex’s edges. At that point, you are allowed to add edges incident to this vertex as you desire, provided that the graph remains a tree, such that, at the end, the maximal number of colors will be required. You can not remove an edge after it is put in(that would be cleanining the algorithm, which has already seen the edge). The best way to achieve this construction is by induction. Assume that you know a construction that requires<=k colors with few vertices, and build one that requires k+1 colors without adding too many new vertices.
《End》

How to find one hamiltonian path in cube((2n)^3)?

I solve acm-icpc problem a kind of graph theory.
Traveling Spiders is a logical puzzle that needs a sequence of
systematic choices among many alternatives. The puzzle assumes a
geometric object whose surface is partitioned into a set of areas
called cells that are usually congruent, or very similar in their
shapes and sizes. A spider may move around freely on the surface of
the object, but, can move forward from a cell to only one of its
neighboring cells in each step. Now, given some pairs of male and
female spiders, initially positioned all in different cells, we want
to find a set of paths that, respectively, leads each male spider to
his partner. The only condition is that every cell of the object must
be visited once and only once by a male spider during their traversal.
the cube exists in the space [0, 2n] x [0, 2n] x [0, 2n], and n can be 2 <= n <= 50.
we have to find one hamiltonian path when I game two position start from A to B.
and print all of path(1 0 1 -> 1 0 3 -> .... -> 3 1 4).
My friend said it is can't see general answer. because It is quite difficult to exact planar graph. and It cannot judge hamiltonian path or not.
how I can find hamiltonian path in general case?;
Looks like Gray code problem, specifically n-ary Gray code.
Gray codes are Hamiltonian cycles, but you are looking for a Hamiltonian path with end vertices A and B. I'm not sure, but maybe Monotonic Gray codes can help. If cube vertices can be partitioned in V_i, so that V_0 = {A}, V_n = {B}, than construction in article can solve problem.
Edit: There is a reference, on Wikipedia page, to Knuth's draft of n-tuple generation from The Art Volume 4A.
The problem is NP complete and you need to brute force every possible path. See here: Algorithms to find the number of Hamiltonian paths in a graph. However, in your task there is a shortcut because it's a rubic cube and a gray code traversal of a rubics cube is a hamiltonian path. There are 6 ways of a gray code traversal of a rubic cube. For example if you have an octree and you find a hamiltonian path most likely you have found a space-filling-curve reducing the 3d to a 1d complexity. Then you can sort the path from top to bottom or from bottom to top. When you have the other 5 space-filling-curves you can have other lists. Here is a link that explains the difference between an euler path and a hamiltonian path: Difference between hamiltonian path and euler path. Here is an example of a hilbert curve http://www.tiac.net/~sw/2008/10/Hilbert/ and it uses a binary-reflected code, not a n-ary code or a monotonic gray code.

Resources