{"id":25687,"date":"2017-10-25T19:57:26","date_gmt":"2017-10-25T14:27:26","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=25687"},"modified":"2017-10-25T19:57:26","modified_gmt":"2017-10-25T14:27:26","slug":"backtracking-set-5-m-coloring-problem","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/backtracking-set-5-m-coloring-problem\/","title":{"rendered":"C Programming-Backtracking Set 5 (m Coloring Problem)"},"content":{"rendered":"<p>Given an undirected graph and a number m, determine if the graph can be colored with at most m colors such that no two adjacent vertices of the graph are colored with same color. Here coloring of a graph means assignment of colors to all vertices.<\/p>\n<p>Input:<br \/>\n1) A 2D array graph[V][V] where V is the number of vertices in graph and graph[V][V] is adjacency matrix representation of the graph. A value graph[i][j] is 1 if there is a direct edge from i to j, otherwise graph[i][j] is 0.<br \/>\n2) An integer m which is maximum number of colors that can be used.<\/p>\n<p>Output:<br \/>\nAn array color[V] that should have numbers from 1 to m. color[i] should represent the color assigned to the ith vertex. The code should also return false if the graph cannot be colored with m colors.<\/p>\n<p>Following is an example graph (from Wiki page ) that can be colored with 3 colors.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-25691\" src=\"https:\/\/www.wikitechy.com\/technology\/wp-content\/uploads\/2017\/05\/Backtracking-Set-5-m-Coloring-Problem.png\" alt=\"Backtracking Set 5 (m Coloring Problem)\" width=\"220\" height=\"211\" \/><\/p>\n<p><strong>Naive Algorithm<\/strong><br \/>\nGenerate all possible configurations of colors and print a configuration that satisfies the given constraints.<\/p>\n<pre>while there are untried conflagrations\r\n{\r\n   generate the next configuration\r\n   if no adjacent vertices are colored with same color\r\n   {\r\n      print this configuration;\r\n   }\r\n}\r\n<\/pre>\n<p>There will be V^m configurations of colors.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p><strong>Backtracking Algorithm<\/strong><br \/>\nThe idea is to assign colors one by one to different vertices, starting from the vertex 0. Before assigning a color, we check for safety by considering already assigned colors to the adjacent vertices. If we find a color assignment which is safe, we mark the color assignment as part of solution. If we do not a find color due to clashes then we backtrack and return false.<\/p>\n<p><strong>Implementation of Backtracking solution<\/strong><\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <span class=\"code-embed-name\">C<\/span> <\/div> <pre class=\"language-c code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-c code-embed-code\">#include&lt;stdio.h&gt;<br\/> <br\/>\/\/ Number of vertices in the graph<br\/>#define V 4<br\/> <br\/>void printSolution(int color[]);<br\/> <br\/>\/* A utility function to check if the current color assignment<br\/>   is safe for vertex v *\/<br\/>bool isSafe (int v, bool graph[V][V], int color[], int c)<br\/>{<br\/>    for (int i = 0; i &lt; V; i++)<br\/>        if (graph[v][i] &amp;&amp; c == color[i])<br\/>            return false;<br\/>    return true;<br\/>}<br\/> <br\/>\/* A recursive utility function to solve m coloring problem *\/<br\/>bool graphColoringUtil(bool graph[V][V], int m, int color[], int v)<br\/>{<br\/>    \/* base case: If all vertices are assigned a color then<br\/>       return true *\/<br\/>    if (v == V)<br\/>        return true;<br\/> <br\/>    \/* Consider this vertex v and try different colors *\/<br\/>    for (int c = 1; c &lt;= m; c++)<br\/>    {<br\/>        \/* Check if assignment of color c to v is fine*\/<br\/>        if (isSafe(v, graph, color, c))<br\/>        {<br\/>           color[v] = c;<br\/> <br\/>           \/* recur to assign colors to rest of the vertices *\/<br\/>           if (graphColoringUtil (graph, m, color, v+1) == true)<br\/>             return true;<br\/> <br\/>            \/* If assigning color c doesn&#039;t lead to a solution<br\/>               then remove it *\/<br\/>           color[v] = 0;<br\/>        }<br\/>    }<br\/> <br\/>    \/* If no color can be assigned to this vertex then return false *\/<br\/>    return false;<br\/>}<br\/> <br\/>\/* This function solves the m Coloring problem using Backtracking.<br\/>  It mainly uses graphColoringUtil() to solve the problem. It returns<br\/>  false if the m colors cannot be assigned, otherwise return true and<br\/>  prints assignments of colors to all vertices. Please note that there<br\/>  may be more than one solutions, this function prints one of the<br\/>  feasible solutions.*\/<br\/>bool graphColoring(bool graph[V][V], int m)<br\/>{<br\/>    \/\/ Initialize all color values as 0. This initialization is needed<br\/>    \/\/ correct functioning of isSafe()<br\/>    int *color = new int[V];<br\/>    for (int i = 0; i &lt; V; i++)<br\/>       color[i] = 0;<br\/> <br\/>    \/\/ Call graphColoringUtil() for vertex 0<br\/>    if (graphColoringUtil(graph, m, color, 0) == false)<br\/>    {<br\/>      printf(&quot;Solution does not exist&quot;);<br\/>      return false;<br\/>    }<br\/> <br\/>    \/\/ Print the solution<br\/>    printSolution(color);<br\/>    return true;<br\/>}<br\/> <br\/>\/* A utility function to print solution *\/<br\/>void printSolution(int color[])<br\/>{<br\/>    printf(&quot;Solution Exists:&quot;<br\/>            &quot; Following are the assigned colors \\n&quot;);<br\/>    for (int i = 0; i &lt; V; i++)<br\/>      printf(&quot; %d &quot;, color[i]);<br\/>    printf(&quot;\\n&quot;);<br\/>}<br\/> <br\/>\/\/ driver program to test above function<br\/>int main()<br\/>{<br\/>    \/* Create following graph and test whether it is 3 colorable<br\/>      (3)---(2)<br\/>       |   \/ |<br\/>       |  \/  |<br\/>       | \/   |<br\/>      (0)---(1)<br\/>    *\/<br\/>    bool graph[V][V] = {{0, 1, 1, 1},<br\/>        {1, 0, 1, 0},<br\/>        {1, 1, 0, 1},<br\/>        {1, 0, 1, 0},<br\/>    };<br\/>    int m = 3; \/\/ Number of colors<br\/>    graphColoring (graph, m);<br\/>    return 0;<br\/>}<\/code><\/pre> <\/div>\n<p><strong>Output:<\/strong><\/p>\n<pre>Solution Exists: Following are the assigned colors\r\n 1  2  3  2<\/pre>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>Backtracking Set 5 (m Coloring Problem)-Backtracking-Given an undirected graph and a number m, determine if the graph can be colored with at most m colors .<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[69969,73907],"tags":[74805,74485,74484,74498,74801,74791,74799,74823,73908,74160,74164,74493,74162,74810,74816,74819,74811,74803,74802,74822,74793,74804,74806,74792,74795,74488,74797,74491,74800,74807,70598,74815,74798,74796,74812,74817,74163,74818,74821,74813,74814,74809,74501,74509,74820,74496,74614,74794,74808],"class_list":["post-25687","post","type-post","status-publish","format-standard","hentry","category-algorithm","category-backtracking-algorithm","tag-2-color","tag-4-queen-problem-using-backtracking","tag-4-queens-problem-using-backtracking-algorithm","tag-8-queen-problem-using-backtracking","tag-aakash-assignment-solutions","tag-aakash-solutions","tag-aakash-target-solutions","tag-algorithm-graph","tag-backtracking-algorithm","tag-backtracking-algorithm-example","tag-backtracking-algorithm-pdf","tag-backtracking-in-daa","tag-backtracking-problems","tag-ccc-color","tag-chromatic-number","tag-chromatic-number-of-a-graph","tag-color-definition","tag-color-numbers","tag-color-set","tag-color-tree","tag-coloring","tag-colour-numbers","tag-colour-tree","tag-colouring-in","tag-conjuring2","tag-constraint-satisfaction-problem","tag-constraint-satisfaction-problem-in-artificial-intelligence","tag-cryptarithmetic-problem-solved-examples","tag-face-colour","tag-facecolor","tag-graph-algorithms","tag-graph-coloring","tag-graph-coloring-algorithm","tag-graph-coloring-algorithm-using-backtracking","tag-graph-coloring-example","tag-graph-coloring-problem","tag-graph-coloring-using-backtracking","tag-graph-colouring","tag-graph-problems","tag-hamiltonian-cycle-algorithm-using-backtracking","tag-knapsack-problem-using-backtracking","tag-m-coloring-problem","tag-n-queen-problem-using-backtracking","tag-n-queens-problem-using-backtracking","tag-state-space-tree","tag-sum-of-subset-problem-using-backtracking","tag-sum-of-subsets-using-backtracking","tag-types-of-colours","tag-vertex-color"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25687","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/comments?post=25687"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/25687\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=25687"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=25687"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=25687"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}