A vertex cover of an undirected graph is a subset of its vertices such that for every edge (u, v) of the graph, either ‘u’ or ‘v’ is in vertex cover. Although the name is Vertex Cover, the set covers all edges of the given graph. Given an undirected graph, the vertex cover problem is to find minimum size vertex cover.

Following are some examples.

Vertex Cover Problem is a known NP Complete problem, 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 CLRS book.

Approximate Algorithm for Vertex Cover:

1) Initialize the result as {}
2) Consider a set of all edges in given graph.  Let the set be E.
3) Do following while E is not empty
...a) Pick an arbitrary edge (u, v) from set E and add 'u' and 'v' to result
...b) Remove all edges from E which are either incident on u or v.
4) Return result 
Following diagram taken from CLRS book shows execution of above approximate algorithm.

How well the above algorithm perform?
It 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 this for proof)

[ad type=”banner”]

C++ program:

[pastacode lang=”cpp” manual=”%2F%2F%20Program%20to%20print%20Vertex%20Cover%20of%20a%20given%20undirected%20graph%0A%23include%3Ciostream%3E%0A%23include%20%3Clist%3E%0Ausing%20namespace%20std%3B%0A%20%0A%2F%2F%20This%20class%20represents%20a%20undirected%20graph%20using%20adjacency%20list%20%0Aclass%20Graph%0A%7B%0A%20%20%20%20int%20V%3B%20%20%20%20%2F%2F%20No.%20of%20vertices%0A%20%20%20%20list%3Cint%3E%20*adj%3B%20%20%2F%2F%20Pointer%20to%20an%20array%20containing%20adjacency%20lists%0Apublic%3A%0A%20%20%20%20Graph(int%20V)%3B%20%20%2F%2F%20Constructor%0A%20%20%20%20void%20addEdge(int%20v%2C%20int%20w)%3B%20%2F%2F%20function%20to%20add%20an%20edge%20to%20graph%0A%20%20%20%20void%20printVertexCover()%3B%20%20%2F%2F%20prints%20vertex%20cover%0A%7D%3B%0A%20%0AGraph%3A%3AGraph(int%20V)%0A%7B%0A%20%20%20%20this-%3EV%20%3D%20V%3B%0A%20%20%20%20adj%20%3D%20new%20list%3Cint%3E%5BV%5D%3B%0A%7D%0A%20%0Avoid%20Graph%3A%3AaddEdge(int%20v%2C%20int%20w)%0A%7B%0A%20%20%20%20adj%5Bv%5D.push_back(w)%3B%20%2F%2F%20Add%20w%20to%20v%E2%80%99s%20list.%0A%20%20%20%20adj%5Bw%5D.push_back(v)%3B%20%2F%2F%20Since%20the%20graph%20is%20undirected%0A%7D%0A%20%0A%2F%2F%20The%20function%20to%20print%20vertex%20cover%0Avoid%20Graph%3A%3AprintVertexCover()%0A%7B%0A%20%20%20%20%2F%2F%20Initialize%20all%20vertices%20as%20not%20visited.%0A%20%20%20%20bool%20visited%5BV%5D%3B%0A%20%20%20%20for%20(int%20i%3D0%3B%20i%3CV%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20visited%5Bi%5D%20%3D%20false%3B%0A%20%0A%20%20%20%20list%3Cint%3E%3A%3Aiterator%20i%3B%0A%20%0A%20%20%20%20%2F%2F%20Consider%20all%20edges%20one%20by%20one%0A%20%20%20%20for%20(int%20u%3D0%3B%20u%3CV%3B%20u%2B%2B)%0A%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%2F%2F%20An%20edge%20is%20only%20picked%20when%20both%20visited%5Bu%5D%20and%20visited%5Bv%5D%0A%20%20%20%20%20%20%20%20%2F%2F%20are%20false%0A%20%20%20%20%20%20%20%20if%20(visited%5Bu%5D%20%3D%3D%20false)%0A%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Go%20through%20all%20adjacents%20of%20u%20and%20pick%20the%20first%20not%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20yet%20visited%20vertex%20(We%20are%20basically%20picking%20an%20edge%0A%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20(u%2C%20v)%20from%20remaining%20edges.%0A%20%20%20%20%20%20%20%20%20%20%20%20for%20(i%3D%20adj%5Bu%5D.begin()%3B%20i%20!%3D%20adj%5Bu%5D.end()%3B%20%2B%2Bi)%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20int%20v%20%3D%20*i%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20if%20(visited%5Bv%5D%20%3D%3D%20false)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20Add%20the%20vertices%20(u%2C%20v)%20to%20the%20result%20set.%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20We%20make%20the%20vertex%20u%20and%20v%20visited%20so%20that%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%2F%2F%20all%20edges%20from%2Fto%20them%20would%20be%20ignored%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20visited%5Bv%5D%20%3D%20true%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20visited%5Bu%5D%20%20%3D%20true%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20break%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%0A%20%0A%20%20%20%20%2F%2F%20Print%20the%20vertex%20cover%0A%20%20%20%20for%20(int%20i%3D0%3B%20i%3CV%3B%20i%2B%2B)%0A%20%20%20%20%20%20%20%20if%20(visited%5Bi%5D)%0A%20%20%20%20%20%20%20%20%20%20cout%20%3C%3C%20i%20%3C%3C%20%22%20%22%3B%0A%7D%0A%20%0A%2F%2F%20Driver%20program%20to%20test%20methods%20of%20graph%20class%0Aint%20main()%0A%7B%0A%20%20%20%20%2F%2F%20Create%20a%20graph%20given%20in%20the%20above%20diagram%0A%20%20%20%20Graph%20g(7)%3B%0A%20%20%20%20g.addEdge(0%2C%201)%3B%0A%20%20%20%20g.addEdge(0%2C%202)%3B%0A%20%20%20%20g.addEdge(1%2C%203)%3B%0A%20%20%20%20g.addEdge(3%2C%204)%3B%0A%20%20%20%20g.addEdge(4%2C%205)%3B%0A%20%20%20%20g.addEdge(5%2C%206)%3B%0A%20%0A%20%20%20%20g.printVertexCover()%3B%0A%20%0A%20%20%20%20return%200%3B%0A%7D” message=”” highlight=”” provider=”manual”/]