{"id":26426,"date":"2017-12-20T21:09:39","date_gmt":"2017-12-20T15:39:39","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26426"},"modified":"2017-12-20T21:09:39","modified_gmt":"2017-12-20T15:39:39","slug":"java-programming-graph-coloring-set-2-greedy-algorithm","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/java-programming-graph-coloring-set-2-greedy-algorithm\/","title":{"rendered":"java programming &#8211; Graph Coloring | Set 2 (Greedy Algorithm)"},"content":{"rendered":"<p>We introduced <a href=\"http:\/\/www.geeksforgeeks.org\/graph-coloring-applications\/\" target=\"_blank\" rel=\"noopener noreferrer\">graph coloring and applications<\/a> in previous post. As discussed in the previous post, graph coloring is widely used. <span id=\"more-123945\"><\/span>Unfortunately, there is no efficient algorithm available for coloring a graph with minimum number of colors as the problem is a known <a href=\"http:\/\/www.geeksforgeeks.org\/np-completeness-set-1\/\" target=\"_blank\" rel=\"noopener noreferrer\">NP Complete problem<\/a>. There are approximate algorithms to solve the problem though. Following is the basic Greedy Algorithm to assign colors. It doesn\u2019t 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.<\/p>\n<p><strong>Basic Greedy Coloring Algorithm:<\/strong><\/p>\n<pre><strong>1.<\/strong> Color first vertex with first color.\r\n<strong>2.<\/strong> Do following for remaining V-1 vertices.\r\n         <strong> a) <\/strong>Consider the currently picked vertex and color it with the \r\n             lowest numbered color that has not been used on any previously\r\n             colored vertices adjacent to it. If all previously used colors \r\n             appear on vertices adjacent to v, assign a new color to it.\r\n<strong>Java program:\r\n\/\/ A Java program to implement greedy algorithm for graph coloring\r\nimport java.io.*;\r\nimport java.util.*;\r\nimport java.util.LinkedList;\r\n \r\n\/\/ This class represents an undirected graph using adjacency list\r\nclass Graph\r\n{\r\n private int V; \/\/ No. of vertices\r\n private LinkedList&lt;Integer&gt; adj[]; \/\/Adjacency List\r\n \r\n \/\/Constructor\r\n Graph(int v)\r\n {\r\n V = v;\r\n adj = new LinkedList[v];\r\n for (int i=0; i&lt;v; ++i)\r\n adj[i] = new LinkedList();\r\n }\r\n \r\n \/\/Function to add an edge into the graph\r\n void addEdge(int v,int w)\r\n {\r\n adj[v].add(w);\r\n adj[w].add(v); \/\/Graph is undirected\r\n }\r\n \r\n \/\/ Assigns colors (starting from 0) to all vertices and\r\n \/\/ prints the assignment of colors\r\n void greedyColoring()\r\n {\r\n int result[] = new int[V];\r\n \r\n \/\/ Assign the first color to first vertex\r\n result[0] = 0;\r\n \r\n \/\/ Initialize remaining V-1 vertices as unassigned\r\n for (int u = 1; u &lt; V; u++)\r\n result[u] = -1; \/\/ no color is assigned to u\r\n \r\n \/\/ A temporary array to store the available colors. True\r\n \/\/ value of available[cr] would mean that the color cr is\r\n \/\/ assigned to one of its adjacent vertices\r\n boolean available[] = new boolean[V];\r\n for (int cr = 0; cr &lt; V; cr++)\r\n available[cr] = false;\r\n \r\n \/\/ Assign colors to remaining V-1 vertices\r\n for (int u = 1; u &lt; V; u++)\r\n {\r\n \/\/ Process all adjacent vertices and flag their colors\r\n \/\/ as unavailable\r\n Iterator&lt;Integer&gt; it = adj[u].iterator() ;\r\n while (it.hasNext())\r\n {\r\n int i = it.next();\r\n if (result[i] != -1)\r\n available[result[i]] = true;\r\n }\r\n \r\n \/\/ Find the first available color\r\n int cr;\r\n for (cr = 0; cr &lt; V; cr++)\r\n if (available[cr] == false)\r\n break;\r\n \r\n result[u] = cr; \/\/ Assign the found color\r\n \r\n \/\/ Reset the values back to false for the next iteration\r\n it = adj[u].iterator() ;\r\n while (it.hasNext())\r\n {\r\n int i = it.next();\r\n if (result[i] != -1)\r\n available[result[i]] = false;\r\n }\r\n }\r\n \r\n \/\/ print the result\r\n for (int u = 0; u &lt; V; u++)\r\n System.out.println(\"Vertex \" + u + \" ---&gt; Color \"\r\n + result[u]);\r\n }\r\n \r\n \/\/ Driver method\r\n public static void main(String args[])\r\n {\r\n Graph g1 = new Graph(5);\r\n g1.addEdge(0, 1);\r\n g1.addEdge(0, 2);\r\n g1.addEdge(1, 2);\r\n g1.addEdge(1, 3);\r\n g1.addEdge(2, 3);\r\n g1.addEdge(3, 4);\r\n System.out.println(\"Coloring of graph 1\");\r\n g1.greedyColoring();\r\n \r\n System.out.println();\r\n Graph g2 = new Graph(5);\r\n g2.addEdge(0, 1);\r\n g2.addEdge(0, 2);\r\n g2.addEdge(1, 2);\r\n g2.addEdge(1, 4);\r\n g2.addEdge(2, 4);\r\n g2.addEdge(4, 3);\r\n System.out.println(\"Coloring of graph 2 \");\r\n g2.greedyColoring();\r\n }\r\n}\r\n\r\nOutput:<\/strong><\/pre>\n<pre>Coloring of graph 1\r\nVertex 0 ---&gt;  Color 0\r\nVertex 1 ---&gt;  Color 1\r\nVertex 2 ---&gt;  Color 2\r\nVertex 3 ---&gt;  Color 0\r\nVertex 4 ---&gt;  Color 1\r\n\r\nColoring of graph 2\r\nVertex 0 ---&gt;  Color 0\r\nVertex 1 ---&gt;  Color 1\r\nVertex 2 ---&gt;  Color 2\r\nVertex 3 ---&gt;  Color 0\r\nVertex 4 ---&gt;  Color 3<\/pre>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>java programming &#8211; Graph Coloring &#8211; Greedy Algorithm &#8211; there is no efficient algorithm available for coloring a graph with minimum number of colors<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[73906,78521],"tags":[74796,78573,78690,74812,78779,78780,78778],"class_list":["post-26426","post","type-post","status-publish","format-standard","hentry","category-graph-algorithms","category-hard-problems","tag-graph-coloring-algorithm-using-backtracking","tag-graph-coloring-algorithm-with-example","tag-graph-coloring-applications","tag-graph-coloring-example","tag-graph-coloring-program-in-java","tag-graph-coloring-using-backtracking-ppt","tag-graph-coloring-using-backtracking-program-in-c"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26426","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=26426"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26426\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26426"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26426"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26426"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}