# C++ program to find minimum vertex cover size of a graph using binary search

C++Server Side ProgrammingProgramming

In this article, we will be discussing a program to find the minimum vertex cover size of a given graph using binary search.

Minimum vertex cover is a set of vertices of the given graph such that every edge in the graph is incident of either of the vertices in that set.

For example, take the graph

2 ---- 4 ---- 6
|     |
|     |
|     |
3 ---- 5

Here, the minimum vertex cover involves vertices 3 and 4. All the edges of the graphs are incident on either 3 or 4 vertice of the graph.

## Example

Live Demo

#include<bits/stdc++.h>
using namespace std;
#define max 15
//array to store graph
bool arr[max][max];
//check if minimum vertex cover exists
bool check_cover(int V, int k, int E) {
int set = (1 << k) - 1;
int limit = (1 << V);
//to mark the edges of size 'k'
bool vis[max][max];
while (set < limit) {
//resetting the vertex cover for each iteration
memset(vis, 0, sizeof vis);
int count = 0;
//checking the values which has a high value
for (int j = 1, v = 1 ; j < limit ; j = j << 1, v++) {
if (set & j) {
//marking the edges visited
for (int k = 1 ; k <= V ; k++) {
if (arr[v][k] && !vis[v][k]) {
vis[v][k] = 1;
vis[k][v] = 1;
count++;
}
}
}
}
//if all the edges are covered
if (count == E)
return true;
int c = set & -set;
int r = set + c;
set = (((r^set) >> 2) / c) | r;
}
return false;
}
//to find minimum vertex cover
int find_cover(int n, int m) {
//performing binary search
int left = 1, right = n;
while (right > left){
int mid = (left + right) >> 1;
if (check_cover(n, mid, m) == false)
left = mid + 1;
else
right = mid;
}
return left;
}
//inserting edge in the graph
void add_edge(int u, int v) {
arr[u][v] = 1;
arr[v][u] = 1;
}
int main() {
memset(arr, 0, sizeof arr);
int V = 6, E = 5;
}
Size of Minimum Vertex Cover : 2