We introduced graph coloring and applications in previous post. As discussed in the previous post, graph coloring is widely used. Unfortunately, there is no efficient algorithm available for coloring a graph with minimum number of colors as the problem is a known NP Complete problem. There are approximate algorithms to solve the problem though. Following is the basic Greedy Algorithm to assign colors. It doesn’t guarantee to use minimum colors, but it guarantees an upper bound on the number of colors. The basic algorithm never uses more than d+1 colors where d is the maximum degree of a vertex in the given graph.
Basic Greedy Coloring Algorithm:
1. Color first vertex with first color.
2. Do following for remaining V-1 vertices.
….. a) Consider the currently picked vertex and color it with the
lowest numbered color that has not been used on any previously
colored vertices adjacent to it. If all previously used colors
appear on vertices adjacent to v, assign a new color to it.
Following are C++ and Java implementations of the above Greedy Algorithm.
Coloring of graph 1 Vertex 0 ---> Color 0 Vertex 1 ---> Color 1 Vertex 2 ---> Color 2 Vertex 3 ---> Color 0 Vertex 4 ---> Color 1 Coloring of graph 2 Vertex 0 ---> Color 0 Vertex 1 ---> Color 1 Vertex 2 ---> Color 2 Vertex 3 ---> Color 0 Vertex 4 ---> Color 3
Time Complexity: O(V^2 + E) in worst case.
Analysis of Basic Algorithm
The above algorithm doesn’t always use minimum number of colors. Also, the number of colors used sometime depend on the order in which vertices are processed. For example, consider the following two graphs. Note that in graph on right side, vertices 3 and 4 are swapped. If we consider the vertices 0, 1, 2, 3, 4 in left graph, we can color the graph using 3 colors. But if we consider the vertices 0, 1, 2, 3, 4 in right graph, we need 4 colors.
So the order in which the vertices are picked is important. Many people have suggested different ways to find an ordering that work better than the basic algorithm on average. The most common is Welsh–Powell Algorithm which considers vertices in descending order of degrees.
How does the basic algorithm guarantee an upper bound of d+1?
Here d is the maximum degree in the given graph. Since d is maximum degree, a vertex cannot be attached to more than d vertices. When we color a vertex, at most d colors could have already been used by its adjacent. To color this vertex, we need to pick the smallest numbered color that is not used by the adjacent vertices. If colors are numbered like 1, 2, …., then the value of such smallest number must be between 1 to d+1 (Note that d numbers are already picked by adjacent vertices).
This can also be proved using induction. See this video lecture for proof.
We will soon be discussing some interesting facts about chromatic number and graph coloring.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- Edge Coloring of a Graph
- Coloring a Cycle Graph
- Graph Coloring | Set 1 (Introduction and Applications)
- Mathematics | Planar Graphs and Graph Coloring
- Boruvka's algorithm | Greedy Algo-9
- Greedy Algorithm for Egyptian Fraction
- Set Cover Problem | Set 1 (Greedy Approximate Algorithm)
- K Centers Problem | Set 1 (Greedy Approximate Algorithm)
- Greedy Algorithm to find Minimum number of Coins
- Dijkstra's shortest path algorithm | Greedy Algo-7
- Dijkstra’s Algorithm for Adjacency List Representation | Greedy Algo-8
- Kruskal’s Minimum Spanning Tree Algorithm | Greedy Algo-2
- Hierholzer's Algorithm for directed graph
- Welsh Powell Graph colouring Algorithm
- Find if a degree sequence can form a simple graph | Havel-Hakimi Algorithm
Improved By : ChamanJhinga