
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ program to find minimum vertex cover size of a graph using binary search
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
#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; add_edge(2, 3); add_edge(2, 4); add_edge(3, 5); add_edge(4, 5); add_edge(4, 6); cout << "Size of Minimum Vertex Cover : " << find_cover(V, E) << endl; return 0; }
Output
Size of Minimum Vertex Cover : 2
- Related Articles
- C++ Program to Implement a Heuristic to Find the Vertex Cover of a Graph
- C++ Program to Find the Minimum element of an Array using Binary Search approach
- C++ Program to Find the Minimum value of Binary Search Tree
- C++ Program to Find the Vertex Connectivity of a Graph
- Program to find out the minimum size of the largest clique in a graph (Python)
- Program to find minimum swaps to arrange a binary grid using Python
- Vertex cover Problem
- How to find minimum element in an array using binary search in C language?
- Python Program to Sort using a Binary Search Tree
- C++ program to Calculate the Edge Cover of a Graph
- C++ Program to Find the Number of occurrences of a given Number using Binary Search approach
- C++ Program to Find Minimum Element in an Array using Linear Search
- Pendent Vertex, Isolated Vertex and Adjacency of a graph
- C++ Program to Find Maximum Element in an Array using Binary Search
- C++ Program to Find the maximum subarray sum using Binary Search approach

Advertisements