# vertex cover problem in graph

*Vertex cover problem in a given graph is a problem to find the minimum number of vertices which covers every edges in graph. The minimum number of vertices is a subset of vertices. Here covering of every edge means, at least one of end point of every edge falls into the subset. we can also say that at least end point of every edge is member of subset.*

*In mathematical term above concept can be summarized as, Given a graph G=(V,E) where V denotes vertex and E denotes edge, find a smallest subset V’ ⊆ V such that if ( u,v) ∈ E then u ∈ V’ or v ∈ V’ or both. Let us take an example graph. Vertex cover problem is NP complete problem.
*

*We can easily observe that in above graph we found a possible subset V’= {2,3,5} ( set of vertex) , where every edge (1,2), (2,1),(1,3),(3,1), (2,3),(3,2),(3,4),(4,3),(4,5),(5,4),(2,5) and (5,2) is covered by subset V’= {2,3,5} i.e at least one of end point of these edges is in subset V’={2,3,5}. Note: The subset V’ is one of the solution of vertex cover problem, the subset may contain different vertices in it but the size of vertex cover (subset) will be always same.*

*How to solve vertex cover problem, greedy approach *

*Create an empty set S.**select an edge E(u,v) from graph.**If any edge’s end point u or v is in set S, then the edge E(u,v) is already covered. else**Insert vertex u and vertex v in set S. mark this edge E(u,v) covered.**go to step 2, select next uncovered edge.**The algorithm terminates , when all edges are processed, i.e there is no uncovered edge available.*

*Here is sample implementation of above greedy approach. An array of size equal to the number of vertices,can be used as the set S. for example if there are 5 nodes, create an array S of size 5.*

** S[5] = {-1,-1,-1,-1,-1}**

*Let an Edge( 0,2) is taken to find vertex cover*

*now 0 and 2 are not member of S.*

*Then insert these vertices in set S. like below*

*S[0] = 1, S[2]=1, here index of 0 and 1 will be true. S[5] ={1,-1,1,-1,-1}*

*Let next edge to be process to find cover vertex is E(2,1)*

*Then to be part of vertex cover ,vertex 2 and vertex 1 should not be member of set S.*

*we can check membership of 2 easily. if (S[2] == 1 ) evaluates to true.*

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
#include<stdio.h> #include<stdbool.h> //number of nodes #define V 5 // vertex cover subset int VertexCover[V]; void findVertexCover (int G[V][V]); void printVertexCover (); int main () { /* sample graph (0) / | / | / | (1)--(2)--(3) \ / \ / \ / \ / (4) */ // store this graph in adjacency matrix //0 1 2 3 4 int G[V][V] = { {0, 1, 1, 0, 0}, //0 {1, 0, 1, 0, 1}, //1 {1, 1, 0, 1, 0}, //2 {0, 0, 1, 0, 1}, //3 {0, 1, 0, 1, 0}}; //4 int i = 0; // init vertex cover -1,represnting empty set for (i = 0; i < V; i++) VertexCover[i] = -1; // find vertex cover findVertexCover (G); // print result printVertexCover (); return 0; } void findVertexCover (int G[V][V]) { int u = 0; int v = 0; // process all edges in graph for (u = 0; u < V; u++) { for (v = 0; v < V; v++) { // if there is edge from u to v if (G[u][v] && (VertexCover[u] != 1) && (VertexCover[v] != 1)) { VertexCover[u] = 1; VertexCover[v] = 1; } } } // the starting vertex should be excluded from covered vertex // because its immediate child is included in the covered vertex VertexCover[0] = -1; } void printVertexCover () { int i = 0; int VertexCount = 0; printf ("Vertex cover subset is \n"); for (i = 0; i < V; i++) { if (VertexCover[i] == 1) { printf ("%d ", i); VertexCount++; } } printf ("\n"); printf ("And the minimum size is %d\n", VertexCount); } |

## Leave a Reply