21. Vertex Cover

The problem can be found at the following link: Question Link

My Approach

I approached this problem using a recursive method to explore all possible combinations of vertices in the vertex cover. Here are the steps:

  • I implemented a recursive function solve to explore different combinations of vertices.

  • Inside the solve function, I checked if the current combination is a valid vertex cover by iterating through the edges.

  • I used bitwise operations to manipulate the mask to represent the vertices included in the cover.

  • I kept track of the minimum vertex cover size using the variable out.

  • Finally, I invoked the solve function with the initial parameters in the vertexCover method.

Time and Auxiliary Space Complexity

  • Time Complexity: Exponential, O(2^n), where n is the number of vertices.

  • Auxiliary Space Complexity: O(n), where n is the number of vertices. (This is the maximum depth of the recursive call stack)

Code (C++)

Contribution and Support

For discussions, questions, or doubts related to this solution, please visit our discussion section. We welcome your input and aim to foster a collaborative learning environment.

If you find this solution helpful, consider supporting us by giving a ⭐ star to the getlost01/gfg-potd repository.

Last updated

Was this helpful?