{"id":26538,"date":"2017-12-20T21:33:32","date_gmt":"2017-12-20T16:03:32","guid":{"rendered":"https:\/\/www.wikitechy.com\/technology\/?p=26538"},"modified":"2017-12-20T21:33:32","modified_gmt":"2017-12-20T16:03:32","slug":"java-program-vertex-cover-problem-introduction-approximate-algorithm","status":"publish","type":"post","link":"https:\/\/www.wikitechy.com\/technology\/java-program-vertex-cover-problem-introduction-approximate-algorithm\/","title":{"rendered":"java program &#8211; Vertex Cover Problem | Set 1 (Introduction and Approximate Algorithm)"},"content":{"rendered":"<p>A vertex cover of an undirected graph is a subset of its vertices such that for every edge (u, v) of the graph, either \u2018u\u2019 or \u2018v\u2019 is in vertex cover. <span id=\"more-132684\"><\/span>Although the name is Vertex Cover, the set covers all edges of the given graph. <em><strong>Given an undirected graph, the vertex cover problem is to find minimum size vertex cover<\/strong><\/em>.<\/p>\n<p>Following are some examples.<\/p>\n<p><a href=\"http:\/\/en.wikipedia.org\/wiki\/Vertex_cover\" target=\"_blank\" rel=\"noopener noreferrer\">Vertex Cover Problem<\/a> is a known <a href=\"http:\/\/www.geeksforgeeks.org\/np-completeness-set-1\/\" target=\"_blank\" rel=\"noopener noreferrer\">NP Complete problem<\/a>, i.e., there is no polynomial time solution for this unless P = NP. There are approximate polynomial time algorithms to solve the problem though. Following is a simple approximate algorithm adapted from <a href=\"http:\/\/www.flipkart.com\/introduction-algorithms-english-3rd\/p\/itmdwxyrafdburzg?pid=9788120340077&amp;affid=sandeepgfg\" target=\"_blank\" rel=\"noopener noreferrer\">CLRS book<\/a>.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p><strong>Approximate Algorithm for Vertex Cover:<\/strong><\/p>\n<pre>1) Initialize the result as {}\r\n2) Consider a set of all edges in given graph.  Let the set be E.\r\n3) Do following while E is not empty\r\n...a) Pick an arbitrary edge (u, v) from set E and add 'u' and 'v' to result\r\n...b) Remove all edges from E which are either incident on u or v.\r\n4) Return result \r\nFollowing diagram taken from <a href=\"http:\/\/www.flipkart.com\/introduction-algorithms-english-3rd\/p\/itmdwxyrafdburzg?pid=9788120340077&amp;affid=sandeepgfg\" target=\"_blank\" rel=\"noopener noreferrer\">CLRS book<\/a> shows execution of above approximate algorithm.<\/pre>\n<p><strong>How well the above algorithm perform?<\/strong><br \/>\nIt can be proved that the above approximate algorithm never finds a vertex cover whose size is more than twice the size of minimum possible vertex cover (Refer <a href=\"http:\/\/www.personal.kent.edu\/~rmuhamma\/Algorithms\/MyAlgorithms\/AproxAlgor\/vertexCover.htm\" target=\"_blank\" rel=\"noopener noreferrer\">this <\/a>for proof)<\/p>\n<p><a href=\"http:\/\/en.wikipedia.org\/wiki\/Vertex_cover\" target=\"_blank\" rel=\"noopener noreferrer\">Vertex Cover Problem<\/a> is a known <a href=\"http:\/\/www.geeksforgeeks.org\/np-completeness-set-1\/\" target=\"_blank\" rel=\"noopener noreferrer\">NP Complete problem<\/a>, i.e., there is no polynomial time solution for this unless P = NP. There are approximate polynomial time algorithms to solve the problem though. Following is a simple approximate algorithm adapted from <a href=\"http:\/\/www.flipkart.com\/introduction-algorithms-english-3rd\/p\/itmdwxyrafdburzg?pid=9788120340077&amp;affid=sandeepgfg\" target=\"_blank\" rel=\"noopener noreferrer\">CLRS book<\/a>.<\/p>\n[ad type=&#8221;banner&#8221;]\n<p><strong>Approximate Algorithm for Vertex Cover:<\/strong><\/p>\n<pre>1) Initialize the result as {}\r\n2) Consider a set of all edges in given graph.  Let the set be E.\r\n3) Do following while E is not empty\r\n...a) Pick an arbitrary edge (u, v) from set E and add 'u' and 'v' to result\r\n...b) Remove all edges from E which are either incident on u or v.\r\n4) Return result<\/pre>\n<p>Following diagram taken from <a href=\"http:\/\/www.flipkart.com\/introduction-algorithms-english-3rd\/p\/itmdwxyrafdburzg?pid=9788120340077&amp;affid=sandeepgfg\" target=\"_blank\" rel=\"noopener noreferrer\">CLRS book<\/a> shows execution of above approximate algorithm.<\/p>\n<p><strong>How well the above algorithm perform?<\/strong><br \/>\nIt can be proved that the above approximate algorithm never finds a vertex cover whose size is more than twice the size of minimum possible vertex cover (Refer <a href=\"http:\/\/www.personal.kent.edu\/~rmuhamma\/Algorithms\/MyAlgorithms\/AproxAlgor\/vertexCover.htm\" target=\"_blank\" rel=\"noopener noreferrer\">this <\/a>for proof)<\/p>\n<div class=\"code-embed-wrapper\"> <div class=\"code-embed-infos\"> <\/div> <pre class=\"language-java code-embed-pre line-numbers\"  data-start=\"1\" data-line-offset=\"0\"><code class=\"language-java code-embed-code\">\/\/ Java Program to print Vertex Cover of a given undirected graph<br\/>import java.io.*;<br\/>import java.util.*;<br\/>import java.util.LinkedList;<br\/> <br\/>\/\/ This class represents an undirected graph using adjacency list<br\/>class Graph<br\/>{<br\/>    private int V;   \/\/ No. of vertices<br\/> <br\/>    \/\/ Array  of lists for Adjacency List Representation<br\/>    private LinkedList&lt;Integer&gt; adj[];<br\/> <br\/>    \/\/ Constructor<br\/>    Graph(int v)<br\/>    {<br\/>        V = v;<br\/>        adj = new LinkedList[v];<br\/>        for (int i=0; i&lt;v; ++i)<br\/>            adj[i] = new LinkedList();<br\/>    }<br\/> <br\/>    \/\/Function to add an edge into the graph<br\/>    void addEdge(int v, int w)<br\/>    {<br\/>        adj[v].add(w);  \/\/ Add w to v&#039;s list.<br\/>        adj[w].add(v);  \/\/Graph is undirected<br\/>    }<br\/> <br\/>    \/\/ The function to print vertex cover<br\/>    void printVertexCover()<br\/>    {<br\/>        \/\/ Initialize all vertices as not visited.<br\/>        boolean visited[] = new boolean[V];<br\/>        for (int i=0; i&lt;V; i++)<br\/>            visited[i] = false;<br\/> <br\/>        Iterator&lt;Integer&gt; i;<br\/> <br\/>        \/\/ Consider all edges one by one<br\/>        for (int u=0; u&lt;V; u++)<br\/>        {<br\/>            \/\/ An edge is only picked when both visited[u]<br\/>            \/\/ and visited[v] are false<br\/>            if (visited[u] == false)<br\/>            {<br\/>                \/\/ Go through all adjacents of u and pick the<br\/>                \/\/ first not yet visited vertex (We are basically<br\/>                \/\/  picking an edge (u, v) from remaining edges.<br\/>                i = adj[u].iterator();<br\/>                while (i.hasNext())<br\/>                {<br\/>                    int v = i.next();<br\/>                    if (visited[v] == false)<br\/>                    {<br\/>                         \/\/ Add the vertices (u, v) to the result<br\/>                         \/\/ set. We make the vertex u and v visited<br\/>                         \/\/ so that all edges from\/to them would<br\/>                         \/\/ be ignored<br\/>                         visited[v] = true;<br\/>                         visited[u]  = true;<br\/>                         break;<br\/>                    }<br\/>                }<br\/>            }<br\/>        }<br\/> <br\/>        \/\/ Print the vertex cover<br\/>        for (int j=0; j&lt;V; j++)<br\/>            if (visited[j])<br\/>              System.out.print(j+&quot; &quot;);<br\/>    }<br\/> <br\/>    \/\/ Driver method<br\/>    public static void main(String args[])<br\/>    {<br\/>        \/\/ Create a graph given in the above diagram<br\/>        Graph g = new Graph(7);<br\/>        g.addEdge(0, 1);<br\/>        g.addEdge(0, 2);<br\/>        g.addEdge(1, 3);<br\/>        g.addEdge(3, 4);<br\/>        g.addEdge(4, 5);<br\/>        g.addEdge(5, 6);<br\/> <br\/>        g.printVertexCover();<br\/>    }<br\/>}<\/code><\/pre> <\/div>\n[ad type=&#8221;banner&#8221;]\n","protected":false},"excerpt":{"rendered":"<p>java program &#8211; Vertex Cover Problem &#8211; Introduction and Approximate Algorithm &#8211; It can be proved that the above approximate algorithm never finds a vertex<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,73906,78521],"tags":[79056,79036,79037,79038,79035,79055,79034],"class_list":["post-26538","post","type-post","status-publish","format-standard","hentry","category-coding","category-graph-algorithms","category-hard-problems","tag-edge-cover","tag-minimum-vertex-cover-algorithm-dynamic-programming","tag-minimum-vertex-cover-bipartite-graph","tag-set-cover","tag-vertex-cover-np-complete","tag-vertex-cover-problem-ppt","tag-what-is-a-vertex-cover"],"_links":{"self":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26538","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=26538"}],"version-history":[{"count":0,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/posts\/26538\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/media?parent=26538"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/categories?post=26538"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wikitechy.com\/technology\/wp-json\/wp\/v2\/tags?post=26538"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}